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
|
# File 'lib/pytty/daemon/components/yield_handler.rb', line 7
def self.handle(component:, id:, params:)
process_yield = Pytty::Daemon.yields[id]
return [404, "not found"] unless process_yield
return case component
when "port"
endpoint = Async::IO::Endpoint.tcp('0.0.0.0', params["from"].to_i)
endpoint.accept do |client|
process_yield.spawn unless process_yield.running?
p ["port accept", client.object_id]
upstream = Async::IO::Endpoint.tcp('0.0.0.0', params["to"].to_i)
peer = nil
upstream.connect do |peer|
Async::Task.current.async do |task|
while rata = peer.read(1)
client.write rata
end
client.close
end
while data = client.read(2)
p data
peer.write(data)
end
end
rescue Errno::ECONNREFUSED
sleep 0.1
p ["port upstream retry"]
retry
rescue Exception => ex
p ["accex", ex]
ensure
peer.close
client.close
end
[200, "ok"]
when "stdin"
process_yield.stdin.enqueue params["c"]
[200, "ok"]
when "status"
return [500, "still running"] if process_yield.running?
[200, process_yield.status]
when "signal"
return [500, "not running"] unless process_yield.running?
process_yield.signal params["signal"]
[200, "ok"]
when "spawn"
return [500, "already running"] if process_yield.running?
if process_yield.spawn tty: params["tty"], interactive: params["interactive"]
Pytty::Daemon.dump
else
return [500, "could not spawn"]
end
[200, "ok"]
when "rm"
process_yield.signal("KILL") if process_yield.running?
process_yield.cleanup
Pytty::Daemon.yields.delete process_yield.id
Pytty::Daemon.dump
[200, id]
else
raise "unknown: #{component} with id: #{id}"
end
end
|