|
@ -2,20 +2,15 @@ package engine |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"errors" |
|
|
"errors" |
|
|
"fmt" |
|
|
|
|
|
"net" |
|
|
"net" |
|
|
"os" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/xjasonlyu/tun2socks/v2/component/dialer" |
|
|
"github.com/xjasonlyu/tun2socks/v2/component/dialer" |
|
|
"github.com/xjasonlyu/tun2socks/v2/core/device" |
|
|
"github.com/xjasonlyu/tun2socks/v2/core/device" |
|
|
"github.com/xjasonlyu/tun2socks/v2/core/stack" |
|
|
"github.com/xjasonlyu/tun2socks/v2/core/stack" |
|
|
"github.com/xjasonlyu/tun2socks/v2/internal/version" |
|
|
|
|
|
"github.com/xjasonlyu/tun2socks/v2/log" |
|
|
"github.com/xjasonlyu/tun2socks/v2/log" |
|
|
"github.com/xjasonlyu/tun2socks/v2/proxy" |
|
|
"github.com/xjasonlyu/tun2socks/v2/proxy" |
|
|
"github.com/xjasonlyu/tun2socks/v2/stats" |
|
|
"github.com/xjasonlyu/tun2socks/v2/stats" |
|
|
"github.com/xjasonlyu/tun2socks/v2/tunnel" |
|
|
"github.com/xjasonlyu/tun2socks/v2/tunnel" |
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3" |
|
|
|
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
var _engine = &engine{} |
|
|
var _engine = &engine{} |
|
@ -45,8 +40,6 @@ type Key struct { |
|
|
Device string `yaml:"device"` |
|
|
Device string `yaml:"device"` |
|
|
LogLevel string `yaml:"loglevel"` |
|
|
LogLevel string `yaml:"loglevel"` |
|
|
Interface string `yaml:"interface"` |
|
|
Interface string `yaml:"interface"` |
|
|
Config string `yaml:"-"` |
|
|
|
|
|
Version bool `yaml:"-"` |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
type engine struct { |
|
|
type engine struct { |
|
@ -62,22 +55,15 @@ func (e *engine) start() error { |
|
|
return errors.New("empty key") |
|
|
return errors.New("empty key") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if e.Version { |
|
|
|
|
|
fmt.Println(version.String()) |
|
|
|
|
|
fmt.Println(version.BuildString()) |
|
|
|
|
|
os.Exit(0) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for _, f := range []func() error{ |
|
|
for _, f := range []func() error{ |
|
|
e.setConfig, |
|
|
e.applyLogLevel, |
|
|
e.setLogLevel, |
|
|
e.applyMark, |
|
|
e.setMark, |
|
|
e.applyInterface, |
|
|
e.setInterface, |
|
|
e.applyStats, |
|
|
e.setStats, |
|
|
e.applyUDPTimeout, |
|
|
e.setUDPTimeout, |
|
|
e.applyProxy, |
|
|
e.setProxy, |
|
|
e.applyDevice, |
|
|
e.setDevice, |
|
|
e.applyStack, |
|
|
e.setStack, |
|
|
|
|
|
} { |
|
|
} { |
|
|
if err := f(); err != nil { |
|
|
if err := f(); err != nil { |
|
|
return err |
|
|
return err |
|
@ -97,19 +83,7 @@ func (e *engine) insert(k *Key) { |
|
|
e.Key = k |
|
|
e.Key = k |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (e *engine) setConfig() error { |
|
|
func (e *engine) applyLogLevel() 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) |
|
|
level, err := log.ParseLevel(e.LogLevel) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return err |
|
|
return err |
|
@ -118,7 +92,7 @@ func (e *engine) setLogLevel() error { |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (e *engine) setMark() error { |
|
|
func (e *engine) applyMark() error { |
|
|
if e.Mark != 0 { |
|
|
if e.Mark != 0 { |
|
|
dialer.SetMark(e.Mark) |
|
|
dialer.SetMark(e.Mark) |
|
|
log.Infof("[DIALER] set fwmark: %#x", e.Mark) |
|
|
log.Infof("[DIALER] set fwmark: %#x", e.Mark) |
|
@ -126,7 +100,7 @@ func (e *engine) setMark() error { |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (e *engine) setInterface() error { |
|
|
func (e *engine) applyInterface() error { |
|
|
if e.Interface != "" { |
|
|
if e.Interface != "" { |
|
|
if err := dialer.BindToInterface(e.Interface); err != nil { |
|
|
if err := dialer.BindToInterface(e.Interface); err != nil { |
|
|
return err |
|
|
return err |
|
@ -136,7 +110,7 @@ func (e *engine) setInterface() error { |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (e *engine) setStats() error { |
|
|
func (e *engine) applyStats() error { |
|
|
if e.Stats != "" { |
|
|
if e.Stats != "" { |
|
|
addr, err := net.ResolveTCPAddr("tcp", e.Stats) |
|
|
addr, err := net.ResolveTCPAddr("tcp", e.Stats) |
|
|
if err != nil { |
|
|
if err != nil { |
|
@ -153,14 +127,14 @@ func (e *engine) setStats() error { |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (e *engine) setUDPTimeout() error { |
|
|
func (e *engine) applyUDPTimeout() error { |
|
|
if e.UDPTimeout > 0 { |
|
|
if e.UDPTimeout > 0 { |
|
|
tunnel.SetUDPTimeout(e.UDPTimeout) |
|
|
tunnel.SetUDPTimeout(e.UDPTimeout) |
|
|
} |
|
|
} |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (e *engine) setProxy() (err error) { |
|
|
func (e *engine) applyProxy() (err error) { |
|
|
if e.Proxy == "" { |
|
|
if e.Proxy == "" { |
|
|
return errors.New("empty proxy") |
|
|
return errors.New("empty proxy") |
|
|
} |
|
|
} |
|
@ -170,7 +144,7 @@ func (e *engine) setProxy() (err error) { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (e *engine) setDevice() (err error) { |
|
|
func (e *engine) applyDevice() (err error) { |
|
|
if e.Device == "" { |
|
|
if e.Device == "" { |
|
|
return errors.New("empty device") |
|
|
return errors.New("empty device") |
|
|
} |
|
|
} |
|
@ -179,7 +153,7 @@ func (e *engine) setDevice() (err error) { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (e *engine) setStack() (err error) { |
|
|
func (e *engine) applyStack() (err error) { |
|
|
defer func() { |
|
|
defer func() { |
|
|
if err == nil { |
|
|
if err == nil { |
|
|
log.Infof( |
|
|
log.Infof( |
|
|