proxy.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. type Proxy interface {
  23. Run()
  24. InWorkConn(conn net.Conn)
  25. Close()
  26. }
  27. func NewProxy(ctl *Control, pxyConf config.ProxyConf) (pxy Proxy) {
  28. switch cfg := pxyConf.(type) {
  29. case *config.TcpProxyConf:
  30. pxy = &TcpProxy{
  31. cfg: cfg,
  32. ctl: ctl,
  33. }
  34. case *config.UdpProxyConf:
  35. pxy = &UdpProxy{
  36. cfg: cfg,
  37. ctl: ctl,
  38. }
  39. case *config.HttpProxyConf:
  40. pxy = &HttpProxy{
  41. cfg: cfg,
  42. ctl: ctl,
  43. }
  44. case *config.HttpsProxyConf:
  45. pxy = &HttpsProxy{
  46. cfg: cfg,
  47. ctl: ctl,
  48. }
  49. }
  50. return
  51. }
  52. // TCP
  53. type TcpProxy struct {
  54. cfg *config.TcpProxyConf
  55. ctl *Control
  56. }
  57. func (pxy *TcpProxy) Run() {
  58. }
  59. func (pxy *TcpProxy) Close() {
  60. }
  61. func (pxy *TcpProxy) InWorkConn(conn net.Conn) {
  62. defer conn.Close()
  63. localConn, err := net.ConnectTcpServer(fmt.Sprintf("%s:%d", pxy.cfg.LocalIp, pxy.cfg.LocalPort))
  64. if err != nil {
  65. conn.Error("connect to local service [%s:%d] error: %v", pxy.cfg.LocalIp, pxy.cfg.LocalPort, err)
  66. return
  67. }
  68. var remote io.ReadWriteCloser
  69. remote = conn
  70. if pxy.cfg.UseEncryption {
  71. remote, err = tcp.WithEncryption(remote, []byte(config.ClientCommonCfg.PrivilegeToken))
  72. if err != nil {
  73. conn.Error("create encryption stream error: %v", err)
  74. return
  75. }
  76. }
  77. if pxy.cfg.UseCompression {
  78. remote = tcp.WithCompression(remote)
  79. }
  80. conn.Debug("join connections")
  81. tcp.Join(localConn, remote)
  82. conn.Debug("join connections closed")
  83. }
  84. // UDP
  85. type UdpProxy struct {
  86. cfg *config.UdpProxyConf
  87. ctl *Control
  88. }
  89. func (pxy *UdpProxy) Run() {
  90. }
  91. func (pxy *UdpProxy) Close() {
  92. }
  93. func (pxy *UdpProxy) InWorkConn(conn net.Conn) {
  94. defer conn.Close()
  95. }
  96. // HTTP
  97. type HttpProxy struct {
  98. cfg *config.HttpProxyConf
  99. ctl *Control
  100. }
  101. func (pxy *HttpProxy) Run() {
  102. }
  103. func (pxy *HttpProxy) Close() {
  104. }
  105. func (pxy *HttpProxy) InWorkConn(conn net.Conn) {
  106. defer conn.Close()
  107. }
  108. // HTTPS
  109. type HttpsProxy struct {
  110. cfg *config.HttpsProxyConf
  111. ctl *Control
  112. }
  113. func (pxy *HttpsProxy) Run() {
  114. }
  115. func (pxy *HttpsProxy) Close() {
  116. }
  117. func (pxy *HttpsProxy) InWorkConn(conn net.Conn) {
  118. defer conn.Close()
  119. }