From 3e349455a06d7ec67eab7e57ac7d352879d6151e Mon Sep 17 00:00:00 2001 From: fatedier Date: Wed, 16 May 2018 23:45:44 +0800 Subject: [PATCH] commands for xtcp, stcp add 'bind_port', fix #767 --- cmd/frpc/sub/root.go | 1 + cmd/frpc/sub/stcp.go | 4 +++- cmd/frpc/sub/xtcp.go | 4 +++- models/config/proxy.go | 8 ++++++++ server/nathole.go | 9 ++++++++- 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/cmd/frpc/sub/root.go b/cmd/frpc/sub/root.go index 7f09c03..9b9a226 100644 --- a/cmd/frpc/sub/root.go +++ b/cmd/frpc/sub/root.go @@ -69,6 +69,7 @@ var ( sk string serverName string bindAddr string + bindPort int ) func init() { diff --git a/cmd/frpc/sub/stcp.go b/cmd/frpc/sub/stcp.go index d3a5d4a..4915e52 100644 --- a/cmd/frpc/sub/stcp.go +++ b/cmd/frpc/sub/stcp.go @@ -39,7 +39,8 @@ func init() { stcpCmd.PersistentFlags().StringVarP(&serverName, "server_name", "", "", "server name") stcpCmd.PersistentFlags().StringVarP(&localIp, "local_ip", "i", "127.0.0.1", "local ip") stcpCmd.PersistentFlags().IntVarP(&localPort, "local_port", "l", 0, "local port") - stcpCmd.PersistentFlags().StringVarP(&bindAddr, "bind_addr", "", "", "bind addr such as 127.0.0.1:9000") + stcpCmd.PersistentFlags().StringVarP(&bindAddr, "bind_addr", "", "", "bind addr") + stcpCmd.PersistentFlags().IntVarP(&bindPort, "bind_port", "", 0, "bind port") stcpCmd.PersistentFlags().BoolVarP(&useEncryption, "ue", "", false, "use encryption") stcpCmd.PersistentFlags().BoolVarP(&useCompression, "uc", "", false, "use compression") @@ -69,6 +70,7 @@ var stcpCmd = &cobra.Command{ cfg.LocalIp = localIp cfg.LocalPort = localPort cfg.BindAddr = bindAddr + cfg.BindPort = bindPort cfg.UseEncryption = useEncryption cfg.UseCompression = useCompression diff --git a/cmd/frpc/sub/xtcp.go b/cmd/frpc/sub/xtcp.go index e7d0752..8c18a85 100644 --- a/cmd/frpc/sub/xtcp.go +++ b/cmd/frpc/sub/xtcp.go @@ -39,7 +39,8 @@ func init() { xtcpCmd.PersistentFlags().StringVarP(&serverName, "server_name", "", "", "server name") xtcpCmd.PersistentFlags().StringVarP(&localIp, "local_ip", "i", "127.0.0.1", "local ip") xtcpCmd.PersistentFlags().IntVarP(&localPort, "local_port", "l", 0, "local port") - xtcpCmd.PersistentFlags().StringVarP(&bindAddr, "bind_addr", "", "", "bind addr such as 127.0.0.1:9000") + xtcpCmd.PersistentFlags().StringVarP(&bindAddr, "bind_addr", "", "", "bind addr") + xtcpCmd.PersistentFlags().IntVarP(&bindPort, "bind_port", "", 0, "bind port") xtcpCmd.PersistentFlags().BoolVarP(&useEncryption, "ue", "", false, "use encryption") xtcpCmd.PersistentFlags().BoolVarP(&useCompression, "uc", "", false, "use compression") @@ -69,6 +70,7 @@ var xtcpCmd = &cobra.Command{ cfg.LocalIp = localIp cfg.LocalPort = localPort cfg.BindAddr = bindAddr + cfg.BindPort = bindPort cfg.UseEncryption = useEncryption cfg.UseCompression = useCompression diff --git a/models/config/proxy.go b/models/config/proxy.go index 9558f5b..8b6c082 100644 --- a/models/config/proxy.go +++ b/models/config/proxy.go @@ -675,6 +675,10 @@ func (cfg *StcpProxyConf) CheckForCli() (err error) { err = fmt.Errorf("bind_addr shouldn't be empty") return } + if cfg.BindPort == 0 { + err = fmt.Errorf("bind_port should be set") + return + } } return } @@ -777,6 +781,10 @@ func (cfg *XtcpProxyConf) CheckForCli() (err error) { err = fmt.Errorf("bind_addr shouldn't be empty") return } + if cfg.BindPort == 0 { + err = fmt.Errorf("bind_port should be set") + return + } } return } diff --git a/server/nathole.go b/server/nathole.go index 042ffcc..0ad0c48 100644 --- a/server/nathole.go +++ b/server/nathole.go @@ -106,10 +106,17 @@ func (nc *NatHoleController) HandleVisitor(m *msg.NatHoleVisitor, raddr *net.UDP } nc.mu.Lock() clientCfg, ok := nc.clientCfgs[m.ProxyName] - if !ok || m.SignKey != util.GetAuthKey(clientCfg.Sk, m.Timestamp) { + if !ok { nc.mu.Unlock() + log.Debug("xtcp server for [%s] doesn't exist", m.ProxyName) return } + if m.SignKey != util.GetAuthKey(clientCfg.Sk, m.Timestamp) { + nc.mu.Unlock() + log.Debug("xtcp connection of [%s] auth failed", m.ProxyName) + return + } + nc.sessions[sid] = session nc.mu.Unlock() log.Trace("handle visitor message, sid [%s]", sid)