-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.lua
181 lines (154 loc) · 3.83 KB
/
agent.lua
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
local skynet = require "skynet"
local socket = require "skynet.socket"
local CMD = {}
local srcFds = {}
local dstFds = {}
local srcTodstFds = {}
local ATYP = {
ipv4 = 1,
domain = 3,
ipv6 = 4,
}
local function getSocketInfo(fd)
local socketInfos = socket.netstat()
for _, v in ipairs(socketInfos) do
if v.id == fd then
return v
end
end
end
local function read(fd, n)
local data, err = socket.read(fd, n)
if not data then
if srcFds[fd] or dstFds[fd] then
error(err)
end
return
end
return data
end
local function write(srcFd, pack)
return socket.write(srcFd, pack)
end
local function handIdentifie(srcFd)
local data = read(srcFd, 2)
local ver = string.byte(data, 1)
local nmethods = string.byte(data, 2)
data = read(srcFd,nmethods)
local methods = {}
for i = 1, nmethods do
methods[i] = string.byte(data, i)
end
write(srcFd,"\5\0")
end
local function sendResponse(srcFd)
local response = "\5\0\0\1\127\127\127\1\0\0"
-- buf[8] = 8555 >> 8;
-- buf[9] = 8555 % 256;
write(srcFd,response)
end
local function handleRequest(srcFd)
local data = read(srcFd,4)
local ver = string.byte(data, 1)
local cmd = string.byte(data, 2)
local rsv = string.byte(data, 3)
local atyp = string.byte(data, 4)
log(ver, cmd, rsv, atyp)
local dstFd
local err
local dstAddr
local dstPort
if atyp == ATYP.domain then
local data = read(srcFd,1)
local addrLen = string.byte(data, 1)
dstAddr = read(srcFd,addrLen)
data = read(srcFd,2)
dstPort = (string.byte(data, 1) << 8) + string.byte(data, 2)
INFO("handle request. dst domain", dstAddr, dstPort)
dstFd, err = socket.open(dstAddr, dstPort)
if not dstFd then
ERROR("connect to dst addr failed.", dstAddr, dstPort, err)
return
end
elseif atyp == ATYP.ipv4 then
local data = read(srcFd,4)
dstAddr = string.byte(data, 1) .. "." .. string.byte(data, 2) .. "." .. string.byte(data, 3) .. "." .. string.byte(data, 4)
data = read(srcFd, 2)
dstPort = (string.byte(data, 1) << 8) + string.byte(data, 2)
INFO("handle request. dst ipv4", dstAddr, dstPort)
dstFd, err = socket.open(dstAddr, dstPort)
if not dstFd then
ERROR("connect to dst ipv4 failed.", dstAddr, dstPort, err)
return
end
else
ERROR("atype not handed", atyp)
return
end
WARN("connected dstFd:", dstFd, dstAddr, dstPort)
dstFds[dstFd] = true
srcTodstFds[srcFd] = dstFd
socket.onclose(dstFd, function()
INFO("client dstFd close, srcFd-dstFd:", srcFd, dstFd)
dstFds[dstFd] = nil
end)
sendResponse(srcFd)
skynet.fork(function()
while srcFds[srcFd] do
local data = read(srcFd)
if not data then
break
end
socket.write(dstFd, data)
end
WARN("agent srcFd read end<===========", srcFd, dstFd)
end)
while dstFds[dstFd] do
local data = read(dstFd)
if not data then
break
end
write(srcFd, data)
local socketInfo = getSocketInfo(srcFd)
while socketInfo.wbuffer > 1024 * 1024 * 10 do
-- WARN("sleep start...", socketInfo.wbuffer)
skynet.sleep(10) -- sleep wait srcFd send data
-- WARN("sleep end")
socketInfo = getSocketInfo(srcFd)
end
end
WARN("agent dstFd read end<===========", srcFd, dstFd)
end
function CMD.start(conf)
local fd = conf.client
local srcFd = fd
socket.start(srcFd)
WARN("agent start===========>", srcFd)
srcFds[srcFd] = true
socket.onclose(srcFd, function()
srcFds[srcFd] = nil
local dstFd = srcTodstFds[srcFd]
INFO("client srcFd close. srcFd-dstFd:", srcFd, dstFd)
socket.close(dstFd)
srcTodstFds[srcFd] = nil
end)
handIdentifie(srcFd)
handleRequest(srcFd)
end
skynet.info_func(function()
local info = {
srcFds = srcFds,
dstFds = dstFds,
srcTodstFds = srcTodstFds,
}
return info
end)
skynet.start(function()
skynet.dispatch("lua", function(_,_, command, ...)
-- skynet.trace()
local f = CMD[command]
skynet.retpack(f(...))
-- local ok, data = xpcall(f, __G__TRACKBACK__, ...)
-- skynet.retpack(data)
end)
end)