mirror of
				https://github.com/xjasonlyu/tun2socks.git
				synced 2025-10-31 20:12:41 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			71 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			71 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package main
 | |
| 
 | |
| import (
 | |
| 	"flag"
 | |
| 	"fmt"
 | |
| 	"os"
 | |
| 	"os/signal"
 | |
| 	"syscall"
 | |
| 
 | |
| 	_ "github.com/xjasonlyu/tun2socks/v2/dns"
 | |
| 	"github.com/xjasonlyu/tun2socks/v2/engine"
 | |
| 	"github.com/xjasonlyu/tun2socks/v2/internal/version"
 | |
| 	"github.com/xjasonlyu/tun2socks/v2/log"
 | |
| 
 | |
| 	"go.uber.org/automaxprocs/maxprocs"
 | |
| 	"gopkg.in/yaml.v3"
 | |
| )
 | |
| 
 | |
| var (
 | |
| 	key = new(engine.Key)
 | |
| 
 | |
| 	configFile  string
 | |
| 	versionFlag bool
 | |
| )
 | |
| 
 | |
| func init() {
 | |
| 	flag.IntVar(&key.Mark, "fwmark", 0, "Set firewall MARK (Linux only)")
 | |
| 	flag.IntVar(&key.MTU, "mtu", 0, "Set device maximum transmission unit (MTU)")
 | |
| 	flag.DurationVar(&key.UDPTimeout, "udp-timeout", 0, "Set timeout for each UDP session")
 | |
| 	flag.StringVar(&configFile, "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|warning|error|silent]")
 | |
| 	flag.StringVar(&key.Proxy, "proxy", "", "Use this proxy [protocol://]host[:port]")
 | |
| 	flag.StringVar(&key.RestAPI, "restapi", "", "HTTP statistic server listen address")
 | |
| 	flag.StringVar(&key.TCPSendBufferSize, "tcp-sndbuf", "", "Set TCP send buffer size for netstack")
 | |
| 	flag.StringVar(&key.TCPReceiveBufferSize, "tcp-rcvbuf", "", "Set TCP receive buffer size for netstack")
 | |
| 	flag.BoolVar(&key.TCPModerateReceiveBuffer, "tcp-auto-tuning", false, "Enable TCP receive buffer auto-tuning")
 | |
| 	flag.BoolVar(&versionFlag, "version", false, "Show version and then quit")
 | |
| 	flag.Parse()
 | |
| }
 | |
| 
 | |
| func main() {
 | |
| 	maxprocs.Set(maxprocs.Logger(func(string, ...any) {}))
 | |
| 
 | |
| 	if versionFlag {
 | |
| 		fmt.Println(version.String())
 | |
| 		fmt.Println(version.BuildString())
 | |
| 		os.Exit(0)
 | |
| 	}
 | |
| 
 | |
| 	if configFile != "" {
 | |
| 		data, err := os.ReadFile(configFile)
 | |
| 		if err != nil {
 | |
| 			log.Fatalf("Failed to read config file '%s': %v", configFile, err)
 | |
| 		}
 | |
| 		if err = yaml.Unmarshal(data, key); err != nil {
 | |
| 			log.Fatalf("Failed to unmarshal config file '%s': %v", configFile, err)
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	engine.Insert(key)
 | |
| 
 | |
| 	engine.Start()
 | |
| 	defer engine.Stop()
 | |
| 
 | |
| 	sigCh := make(chan os.Signal, 1)
 | |
| 	signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
 | |
| 	<-sigCh
 | |
| }
 | 
