Feature: support customizing bind-address when allow-lan is true (#255)
This commit is contained in:
parent
288afd1308
commit
5829c3d5be
5 changed files with 48 additions and 20 deletions
|
@ -88,6 +88,12 @@ socks-port: 7891
|
||||||
|
|
||||||
allow-lan: false
|
allow-lan: false
|
||||||
|
|
||||||
|
# Only applicable when setting allow-lan to true
|
||||||
|
# "*": bind all IP addresses
|
||||||
|
# 192.168.122.11: bind a single IPv4 address
|
||||||
|
# "[aaaa::a8aa:ff:fe09:57d8]": bind a single IPv6 address
|
||||||
|
bind-address: "*"
|
||||||
|
|
||||||
# Rule / Global/ Direct (default is Rule)
|
# Rule / Global/ Direct (default is Rule)
|
||||||
mode: Rule
|
mode: Rule
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,7 @@ type General struct {
|
||||||
RedirPort int `json:"redir-port"`
|
RedirPort int `json:"redir-port"`
|
||||||
Authentication []string `json:"authentication"`
|
Authentication []string `json:"authentication"`
|
||||||
AllowLan bool `json:"allow-lan"`
|
AllowLan bool `json:"allow-lan"`
|
||||||
|
BindAddress string `json:"bind-address"`
|
||||||
Mode T.Mode `json:"mode"`
|
Mode T.Mode `json:"mode"`
|
||||||
LogLevel log.LogLevel `json:"log-level"`
|
LogLevel log.LogLevel `json:"log-level"`
|
||||||
ExternalController string `json:"-"`
|
ExternalController string `json:"-"`
|
||||||
|
@ -81,6 +82,7 @@ type rawConfig struct {
|
||||||
RedirPort int `yaml:"redir-port"`
|
RedirPort int `yaml:"redir-port"`
|
||||||
Authentication []string `yaml:"authentication"`
|
Authentication []string `yaml:"authentication"`
|
||||||
AllowLan bool `yaml:"allow-lan"`
|
AllowLan bool `yaml:"allow-lan"`
|
||||||
|
BindAddress string `yaml:"bind-address"`
|
||||||
Mode T.Mode `yaml:"mode"`
|
Mode T.Mode `yaml:"mode"`
|
||||||
LogLevel log.LogLevel `yaml:"log-level"`
|
LogLevel log.LogLevel `yaml:"log-level"`
|
||||||
ExternalController string `yaml:"external-controller"`
|
ExternalController string `yaml:"external-controller"`
|
||||||
|
@ -129,6 +131,7 @@ func readConfig(path string) (*rawConfig, error) {
|
||||||
// config with some default value
|
// config with some default value
|
||||||
rawConfig := &rawConfig{
|
rawConfig := &rawConfig{
|
||||||
AllowLan: false,
|
AllowLan: false,
|
||||||
|
BindAddress: "*",
|
||||||
Mode: T.Rule,
|
Mode: T.Rule,
|
||||||
Authentication: []string{},
|
Authentication: []string{},
|
||||||
LogLevel: log.INFO,
|
LogLevel: log.INFO,
|
||||||
|
@ -192,6 +195,7 @@ func parseGeneral(cfg *rawConfig) (*General, error) {
|
||||||
socksPort := cfg.SocksPort
|
socksPort := cfg.SocksPort
|
||||||
redirPort := cfg.RedirPort
|
redirPort := cfg.RedirPort
|
||||||
allowLan := cfg.AllowLan
|
allowLan := cfg.AllowLan
|
||||||
|
bindAddress := cfg.BindAddress
|
||||||
externalController := cfg.ExternalController
|
externalController := cfg.ExternalController
|
||||||
externalUI := cfg.ExternalUI
|
externalUI := cfg.ExternalUI
|
||||||
secret := cfg.Secret
|
secret := cfg.Secret
|
||||||
|
@ -213,6 +217,7 @@ func parseGeneral(cfg *rawConfig) (*General, error) {
|
||||||
SocksPort: socksPort,
|
SocksPort: socksPort,
|
||||||
RedirPort: redirPort,
|
RedirPort: redirPort,
|
||||||
AllowLan: allowLan,
|
AllowLan: allowLan,
|
||||||
|
BindAddress: bindAddress,
|
||||||
Mode: mode,
|
Mode: mode,
|
||||||
LogLevel: logLevel,
|
LogLevel: logLevel,
|
||||||
ExternalController: externalController,
|
ExternalController: externalController,
|
||||||
|
|
|
@ -46,6 +46,7 @@ func GetGeneral() *config.General {
|
||||||
RedirPort: ports.RedirPort,
|
RedirPort: ports.RedirPort,
|
||||||
Authentication: authenticator,
|
Authentication: authenticator,
|
||||||
AllowLan: P.AllowLan(),
|
AllowLan: P.AllowLan(),
|
||||||
|
BindAddress: P.BindAddress(),
|
||||||
Mode: T.Instance().Mode(),
|
Mode: T.Instance().Mode(),
|
||||||
LogLevel: log.Level(),
|
LogLevel: log.Level(),
|
||||||
}
|
}
|
||||||
|
@ -105,6 +106,9 @@ func updateGeneral(general *config.General) {
|
||||||
allowLan := general.AllowLan
|
allowLan := general.AllowLan
|
||||||
P.SetAllowLan(allowLan)
|
P.SetAllowLan(allowLan)
|
||||||
|
|
||||||
|
bindAddress := general.BindAddress
|
||||||
|
P.SetBindAddress(bindAddress)
|
||||||
|
|
||||||
if err := P.ReCreateHTTP(general.Port); err != nil {
|
if err := P.ReCreateHTTP(general.Port); err != nil {
|
||||||
log.Errorln("Start HTTP server error: %s", err.Error())
|
log.Errorln("Start HTTP server error: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,12 +22,13 @@ func configRouter() http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
type configSchema struct {
|
type configSchema struct {
|
||||||
Port *int `json:"port"`
|
Port *int `json:"port"`
|
||||||
SocksPort *int `json:"socks-port"`
|
SocksPort *int `json:"socks-port"`
|
||||||
RedirPort *int `json:"redir-port"`
|
RedirPort *int `json:"redir-port"`
|
||||||
AllowLan *bool `json:"allow-lan"`
|
AllowLan *bool `json:"allow-lan"`
|
||||||
Mode *T.Mode `json:"mode"`
|
BindAddress *string `json:"bind-address"`
|
||||||
LogLevel *log.LogLevel `json:"log-level"`
|
Mode *T.Mode `json:"mode"`
|
||||||
|
LogLevel *log.LogLevel `json:"log-level"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConfigs(w http.ResponseWriter, r *http.Request) {
|
func getConfigs(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -55,6 +56,10 @@ func patchConfigs(w http.ResponseWriter, r *http.Request) {
|
||||||
P.SetAllowLan(*general.AllowLan)
|
P.SetAllowLan(*general.AllowLan)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if general.BindAddress != nil {
|
||||||
|
P.SetBindAddress(*general.BindAddress)
|
||||||
|
}
|
||||||
|
|
||||||
ports := P.GetPorts()
|
ports := P.GetPorts()
|
||||||
P.ReCreateHTTP(pointerOrDefault(general.Port, ports.Port))
|
P.ReCreateHTTP(pointerOrDefault(general.Port, ports.Port))
|
||||||
P.ReCreateSocks(pointerOrDefault(general.SocksPort, ports.SocksPort))
|
P.ReCreateSocks(pointerOrDefault(general.SocksPort, ports.SocksPort))
|
||||||
|
|
|
@ -11,7 +11,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
allowLan = false
|
allowLan = false
|
||||||
|
bindAddress = "*"
|
||||||
|
|
||||||
socksListener *socks.SockListener
|
socksListener *socks.SockListener
|
||||||
socksUDPListener *socks.SockUDPListener
|
socksUDPListener *socks.SockUDPListener
|
||||||
|
@ -34,12 +35,20 @@ func AllowLan() bool {
|
||||||
return allowLan
|
return allowLan
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BindAddress() string {
|
||||||
|
return bindAddress
|
||||||
|
}
|
||||||
|
|
||||||
func SetAllowLan(al bool) {
|
func SetAllowLan(al bool) {
|
||||||
allowLan = al
|
allowLan = al
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetBindAddress(host string) {
|
||||||
|
bindAddress = host
|
||||||
|
}
|
||||||
|
|
||||||
func ReCreateHTTP(port int) error {
|
func ReCreateHTTP(port int) error {
|
||||||
addr := genAddr(port, allowLan)
|
addr := genAddr(bindAddress, port, allowLan)
|
||||||
|
|
||||||
if httpListener != nil {
|
if httpListener != nil {
|
||||||
if httpListener.Address() == addr {
|
if httpListener.Address() == addr {
|
||||||
|
@ -63,7 +72,7 @@ func ReCreateHTTP(port int) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReCreateSocks(port int) error {
|
func ReCreateSocks(port int) error {
|
||||||
addr := genAddr(port, allowLan)
|
addr := genAddr(bindAddress, port, allowLan)
|
||||||
|
|
||||||
if socksListener != nil {
|
if socksListener != nil {
|
||||||
if socksListener.Address() == addr {
|
if socksListener.Address() == addr {
|
||||||
|
@ -83,12 +92,10 @@ func ReCreateSocks(port int) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return reCreateSocksUDP(port)
|
return reCreateSocksUDP(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func reCreateSocksUDP(port int) error {
|
func reCreateSocksUDP(addr string) error {
|
||||||
addr := genAddr(port, allowLan)
|
|
||||||
|
|
||||||
if socksUDPListener != nil {
|
if socksUDPListener != nil {
|
||||||
if socksUDPListener.Address() == addr {
|
if socksUDPListener.Address() == addr {
|
||||||
return nil
|
return nil
|
||||||
|
@ -97,10 +104,6 @@ func reCreateSocksUDP(port int) error {
|
||||||
socksUDPListener = nil
|
socksUDPListener = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if portIsZero(addr) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
socksUDPListener, err = socks.NewSocksUDPProxy(addr)
|
socksUDPListener, err = socks.NewSocksUDPProxy(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -111,7 +114,7 @@ func reCreateSocksUDP(port int) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReCreateRedir(port int) error {
|
func ReCreateRedir(port int) error {
|
||||||
addr := genAddr(port, allowLan)
|
addr := genAddr(bindAddress, port, allowLan)
|
||||||
|
|
||||||
if redirListener != nil {
|
if redirListener != nil {
|
||||||
if redirListener.Address() == addr {
|
if redirListener.Address() == addr {
|
||||||
|
@ -167,9 +170,14 @@ func portIsZero(addr string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func genAddr(port int, allowLan bool) string {
|
func genAddr(host string, port int, allowLan bool) string {
|
||||||
if allowLan {
|
if allowLan {
|
||||||
return fmt.Sprintf(":%d", port)
|
if host == "*" {
|
||||||
|
return fmt.Sprintf(":%d", port)
|
||||||
|
} else {
|
||||||
|
return fmt.Sprintf("%s:%d", host, port)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("127.0.0.1:%d", port)
|
return fmt.Sprintf("127.0.0.1:%d", port)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue