proxy.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package client
  15. import (
  16. "fmt"
  17. "io"
  18. "github.com/fatedier/frp/models/config"
  19. "github.com/fatedier/frp/models/proto/tcp"
  20. "github.com/fatedier/frp/utils/net"
  21. )
  22. // Proxy defines how to work for different proxy type.
  23. type Proxy interface {
  24. Run()
  25. // InWorkConn accept work connections registered to server.
  26. InWorkConn(conn net.Conn)
  27. Close()
  28. }
  29. func NewProxy(ctl *Control, pxyConf config.ProxyConf) (pxy Proxy) {
  30. switch cfg := pxyConf.(type) {
  31. case *config.TcpProxyConf:
  32. pxy = &TcpProxy{
  33. cfg: cfg,
  34. ctl: ctl,
  35. }
  36. case *config.UdpProxyConf:
  37. pxy = &UdpProxy{
  38. cfg: cfg,
  39. ctl: ctl,
  40. }
  41. case *config.HttpProxyConf:
  42. pxy = &HttpProxy{
  43. cfg: cfg,
  44. ctl: ctl,
  45. }
  46. case *config.HttpsProxyConf:
  47. pxy = &HttpsProxy{
  48. cfg: cfg,
  49. ctl: ctl,
  50. }
  51. }
  52. return
  53. }
  54. // TCP
  55. type TcpProxy struct {
  56. cfg *config.TcpProxyConf
  57. ctl *Control
  58. }
  59. func (pxy *TcpProxy) Run() {
  60. }
  61. func (pxy *TcpProxy) Close() {
  62. }
  63. func (pxy *TcpProxy) InWorkConn(conn net.Conn) {
  64. defer conn.Close()
  65. localConn, err := net.ConnectTcpServer(fmt.Sprintf("%s:%d", pxy.cfg.LocalIp, pxy.cfg.LocalPort))
  66. if err != nil {
  67. conn.Error("connect to local service [%s:%d] error: %v", pxy.cfg.LocalIp, pxy.cfg.LocalPort, err)
  68. return
  69. }
  70. var remote io.ReadWriteCloser
  71. remote = conn
  72. if pxy.cfg.UseEncryption {
  73. remote, err = tcp.WithEncryption(remote, []byte(config.ClientCommonCfg.PrivilegeToken))
  74. if err != nil {
  75. conn.Error("create encryption stream error: %v", err)
  76. return
  77. }
  78. }
  79. if pxy.cfg.UseCompression {
  80. remote = tcp.WithCompression(remote)
  81. }
  82. conn.Debug("join connections")
  83. tcp.Join(localConn, remote)
  84. conn.Debug("join connections closed")
  85. }
  86. // UDP
  87. type UdpProxy struct {
  88. cfg *config.UdpProxyConf
  89. ctl *Control
  90. }
  91. func (pxy *UdpProxy) Run() {
  92. }
  93. func (pxy *UdpProxy) Close() {
  94. }
  95. func (pxy *UdpProxy) InWorkConn(conn net.Conn) {
  96. defer conn.Close()
  97. }
  98. // HTTP
  99. type HttpProxy struct {
  100. cfg *config.HttpProxyConf
  101. ctl *Control
  102. }
  103. func (pxy *HttpProxy) Run() {
  104. }
  105. func (pxy *HttpProxy) Close() {
  106. }
  107. func (pxy *HttpProxy) InWorkConn(conn net.Conn) {
  108. defer conn.Close()
  109. }
  110. // HTTPS
  111. type HttpsProxy struct {
  112. cfg *config.HttpsProxyConf
  113. ctl *Control
  114. }
  115. func (pxy *HttpsProxy) Run() {
  116. }
  117. func (pxy *HttpsProxy) Close() {
  118. }
  119. func (pxy *HttpsProxy) InWorkConn(conn net.Conn) {
  120. defer conn.Close()
  121. }