xjasonlyu / tun2socks

tun2socks - powered by gVisor TCP/IP stack
https://github.com/xjasonlyu/tun2socks/wiki
GNU General Public License v3.0
2.85k stars 405 forks source link

How to build this into tun2socks.aar for Android? #145

Closed yaem1k0 closed 1 year ago

yaem1k0 commented 2 years ago

Description

I want to use this in my Android project, but i am totally inexperienced for Golang programming. When i try to use gomobile to build it into tun2socks.aar, it shows: no exported names in the package "github.com/xjasonlyu/tun2socks/v2" and unable to import bind: [-: no required module provides package golang.org/x/mobile/bind; to add it: go get golang.org/x/mobile/bind] Can someone tell me how to build this into tun2socks.aar? I modified file "main.go" :

package tun2socks

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 tun2socks_start() {
    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
}

And modified the file name "main.go" into "tun2socks.go".

Is this feature related to a specific bug?

No response

Do you have a specific solution in mind?

No response

Cyberbolt commented 5 months ago

The build is OK. Do you know how to call it after building?