| // |
| 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 |
| 15 | package visitor |
| 16 |
| 17 | import ( |
| 18 | "context" |
| 19 | "fmt" |
| 20 | "io" |
| 21 | "net" |
| 22 | "sync" |
| 23 | "time" |
| 24 |
| 25 | libio "github.com/fatedier/golib/io" |
| 26 |
| 27 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 28 | "github.com/fatedier/frp/pkg/msg" |
| 29 | "github.com/fatedier/frp/pkg/naming" |
| 30 | plugin "github.com/fatedier/frp/pkg/plugin/visitor" |
| 31 | "github.com/fatedier/frp/pkg/transport" |
| 32 | netpkg "github.com/fatedier/frp/pkg/util/net" |
| 33 | "github.com/fatedier/frp/pkg/util/util" |
| 34 | "github.com/fatedier/frp/pkg/util/xlog" |
| 35 | "github.com/fatedier/frp/pkg/vnet" |
| 36 | ) |
| 37 |
| 38 | // Helper wraps some functions for visitor to use. |
| 39 | type Helper interface { |
| 40 | // ConnectServer directly connects to the frp server. |
| 41 | ConnectServer() (*msg.Conn, error) |
| 42 | // TransferConn transfers the connection to another visitor. |
| 43 | TransferConn(string, net.Conn) error |
| 44 | // MsgTransporter returns the message transporter that is used to send and receive messages |
| 45 | // to the frp server through the controller. |
| 46 | MsgTransporter() transport.MessageTransporter |
| 47 | // VNetController returns the vnet controller that is used to manage the virtual network. |
| 48 | VNetController() *vnet.Controller |
| 49 | // RunID returns the run id of current controller. |
| 50 | RunID() string |
| 51 | } |
| 52 |
| 53 | // Visitor is used for forward traffics from local port tot remote service. |
| 54 | type Visitor interface { |
| 55 | Run() error |
| 56 | AcceptConn(conn net.Conn) error |
| 57 | Close() |
| 58 | } |
| 59 |
| 60 | func NewVisitor( |
| 61 | ctx context.Context, |
| 62 | cfg v1.VisitorConfigurer, |
| 63 | clientCfg *v1.ClientCommonConfig, |
| 64 | helper Helper, |
| 65 | ) (Visitor, error) { |
| 66 | xl := xlog.FromContextSafe(ctx).Spawn().AppendPrefix(cfg.GetBaseConfig().Name) |
| 67 | ctx = xlog.NewContext(ctx, xl) |
| 68 | var visitor Visitor |
| 69 | baseVisitor := BaseVisitor{ |
| 70 | clientCfg: clientCfg, |
| 71 | helper: helper, |
| 72 | ctx: ctx, |
| 73 | internalLn: netpkg.NewInternalListener(), |
| 74 | } |
| 75 | if cfg.GetBaseConfig().Plugin.Type != "" { |
| 76 | p, err := plugin.Create( |
| 77 | cfg.GetBaseConfig().Plugin.Type, |
| 78 | plugin.PluginContext{ |
| 79 | Name: cfg.GetBaseConfig().Name, |
| 80 | Ctx: ctx, |
| 81 | VnetController: helper.VNetController(), |
| 82 | SendConnToVisitor: func(conn net.Conn) { |
| 83 | _ = baseVisitor.AcceptConn(conn) |
| 84 | }, |
| 85 | }, |
| 86 | cfg.GetBaseConfig().Plugin.VisitorPluginOptions, |
| 87 | ) |
| 88 | if err != nil { |
| 89 | return nil, err |
| 90 | } |
| 91 | baseVisitor.plugin = p |
| 92 | } |
| 93 | switch cfg := cfg.(type) { |
| 94 | case *v1.STCPVisitorConfig: |
| 95 | visitor = &STCPVisitor{ |
| 96 | BaseVisitor: &baseVisitor, |
| 97 | cfg: cfg, |
| 98 | } |
| 99 | case *v1.XTCPVisitorConfig: |
| 100 | visitor = &XTCPVisitor{ |
| 101 | BaseVisitor: &baseVisitor, |
| 102 | cfg: cfg, |
| 103 | startTunnelCh: make(chan struct{}), |
| 104 | } |
| 105 | case *v1.SUDPVisitorConfig: |
| 106 | visitor = &SUDPVisitor{ |
| 107 | BaseVisitor: &baseVisitor, |
| 108 | cfg: cfg, |
| 109 | checkCloseCh: make(chan struct{}), |
| 110 | } |
| 111 | } |
| 112 | return visitor, nil |
| 113 | } |
| 114 |
| 115 | type BaseVisitor struct { |
| 116 | clientCfg *v1.ClientCommonConfig |
| 117 | helper Helper |
| 118 | l net.Listener |
| 119 | internalLn *netpkg.InternalListener |
| 120 | plugin plugin.Plugin |
| 121 |
| 122 | mu sync.RWMutex |
| 123 | ctx context.Context |
| 124 | } |
| 125 |
| 126 | func (v *BaseVisitor) AcceptConn(conn net.Conn) error { |
| 127 | return v.internalLn.PutConn(conn) |
| 128 | } |
| 129 |
| 130 | func (v *BaseVisitor) acceptLoop(l net.Listener, name string, handleConn func(net.Conn)) { |
| 131 | xl := xlog.FromContextSafe(v.ctx) |
| 132 | for { |
| 133 | conn, err := l.Accept() |
| 134 | if err != nil { |
| 135 | xl.Warnf("%s listener closed", name) |
| 136 | return |
| 137 | } |
| 138 | go handleConn(conn) |
| 139 | } |
| 140 | } |
| 141 |
| 142 | func (v *BaseVisitor) Close() { |
| 143 | if v.l != nil { |
| 144 | v.l.Close() |
| 145 | } |
| 146 | if v.internalLn != nil { |
| 147 | v.internalLn.Close() |
| 148 | } |
| 149 | if v.plugin != nil { |
| 150 | v.plugin.Close() |
| 151 | } |
| 152 | } |
| 153 |
| 154 | func (v *BaseVisitor) dialRawVisitorConn(cfg *v1.VisitorBaseConfig) (net.Conn, error) { |
| 155 | visitorConn, err := v.helper.ConnectServer() |
| 156 | if err != nil { |
| 157 | return nil, fmt.Errorf("connect to server error: %v", err) |
| 158 | } |
| 159 |
| 160 | now := time.Now().Unix() |
| 161 | targetProxyName := naming.BuildTargetServerProxyName(v.clientCfg.User, cfg.ServerUser, cfg.ServerName) |
| 162 | newVisitorConnMsg := &msg.NewVisitorConn{ |
| 163 | RunID: v.helper.RunID(), |
| 164 | ProxyName: targetProxyName, |
| 165 | SignKey: util.GetAuthKey(cfg.SecretKey, now), |
| 166 | Timestamp: now, |
| 167 | UseEncryption: cfg.Transport.UseEncryption, |
| 168 | UseCompression: cfg.Transport.UseCompression, |
| 169 | } |
| 170 | err = visitorConn.WriteMsg(newVisitorConnMsg) |
| 171 | if err != nil { |
| 172 | visitorConn.Close() |
| 173 | return nil, fmt.Errorf("send newVisitorConnMsg to server error: %v", err) |
| 174 | } |
| 175 |
| 176 | _ = visitorConn.SetReadDeadline(time.Now().Add(10 * time.Second)) |
| 177 | var newVisitorConnRespMsg msg.NewVisitorConnResp |
| 178 | err = visitorConn.ReadMsgInto(&newVisitorConnRespMsg) |
| 179 | if err != nil { |
| 180 | visitorConn.Close() |
| 181 | return nil, fmt.Errorf("read newVisitorConnRespMsg error: %v", err) |
| 182 | } |
| 183 | _ = visitorConn.SetReadDeadline(time.Time{}) |
| 184 |
| 185 | if newVisitorConnRespMsg.Error != "" { |
| 186 | visitorConn.Close() |
| 187 | return nil, fmt.Errorf("start new visitor connection error: %s", newVisitorConnRespMsg.Error) |
| 188 | } |
| 189 | return visitorConn, nil |
| 190 | } |
| 191 |
| 192 | func wrapVisitorConn(conn io.ReadWriteCloser, cfg *v1.VisitorBaseConfig) (io.ReadWriteCloser, func(), error) { |
| 193 | rwc := conn |
| 194 | if cfg.Transport.UseEncryption { |
| 195 | var err error |
| 196 | rwc, err = libio.WithEncryption(rwc, []byte(cfg.SecretKey)) |
| 197 | if err != nil { |
| 198 | return nil, func() {}, fmt.Errorf("create encryption stream error: %v", err) |
| 199 | } |
| 200 | } |
| 201 | recycleFn := func() {} |
| 202 | if cfg.Transport.UseCompression { |
| 203 | rwc, recycleFn = libio.WithCompressionFromPool(rwc) |
| 204 | } |
| 205 | return rwc, recycleFn, nil |
| 206 | } |