From a2c318d24c078196ed1dde7ab61a11c22cdc33c3 Mon Sep 17 00:00:00 2001 From: fatedier Date: Tue, 13 Jun 2017 23:36:10 +0800 Subject: [PATCH] update kcp mode --- models/config/server_common.go | 25 +++++++++++++------------ server/service.go | 16 ++++++++-------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/models/config/server_common.go b/models/config/server_common.go index 780fef2..1bf1f25 100644 --- a/models/config/server_common.go +++ b/models/config/server_common.go @@ -27,9 +27,10 @@ var ServerCommonCfg *ServerCommonConf // common config type ServerCommonConf struct { - ConfigFile string - BindAddr string - BindPort int64 + ConfigFile string + BindAddr string + BindPort int64 + KcpBindPort int64 // If VhostHttpPort equals 0, don't listen a public port for http protocol. VhostHttpPort int64 @@ -51,7 +52,6 @@ type ServerCommonConf struct { AuthTimeout int64 SubDomainHost string TcpMux bool - SupportKcp bool // if PrivilegeAllowPorts is not nil, tcp proxies which remote port exist in this map can be connected PrivilegeAllowPorts [][2]int64 @@ -65,6 +65,7 @@ func GetDefaultServerCommonConf() *ServerCommonConf { ConfigFile: "./frps.ini", BindAddr: "0.0.0.0", BindPort: 7000, + KcpBindPort: 0, VhostHttpPort: 0, VhostHttpsPort: 0, DashboardPort: 0, @@ -80,7 +81,6 @@ func GetDefaultServerCommonConf() *ServerCommonConf { AuthTimeout: 900, SubDomainHost: "", TcpMux: true, - SupportKcp: true, MaxPoolCount: 5, HeartBeatTimeout: 90, UserConnTimeout: 10, @@ -109,6 +109,14 @@ func LoadServerCommonConf(conf ini.File) (cfg *ServerCommonConf, err error) { } } + tmpStr, ok = conf.Get("common", "kcp_bind_port") + if ok { + v, err = strconv.ParseInt(tmpStr, 10, 64) + if err == nil && v > 0 { + cfg.KcpBindPort = v + } + } + tmpStr, ok = conf.Get("common", "vhost_http_port") if ok { cfg.VhostHttpPort, err = strconv.ParseInt(tmpStr, 10, 64) @@ -233,13 +241,6 @@ func LoadServerCommonConf(conf ini.File) (cfg *ServerCommonConf, err error) { cfg.TcpMux = true } - tmpStr, ok = conf.Get("common", "support_kcp") - if ok && tmpStr == "false" { - cfg.SupportKcp = false - } else { - cfg.SupportKcp = true - } - tmpStr, ok = conf.Get("common", "heartbeat_timeout") if ok { v, errRet := strconv.ParseInt(tmpStr, 10, 64) diff --git a/server/service.go b/server/service.go index b3d7164..6583b1c 100644 --- a/server/service.go +++ b/server/service.go @@ -79,17 +79,17 @@ func NewService() (svr *Service, err error) { log.Info("frps tcp listen on %s:%d", config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.BindPort) // Listen for accepting connections from client using kcp protocol. - if config.ServerCommonCfg.SupportKcp { - svr.kcpListener, err = frpNet.ListenKcp(config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.BindPort) + if config.ServerCommonCfg.KcpBindPort > 0 { + svr.kcpListener, err = frpNet.ListenKcp(config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.KcpBindPort) if err != nil { - err = fmt.Errorf("Listen on kcp address [%s:%d] error: %v", config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.BindPort, err) + err = fmt.Errorf("Listen on kcp address udp [%s:%d] error: %v", config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.KcpBindPort, err) return } - log.Info("frps kcp listen on %s:%d", config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.BindPort) + log.Info("frps kcp listen on udp %s:%d", config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.BindPort) } // Create http vhost muxer. - if config.ServerCommonCfg.VhostHttpPort != 0 { + if config.ServerCommonCfg.VhostHttpPort > 0 { var l frpNet.Listener l, err = frpNet.ListenTcp(config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.VhostHttpPort) if err != nil { @@ -105,7 +105,7 @@ func NewService() (svr *Service, err error) { } // Create https vhost muxer. - if config.ServerCommonCfg.VhostHttpsPort != 0 { + if config.ServerCommonCfg.VhostHttpsPort > 0 { var l frpNet.Listener l, err = frpNet.ListenTcp(config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.VhostHttpsPort) if err != nil { @@ -121,7 +121,7 @@ func NewService() (svr *Service, err error) { } // Create dashboard web server. - if config.ServerCommonCfg.DashboardPort != 0 { + if config.ServerCommonCfg.DashboardPort > 0 { err = RunDashboardServer(config.ServerCommonCfg.BindAddr, config.ServerCommonCfg.DashboardPort) if err != nil { err = fmt.Errorf("Create dashboard web server error, %v", err) @@ -133,7 +133,7 @@ func NewService() (svr *Service, err error) { } func (svr *Service) Run() { - if config.ServerCommonCfg.SupportKcp { + if config.ServerCommonCfg.KcpBindPort > 0 { go svr.HandleListener(svr.kcpListener) } svr.HandleListener(svr.listener)