Refactor(engine): remove struct

This commit is contained in:
xjasonlyu
2022-03-30 23:09:50 +08:00
parent cba7e19d22
commit abdbaa6b83
3 changed files with 77 additions and 83 deletions
+64 -83
View File
@@ -6,7 +6,6 @@ import (
"github.com/xjasonlyu/tun2socks/v2/component/dialer" "github.com/xjasonlyu/tun2socks/v2/component/dialer"
"github.com/xjasonlyu/tun2socks/v2/core" "github.com/xjasonlyu/tun2socks/v2/core"
"github.com/xjasonlyu/tun2socks/v2/core/device" "github.com/xjasonlyu/tun2socks/v2/core/device"
_ "github.com/xjasonlyu/tun2socks/v2/dns"
"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/restapi" "github.com/xjasonlyu/tun2socks/v2/restapi"
@@ -16,106 +15,93 @@ import (
"gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/stack"
) )
var _engine = &engine{} var (
// _defaultKey holds the default key for the engine.
_defaultKey *Key
// _defaultProxy holds the default proxy for the engine.
_defaultProxy proxy.Proxy
// _defaultDevice holds the default device for the engine.
_defaultDevice device.Device
// _defaultStack holds the default stack for the engine.
_defaultStack *stack.Stack
)
// Start starts the default engine up. // Start starts the default engine up.
func Start() { func Start() {
if err := _engine.start(); err != nil { if err := start(); err != nil {
log.Fatalf("[ENGINE] failed to start: %v", err) log.Fatalf("[ENGINE] failed to start: %v", err)
} }
} }
// Stop shuts the default engine down. // Stop shuts the default engine down.
func Stop() { func Stop() {
if err := _engine.stop(); err != nil { if err := stop(); err != nil {
log.Fatalf("[ENGINE] failed to stop: %v", err) log.Fatalf("[ENGINE] failed to stop: %v", err)
} }
} }
// Insert loads *Key to the default engine. // Insert loads *Key to the default engine.
func Insert(k *Key) { func Insert(k *Key) {
_engine.insert(k) _defaultKey = k
} }
type Key struct { func start() error {
MTU int `yaml:"mtu"` if _defaultKey == nil {
Mark int `yaml:"fwmark"`
UDPTimeout int `yaml:"udp-timeout"`
Proxy string `yaml:"proxy"`
RestAPI string `yaml:"restapi"`
Device string `yaml:"device"`
LogLevel string `yaml:"loglevel"`
Interface string `yaml:"interface"`
}
type engine struct {
*Key
stack *stack.Stack
proxy proxy.Proxy
device device.Device
}
func (e *engine) start() error {
if e.Key == nil {
return errors.New("empty key") return errors.New("empty key")
} }
for _, f := range []func() error{ for _, f := range []func(*Key) error{
e.withGeneral, general,
e.withRestAPI, restAPI,
e.withProxy, netstack,
e.withDevice,
e.withStack,
} { } {
if err := f(); err != nil { if err := f(_defaultKey); err != nil {
return err return err
} }
} }
return nil return nil
} }
func (e *engine) stop() (err error) { func stop() (err error) {
if e.device != nil { if _defaultDevice != nil {
err = e.device.Close() err = _defaultDevice.Close()
} }
if e.stack != nil { if _defaultStack != nil {
e.stack.Close() _defaultStack.Close()
e.stack.Wait() _defaultStack.Wait()
} }
return err return err
} }
func (e *engine) insert(k *Key) { func general(k *Key) error {
e.Key = k level, err := log.ParseLevel(k.LogLevel)
}
func (e *engine) withGeneral() error {
level, err := log.ParseLevel(e.LogLevel)
if err != nil { if err != nil {
return err return err
} }
log.SetLevel(level) log.SetLevel(level)
if e.Interface != "" { if k.Interface != "" {
dialer.DefaultInterfaceName.Store(e.Interface) dialer.DefaultInterfaceName.Store(k.Interface)
log.Infof("[DIALER] bind to interface: %s", e.Interface) log.Infof("[DIALER] bind to interface: %s", k.Interface)
} }
if e.Mark != 0 { if k.Mark != 0 {
dialer.DefaultRoutingMark.Store(int32(e.Mark)) dialer.DefaultRoutingMark.Store(int32(k.Mark))
log.Infof("[DIALER] set fwmark: %#x", e.Mark) log.Infof("[DIALER] set fwmark: %#x", k.Mark)
} }
if e.UDPTimeout > 0 { if k.UDPTimeout > 0 {
tunnel.SetUDPTimeout(e.UDPTimeout) tunnel.SetUDPTimeout(k.UDPTimeout)
} }
return nil return nil
} }
func (e *engine) withRestAPI() error { func restAPI(k *Key) error {
if e.RestAPI != "" { if k.RestAPI != "" {
u, err := parseRestAPI(e.RestAPI) u, err := parseRestAPI(k.RestAPI)
if err != nil { if err != nil {
return err return err
} }
@@ -131,42 +117,37 @@ func (e *engine) withRestAPI() error {
return nil return nil
} }
func (e *engine) withProxy() (err error) { func netstack(k *Key) (err error) {
if e.Proxy == "" { if k.Proxy == "" {
return errors.New("empty proxy") return errors.New("empty proxy")
} }
if k.Device == "" {
e.proxy, err = parseProxy(e.Proxy)
proxy.SetDialer(e.proxy)
return
}
func (e *engine) withDevice() (err error) {
if e.Device == "" {
return errors.New("empty device") return errors.New("empty device")
} }
e.device, err = parseDevice(e.Device, uint32(e.MTU)) if _defaultProxy, err = parseProxy(k.Proxy); err != nil {
return return
} }
proxy.SetDialer(_defaultProxy)
func (e *engine) withStack() (err error) { if _defaultDevice, err = parseDevice(k.Device, uint32(k.MTU)); err != nil {
defer func() { return
if err == nil { }
log.Infof(
"[STACK] %s://%s <-> %s://%s",
e.device.Type(), e.device.Name(),
e.proxy.Proto(), e.proxy.Addr(),
)
}
}()
e.stack, err = core.CreateStack(&core.Config{ if _defaultStack, err = core.CreateStack(&core.Config{
LinkEndpoint: e.device, LinkEndpoint: _defaultDevice,
TransportHandler: &fakeTunnel{}, TransportHandler: &fakeTunnel{},
ErrorFunc: func(err tcpip.Error) { ErrorFunc: func(err tcpip.Error) {
log.Warnf("[STACK] %s", err) log.Warnf("[STACK] %s", err)
}, },
}) }); err != nil {
return return
}
log.Infof(
"[STACK] %s://%s <-> %s://%s",
_defaultDevice.Type(), _defaultDevice.Name(),
_defaultProxy.Proto(), _defaultProxy.Addr(),
)
return nil
} }
+12
View File
@@ -0,0 +1,12 @@
package engine
type Key struct {
MTU int `yaml:"mtu"`
Mark int `yaml:"fwmark"`
UDPTimeout int `yaml:"udp-timeout"`
Proxy string `yaml:"proxy"`
RestAPI string `yaml:"restapi"`
Device string `yaml:"device"`
LogLevel string `yaml:"loglevel"`
Interface string `yaml:"interface"`
}
+1
View File
@@ -7,6 +7,7 @@ import (
"os/signal" "os/signal"
"syscall" "syscall"
_ "github.com/xjasonlyu/tun2socks/v2/dns"
"github.com/xjasonlyu/tun2socks/v2/engine" "github.com/xjasonlyu/tun2socks/v2/engine"
"github.com/xjasonlyu/tun2socks/v2/internal/version" "github.com/xjasonlyu/tun2socks/v2/internal/version"
"github.com/xjasonlyu/tun2socks/v2/log" "github.com/xjasonlyu/tun2socks/v2/log"