-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathproxy.lua
More file actions
261 lines (242 loc) · 7.11 KB
/
proxy.lua
File metadata and controls
261 lines (242 loc) · 7.11 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
local network = require 'common.network'
local debuger_factory = require 'frontend.debuger_factory'
local fs = require 'bee.filesystem'
local sp = require 'bee.subprocess'
local platform_os = require 'frontend.platform_os'
local process_inject = require 'frontend.process_inject'
local json = require 'common.json'
local server
local client
local initReq
local m = {}
local function getUnixAddress(pid)
local path = WORKDIR / "tmp"
fs.create_directories(path)
return "@"..(path / ("pid_%d"):format(pid)):string()
end
local function ipc_send_config(pid, args)
--TODO: clear config file
fs.create_directories(WORKDIR / "tmp")
local ipc = require "common.ipc"
local fd = assert(ipc(WORKDIR, pid, "config", "w"))
local config = {
version = args.luaVersion,
module = args.module
}
fd:write(json.encode(config))
fd:close()
end
local function response_initialize(req)
client.sendmsg {
type = 'response',
seq = 0,
command = 'initialize',
request_seq = req.seq,
success = true,
body = require 'common.capabilities',
}
end
local function response_error(req, msg)
client.sendmsg {
type = 'response',
seq = 0,
command = req.command,
request_seq = req.seq,
success = false,
message = msg,
}
end
local function request_runinterminal(args)
client.sendmsg {
type = 'request',
seq = 0,
command = 'runInTerminal',
arguments = args
}
end
local function attach_process(pkg, pid)
local args = pkg.arguments
ipc_send_config(pid, args)
local ok, errmsg = process_inject.inject(pid, "attach", args)
if not ok then
return false, errmsg
end
server = network(getUnixAddress(pid), true)
server.sendmsg(initReq)
server.sendmsg(pkg)
return true
end
local function attach_tcp(pkg, args)
server = network(args.address, args.client)
server.sendmsg(initReq)
server.sendmsg(pkg)
end
local function proxy_attach(pkg)
local args = pkg.arguments
platform_os.init(args)
if platform_os() ~= "Windows" and platform_os() ~= "macOS" then
attach_tcp(pkg, args)
return
end
if args.processId then
local processId = tonumber(args.processId)
local ok, errmsg = attach_process(pkg, processId)
if not ok then
response_error(pkg, ('Cannot attach process `%d`. %s'):format(processId, errmsg))
end
return
end
if args.processName then
local pids = require "frontend.query_process" (args.processName)
if #pids == 0 then
response_error(pkg, ('Cannot found process `%s`.'):format(args.processName))
return
elseif #pids > 1 then
response_error(pkg, ('There are %d processes `%s`.'):format(#pids, args.processName))
return
end
local ok, errmsg = attach_process(pkg, pids[1])
if not ok then
response_error(pkg, ('Cannot attach process `%s` `%d`. %s'):format(args.processName, pids[1], errmsg))
end
return
end
attach_tcp(pkg, args)
end
local function create_server(args, pid)
local s, address
if args.address ~= nil then
s = network(args.address, args.client)
address = (args.client and "s:" or "c:")..args.address
else
pid = pid or sp.get_id()
s = network(getUnixAddress(pid), true)
address = pid
end
return s, address
end
local function proxy_launch_terminal(pkg)
local args = pkg.arguments
if args.runtimeExecutable then
if args.inject ~= "none" then
--TODO: support inject's integratedTerminal/externalTerminal
response_error(pkg, "`inject` is not supported in `"..args.console.."`.")
return
end
server = create_server(args)
local arguments, err = debuger_factory.create_process_in_terminal(initReq, args)
if not arguments then
response_error(pkg, err)
return
end
request_runinterminal(arguments)
return true
else
local address
server, address = create_server(args)
local arguments, err = debuger_factory.create_luaexe_in_terminal(initReq, args, WORKDIR, address)
if not arguments then
response_error(pkg, err)
return
end
request_runinterminal(arguments)
return true
end
end
local function proxy_launch_console(pkg)
local args = pkg.arguments
if args.runtimeExecutable then
if args.inject == "none" and args.address == nil then
response_error(pkg, "`runtimeExecutable` need specify `inject` or `address`.")
return
end
local process, err = debuger_factory.create_process_in_console(args, function(process)
local address
server, address = create_server(args, process:get_id())
if type(address) == "number" then
ipc_send_config(address, args)
end
end)
if not process then
response_error(pkg, err)
return
end
else
local address
server, address = create_server(args)
local ok, err = debuger_factory.create_luaexe_in_console(args, WORKDIR, address)
if not ok then
response_error(pkg, err)
return
end
end
return true
end
local function proxy_launch(pkg)
local args = pkg.arguments
platform_os.init(args)
if args.runtimeExecutable and args.inject ~= "none" then
args.console = "internalConsole"
end
if args.console == 'integratedTerminal' or args.console == 'externalTerminal' then
if not proxy_launch_terminal(pkg) then
return
end
else
if not proxy_launch_console(pkg) then
return
end
end
server.sendmsg(initReq)
server.sendmsg(pkg)
end
local function proxy_start(pkg)
if pkg.arguments.request == 'attach' then
proxy_attach(pkg)
elseif pkg.arguments.request == 'launch' then
proxy_launch(pkg)
end
end
function m.send(pkg)
if server then
if pkg.type == 'response' and pkg.command == 'runInTerminal' then
return
end
server.sendmsg(pkg)
elseif not initReq then
if pkg.type == 'request' and pkg.command == 'initialize' then
pkg.__norepl = true
initReq = pkg
response_initialize(pkg)
else
response_error(pkg, 'not initialized')
end
else
if pkg.type == 'request' then
if pkg.command == 'attach' or pkg.command == 'launch' then
proxy_start(pkg)
else
response_error(pkg, 'error request')
end
end
end
end
function m.update()
if server then
server.event_close(function()
os.exit(0, true)
end)
while true do
local pkg = server.recvmsg()
if pkg then
client.sendmsg(pkg)
else
break
end
end
end
end
function m.init(io)
client = io
end
return m