forked from yizhu2000/suproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwshandler.lua
More file actions
79 lines (69 loc) · 2.23 KB
/
wshandler.lua
File metadata and controls
79 lines (69 loc) · 2.23 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
77
78
79
local logger = require "strmproxy.utils.compatibleLog"
local sockLogger = require "resty.logger.socket"
local format = string.format
local _M = {}
_M._PROTOCOL = "WS"
if not sockLogger.initted() then
local ok, err = sockLogger.init {
-- logger server address
host = '127.0.0.1',
port = 12080,
flush_limit = 10,
drop_limit = 567800,
}
if not ok then
logger.err("failed to initialize the logger: ", err)
end
else
logger.err("logger module already initialized")
end
local function wsLog(data)
if sockLogger then
local bytes, err = sockLogger.log(data)
if err then
logger.err("failed to log reply: ", err)
end
else
logger.dbg( data)
end
end
local function OnConnect(context, source, session)
if session then
local log = format("[".._M._PROTOCOL .. "] connected from %s:%s to %s:%s\r\n", session.clientIP, session.clientPort, session.srvIP, session.srvPort)
wsLog(log)
else
logger.dbg("session is nil")
end
end
local function OnHandshakeRequestEvent(context, source, headers)
local log = format('['.._M._PROTOCOL .. "] Handshake Request: %s %s %s\r\n",
headers["req_line"], headers["Connection"], headers["Upgrade"])
logger.dbg("[" .. _M._PROTOCOL .. " ] ", log)
wsLog(log)
end
local function OnHandshakeResponseEvent(context, source, headers)
local log = format('['.._M._PROTOCOL .. "] Handshake Response: %s %s %s\r\n",
headers["resp_line"], headers["Connection"], headers["Upgrade"])
logger.dbg("[" .. _M._PROTOCOL .. " ] ", log)
wsLog(log)
end
local function OnFrameEvent(context, source, frame)
local log
local packet = frame.packet
if frame.up then
log = format('['.._M._PROTOCOL .. "] %s:%s sent \t\t[%s] %s\r\n",
source.ctx.clientIP, source.ctx.clientPort,
packet.opcode, packet.payload)
else
log = format('['.._M._PROTOCOL .. "] received from %s:%s \t[%s] %s\r\n",
source.ctx.srvIP, source.ctx.srvPort,
packet.opcode, packet.payload)
end
logger.dbg("[" .. _M._PROTOCOL .. " ] ", log)
wsLog(log)
end
_M.OnConnect = OnConnect
_M.OnHandshakeRequestEvent = OnHandshakeRequestEvent
_M.OnHandshakeResponseEvent = OnHandshakeResponseEvent
_M.OnFrameEvent = OnFrameEvent
return _M