-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.py
85 lines (70 loc) · 2.22 KB
/
service.py
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
import random
import subprocess
from monarch.builder import *
from subprocess import STDOUT, check_output
import os
import base64
def routine(req: BuildRequest) -> BuildResponse:
out = "empress"
agent_id = req.params.get("id") # definitely exists
host = req.params.get("lhost")
port = req.params.get("lport")
ci = req.params.get("callback_interval")
cs = req.params.get("callback_salt")
interval = req.params.get("interval")
salt = req.params.get("salt")
ca_cert = req.params.get("ca_cert")
# if true, then debug is true. otherwise false
debug = str(req.params.get("debug")).lower() == "true"
use_tcp = str(req.params.get("use_tcp")).lower() == "true"
mode = req.params.get("mode")
if mode == "session":
mode = 0
else:
mode = 1
with open("config/config.json", "w") as f:
j = {
"id": agent_id,
"host": host,
"port": port,
"callback_interval": int(ci),
"callback_salt": int(cs),
"interval": int(interval),
"salt": int(salt),
"mode": mode,
"tcp": use_tcp,
"ca_cert": ca_cert,
}
j_string = json.dumps(j)
f.write(j_string)
if os.path.exists(out):
try:
os.remove(out)
# if dir has the same name or whatever
except OSError:
out = random.randbytes(10).decode('utf-8')
cmd = "go build -o %s " % out
if debug:
cmd += "-tags debug "
cmd += "."
data = b""
my_env = os.environ.copy()
my_env["GOOS"] = req.params.get("os")
my_env["GOARCH"] = req.params.get("arch")
try:
error = check_output(cmd.split(), stderr=STDOUT, env=my_env)
except subprocess.CalledProcessError as e:
error = e.output.decode('utf-8')
# path doesn't exist if build failed
if not os.path.exists(out):
status = 1
else:
status = 0
error = ""
with open(out, "rb") as f:
data = f.read()
res = BuildResponse(status, error, base64.b64encode(data).decode('utf-8'))
return res
if __name__ == "__main__":
build_server = builder_service(BuildFunction(routine))
build_server.serve_forever()