-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLaunchServer.lua
More file actions
193 lines (173 loc) · 4.51 KB
/
Copy pathLaunchServer.lua
File metadata and controls
193 lines (173 loc) · 4.51 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
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
local ffi = require("ffi")
local bit = require("bit")
local M = {}
ffi.cdef[[
typedef unsigned short sa_family_t;
typedef unsigned int socklen_t;
typedef uint16_t in_port_t;
typedef uint32_t in_addr_t;
struct in_addr { in_addr_t s_addr; };
struct sockaddr { sa_family_t sa_family; char sa_data[14]; };
struct sockaddr_in { sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; unsigned char sin_zero[8]; };
int socket(int domain, int type, int protocol);
int bind(int sockfd, const struct sockaddr* addr, socklen_t addrlen);
int listen(int sockfd, int backlog);
int accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen);
int close(int fd);
int fcntl(int fd, int cmd, int arg);
int setsockopt(int sockfd, int level, int optname, const void* optval, socklen_t optlen);
uint16_t htons(uint16_t hostshort);
uint32_t htonl(uint32_t hostlong);
int recv(int sockfd, void* buf, size_t len, int flags);
int send(int sockfd, const void* buf, size_t len, int flags);
]]
local C = ffi.C
local AF_INET = 2
local SOCK_STREAM = 1
local SOL_SOCKET = 1
local SO_REUSEADDR = 2
local F_GETFL = 3
local F_SETFL = 4
local O_NONBLOCK = 0x0004
local EAGAIN = 35
local function set_nonblock(fd)
local flags = C.fcntl(fd, F_GETFL, 0)
if flags ~= -1 then
C.fcntl(fd, F_SETFL, bit.bor(flags, O_NONBLOCK))
end
end
local function close_fd(fd)
if fd and fd >= 0 then
C.close(fd)
end
end
local function url_decode(str)
str = tostring(str or ""):gsub("+", " ")
return (str:gsub("%%(%x%x)", function(hex)
return string.char(tonumber(hex, 16))
end))
end
local function parse_query(path)
local query = path:match("%?(.*)")
local params = {}
if not query then
return params
end
for key, val in query:gmatch("([^&=]+)=([^&=]*)") do
params[key] = url_decode(val)
end
return params
end
local function bind_port(port)
local fd = C.socket(AF_INET, SOCK_STREAM, 0)
if fd < 0 then
return nil, "socket failed"
end
local opt = ffi.new("int[1]", 1)
C.setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, opt, ffi.sizeof(opt))
local addr = ffi.new("struct sockaddr_in")
addr.sin_family = AF_INET
addr.sin_port = C.htons(port)
addr.sin_addr.s_addr = C.htonl(0x7f000001)
if C.bind(fd, ffi.cast("struct sockaddr*", addr), ffi.sizeof(addr)) ~= 0 then
close_fd(fd)
return nil, "bind failed"
end
if C.listen(fd, 1) ~= 0 then
close_fd(fd)
return nil, "listen failed"
end
set_nonblock(fd)
return fd
end
function M.StartServer()
local lastErr
for p = 49082, 49084 do
local fd, err = bind_port(p)
if fd then
return { listen_fd = fd, port = p, client_fd = nil, buffer = "" }
end
lastErr = err
end
return nil, lastErr or "Failed to bind local port 49082-49084"
end
function M.Poll(server)
if not server then
return nil
end
if not server.client_fd then
local addr = ffi.new("struct sockaddr_in")
local addrlen = ffi.new("socklen_t[1]", ffi.sizeof(addr))
local fd = C.accept(server.listen_fd, ffi.cast("struct sockaddr*", addr), addrlen)
if fd == -1 then
local err = ffi.errno()
if err == EAGAIN then
return nil
end
return nil, "accept failed"
end
server.client_fd = fd
set_nonblock(fd)
end
local buf = ffi.new("char[4096]")
local n = C.recv(server.client_fd, buf, 4095, 0)
if n == -1 then
local err = ffi.errno()
if err == EAGAIN then
return nil
end
return nil, "recv failed"
elseif n == 0 then
return nil, "client closed"
end
server.buffer = server.buffer .. ffi.string(buf, n)
local line = server.buffer:match("^(.-)\r?\n")
if not line then
return nil
end
local path = line:match("GET%s+([^%s]+)")
if not path then
return nil, "invalid request"
end
local params = parse_query(path)
local result = {
code = params.code,
state = params.state,
err = params.error_description or params.error,
}
return result
end
function M.Finish(server, ok, msg)
if not server then
return
end
if server.client_fd then
local body
if ok then
body = "<html><body><h3>Authentication complete.</h3>You can close this window.</body></html>"
else
body = "<html><body><h3>Authentication failed.</h3>" .. tostring(msg or "") .. "</body></html>"
end
local headers = {
"HTTP/1.1 200 OK",
"Content-Type: text/html; charset=utf-8",
"Content-Length: " .. tostring(#body),
"Connection: close",
"",
"",
}
local resp = table.concat(headers, "\r\n") .. body
C.send(server.client_fd, resp, #resp, 0)
end
M.Close(server)
end
function M.Close(server)
if not server then
return
end
close_fd(server.client_fd)
close_fd(server.listen_fd)
server.client_fd = nil
server.listen_fd = nil
end
return M