You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
3.4 KiB

4 years ago
package engine
import (
"errors"
"fmt"
"os"
"github.com/xjasonlyu/tun2socks/v2/component/dialer"
"github.com/xjasonlyu/tun2socks/v2/core/device"
"github.com/xjasonlyu/tun2socks/v2/core/stack"
"github.com/xjasonlyu/tun2socks/v2/log"
"github.com/xjasonlyu/tun2socks/v2/proxy"
"github.com/xjasonlyu/tun2socks/v2/stats"
"github.com/xjasonlyu/tun2socks/v2/tunnel"
"github.com/xjasonlyu/tun2socks/v2/version"
"gopkg.in/yaml.v3"
4 years ago
)
var _engine = &engine{}
// Start starts the default engine up.
func Start() error {
return _engine.start()
}
// Stop shuts the default engine down.
func Stop() error {
return _engine.stop()
}
// Insert loads *Key to the default engine.
func Insert(k *Key) {
_engine.insert(k)
}
type Key struct {
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 {
*Key
4 years ago
stack *stack.Stack
proxy proxy.Proxy
4 years ago
device device.Device
}
func (e *engine) start() error {
if e.Key == nil {
return errors.New("empty key")
}
4 years ago
if e.Version {
fmt.Println(version.String())
fmt.Println(version.BuildString())
os.Exit(0)
4 years ago
}
for _, f := range []func() error{
e.setConfig,
4 years ago
e.setLogLevel,
e.setMark,
4 years ago
e.setInterface,
e.setStats,
e.setUDPTimeout,
4 years ago
e.setProxy,
e.setDevice,
e.setStack,
} {
if err := f(); err != nil {
4 years ago
return err
}
}
return nil
}
func (e *engine) stop() error {
4 years ago
if e.device != nil {
return e.device.Close()
4 years ago
}
return nil
}
func (e *engine) insert(k *Key) {
e.Key = k
4 years ago
}
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)
4 years ago
if err != nil {
return err
}
log.SetLevel(level)
return nil
}
func (e *engine) setMark() error {
if e.Mark != 0 {
dialer.SetMark(e.Mark)
log.Infof("[DIALER] set fwmark: %#x", e.Mark)
}
return nil
}
func (e *engine) setInterface() error {
if e.Interface != "" {
if err := dialer.BindToInterface(e.Interface); err != nil {
4 years ago
return err
}
log.Infof("[DIALER] use interface: %s", e.Interface)
4 years ago
}
return nil
}
func (e *engine) setStats() error {
if e.Stats != "" {
4 years ago
go func() {
_ = stats.Start(e.Stats, e.Token)
4 years ago
}()
log.Infof("[STATS] serve at: http://%s", e.Stats)
4 years ago
}
return nil
}
func (e *engine) setUDPTimeout() error {
if e.UDPTimeout > 0 {
tunnel.SetUDPTimeout(e.UDPTimeout)
}
return nil
}
func (e *engine) setProxy() (err error) {
if e.Proxy == "" {
return errors.New("empty proxy")
}
e.proxy, err = parseProxy(e.Proxy)
proxy.SetDialer(e.proxy)
return
4 years ago
}
func (e *engine) setDevice() (err error) {
if e.Device == "" {
return errors.New("empty device")
}
e.device, err = parseDevice(e.Device, uint32(e.MTU))
return
4 years ago
}
func (e *engine) setStack() (err error) {
defer func() {
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 = stack.New(e.device, &fakeTunnel{}, stack.WithDefault())
return
4 years ago
}