Repository navigation
Expand file tree
/
Copy pathkcp.go
More file actions
76 lines (66 loc) · 1.72 KB
/
Copy pathkcp.go
File metadata and controls
76 lines (66 loc) · 1.72 KB
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
package main
import (
"PortForwardGo/zlog"
"net"
kcp "github.com/xtaci/kcp-go"
"time"
proxyprotocol "github.com/pires/go-proxyproto"
)
func LoadKCPRules(i string) {
Setting.mu.Lock()
ln, err := kcp.ListenWithOptions(":" + Setting.Config.Rules[i].Listen, nil,10,3)
if err == nil {
zlog.Info("Loaded [",i,"] (KCP)", Setting.Config.Rules[i].Listen, " => ", Setting.Config.Rules[i].Forward)
}else{
zlog.Error("Load failed [",i,"] (KCP) Error: ",err)
Setting.mu.Unlock()
SendListenError(i)
return
}
Setting.Listener.KCP[i] = ln
Setting.mu.Unlock()
for {
conn, err := ln.Accept()
if err != nil {
if err, ok := err.(net.Error); ok && err.Temporary() {
continue
}
break
}
Setting.mu.RLock()
rule := Setting.Config.Rules[i]
Setting.mu.RUnlock()
if rule.Status != "Active" && rule.Status != "Created" {
conn.Close()
continue
}
go kcp_handleRequest(conn, i, rule)
}
}
func DeleteKCPRules(i string){
if _,ok :=Setting.Listener.KCP[i];ok {
err :=Setting.Listener.KCP[i].Close()
for err!=nil {
time.Sleep(time.Second)
err = Setting.Listener.KCP[i].Close()
}
delete(Setting.Listener.KCP,i)
}
Setting.mu.Lock()
zlog.Info("Deleted [",i,"] (KCP)", Setting.Config.Rules[i].Listen, " => ", Setting.Config.Rules[i].Forward)
delete(Setting.Config.Rules,i)
Setting.mu.Unlock()
}
func kcp_handleRequest(conn net.Conn, index string, r Rule) {
proxy, err := kcp.Dial(r.Forward)
if err != nil {
_ = conn.Close()
return
}
if r.ProxyProtocolVersion != 0 {
header := proxyprotocol.HeaderProxyFromAddrs(byte(r.ProxyProtocolVersion),conn.RemoteAddr(),conn.LocalAddr())
header.WriteTo(proxy)
}
go copyIO(conn, proxy, index)
go copyIO(proxy, conn, index)
}