-
Notifications
You must be signed in to change notification settings - Fork 34
/
kcp_server.go
144 lines (110 loc) · 2.64 KB
/
kcp_server.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
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"crypto/tls"
"errors"
"net"
"sync"
"github.com/pion/dtls/v3"
"github.com/polevpn/anyvalue"
"github.com/polevpn/elog"
"github.com/polevpn/kcp"
)
const (
KCP_SHARED_KEY_LEN = 16
KCP_MTU = 1350
KCP_RECV_WINDOW = 512
KCP_SEND_WINDOW = 512
KCP_READ_BUFFER = 4194304
KCP_WRITE_BUFFER = 4194304
)
type KCPServer struct {
requestHandler *RequestHandler
}
func NewKCPServer(requestHandler *RequestHandler) *KCPServer {
return &KCPServer{
requestHandler: requestHandler,
}
}
func (ks *KCPServer) ListenTLS(wg *sync.WaitGroup, addr string, certFile string, keyFile string) {
defer wg.Done()
udpAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
elog.Error("resolve address fail,", err)
return
}
certificate, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
elog.Error("load cert fail,", err)
return
}
// Prepare the configuration of the DTLS connection
config := &dtls.Config{
Certificates: []tls.Certificate{certificate},
MTU: 1400,
}
listener, err := kcp.Listen(udpAddr, config)
if err != nil {
elog.Error("kcp listen fail,", err)
return
}
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
elog.Error("accept kcp conn fail,", err)
continue
}
conn.SetACKNoDelay(true)
conn.SetNoDelay(1, 10, 2, 1)
conn.SetWindowSize(KCP_SEND_WINDOW, KCP_RECV_WINDOW)
conn.SetReadBuffer(KCP_READ_BUFFER)
conn.SetReadBuffer(KCP_WRITE_BUFFER)
err = ks.auth(conn)
if err != nil {
elog.Error("auth fail,", err)
conn.Close()
continue
}
go ks.handleConn(conn)
}
}
func (ks *KCPServer) auth(conn *kcp.UDPSession) error {
pkt, err := ReadPacket(conn)
if err != nil {
return err
}
ppkt := PolePacket(pkt)
if ppkt.Cmd() != CMD_AUTH {
return errors.New("invalid cmd")
}
av, err := anyvalue.NewFromJson(ppkt.Payload())
if err != nil {
return err
}
resp := anyvalue.New()
if av.Get("key").AsStr() != Config.Get("key").AsStr() {
resp.Set("error", "invalid key")
}
body, _ := resp.EncodeJson()
buf := make([]byte, POLE_PACKET_HEADER_LEN+len(body))
copy(buf[POLE_PACKET_HEADER_LEN:], body)
resppkt := PolePacket(buf)
resppkt.SetLen(uint16(len(buf)))
resppkt.SetCmd(CMD_AUTH)
resppkt.SetSeq(ppkt.Seq())
_, err = conn.Write(resppkt)
if err != nil {
elog.Error("write error", err)
}
if resp.Get("error").AsStr() != "" {
return errors.New(resp.Get("error").AsStr())
}
return nil
}
func (ks *KCPServer) handleConn(conn *kcp.UDPSession) {
kcpconn := NewKCPConn(conn, ks.requestHandler)
if ks.requestHandler != nil {
ks.requestHandler.OnConnection(kcpconn)
}
kcpconn.StartProcess()
}