Feature: YAML config support

This commit is contained in:
xjasonlyu
2021-11-10 13:38:20 +08:00
parent 733c4cb779
commit 5ee4c676a1
6 changed files with 58 additions and 14 deletions

26
config.example.yml Normal file
View File

@@ -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

View File

@@ -11,6 +11,8 @@ import (
"github.com/xjasonlyu/tun2socks/proxy" "github.com/xjasonlyu/tun2socks/proxy"
"github.com/xjasonlyu/tun2socks/stats" "github.com/xjasonlyu/tun2socks/stats"
"github.com/xjasonlyu/tun2socks/tunnel" "github.com/xjasonlyu/tun2socks/tunnel"
"gopkg.in/yaml.v3"
) )
var _engine = &engine{} var _engine = &engine{}
@@ -31,16 +33,17 @@ func Insert(k *Key) {
} }
type Key struct { type Key struct {
MTU int MTU int `yaml:"mtu"`
Mark int Mark int `yaml:"fwmark"`
UDPTimeout int UDPTimeout int `yaml:"udp-timeout"`
Proxy string Proxy string `yaml:"proxy"`
Stats string Stats string `yaml:"stats"`
Token string Token string `yaml:"token"`
Device string Device string `yaml:"device"`
LogLevel string LogLevel string `yaml:"loglevel"`
Interface string Interface string `yaml:"interface"`
Version bool Config string `yaml:"-"`
Version bool `yaml:"-"`
} }
type engine struct { type engine struct {
@@ -62,6 +65,7 @@ func (e *engine) start() error {
} }
for _, f := range []func() error{ for _, f := range []func() error{
e.setConfig,
e.setLogLevel, e.setLogLevel,
e.setMark, e.setMark,
e.setInterface, e.setInterface,
@@ -89,6 +93,18 @@ func (e *engine) insert(k *Key) {
e.Key = k 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 { func (e *engine) setLogLevel() error {
level, err := log.ParseLevel(e.LogLevel) level, err := log.ParseLevel(e.LogLevel)
if err != nil { if err != nil {

2
go.mod
View File

@@ -17,6 +17,7 @@ require (
golang.org/x/sys v0.0.0-20211031064116-611d5d643895 golang.org/x/sys v0.0.0-20211031064116-611d5d643895
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac
golang.zx2c4.com/wireguard v0.0.0-20211030003956-52704c4b9288 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 gvisor.dev/gvisor v0.0.0-20211029210705-806fa5c3235c
) )
@@ -24,5 +25,4 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/btree v1.0.1 // indirect github.com/google/btree v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
) )

3
go.sum
View File

@@ -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= 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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 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-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 h1:S4L2OVKs48LB9vVbPaiEfRqon+dn0wm3MABd2cyvjkc=
gvisor.dev/gvisor v0.0.0-20211029210705-806fa5c3235c/go.mod h1:btyTBPTxT8AFMvW7yctFJ2nPCEDWZLpmKQEZ0gG+bbQ= gvisor.dev/gvisor v0.0.0-20211029210705-806fa5c3235c/go.mod h1:btyTBPTxT8AFMvW7yctFJ2nPCEDWZLpmKQEZ0gG+bbQ=

View File

@@ -60,7 +60,7 @@ func ParseLevel(lvl string) (Level, error) {
return SilentLevel, nil return SilentLevel, nil
case "error": case "error":
return ErrorLevel, nil return ErrorLevel, nil
case "warn": case "warning":
return WarnLevel, nil return WarnLevel, nil
case "info": case "info":
return InfoLevel, nil return InfoLevel, nil

View File

@@ -17,9 +17,10 @@ func init() {
flag.IntVar(&key.MTU, "mtu", 0, "Set device maximum transmission unit (MTU)") 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.IntVar(&key.UDPTimeout, "udp-timeout", 0, "Set timeout for each UDP session")
flag.BoolVar(&key.Version, "version", false, "Show version information and quit") 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.Device, "device", "", "Use this device [driver://]name")
flag.StringVar(&key.Interface, "interface", "", "Use network INTERFACE (Linux/MacOS only)") 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.Proxy, "proxy", "", "Use this proxy [protocol://]host[:port]")
flag.StringVar(&key.Stats, "stats", "", "HTTP statistic server listen address") flag.StringVar(&key.Stats, "stats", "", "HTTP statistic server listen address")
flag.StringVar(&key.Token, "token", "", "HTTP statistic server auth token") flag.StringVar(&key.Token, "token", "", "HTTP statistic server auth token")