From c652b8ef070507344f2cdd7230af1c6c10f0ee4c Mon Sep 17 00:00:00 2001 From: fatedier Date: Tue, 14 Jun 2022 14:24:34 +0800 Subject: [PATCH] fix ipv6 address parsing (#2978) --- client/admin_api.go | 20 +++++++++++--------- client/proxy/proxy.go | 9 ++++----- client/visitor.go | 2 +- pkg/config/proxy.go | 4 +++- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/client/admin_api.go b/client/admin_api.go index 5d40e2afb4f..e434fcb19a7 100644 --- a/client/admin_api.go +++ b/client/admin_api.go @@ -18,9 +18,11 @@ import ( "encoding/json" "fmt" "io" + "net" "net/http" "os" "sort" + "strconv" "strings" "github.com/fatedier/frp/client/proxy" @@ -105,48 +107,48 @@ func NewProxyStatusResp(status *proxy.WorkingStatus, serverAddr string) ProxySta switch cfg := status.Cfg.(type) { case *config.TCPProxyConf: if cfg.LocalPort != 0 { - psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort) + psr.LocalAddr = net.JoinHostPort(cfg.LocalIP, strconv.Itoa(cfg.LocalPort)) } psr.Plugin = cfg.Plugin if status.Err != "" { - psr.RemoteAddr = fmt.Sprintf("%s:%d", serverAddr, cfg.RemotePort) + psr.RemoteAddr = net.JoinHostPort(serverAddr, strconv.Itoa(cfg.RemotePort)) } else { psr.RemoteAddr = serverAddr + status.RemoteAddr } case *config.UDPProxyConf: if cfg.LocalPort != 0 { - psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort) + psr.LocalAddr = net.JoinHostPort(cfg.LocalIP, strconv.Itoa(cfg.LocalPort)) } if status.Err != "" { - psr.RemoteAddr = fmt.Sprintf("%s:%d", serverAddr, cfg.RemotePort) + psr.RemoteAddr = net.JoinHostPort(serverAddr, strconv.Itoa(cfg.RemotePort)) } else { psr.RemoteAddr = serverAddr + status.RemoteAddr } case *config.HTTPProxyConf: if cfg.LocalPort != 0 { - psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort) + psr.LocalAddr = net.JoinHostPort(cfg.LocalIP, strconv.Itoa(cfg.LocalPort)) } psr.Plugin = cfg.Plugin psr.RemoteAddr = status.RemoteAddr case *config.HTTPSProxyConf: if cfg.LocalPort != 0 { - psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort) + psr.LocalAddr = net.JoinHostPort(cfg.LocalIP, strconv.Itoa(cfg.LocalPort)) } psr.Plugin = cfg.Plugin psr.RemoteAddr = status.RemoteAddr case *config.STCPProxyConf: if cfg.LocalPort != 0 { - psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort) + psr.LocalAddr = net.JoinHostPort(cfg.LocalIP, strconv.Itoa(cfg.LocalPort)) } psr.Plugin = cfg.Plugin case *config.XTCPProxyConf: if cfg.LocalPort != 0 { - psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort) + psr.LocalAddr = net.JoinHostPort(cfg.LocalIP, strconv.Itoa(cfg.LocalPort)) } psr.Plugin = cfg.Plugin case *config.SUDPProxyConf: if cfg.LocalPort != 0 { - psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort) + psr.LocalAddr = net.JoinHostPort(cfg.LocalIP, strconv.Itoa(cfg.LocalPort)) } psr.Plugin = cfg.Plugin } diff --git a/client/proxy/proxy.go b/client/proxy/proxy.go index df5e87d86c9..340da950c30 100644 --- a/client/proxy/proxy.go +++ b/client/proxy/proxy.go @@ -17,7 +17,6 @@ package proxy import ( "bytes" "context" - "fmt" "io" "net" "strconv" @@ -307,7 +306,7 @@ func (pxy *XTCPProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) { Sid: natHoleSidMsg.Sid, } raddr, _ := net.ResolveUDPAddr("udp", - fmt.Sprintf("%s:%d", pxy.clientCfg.ServerAddr, pxy.serverUDPPort)) + net.JoinHostPort(pxy.clientCfg.ServerAddr, strconv.Itoa(pxy.serverUDPPort))) clientConn, err := net.DialUDP("udp", nil, raddr) if err != nil { xl.Error("dial server udp addr error: %v", err) @@ -415,7 +414,7 @@ func (pxy *XTCPProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) { } func (pxy *XTCPProxy) sendDetectMsg(addr string, port int, laddr *net.UDPAddr, content []byte) (err error) { - daddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", addr, port)) + daddr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(addr, strconv.Itoa(port))) if err != nil { return err } @@ -448,7 +447,7 @@ type UDPProxy struct { } func (pxy *UDPProxy) Run() (err error) { - pxy.localAddr, err = net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", pxy.cfg.LocalIP, pxy.cfg.LocalPort)) + pxy.localAddr, err = net.ResolveUDPAddr("udp", net.JoinHostPort(pxy.cfg.LocalIP, strconv.Itoa(pxy.cfg.LocalPort))) if err != nil { return } @@ -570,7 +569,7 @@ type SUDPProxy struct { } func (pxy *SUDPProxy) Run() (err error) { - pxy.localAddr, err = net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", pxy.cfg.LocalIP, pxy.cfg.LocalPort)) + pxy.localAddr, err = net.ResolveUDPAddr("udp", net.JoinHostPort(pxy.cfg.LocalIP, strconv.Itoa(pxy.cfg.LocalPort))) if err != nil { return } diff --git a/client/visitor.go b/client/visitor.go index ea548298ef0..ba1cd7cb98b 100644 --- a/client/visitor.go +++ b/client/visitor.go @@ -212,7 +212,7 @@ func (sv *XTCPVisitor) handleConn(userConn net.Conn) { } raddr, err := net.ResolveUDPAddr("udp", - fmt.Sprintf("%s:%d", sv.ctl.clientCfg.ServerAddr, sv.ctl.serverUDPPort)) + net.JoinHostPort(sv.ctl.clientCfg.ServerAddr, strconv.Itoa(sv.ctl.serverUDPPort))) if err != nil { xl.Error("resolve server UDP addr error") return diff --git a/pkg/config/proxy.go b/pkg/config/proxy.go index eb73cb3795e..e168520d377 100644 --- a/pkg/config/proxy.go +++ b/pkg/config/proxy.go @@ -16,7 +16,9 @@ package config import ( "fmt" + "net" "reflect" + "strconv" "strings" "github.com/fatedier/frp/pkg/consts" @@ -372,7 +374,7 @@ func (cfg *BaseProxyConf) decorate(prefix string, name string, section *ini.Sect } if cfg.HealthCheckType == "http" && cfg.Plugin == "" && cfg.HealthCheckURL != "" { - s := fmt.Sprintf("http://%s:%d", cfg.LocalIP, cfg.LocalPort) + s := "http://" + net.JoinHostPort(cfg.LocalIP, strconv.Itoa(cfg.LocalPort)) if !strings.HasPrefix(cfg.HealthCheckURL, "/") { s += "/" }