add vhost_bind_addr

自定义Vhost绑定的IP
This commit is contained in:
于洋 2021-04-22 13:17:31 +08:00
parent fbaa5f866e
commit 3400d23db5
3 changed files with 14 additions and 4 deletions

View File

@ -42,6 +42,7 @@ var (
bindUDPPort int bindUDPPort int
kcpBindPort int kcpBindPort int
proxyBindAddr string proxyBindAddr string
vhostBindAddr string
vhostHTTPPort int vhostHTTPPort int
vhostHTTPSPort int vhostHTTPSPort int
vhostHTTPTimeout int64 vhostHTTPTimeout int64
@ -73,6 +74,7 @@ func init() {
rootCmd.PersistentFlags().IntVarP(&bindUDPPort, "bind_udp_port", "", 0, "bind udp port") 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().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, "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(&vhostHTTPPort, "vhost_http_port", "", 0, "vhost http port")
rootCmd.PersistentFlags().IntVarP(&vhostHTTPSPort, "vhost_https_port", "", 0, "vhost https 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") 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.BindUDPPort = bindUDPPort
cfg.KCPBindPort = kcpBindPort cfg.KCPBindPort = kcpBindPort
cfg.ProxyBindAddr = proxyBindAddr cfg.ProxyBindAddr = proxyBindAddr
cfg.VhostBindAddr = vhostBindAddr
cfg.VhostHTTPPort = vhostHTTPPort cfg.VhostHTTPPort = vhostHTTPPort
cfg.VhostHTTPSPort = vhostHTTPSPort cfg.VhostHTTPSPort = vhostHTTPSPort
cfg.VhostHTTPTimeout = vhostHTTPTimeout cfg.VhostHTTPTimeout = vhostHTTPTimeout

View File

@ -48,6 +48,9 @@ type ServerCommonConf struct {
// ProxyBindAddr specifies the address that the proxy binds to. This value // ProxyBindAddr specifies the address that the proxy binds to. This value
// may be the same as BindAddr. // may be the same as BindAddr.
ProxyBindAddr string `ini:"proxy_bind_addr" json:"proxy_bind_addr"` 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 // 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. If this value is 0, the server will not listen for HTTP
// requests. By default, this value is 0. // requests. By default, this value is 0.
@ -175,6 +178,7 @@ func GetDefaultServerConf() ServerCommonConf {
BindUDPPort: 0, BindUDPPort: 0,
KCPBindPort: 0, KCPBindPort: 0,
ProxyBindAddr: "", ProxyBindAddr: "",
VhostBindAddr: "",
VhostHTTPPort: 0, VhostHTTPPort: 0,
VhostHTTPSPort: 0, VhostHTTPSPort: 0,
TCPMuxHTTPConnectPort: 0, TCPMuxHTTPConnectPort: 0,
@ -275,6 +279,9 @@ func (cfg *ServerCommonConf) Complete() {
if cfg.ProxyBindAddr == "" { if cfg.ProxyBindAddr == "" {
cfg.ProxyBindAddr = cfg.BindAddr cfg.ProxyBindAddr = cfg.BindAddr
} }
if cfg.VhostBindAddr == "" {
cfg.VhostBindAddr = cfg.BindAddr
}
if cfg.TLSTrustedCaFile != "" { if cfg.TLSTrustedCaFile != "" {
cfg.TLSOnly = true cfg.TLSOnly = true

View File

@ -167,7 +167,7 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) {
httpMuxOn bool httpMuxOn bool
httpsMuxOn bool httpsMuxOn bool
) )
if cfg.BindAddr == cfg.ProxyBindAddr { if cfg.BindAddr == cfg.VhostBindAddr {
if cfg.BindPort == cfg.VhostHTTPPort { if cfg.BindPort == cfg.VhostHTTPPort {
httpMuxOn = true httpMuxOn = true
} }
@ -216,7 +216,7 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) {
}, svr.httpVhostRouter) }, svr.httpVhostRouter)
svr.rc.HTTPReverseProxy = rp 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{ server := &http.Server{
Addr: address, Addr: address,
Handler: rp, Handler: rp,
@ -232,7 +232,7 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) {
} }
} }
go server.Serve(l) 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. // Create https vhost muxer.
@ -241,7 +241,7 @@ func NewService(cfg config.ServerCommonConf) (svr *Service, err error) {
if httpsMuxOn { if httpsMuxOn {
l = svr.muxer.ListenHttps(1) l = svr.muxer.ListenHttps(1)
} else { } 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) l, err = net.Listen("tcp", address)
if err != nil { if err != nil {
err = fmt.Errorf("Create server listener error, %v", err) err = fmt.Errorf("Create server listener error, %v", err)