From 8e25f132014e0566c777320afb42ec685757d345 Mon Sep 17 00:00:00 2001 From: yuyulei Date: Wed, 10 Mar 2021 20:19:58 +0800 Subject: [PATCH] update: support ipv6 (#2288) --- client/admin.go | 4 +--- client/service.go | 3 ++- conf/frpc_full.ini | 1 + conf/frps_full.ini | 1 + pkg/util/net/kcp.go | 4 ++-- server/dashboard.go | 6 ++---- server/service.go | 27 ++++++++++++++++----------- 7 files changed, 25 insertions(+), 21 deletions(-) diff --git a/client/admin.go b/client/admin.go index fdef060f..b3485af2 100644 --- a/client/admin.go +++ b/client/admin.go @@ -15,7 +15,6 @@ package client import ( - "fmt" "net" "net/http" "time" @@ -31,7 +30,7 @@ var ( httpServerWriteTimeout = 10 * time.Second ) -func (svr *Service) RunAdminServer(addr string, port int) (err error) { +func (svr *Service) RunAdminServer(address string) (err error) { // url router router := mux.NewRouter() @@ -51,7 +50,6 @@ func (svr *Service) RunAdminServer(addr string, port int) (err error) { http.Redirect(w, r, "/static/", http.StatusMovedPermanently) }) - address := fmt.Sprintf("%s:%d", addr, port) server := &http.Server{ Addr: address, Handler: router, diff --git a/client/service.go b/client/service.go index c1627022..3033ee2a 100644 --- a/client/service.go +++ b/client/service.go @@ -129,7 +129,8 @@ func (svr *Service) Run() error { return fmt.Errorf("Load assets error: %v", err) } - err = svr.RunAdminServer(svr.cfg.AdminAddr, svr.cfg.AdminPort) + address := net.JoinHostPort(svr.cfg.AdminAddr, strconv.Itoa(svr.cfg.AdminPort)) + err = svr.RunAdminServer(address) if err != nil { log.Warn("run admin server error: %v", err) } diff --git a/conf/frpc_full.ini b/conf/frpc_full.ini index 01432cc0..be299b10 100644 --- a/conf/frpc_full.ini +++ b/conf/frpc_full.ini @@ -2,6 +2,7 @@ [common] # A literal address or host name for IPv6 must be enclosed # in square brackets, as in "[::1]:80", "[ipv6-host]:http" or "[ipv6-host%zone]:80" +# For single "server_addr" field, no need square brackets, like "server_addr = ::". server_addr = 0.0.0.0 server_port = 7000 diff --git a/conf/frps_full.ini b/conf/frps_full.ini index 2f00f5c3..a704cf4f 100644 --- a/conf/frps_full.ini +++ b/conf/frps_full.ini @@ -2,6 +2,7 @@ [common] # A literal address or host name for IPv6 must be enclosed # in square brackets, as in "[::1]:80", "[ipv6-host]:http" or "[ipv6-host%zone]:80" +# For single "bind_addr" field, no need square brackets, like "bind_addr = ::". bind_addr = 0.0.0.0 bind_port = 7000 diff --git a/pkg/util/net/kcp.go b/pkg/util/net/kcp.go index 2788b5f8..e9e0b126 100644 --- a/pkg/util/net/kcp.go +++ b/pkg/util/net/kcp.go @@ -27,8 +27,8 @@ type KCPListener struct { closeFlag bool } -func ListenKcp(bindAddr string, bindPort int) (l *KCPListener, err error) { - listener, err := kcp.ListenWithOptions(fmt.Sprintf("%s:%d", bindAddr, bindPort), nil, 10, 3) +func ListenKcp(address string) (l *KCPListener, err error) { + listener, err := kcp.ListenWithOptions(address, nil, 10, 3) if err != nil { return l, err } diff --git a/server/dashboard.go b/server/dashboard.go index 7af9df27..edac675e 100644 --- a/server/dashboard.go +++ b/server/dashboard.go @@ -15,7 +15,6 @@ package server import ( - "fmt" "net" "net/http" "time" @@ -32,7 +31,7 @@ var ( httpServerWriteTimeout = 10 * time.Second ) -func (svr *Service) RunDashboardServer(addr string, port int) (err error) { +func (svr *Service) RunDashboardServer(address string) (err error) { // url router router := mux.NewRouter() @@ -58,14 +57,13 @@ func (svr *Service) RunDashboardServer(addr string, port int) (err error) { http.Redirect(w, r, "/static/", http.StatusMovedPermanently) }) - address := fmt.Sprintf("%s:%d", addr, port) server := &http.Server{ Addr: address, Handler: router, ReadTimeout: httpServerReadTimeout, WriteTimeout: httpServerWriteTimeout, } - if address == "" { + if address == "" || address == ":" { address = ":http" } ln, err := net.Listen("tcp", address) diff --git a/server/service.go b/server/service.go index 0bbe9552..fe54cf78 100644 --- a/server/service.go +++ b/server/service.go @@ -23,6 +23,7 @@ import ( "net" "net/http" "sort" + "strconv" "time" "github.com/fatedier/frp/assets" @@ -176,7 +177,8 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) { } // Listen for accepting connections from client. - ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.BindAddr, cfg.BindPort)) + address := net.JoinHostPort(cfg.BindAddr, strconv.Itoa(cfg.BindPort)) + ln, err := net.Listen("tcp", address) if err != nil { err = fmt.Errorf("Create server listener error, %v", err) return @@ -187,13 +189,14 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) { ln = svr.muxer.DefaultListener() svr.listener = ln - log.Info("frps tcp listen on %s:%d", cfg.BindAddr, cfg.BindPort) + log.Info("frps tcp listen on %s", address) // Listen for accepting connections from client using kcp protocol. if cfg.KCPBindPort > 0 { - svr.kcpListener, err = frpNet.ListenKcp(cfg.BindAddr, cfg.KCPBindPort) + address := net.JoinHostPort(cfg.BindAddr, strconv.Itoa(cfg.KCPBindPort)) + svr.kcpListener, err = frpNet.ListenKcp(address) if err != nil { - err = fmt.Errorf("Listen on kcp address udp [%s:%d] error: %v", cfg.BindAddr, cfg.KCPBindPort, err) + err = fmt.Errorf("Listen on kcp address udp %s error: %v", address, err) return } log.Info("frps kcp listen on udp %s:%d", cfg.BindAddr, cfg.KCPBindPort) @@ -213,7 +216,7 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) { }, svr.httpVhostRouter) svr.rc.HTTPReverseProxy = rp - address := fmt.Sprintf("%s:%d", cfg.ProxyBindAddr, cfg.VhostHTTPPort) + address := net.JoinHostPort(cfg.ProxyBindAddr, strconv.Itoa(cfg.VhostHTTPPort)) server := &http.Server{ Addr: address, Handler: rp, @@ -238,11 +241,13 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) { if httpsMuxOn { l = svr.muxer.ListenHttps(1) } else { - l, err = net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.ProxyBindAddr, cfg.VhostHTTPSPort)) + address := net.JoinHostPort(cfg.ProxyBindAddr, strconv.Itoa(cfg.VhostHTTPSPort)) + l, err = net.Listen("tcp", address) if err != nil { err = fmt.Errorf("Create server listener error, %v", err) return } + log.Info("https service listen on %s", address) } svr.rc.VhostHTTPSMuxer, err = vhost.NewHTTPSMuxer(l, vhostReadWriteTimeout) @@ -250,7 +255,6 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) { err = fmt.Errorf("Create vhost httpsMuxer error, %v", err) return } - log.Info("https service listen on %s:%d", cfg.ProxyBindAddr, cfg.VhostHTTPSPort) } // frp tls listener @@ -261,14 +265,14 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) { // Create nat hole controller. if cfg.BindUDPPort > 0 { var nc *nathole.Controller - addr := fmt.Sprintf("%s:%d", cfg.BindAddr, cfg.BindUDPPort) - nc, err = nathole.NewController(addr) + address := net.JoinHostPort(cfg.BindAddr, strconv.Itoa(cfg.BindPort)) + nc, err = nathole.NewController(address) if err != nil { err = fmt.Errorf("Create nat hole controller error, %v", err) return } svr.rc.NatHoleController = nc - log.Info("nat hole udp service listen on %s:%d", cfg.BindAddr, cfg.BindUDPPort) + log.Info("nat hole udp service listen on %s", address) } var statsEnable bool @@ -281,7 +285,8 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) { return } - err = svr.RunDashboardServer(cfg.DashboardAddr, cfg.DashboardPort) + address := net.JoinHostPort(cfg.DashboardAddr, strconv.Itoa(cfg.DashboardPort)) + err = svr.RunDashboardServer(address) if err != nil { err = fmt.Errorf("Create dashboard web server error, %v", err) return