Add new option for preferring of new IP

This commit is contained in:
9seconds
2020-03-20 12:51:56 +03:00
parent d32ab34e60
commit 07f00a363e
2 changed files with 26 additions and 0 deletions
+20
View File
@@ -32,6 +32,13 @@ const (
SecretModeTLS
)
type PreferIP uint8
const (
PreferIPv4 PreferIP = iota
PreferIPv6
)
const SimpleSecretLength = 16
type OptionType uint8
@@ -40,6 +47,8 @@ const (
OptionTypeDebug OptionType = iota
OptionTypeVerbose
OptionTypePreferIP
OptionTypeBind
OptionTypePublicIPv4
OptionTypePublicIPv6
@@ -86,6 +95,7 @@ type Config struct {
Debug bool `json:"debug"`
Verbose bool `json:"verbose"`
SecretMode SecretMode `json:"secret_mode"`
PreferIP PreferIP `json:"prefer_ip"`
Secret []byte `json:"secret"`
AdTag []byte `json:"adtag"`
@@ -105,6 +115,16 @@ func Init(options ...Opt) error { // nolint: gocyclo, funlen
C.Debug = opt.Value.(bool)
case OptionTypeVerbose:
C.Verbose = opt.Value.(bool)
case OptionTypePreferIP:
value := opt.Value.(string)
switch value {
case "ipv4":
C.PreferIP = PreferIPv4
case "ipv6":
C.PreferIP = PreferIPv6
default:
return fmt.Errorf("incorrect direct IP mode %s", value)
}
case OptionTypeBind:
C.Bind = opt.Value.(*net.TCPAddr)
case OptionTypePublicIPv4:
+6
View File
@@ -43,6 +43,11 @@ var (
Short('v').
Envar("MTG_VERBOSE").
Bool()
runPreferIP = runCommand.Flag("prefer-ip",
"Prefer this IP protocol if possible. Valid options are 'ipv4' and 'ipv6'").
Envar("MTG_PREFER_DIRECT_IP").
Default("ipv6").
Enum("ipv4", "ipv6")
runBind = runCommand.Flag("bind",
"Host:Port to bind proxy to.").
Short('b').
@@ -130,6 +135,7 @@ func main() {
err := config.Init(
config.Opt{Option: config.OptionTypeDebug, Value: *runDebug},
config.Opt{Option: config.OptionTypeVerbose, Value: *runVerbose},
config.Opt{Option: config.OptionTypePreferIP, Value: *runPreferIP},
config.Opt{Option: config.OptionTypeBind, Value: *runBind},
config.Opt{Option: config.OptionTypePublicIPv4, Value: *runPublicIPv4},
config.Opt{Option: config.OptionTypePublicIPv6, Value: *runPublicIPv6},