proxy.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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() error
  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() (err error) {
  60. return
  61. }
  62. func (pxy *TcpProxy) Close() {
  63. }
  64. func (pxy *TcpProxy) InWorkConn(conn net.Conn) {
  65. defer conn.Close()
  66. HandleTcpWorkConnection(&pxy.cfg.LocalSvrConf, &pxy.cfg.BaseProxyConf, conn)
  67. }
  68. // HTTP
  69. type HttpProxy struct {
  70. cfg *config.HttpProxyConf
  71. ctl *Control
  72. }
  73. func (pxy *HttpProxy) Run() (err error) {
  74. return
  75. }
  76. func (pxy *HttpProxy) Close() {
  77. }
  78. func (pxy *HttpProxy) InWorkConn(conn net.Conn) {
  79. defer conn.Close()
  80. HandleTcpWorkConnection(&pxy.cfg.LocalSvrConf, &pxy.cfg.BaseProxyConf, conn)
  81. }
  82. // HTTPS
  83. type HttpsProxy struct {
  84. cfg *config.HttpsProxyConf
  85. ctl *Control
  86. }
  87. func (pxy *HttpsProxy) Run() (err error) {
  88. return
  89. }
  90. func (pxy *HttpsProxy) Close() {
  91. }
  92. func (pxy *HttpsProxy) InWorkConn(conn net.Conn) {
  93. defer conn.Close()
  94. HandleTcpWorkConnection(&pxy.cfg.LocalSvrConf, &pxy.cfg.BaseProxyConf, conn)
  95. }
  96. // UDP
  97. type UdpProxy struct {
  98. cfg *config.UdpProxyConf
  99. ctl *Control
  100. }
  101. func (pxy *UdpProxy) Run() (err error) {
  102. return
  103. }
  104. func (pxy *UdpProxy) Close() {
  105. }
  106. func (pxy *UdpProxy) InWorkConn(conn net.Conn) {
  107. defer conn.Close()
  108. }
  109. // Common handler for tcp work connections.
  110. func HandleTcpWorkConnection(localInfo *config.LocalSvrConf, baseInfo *config.BaseProxyConf, workConn net.Conn) {
  111. localConn, err := net.ConnectTcpServer(fmt.Sprintf("%s:%d", localInfo.LocalIp, localInfo.LocalPort))
  112. if err != nil {
  113. workConn.Error("connect to local service [%s:%d] error: %v", localInfo.LocalIp, localInfo.LocalPort, err)
  114. return
  115. }
  116. var remote io.ReadWriteCloser
  117. remote = workConn
  118. if baseInfo.UseEncryption {
  119. remote, err = tcp.WithEncryption(remote, []byte(config.ClientCommonCfg.PrivilegeToken))
  120. if err != nil {
  121. workConn.Error("create encryption stream error: %v", err)
  122. return
  123. }
  124. }
  125. if baseInfo.UseCompression {
  126. remote = tcp.WithCompression(remote)
  127. }
  128. workConn.Debug("join connections, localConn(l[%s] r[%s]) workConn(l[%s] r[%s])", localConn.LocalAddr().String(),
  129. localConn.RemoteAddr().String(), workConn.LocalAddr().String(), workConn.RemoteAddr().String())
  130. tcp.Join(localConn, remote)
  131. workConn.Debug("join connections closed")
  132. }