-
Notifications
You must be signed in to change notification settings - Fork 13
/
ws_conn.go
194 lines (172 loc) · 4 KB
/
ws_conn.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"math/rand"
"time"
"github.com/gorilla/websocket"
"github.com/polevpn/elog"
"github.com/polevpn/netstack/tcpip/header"
"github.com/polevpn/netstack/tcpip/transport/tcp"
"github.com/polevpn/netstack/tcpip/transport/udp"
)
const (
CH_WEBSOCKET_WRITE_SIZE = 100
TRAFFIC_LIMIT_INTERVAL = 10
)
type WebSocketConn struct {
conn *websocket.Conn
wch chan []byte
closed bool
handler *RequestHandler
downlimit uint64
uplimit uint64
tcDownStream *TrafficCounter
tcUpStream *TrafficCounter
}
func NewWebSocketConn(conn *websocket.Conn, downlimit uint64, uplimit uint64, handler *RequestHandler) *WebSocketConn {
return &WebSocketConn{
conn: conn,
closed: false,
wch: make(chan []byte, CH_WEBSOCKET_WRITE_SIZE),
handler: handler,
downlimit: downlimit,
uplimit: uplimit,
tcDownStream: NewTrafficCounter(TRAFFIC_LIMIT_INTERVAL * time.Millisecond),
tcUpStream: NewTrafficCounter(TRAFFIC_LIMIT_INTERVAL * time.Millisecond),
}
}
func (wsc *WebSocketConn) Close(flag bool) error {
if !wsc.closed {
wsc.closed = true
if wsc.wch != nil {
wsc.wch <- nil
close(wsc.wch)
}
err := wsc.conn.Close()
if flag {
go wsc.handler.OnClosed(wsc, false)
}
return err
}
return nil
}
func (wsc *WebSocketConn) String() string {
return wsc.conn.RemoteAddr().String() + "->" + wsc.conn.LocalAddr().String()
}
func (wsc *WebSocketConn) IsClosed() bool {
return wsc.closed
}
func (wsc *WebSocketConn) checkStreamLimit(pkt []byte, tfcounter *TrafficCounter, limit uint64) (bool, time.Duration) {
bytes, ltime := tfcounter.StreamCount(uint64(len(pkt)))
if bytes > limit/(1000/uint64(tfcounter.StreamCountInterval()/time.Millisecond)) {
duration := ltime.Add(tfcounter.StreamCountInterval()).Sub(time.Now())
if duration > 0 {
drop := false
if len(wsc.wch) > CH_WEBSOCKET_WRITE_SIZE*0.5 {
ippkt := header.IPv4(pkt)
if ippkt.Protocol() == uint8(tcp.ProtocolNumber) {
n := rand.Intn(5)
if n > 2 {
drop = true
}
} else if ippkt.Protocol() == uint8(udp.ProtocolNumber) {
udppkt := header.UDP(ippkt.Payload())
if udppkt.DestinationPort() != 53 && udppkt.SourcePort() != 53 {
drop = true
}
}
}
if drop {
return true, 0
} else {
return true, duration
}
}
}
return false, 0
}
func (wsc *WebSocketConn) Read() {
defer func() {
wsc.Close(true)
}()
defer PanicHandler()
for {
mtype, pkt, err := wsc.conn.ReadMessage()
if err != nil {
elog.Error(wsc.String(), " wsconn read end,status=", err)
return
}
if mtype == websocket.BinaryMessage {
ppkt := PolePacket(pkt)
if ppkt.Cmd() == CMD_C2S_IPDATA {
//traffic limit
limit, duration := wsc.checkStreamLimit(ppkt.Payload(), wsc.tcUpStream, wsc.uplimit)
if limit {
if duration > 0 {
time.Sleep(duration)
} else {
continue
}
}
}
wsc.handler.OnRequest(pkt, wsc)
} else {
elog.Info("ws mtype=", mtype)
}
}
}
func (wsc *WebSocketConn) drainWriteCh() {
for {
select {
case _, ok := <-wsc.wch:
if !ok {
return
}
default:
return
}
}
}
func (wsc *WebSocketConn) Write() {
defer PanicHandler()
defer wsc.drainWriteCh()
for {
pkt, ok := <-wsc.wch
if !ok {
elog.Error(wsc.String(), " channel closed")
return
}
if pkt == nil {
elog.Info(wsc.String(), " exit write process")
return
}
ppkt := PolePacket(pkt)
if ppkt.Cmd() == CMD_S2C_IPDATA {
//traffic limit
limit, duration := wsc.checkStreamLimit(ppkt.Payload(), wsc.tcDownStream, wsc.downlimit)
if limit {
if duration > 0 {
time.Sleep(duration)
} else {
continue
}
}
}
err := wsc.conn.WriteMessage(websocket.BinaryMessage, pkt)
if err != nil {
elog.Error(wsc.String(), " wsconn write end,status=", err)
return
}
}
}
func (wsc *WebSocketConn) Send(pkt []byte) {
if wsc.closed {
return
}
if wsc.wch != nil {
select {
case wsc.wch <- pkt:
default:
elog.Error(wsc.String(), " wch is full")
}
}
}