|
|
@ -172,13 +172,13 @@ func NewService(cfg *v1.ServerConfig) (*Service, error) { |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, fmt.Errorf("create vhost tcpMuxer error, %v", err) |
|
|
|
return nil, fmt.Errorf("create vhost tcpMuxer error, %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
log.Info("tcpmux httpconnect multiplexer listen on %s, passthough: %v", address, cfg.TCPMuxPassthrough) |
|
|
|
log.Infof("tcpmux httpconnect multiplexer listen on %s, passthough: %v", address, cfg.TCPMuxPassthrough) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Init all plugins
|
|
|
|
// Init all plugins
|
|
|
|
for _, p := range cfg.HTTPPlugins { |
|
|
|
for _, p := range cfg.HTTPPlugins { |
|
|
|
svr.pluginManager.Register(plugin.NewHTTPPluginOptions(p)) |
|
|
|
svr.pluginManager.Register(plugin.NewHTTPPluginOptions(p)) |
|
|
|
log.Info("plugin [%s] has been registered", p.Name) |
|
|
|
log.Infof("plugin [%s] has been registered", p.Name) |
|
|
|
} |
|
|
|
} |
|
|
|
svr.rc.PluginManager = svr.pluginManager |
|
|
|
svr.rc.PluginManager = svr.pluginManager |
|
|
|
|
|
|
|
|
|
|
@ -222,7 +222,7 @@ func NewService(cfg *v1.ServerConfig) (*Service, error) { |
|
|
|
ln = svr.muxer.DefaultListener() |
|
|
|
ln = svr.muxer.DefaultListener() |
|
|
|
|
|
|
|
|
|
|
|
svr.listener = ln |
|
|
|
svr.listener = ln |
|
|
|
log.Info("frps tcp listen on %s", address) |
|
|
|
log.Infof("frps tcp listen on %s", address) |
|
|
|
|
|
|
|
|
|
|
|
// Listen for accepting connections from client using kcp protocol.
|
|
|
|
// Listen for accepting connections from client using kcp protocol.
|
|
|
|
if cfg.KCPBindPort > 0 { |
|
|
|
if cfg.KCPBindPort > 0 { |
|
|
@ -231,7 +231,7 @@ func NewService(cfg *v1.ServerConfig) (*Service, error) { |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, fmt.Errorf("listen on kcp udp address %s error: %v", address, err) |
|
|
|
return nil, fmt.Errorf("listen on kcp udp address %s error: %v", address, err) |
|
|
|
} |
|
|
|
} |
|
|
|
log.Info("frps kcp listen on udp %s", address) |
|
|
|
log.Infof("frps kcp listen on udp %s", address) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if cfg.QUICBindPort > 0 { |
|
|
|
if cfg.QUICBindPort > 0 { |
|
|
@ -246,7 +246,7 @@ func NewService(cfg *v1.ServerConfig) (*Service, error) { |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, fmt.Errorf("listen on quic udp address %s error: %v", address, err) |
|
|
|
return nil, fmt.Errorf("listen on quic udp address %s error: %v", address, err) |
|
|
|
} |
|
|
|
} |
|
|
|
log.Info("frps quic listen on %s", address) |
|
|
|
log.Infof("frps quic listen on %s", address) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if cfg.SSHTunnelGateway.BindPort > 0 { |
|
|
|
if cfg.SSHTunnelGateway.BindPort > 0 { |
|
|
@ -255,7 +255,7 @@ func NewService(cfg *v1.ServerConfig) (*Service, error) { |
|
|
|
return nil, fmt.Errorf("create ssh gateway error: %v", err) |
|
|
|
return nil, fmt.Errorf("create ssh gateway error: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
svr.sshTunnelGateway = sshGateway |
|
|
|
svr.sshTunnelGateway = sshGateway |
|
|
|
log.Info("frps sshTunnelGateway listen on port %d", cfg.SSHTunnelGateway.BindPort) |
|
|
|
log.Infof("frps sshTunnelGateway listen on port %d", cfg.SSHTunnelGateway.BindPort) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Listen for accepting connections from client using websocket protocol.
|
|
|
|
// Listen for accepting connections from client using websocket protocol.
|
|
|
@ -289,7 +289,7 @@ func NewService(cfg *v1.ServerConfig) (*Service, error) { |
|
|
|
go func() { |
|
|
|
go func() { |
|
|
|
_ = server.Serve(l) |
|
|
|
_ = server.Serve(l) |
|
|
|
}() |
|
|
|
}() |
|
|
|
log.Info("http service listen on %s", address) |
|
|
|
log.Infof("http service listen on %s", address) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Create https vhost muxer.
|
|
|
|
// Create https vhost muxer.
|
|
|
@ -303,7 +303,7 @@ func NewService(cfg *v1.ServerConfig) (*Service, error) { |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, fmt.Errorf("create server listener error, %v", err) |
|
|
|
return nil, fmt.Errorf("create server listener error, %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
log.Info("https service listen on %s", address) |
|
|
|
log.Infof("https service listen on %s", address) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
svr.rc.VhostHTTPSMuxer, err = vhost.NewHTTPSMuxer(l, vhostReadWriteTimeout) |
|
|
|
svr.rc.VhostHTTPSMuxer, err = vhost.NewHTTPSMuxer(l, vhostReadWriteTimeout) |
|
|
@ -335,9 +335,9 @@ func (svr *Service) Run(ctx context.Context) { |
|
|
|
// run dashboard web server.
|
|
|
|
// run dashboard web server.
|
|
|
|
if svr.webServer != nil { |
|
|
|
if svr.webServer != nil { |
|
|
|
go func() { |
|
|
|
go func() { |
|
|
|
log.Info("dashboard listen on %s", svr.webServer.Address()) |
|
|
|
log.Infof("dashboard listen on %s", svr.webServer.Address()) |
|
|
|
if err := svr.webServer.Run(); err != nil { |
|
|
|
if err := svr.webServer.Run(); err != nil { |
|
|
|
log.Warn("dashboard server exit with error: %v", err) |
|
|
|
log.Warnf("dashboard server exit with error: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
}() |
|
|
|
}() |
|
|
|
} |
|
|
|
} |
|
|
@ -408,7 +408,7 @@ func (svr *Service) handleConnection(ctx context.Context, conn net.Conn, interna |
|
|
|
|
|
|
|
|
|
|
|
_ = conn.SetReadDeadline(time.Now().Add(connReadTimeout)) |
|
|
|
_ = conn.SetReadDeadline(time.Now().Add(connReadTimeout)) |
|
|
|
if rawMsg, err = msg.ReadMsg(conn); err != nil { |
|
|
|
if rawMsg, err = msg.ReadMsg(conn); err != nil { |
|
|
|
log.Trace("Failed to read message: %v", err) |
|
|
|
log.Tracef("Failed to read message: %v", err) |
|
|
|
conn.Close() |
|
|
|
conn.Close() |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
@ -430,7 +430,7 @@ func (svr *Service) handleConnection(ctx context.Context, conn net.Conn, interna |
|
|
|
// If login failed, send error message there.
|
|
|
|
// If login failed, send error message there.
|
|
|
|
// Otherwise send success message in control's work goroutine.
|
|
|
|
// Otherwise send success message in control's work goroutine.
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
xl.Warn("register control error: %v", err) |
|
|
|
xl.Warnf("register control error: %v", err) |
|
|
|
_ = msg.WriteMsg(conn, &msg.LoginResp{ |
|
|
|
_ = msg.WriteMsg(conn, &msg.LoginResp{ |
|
|
|
Version: version.Full(), |
|
|
|
Version: version.Full(), |
|
|
|
Error: util.GenerateResponseErrorString("register control error", err, lo.FromPtr(svr.cfg.DetailedErrorsToClient)), |
|
|
|
Error: util.GenerateResponseErrorString("register control error", err, lo.FromPtr(svr.cfg.DetailedErrorsToClient)), |
|
|
@ -443,7 +443,7 @@ func (svr *Service) handleConnection(ctx context.Context, conn net.Conn, interna |
|
|
|
} |
|
|
|
} |
|
|
|
case *msg.NewVisitorConn: |
|
|
|
case *msg.NewVisitorConn: |
|
|
|
if err = svr.RegisterVisitorConn(conn, m); err != nil { |
|
|
|
if err = svr.RegisterVisitorConn(conn, m); err != nil { |
|
|
|
xl.Warn("register visitor conn error: %v", err) |
|
|
|
xl.Warnf("register visitor conn error: %v", err) |
|
|
|
_ = msg.WriteMsg(conn, &msg.NewVisitorConnResp{ |
|
|
|
_ = msg.WriteMsg(conn, &msg.NewVisitorConnResp{ |
|
|
|
ProxyName: m.ProxyName, |
|
|
|
ProxyName: m.ProxyName, |
|
|
|
Error: util.GenerateResponseErrorString("register visitor conn error", err, lo.FromPtr(svr.cfg.DetailedErrorsToClient)), |
|
|
|
Error: util.GenerateResponseErrorString("register visitor conn error", err, lo.FromPtr(svr.cfg.DetailedErrorsToClient)), |
|
|
@ -456,7 +456,7 @@ func (svr *Service) handleConnection(ctx context.Context, conn net.Conn, interna |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
default: |
|
|
|
default: |
|
|
|
log.Warn("Error message type for the new connection [%s]", conn.RemoteAddr().String()) |
|
|
|
log.Warnf("Error message type for the new connection [%s]", conn.RemoteAddr().String()) |
|
|
|
conn.Close() |
|
|
|
conn.Close() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -469,7 +469,7 @@ func (svr *Service) HandleListener(l net.Listener, internal bool) { |
|
|
|
for { |
|
|
|
for { |
|
|
|
c, err := l.Accept() |
|
|
|
c, err := l.Accept() |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Warn("Listener for incoming connections from client closed") |
|
|
|
log.Warnf("Listener for incoming connections from client closed") |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
// inject xlog object into net.Conn context
|
|
|
|
// inject xlog object into net.Conn context
|
|
|
@ -479,17 +479,17 @@ func (svr *Service) HandleListener(l net.Listener, internal bool) { |
|
|
|
c = netpkg.NewContextConn(xlog.NewContext(ctx, xl), c) |
|
|
|
c = netpkg.NewContextConn(xlog.NewContext(ctx, xl), c) |
|
|
|
|
|
|
|
|
|
|
|
if !internal { |
|
|
|
if !internal { |
|
|
|
log.Trace("start check TLS connection...") |
|
|
|
log.Tracef("start check TLS connection...") |
|
|
|
originConn := c |
|
|
|
originConn := c |
|
|
|
forceTLS := svr.cfg.Transport.TLS.Force |
|
|
|
forceTLS := svr.cfg.Transport.TLS.Force |
|
|
|
var isTLS, custom bool |
|
|
|
var isTLS, custom bool |
|
|
|
c, isTLS, custom, err = netpkg.CheckAndEnableTLSServerConnWithTimeout(c, svr.tlsConfig, forceTLS, connReadTimeout) |
|
|
|
c, isTLS, custom, err = netpkg.CheckAndEnableTLSServerConnWithTimeout(c, svr.tlsConfig, forceTLS, connReadTimeout) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Warn("CheckAndEnableTLSServerConnWithTimeout error: %v", err) |
|
|
|
log.Warnf("CheckAndEnableTLSServerConnWithTimeout error: %v", err) |
|
|
|
originConn.Close() |
|
|
|
originConn.Close() |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
log.Trace("check TLS connection success, isTLS: %v custom: %v internal: %v", isTLS, custom, internal) |
|
|
|
log.Tracef("check TLS connection success, isTLS: %v custom: %v internal: %v", isTLS, custom, internal) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Start a new goroutine to handle connection.
|
|
|
|
// Start a new goroutine to handle connection.
|
|
|
@ -501,7 +501,7 @@ func (svr *Service) HandleListener(l net.Listener, internal bool) { |
|
|
|
fmuxCfg.MaxStreamWindowSize = 6 * 1024 * 1024 |
|
|
|
fmuxCfg.MaxStreamWindowSize = 6 * 1024 * 1024 |
|
|
|
session, err := fmux.Server(frpConn, fmuxCfg) |
|
|
|
session, err := fmux.Server(frpConn, fmuxCfg) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Warn("Failed to create mux connection: %v", err) |
|
|
|
log.Warnf("Failed to create mux connection: %v", err) |
|
|
|
frpConn.Close() |
|
|
|
frpConn.Close() |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
@ -509,7 +509,7 @@ func (svr *Service) HandleListener(l net.Listener, internal bool) { |
|
|
|
for { |
|
|
|
for { |
|
|
|
stream, err := session.AcceptStream() |
|
|
|
stream, err := session.AcceptStream() |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Debug("Accept new mux stream error: %v", err) |
|
|
|
log.Debugf("Accept new mux stream error: %v", err) |
|
|
|
session.Close() |
|
|
|
session.Close() |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
@ -527,7 +527,7 @@ func (svr *Service) HandleQUICListener(l *quic.Listener) { |
|
|
|
for { |
|
|
|
for { |
|
|
|
c, err := l.Accept(context.Background()) |
|
|
|
c, err := l.Accept(context.Background()) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Warn("QUICListener for incoming connections from client closed") |
|
|
|
log.Warnf("QUICListener for incoming connections from client closed") |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
// Start a new goroutine to handle connection.
|
|
|
|
// Start a new goroutine to handle connection.
|
|
|
@ -535,7 +535,7 @@ func (svr *Service) HandleQUICListener(l *quic.Listener) { |
|
|
|
for { |
|
|
|
for { |
|
|
|
stream, err := frpConn.AcceptStream(context.Background()) |
|
|
|
stream, err := frpConn.AcceptStream(context.Background()) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Debug("Accept new quic mux stream error: %v", err) |
|
|
|
log.Debugf("Accept new quic mux stream error: %v", err) |
|
|
|
_ = frpConn.CloseWithError(0, "") |
|
|
|
_ = frpConn.CloseWithError(0, "") |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
@ -560,7 +560,7 @@ func (svr *Service) RegisterControl(ctlConn net.Conn, loginMsg *msg.Login, inter |
|
|
|
xl := xlog.FromContextSafe(ctx) |
|
|
|
xl := xlog.FromContextSafe(ctx) |
|
|
|
xl.AppendPrefix(loginMsg.RunID) |
|
|
|
xl.AppendPrefix(loginMsg.RunID) |
|
|
|
ctx = xlog.NewContext(ctx, xl) |
|
|
|
ctx = xlog.NewContext(ctx, xl) |
|
|
|
xl.Info("client login info: ip [%s] version [%s] hostname [%s] os [%s] arch [%s]", |
|
|
|
xl.Infof("client login info: ip [%s] version [%s] hostname [%s] os [%s] arch [%s]", |
|
|
|
ctlConn.RemoteAddr().String(), loginMsg.Version, loginMsg.Hostname, loginMsg.Os, loginMsg.Arch) |
|
|
|
ctlConn.RemoteAddr().String(), loginMsg.Version, loginMsg.Hostname, loginMsg.Os, loginMsg.Arch) |
|
|
|
|
|
|
|
|
|
|
|
// Check auth.
|
|
|
|
// Check auth.
|
|
|
@ -575,7 +575,7 @@ func (svr *Service) RegisterControl(ctlConn net.Conn, loginMsg *msg.Login, inter |
|
|
|
// TODO(fatedier): use SessionContext
|
|
|
|
// TODO(fatedier): use SessionContext
|
|
|
|
ctl, err := NewControl(ctx, svr.rc, svr.pxyManager, svr.pluginManager, authVerifier, ctlConn, !internal, loginMsg, svr.cfg) |
|
|
|
ctl, err := NewControl(ctx, svr.rc, svr.pxyManager, svr.pluginManager, authVerifier, ctlConn, !internal, loginMsg, svr.cfg) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
xl.Warn("create new controller error: %v", err) |
|
|
|
xl.Warnf("create new controller error: %v", err) |
|
|
|
// don't return detailed errors to client
|
|
|
|
// don't return detailed errors to client
|
|
|
|
return fmt.Errorf("unexpected error when creating new controller") |
|
|
|
return fmt.Errorf("unexpected error when creating new controller") |
|
|
|
} |
|
|
|
} |
|
|
@ -601,7 +601,7 @@ func (svr *Service) RegisterWorkConn(workConn net.Conn, newMsg *msg.NewWorkConn) |
|
|
|
xl := netpkg.NewLogFromConn(workConn) |
|
|
|
xl := netpkg.NewLogFromConn(workConn) |
|
|
|
ctl, exist := svr.ctlManager.GetByID(newMsg.RunID) |
|
|
|
ctl, exist := svr.ctlManager.GetByID(newMsg.RunID) |
|
|
|
if !exist { |
|
|
|
if !exist { |
|
|
|
xl.Warn("No client control found for run id [%s]", newMsg.RunID) |
|
|
|
xl.Warnf("No client control found for run id [%s]", newMsg.RunID) |
|
|
|
return fmt.Errorf("no client control found for run id [%s]", newMsg.RunID) |
|
|
|
return fmt.Errorf("no client control found for run id [%s]", newMsg.RunID) |
|
|
|
} |
|
|
|
} |
|
|
|
// server plugin hook
|
|
|
|
// server plugin hook
|
|
|
@ -620,7 +620,7 @@ func (svr *Service) RegisterWorkConn(workConn net.Conn, newMsg *msg.NewWorkConn) |
|
|
|
err = ctl.authVerifier.VerifyNewWorkConn(newMsg) |
|
|
|
err = ctl.authVerifier.VerifyNewWorkConn(newMsg) |
|
|
|
} |
|
|
|
} |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
xl.Warn("invalid NewWorkConn with run id [%s]", newMsg.RunID) |
|
|
|
xl.Warnf("invalid NewWorkConn with run id [%s]", newMsg.RunID) |
|
|
|
_ = msg.WriteMsg(workConn, &msg.StartWorkConn{ |
|
|
|
_ = msg.WriteMsg(workConn, &msg.StartWorkConn{ |
|
|
|
Error: util.GenerateResponseErrorString("invalid NewWorkConn", err, lo.FromPtr(svr.cfg.DetailedErrorsToClient)), |
|
|
|
Error: util.GenerateResponseErrorString("invalid NewWorkConn", err, lo.FromPtr(svr.cfg.DetailedErrorsToClient)), |
|
|
|
}) |
|
|
|
}) |
|
|
|