-
Notifications
You must be signed in to change notification settings - Fork 26
/
clash.go
120 lines (105 loc) · 2.48 KB
/
clash.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
package clash
import (
"path/filepath"
"time"
"github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/hub/executor"
"github.com/Dreamacro/clash/log"
T "github.com/Dreamacro/clash/tunnel"
"github.com/Dreamacro/clash/tunnel/statistic"
"github.com/eycorsican/go-tun2socks/core"
"github.com/eycorsican/go-tun2socks/proxy/socks"
)
var (
stack core.LWIPStack
trafficReceiver TrafficReceiver
logger RealTimeLogger
)
type PacketFlow interface {
WritePacket(packet []byte)
}
type TrafficReceiver interface {
ReceiveTraffic(up int64, down int64)
}
type RealTimeLogger interface {
Log(level string, payload string)
}
func ReadPacket(data []byte) {
stack.Write(data)
}
func Setup(flow PacketFlow, homeDir string, config string) error {
go fetchLogs()
constant.SetHomeDir(homeDir)
constant.SetConfig("")
cfg, err := executor.ParseWithBytes(([]byte)(config))
if err != nil {
return err
}
executor.ApplyConfig(cfg, true)
stack = core.NewLWIPStack()
core.RegisterTCPConnHandler(socks.NewTCPHandler("127.0.0.1", uint16(cfg.General.MixedPort)))
core.RegisterUDPConnHandler(socks.NewUDPHandler("127.0.0.1", uint16(cfg.General.MixedPort), 30*time.Second))
core.RegisterOutputFn(func(data []byte) (int, error) {
flow.WritePacket(data)
return len(data), nil
})
go fetchTraffic()
return nil
}
func ApplyConfig(uuid string) error {
if stack == nil {
return nil
}
path := filepath.Join(constant.Path.HomeDir(), uuid, "config.yaml")
cfg, err := executor.ParseWithPath(path)
if err != nil {
return err
}
constant.SetConfig(path)
CloseAllConnections()
cfg.General = executor.GetGeneral()
executor.ApplyConfig(cfg, false)
return nil
}
func SetTunnelMode(mode string) {
if stack == nil {
return
}
CloseAllConnections()
T.SetMode(T.ModeMapping[mode])
}
func CloseAllConnections() {
snapshot := statistic.DefaultManager.Snapshot()
for _, c := range snapshot.Connections {
c.Close()
}
}
func SetTrafficReceiver(receive TrafficReceiver) {
trafficReceiver = receive
}
func fetchTraffic() {
tick := time.NewTicker(time.Second)
defer tick.Stop()
t := statistic.DefaultManager
for range tick.C {
if trafficReceiver == nil {
continue
}
up, down := t.Now()
trafficReceiver.ReceiveTraffic(up, down)
}
}
func SetRealTimeLogger(l RealTimeLogger) {
logger = l
}
func fetchLogs() {
sub := log.Subscribe()
defer log.UnSubscribe(sub)
for elm := range sub {
if logger == nil {
continue
}
log := elm.(*log.Event)
logger.Log(log.Type(), log.Payload)
}
}