-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtls_server.ecs
More file actions
68 lines (61 loc) · 1.82 KB
/
tls_server.ecs
File metadata and controls
68 lines (61 loc) · 1.82 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
import simple_tls as tls
import argparse
var parser = new argparse.ArgumentParser
parser.program_name = "ecs tls_server.ecs"
parser.description = "A simple TLS server"
parser.add_argument("addr", true, "Address to listen")
parser.add_argument("pub_key", true, "Public key")
parser.add_argument("priv_key", true, "Private key")
parser.add_argument("passwd", true, "Password of private key")
parser.add_option("--port", false, false, "Port to listen").set_defaults("--port", 1024).set_option_alias("--port", "-p")
var args = null
try
args = parser.parse_args(context.cmd_args)
catch e
system.out.println(e.what)
parser.print_help()
system.exit(0)
end
function wait_for(co, time)
var start_time = runtime.time()
loop
co.resume()
runtime.delay(10)
until co.is_finished() || runtime.time() - start_time >= time
return !co.is_finished()
end
var s = new tls.server
system.out.println("Listening on " + args.addr + ":" + args.port)
s.listen(args.pub_key, args.priv_key, args.passwd, args.addr, args.port as integer)
system.out.println("Fingerprint: " + s.fingerprint())
var acceptor = fiber.create(s.accept)
var wait_times = 0
while wait_for(acceptor, 1000)
system.out.println("Waiting for " + ++wait_times + "s")
end
system.out.println("Connection Established.")
function stdio_worker(sock)
loop
var input = runtime.await(system.in.getline)
if input.toupper() == "EXIT"
sock.close()
break
end
if input == ""
continue
end
sock.send(input)
end
end
var stdio_co = fiber.create(stdio_worker, s)
loop
stdio_co.resume()
if !s.is_open()
system.out.println("Connection Closed.")
break
end
s.sync()
while s.available()
system.out.println("Receive: " + s.receive_some())
end
end