-
Notifications
You must be signed in to change notification settings - Fork 80
/
tun2socks.go
66 lines (53 loc) · 1.82 KB
/
tun2socks.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
package tun2socks
import (
"context"
"log"
"strings"
"time"
vcore "v2ray.com/core"
vproxyman "v2ray.com/core/app/proxyman"
vbytespool "v2ray.com/core/common/bytespool"
"github.com/eycorsican/go-tun2socks/core"
"github.com/eycorsican/go-tun2socks/proxy/shadowsocks"
"github.com/eycorsican/go-tun2socks/proxy/v2ray"
)
type PacketFlow interface {
WritePacket(packet []byte)
}
var lwipStack core.LWIPStack
func InputPacket(data []byte) {
lwipStack.Write(data)
}
func StartV2Ray(packetFlow PacketFlow, configBytes []byte) {
if packetFlow != nil {
lwipStack = core.NewLWIPStack()
core.SetBufferPool(vbytespool.GetPool(core.BufSize))
v, err := vcore.StartInstance("json", configBytes)
if err != nil {
log.Fatal("start V instance failed: %v", err)
}
sniffingConfig := &vproxyman.SniffingConfig{
Enabled: true,
DestinationOverride: strings.Split("tls,http", ","),
}
ctx := vproxyman.ContextWithSniffingConfig(context.Background(), sniffingConfig)
vhandler := v2ray.NewHandler(ctx, v)
core.RegisterTCPConnectionHandler(vhandler)
core.RegisterUDPConnectionHandler(vhandler)
core.RegisterOutputFn(func(data []byte) (int, error) {
packetFlow.WritePacket(data)
return len(data), nil
})
}
}
func StartShadowsocks(packetFlow PacketFlow, proxyHost string, proxyPort int, proxyCipher, proxyPassword string) {
if packetFlow != nil {
lwipStack = core.NewLWIPStack()
core.RegisterTCPConnectionHandler(shadowsocks.NewTCPHandler(core.ParseTCPAddr(proxyHost, uint16(proxyPort)).String(), proxyCipher, proxyPassword))
core.RegisterUDPConnectionHandler(shadowsocks.NewUDPHandler(core.ParseUDPAddr(proxyHost, uint16(proxyPort)).String(), proxyCipher, proxyPassword, 30*time.Second))
core.RegisterOutputFn(func(data []byte) (int, error) {
packetFlow.WritePacket(data)
return len(data), nil
})
}
}