-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
133 lines (104 loc) · 3.36 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//reference https://github.com/trojan-gfw/igniter-go-libs/blob/master/tun2socks/tun2socks.go
package main
import (
"C"
"context"
"io"
"net"
"strings"
"time"
//"github.com/Trojan-Qt5/go-tun2socks/common/log"
_ "github.com/Trojan-Qt5/go-tun2socks/common/log/simple"
"github.com/Trojan-Qt5/go-tun2socks/core"
"github.com/Trojan-Qt5/go-tun2socks/proxy/socks"
"github.com/Trojan-Qt5/go-tun2socks/tun"
_ "github.com/p4gefau1t/trojan-go/build"
"github.com/p4gefau1t/trojan-go/common"
"github.com/p4gefau1t/trojan-go/log"
"github.com/Trojan-Qt5/go-shadowsocks2/cmd/shadowsocks"
v2ray "github.com/Trojan-Qt5/v2ray-go/core"
)
const (
MTU = 1500
)
var (
client common.Runnable
lwipWriter io.Writer
tunDev io.ReadWriteCloser
ctx context.Context
cancel context.CancelFunc
isRunning bool = false
)
//export is_tun2socks_running
func is_tun2socks_running() bool {
return isRunning
}
//export stop_tun2socks
func stop_tun2socks() {
log.Info("Stopping tun2socks")
isRunning = false
err := tunDev.Close()
if err != nil {
log.Fatalf("failed to close tun device: %v", err)
}
cancel()
}
//export run_tun2socks
func run_tun2socks(tunName *C.char, tunAddr *C.char, tunGw *C.char, tunDns *C.char, proxyServer *C.char) {
// Open the tun device.
dnsServers := strings.Split(C.GoString(tunDns), ",")
var err error
tunDev, err = tun.OpenTunDevice(C.GoString(tunName), C.GoString(tunAddr), C.GoString(tunGw), "255.255.255.0", dnsServers)
if err != nil {
}
// Setup TCP/IP stack.
lwipWriter := core.NewLWIPStack().(io.Writer)
// Register tun2socks connection handlers.
proxyAddr, err := net.ResolveTCPAddr("tcp", C.GoString(proxyServer))
proxyHost := proxyAddr.IP.String()
proxyPort := uint16(proxyAddr.Port)
if err != nil {
log.Info("invalid proxy server address: %v", err)
}
core.RegisterTCPConnHandler(socks.NewTCPHandler(proxyHost, proxyPort, nil))
core.RegisterUDPConnHandler(socks.NewUDPHandler(proxyHost, proxyPort, 1*time.Minute, nil, nil))
// Register an output callback to write packets output from lwip stack to tun
// device, output function should be set before input any packets.
core.RegisterOutputFn(func(data []byte) (int, error) {
return tunDev.Write(data)
})
ctx, cancel = context.WithCancel(context.Background())
// Copy packets from tun device to lwip stack, it's the main loop.
go func(ctx context.Context) {
_, err := io.CopyBuffer(lwipWriter, tunDev, make([]byte, MTU))
if err != nil {
log.Info(err.Error())
}
}(ctx)
log.Info("Running tun2socks")
isRunning = true
<-ctx.Done()
}
//export startShadowsocksGo
func startShadowsocksGo(ClientAddr *C.char, ServerAddr *C.char, Cipher *C.char, Password *C.char, Plugin *C.char, PluginOptions *C.char, EnableAPI bool, APIAddress *C.char) {
shadowsocks.StartGoShadowsocks(C.GoString(ClientAddr), C.GoString(ServerAddr), C.GoString(Cipher), C.GoString(Password), C.GoString(Plugin), C.GoString(PluginOptions), EnableAPI, C.GoString(APIAddress))
}
//export stopShadowsocksGo
func stopShadowsocksGo() {
shadowsocks.StopGoShadowsocks()
}
//export testV2rayGo
func testV2rayGo(configFile *C.char) (bool, *C.char) {
status, err := v2ray.TestV2ray(C.GoString(configFile))
return status, C.CString(err)
}
//export startV2rayGo
func startV2rayGo(configFile *C.char) {
v2ray.StartV2ray(C.GoString(configFile))
}
//export stopV2rayGo
func stopV2rayGo() {
v2ray.StopV2ray()
}
func main() {
}