proxy.go 3.4 KB

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