client.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2016 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. "encoding/json"
  17. "fmt"
  18. "sync"
  19. "time"
  20. "github.com/fatedier/frp/src/models/config"
  21. "github.com/fatedier/frp/src/models/consts"
  22. "github.com/fatedier/frp/src/models/msg"
  23. "github.com/fatedier/frp/src/utils/conn"
  24. "github.com/fatedier/frp/src/utils/log"
  25. "github.com/fatedier/frp/src/utils/pcrypto"
  26. )
  27. type ProxyClient struct {
  28. config.BaseConf
  29. LocalIp string
  30. LocalPort int64
  31. RemotePort int64
  32. CustomDomains []string
  33. Locations []string
  34. udpTunnel *conn.Conn
  35. once sync.Once
  36. }
  37. // if proxy type is udp, keep a tcp connection for transferring udp packages
  38. func (pc *ProxyClient) StartUdpTunnelOnce(addr string, port int64) {
  39. pc.once.Do(func() {
  40. var err error
  41. var c *conn.Conn
  42. udpProcessor := NewUdpProcesser(nil, pc.LocalIp, pc.LocalPort)
  43. for {
  44. if pc.udpTunnel == nil || pc.udpTunnel.IsClosed() {
  45. if HttpProxy == "" {
  46. c, err = conn.ConnectServer(fmt.Sprintf("%s:%d", addr, port))
  47. } else {
  48. c, err = conn.ConnectServerByHttpProxy(HttpProxy, fmt.Sprintf("%s:%d", addr, port))
  49. }
  50. if err != nil {
  51. log.Error("ProxyName [%s], udp tunnel connect to server [%s:%d] error, %v", pc.Name, addr, port, err)
  52. time.Sleep(10 * time.Second)
  53. continue
  54. }
  55. log.Info("ProxyName [%s], udp tunnel reconnect to server [%s:%d] success", pc.Name, addr, port)
  56. nowTime := time.Now().Unix()
  57. req := &msg.ControlReq{
  58. Type: consts.NewWorkConnUdp,
  59. ProxyName: pc.Name,
  60. PrivilegeMode: pc.PrivilegeMode,
  61. Timestamp: nowTime,
  62. }
  63. if pc.PrivilegeMode == true {
  64. req.PrivilegeKey = pcrypto.GetAuthKey(pc.Name + PrivilegeToken + fmt.Sprintf("%d", nowTime))
  65. } else {
  66. req.AuthKey = pcrypto.GetAuthKey(pc.Name + pc.AuthToken + fmt.Sprintf("%d", nowTime))
  67. }
  68. buf, _ := json.Marshal(req)
  69. err = c.WriteString(string(buf) + "\n")
  70. if err != nil {
  71. log.Error("ProxyName [%s], udp tunnel write to server error, %v", pc.Name, err)
  72. c.Close()
  73. time.Sleep(1 * time.Second)
  74. continue
  75. }
  76. pc.udpTunnel = c
  77. udpProcessor.UpdateTcpConn(pc.udpTunnel)
  78. udpProcessor.Run()
  79. }
  80. time.Sleep(1 * time.Second)
  81. }
  82. })
  83. }
  84. func (pc *ProxyClient) GetLocalConn() (c *conn.Conn, err error) {
  85. c, err = conn.ConnectServer(fmt.Sprintf("%s:%d", pc.LocalIp, pc.LocalPort))
  86. if err != nil {
  87. log.Error("ProxyName [%s], connect to local port error, %v", pc.Name, err)
  88. }
  89. return
  90. }
  91. func (pc *ProxyClient) GetRemoteConn(addr string, port int64) (c *conn.Conn, err error) {
  92. defer func() {
  93. if err != nil && c != nil {
  94. c.Close()
  95. }
  96. }()
  97. if HttpProxy == "" {
  98. c, err = conn.ConnectServer(fmt.Sprintf("%s:%d", addr, port))
  99. } else {
  100. c, err = conn.ConnectServerByHttpProxy(HttpProxy, fmt.Sprintf("%s:%d", addr, port))
  101. }
  102. if err != nil {
  103. log.Error("ProxyName [%s], connect to server [%s:%d] error, %v", pc.Name, addr, port, err)
  104. return
  105. }
  106. nowTime := time.Now().Unix()
  107. req := &msg.ControlReq{
  108. Type: consts.NewWorkConn,
  109. ProxyName: pc.Name,
  110. PrivilegeMode: pc.PrivilegeMode,
  111. Timestamp: nowTime,
  112. }
  113. if pc.PrivilegeMode == true {
  114. req.PrivilegeKey = pcrypto.GetAuthKey(pc.Name + PrivilegeToken + fmt.Sprintf("%d", nowTime))
  115. } else {
  116. req.AuthKey = pcrypto.GetAuthKey(pc.Name + pc.AuthToken + fmt.Sprintf("%d", nowTime))
  117. }
  118. buf, _ := json.Marshal(req)
  119. err = c.WriteString(string(buf) + "\n")
  120. if err != nil {
  121. log.Error("ProxyName [%s], write to server error, %v", pc.Name, err)
  122. return
  123. }
  124. err = nil
  125. return
  126. }
  127. func (pc *ProxyClient) StartTunnel(serverAddr string, serverPort int64) (err error) {
  128. localConn, err := pc.GetLocalConn()
  129. if err != nil {
  130. return
  131. }
  132. remoteConn, err := pc.GetRemoteConn(serverAddr, serverPort)
  133. if err != nil {
  134. return
  135. }
  136. // l means local, r means remote
  137. log.Debug("Join two connections, (l[%s] r[%s]) (l[%s] r[%s])", localConn.GetLocalAddr(), localConn.GetRemoteAddr(),
  138. remoteConn.GetLocalAddr(), remoteConn.GetRemoteAddr())
  139. needRecord := false
  140. go msg.JoinMore(localConn, remoteConn, pc.BaseConf, needRecord)
  141. return nil
  142. }