From 3400d23db57ece808532acd7cddcdb3cc65b4137 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8E=E6=B4=8B?= <2322013233@qq.com> Date: Thu, 22 Apr 2021 13:17:31 +0800 Subject: [PATCH] add vhost_bind_addr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 自定义Vhost绑定的IP --- cmd/frps/root.go | 3 +++ pkg/config/server.go | 7 +++++++ server/service.go | 8 ++++---- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/cmd/frps/root.go b/cmd/frps/root.go index cdf92672..004a6765 100644 --- a/cmd/frps/root.go +++ b/cmd/frps/root.go @@ -42,6 +42,7 @@ var ( bindUDPPort int kcpBindPort int proxyBindAddr string + vhostBindAddr string vhostHTTPPort int vhostHTTPSPort int vhostHTTPTimeout int64 @@ -73,6 +74,7 @@ func init() { rootCmd.PersistentFlags().IntVarP(&bindUDPPort, "bind_udp_port", "", 0, "bind udp port") rootCmd.PersistentFlags().IntVarP(&kcpBindPort, "kcp_bind_port", "", 0, "kcp bind udp port") rootCmd.PersistentFlags().StringVarP(&proxyBindAddr, "proxy_bind_addr", "", "0.0.0.0", "proxy bind address") + rootCmd.PersistentFlags().StringVarP(&proxyBindAddr, "vhost_bind_addr", "", "0.0.0.0", "vhost bind address") rootCmd.PersistentFlags().IntVarP(&vhostHTTPPort, "vhost_http_port", "", 0, "vhost http port") rootCmd.PersistentFlags().IntVarP(&vhostHTTPSPort, "vhost_https_port", "", 0, "vhost https port") rootCmd.PersistentFlags().Int64VarP(&vhostHTTPTimeout, "vhost_http_timeout", "", 60, "vhost http response header timeout") @@ -159,6 +161,7 @@ func parseServerCommonCfgFromCmd() (cfg config.ServerCommonConf, err error) { cfg.BindUDPPort = bindUDPPort cfg.KCPBindPort = kcpBindPort cfg.ProxyBindAddr = proxyBindAddr + cfg.VhostBindAddr = vhostBindAddr cfg.VhostHTTPPort = vhostHTTPPort cfg.VhostHTTPSPort = vhostHTTPSPort cfg.VhostHTTPTimeout = vhostHTTPTimeout diff --git a/pkg/config/server.go b/pkg/config/server.go index 3603bf38..c63f7e88 100644 --- a/pkg/config/server.go +++ b/pkg/config/server.go @@ -48,6 +48,9 @@ type ServerCommonConf struct { // ProxyBindAddr specifies the address that the proxy binds to. This value // may be the same as BindAddr. ProxyBindAddr string `ini:"proxy_bind_addr" json:"proxy_bind_addr"` + // VhostBindAddr specifies the address that the vhost binds to. This value + // may be the same as BindAddr. + VhostBindAddr string `ini:"vhost_bind_addr" json:"vhost_bind_addr"` // VhostHTTPPort specifies the port that the server listens for HTTP Vhost // requests. If this value is 0, the server will not listen for HTTP // requests. By default, this value is 0. @@ -175,6 +178,7 @@ func GetDefaultServerConf() ServerCommonConf { BindUDPPort: 0, KCPBindPort: 0, ProxyBindAddr: "", + VhostBindAddr: "", VhostHTTPPort: 0, VhostHTTPSPort: 0, TCPMuxHTTPConnectPort: 0, @@ -275,6 +279,9 @@ func (cfg *ServerCommonConf) Complete() { if cfg.ProxyBindAddr == "" { cfg.ProxyBindAddr = cfg.BindAddr } + if cfg.VhostBindAddr == "" { + cfg.VhostBindAddr = cfg.BindAddr + } if cfg.TLSTrustedCaFile != "" { cfg.TLSOnly = true diff --git a/server/service.go b/server/service.go index 677bda0d..e8dff6f0 100644 --- a/server/service.go +++ b/server/service.go @@ -167,7 +167,7 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) { httpMuxOn bool httpsMuxOn bool ) - if cfg.BindAddr == cfg.ProxyBindAddr { + if cfg.BindAddr == cfg.VhostBindAddr { if cfg.BindPort == cfg.VhostHTTPPort { httpMuxOn = true } @@ -216,7 +216,7 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) { }, svr.httpVhostRouter) svr.rc.HTTPReverseProxy = rp - address := net.JoinHostPort(cfg.ProxyBindAddr, strconv.Itoa(cfg.VhostHTTPPort)) + address := net.JoinHostPort(cfg.VhostBindAddr, strconv.Itoa(cfg.VhostHTTPPort)) server := &http.Server{ Addr: address, Handler: rp, @@ -232,7 +232,7 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) { } } go server.Serve(l) - log.Info("http service listen on %s:%d", cfg.ProxyBindAddr, cfg.VhostHTTPPort) + log.Info("http service listen on %s:%d", cfg.VhostBindAddr, cfg.VhostHTTPPort) } // Create https vhost muxer. @@ -241,7 +241,7 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) { if httpsMuxOn { l = svr.muxer.ListenHttps(1) } else { - address := net.JoinHostPort(cfg.ProxyBindAddr, strconv.Itoa(cfg.VhostHTTPSPort)) + address := net.JoinHostPort(cfg.VhostBindAddr, strconv.Itoa(cfg.VhostHTTPSPort)) l, err = net.Listen("tcp", address) if err != nil { err = fmt.Errorf("Create server listener error, %v", err)