diff --git a/config.example.yml b/config.example.yml new file mode 100644 index 0000000..66cd4b2 --- /dev/null +++ b/config.example.yml @@ -0,0 +1,26 @@ +# debug / info / warning / error / silent +loglevel: debug + +# URL format: [protocol://]host[:port] +proxy: direct:// + +# URL format: [driver://]name +device: tun://tun0 + +# Maximum transmission unit for each packet +mtu: 1500 + +# Network interface to bind, Linux/macOS only +interface: eth0 + +# Timeout for each UDP session, default value: 60 seconds +udp-timeout: 60 + +# SO_MARK socket option, Linux only +fwmark: 0 + +# Statistic server listen address +stats: 0.0.0.0:9000 + +# Statistic server authorization token +token: secret diff --git a/engine/engine.go b/engine/engine.go index 00eda02..2bb0bae 100755 --- a/engine/engine.go +++ b/engine/engine.go @@ -11,6 +11,8 @@ import ( "github.com/xjasonlyu/tun2socks/proxy" "github.com/xjasonlyu/tun2socks/stats" "github.com/xjasonlyu/tun2socks/tunnel" + + "gopkg.in/yaml.v3" ) var _engine = &engine{} @@ -31,16 +33,17 @@ func Insert(k *Key) { } type Key struct { - MTU int - Mark int - UDPTimeout int - Proxy string - Stats string - Token string - Device string - LogLevel string - Interface string - Version bool + MTU int `yaml:"mtu"` + Mark int `yaml:"fwmark"` + UDPTimeout int `yaml:"udp-timeout"` + Proxy string `yaml:"proxy"` + Stats string `yaml:"stats"` + Token string `yaml:"token"` + Device string `yaml:"device"` + LogLevel string `yaml:"loglevel"` + Interface string `yaml:"interface"` + Config string `yaml:"-"` + Version bool `yaml:"-"` } type engine struct { @@ -62,6 +65,7 @@ func (e *engine) start() error { } for _, f := range []func() error{ + e.setConfig, e.setLogLevel, e.setMark, e.setInterface, @@ -89,6 +93,18 @@ func (e *engine) insert(k *Key) { e.Key = k } +func (e *engine) setConfig() error { + if e.Config == "" { + return nil + } + + data, err := os.ReadFile(e.Config) + if err != nil { + return err + } + return yaml.Unmarshal(data, e.Key) +} + func (e *engine) setLogLevel() error { level, err := log.ParseLevel(e.LogLevel) if err != nil { diff --git a/go.mod b/go.mod index 7cabcc4..dd85460 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( golang.org/x/sys v0.0.0-20211031064116-611d5d643895 golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac golang.zx2c4.com/wireguard v0.0.0-20211030003956-52704c4b9288 + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b gvisor.dev/gvisor v0.0.0-20211029210705-806fa5c3235c ) @@ -24,5 +25,4 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/google/btree v1.0.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) diff --git a/go.sum b/go.sum index fc317a4..2d2eff1 100644 --- a/go.sum +++ b/go.sum @@ -45,7 +45,8 @@ golang.zx2c4.com/wireguard v0.0.0-20211030003956-52704c4b9288 h1:v3PAPzkDfgUxCCH golang.zx2c4.com/wireguard v0.0.0-20211030003956-52704c4b9288/go.mod h1:RTjaYEQboNk7+2qfPGBotaMEh/5HIvmPZ6DIe10lTqI= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gvisor.dev/gvisor v0.0.0-20211029210705-806fa5c3235c h1:S4L2OVKs48LB9vVbPaiEfRqon+dn0wm3MABd2cyvjkc= gvisor.dev/gvisor v0.0.0-20211029210705-806fa5c3235c/go.mod h1:btyTBPTxT8AFMvW7yctFJ2nPCEDWZLpmKQEZ0gG+bbQ= diff --git a/log/level.go b/log/level.go index f8f75c7..0b30949 100755 --- a/log/level.go +++ b/log/level.go @@ -60,7 +60,7 @@ func ParseLevel(lvl string) (Level, error) { return SilentLevel, nil case "error": return ErrorLevel, nil - case "warn": + case "warning": return WarnLevel, nil case "info": return InfoLevel, nil diff --git a/main.go b/main.go index 8966ee5..fe23e5d 100755 --- a/main.go +++ b/main.go @@ -17,9 +17,10 @@ func init() { flag.IntVar(&key.MTU, "mtu", 0, "Set device maximum transmission unit (MTU)") flag.IntVar(&key.UDPTimeout, "udp-timeout", 0, "Set timeout for each UDP session") flag.BoolVar(&key.Version, "version", false, "Show version information and quit") + flag.StringVar(&key.Config, "config", "", "YAML format configuration file") flag.StringVar(&key.Device, "device", "", "Use this device [driver://]name") flag.StringVar(&key.Interface, "interface", "", "Use network INTERFACE (Linux/MacOS only)") - flag.StringVar(&key.LogLevel, "loglevel", "info", "Log level [debug|info|warn|error|silent]") + flag.StringVar(&key.LogLevel, "loglevel", "info", "Log level [debug|info|warning|error|silent]") flag.StringVar(&key.Proxy, "proxy", "", "Use this proxy [protocol://]host[:port]") flag.StringVar(&key.Stats, "stats", "", "HTTP statistic server listen address") flag.StringVar(&key.Token, "token", "", "HTTP statistic server auth token")