forked from instantbox/instantbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinspire.py
189 lines (161 loc) · 5.6 KB
/
inspire.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
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
#!/bin/python
import re
import os
import json
import time
from flask_cors import CORS
from flask import render_template, redirect
from flask import Flask, request, Response, jsonify
from api.instantboxManager import InstantboxManager
app = Flask(__name__)
CORS(app, resources=r'/*')
instantboxManager = InstantboxManager()
SERVERURL = os.environ.get('SERVERURL')
if SERVERURL is None:
SERVERURL = ''
@app.route('/v2/superinspire')
def hello():
return 'hello'
@app.route('/v2/superinspire/getOSList')
def returnList():
response = Response(
json.dumps(instantboxManager.OS_LIST), mimetype='application/json')
response.headers.add('Server', 'python flask')
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET,POST'
response.headers['Access-Control-Allow-Headers'] = 'x-requested-with'
return response
@app.route('/v2/superinspire/rmOS')
def rmOS():
try:
containerId = request.args.get('containerId')
timestamp = request.args.get('timestamp')
shareUrl = request.args.get('shareUrl')
except Exception:
response = Response(
json.dumps({
'message': 'Arguments ERROR',
'statusCode': 0
}),
mimetype='application/json')
else:
try:
isSuccess = instantboxManager.is_rm_container(containerId)
if not isSuccess:
raise Exception
except Exception:
response = Response(
json.dumps({
'message': 'RM docker containers ERROR',
'shareUrl': '',
'statusCode': 0,
}),
mimetype='application/json')
else:
response = Response(
json.dumps({
'message': 'SUCCESS',
'statusCode': 1,
'containerId': containerId,
}),
mimetype='application/json')
response.headers.add('Server', 'python flask')
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET,POST'
response.headers['Access-Control-Allow-Headers'] = 'x-requested-with'
return response
@app.route('/v2/superinspire/getOS')
def getOS():
open_port = None
try:
os_name = request.args.get('os')
if not instantboxManager.is_os_available(os_name):
raise Exception
except Exception:
response = Response(
json.dumps({
'message': 'The image is not supported at this time ERROR',
'statusCode': 0
}),
mimetype='application/json')
else:
os_mem = request.args.get('mem')
os_cpu = request.args.get('cpu')
os_port = request.args.get('port')
os_timeout = request.args.get('timeout')
if os_mem is None:
os_mem = 512
if os_cpu is None:
os_cpu = 1
max_timeout = 3600 * 24 + time.time()
if os_timeout is None:
os_timeout = max_timeout
else:
os_timeout = min(float(os_timeout), max_timeout)
try:
container_name = instantboxManager.is_create_container(
mem=os_mem,
cpu=os_cpu,
os_name=os_name,
open_port=os_port,
os_timeout=os_timeout,
)
if container_name is None:
raise Exception
else:
ports = instantboxManager.get_container_ports(container_name)
webshell_port = ports['1588/tcp']
if os_port is not None:
open_port = ports['{}/tcp'.format(os_port)]
except Exception:
response = Response(
json.dumps({
'message': 'RUN docker containers ERROR',
'shareUrl': '',
'statusCode': 0,
}),
mimetype='application/json')
else:
response = Response(
json.dumps({
'message':
'SUCCESS',
'shareUrl':
'http://{}:{}'.format(SERVERURL, webshell_port),
'openPort':
open_port,
'statusCode':
1,
'containerId':
container_name,
}),
mimetype='application/json')
response.headers.add('Server', 'python flask')
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET,POST'
response.headers['Access-Control-Allow-Headers'] = 'x-requested-with'
return response
@app.route('/v2/superinspire/prune')
def pruneTimedoutOS():
try:
instantboxManager.remove_timeout_containers()
response = Response(
json.dumps({
'message': 'Success',
'statusCode': 1
}),
mimetype='application/json')
except Exception:
response = Response(
json.dumps({
'message': 'ERROR',
'statusCode': 0
}),
mimetype='application/json')
response.headers.add('Server', 'python flask')
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET,POST'
response.headers['Access-Control-Allow-Headers'] = 'x-requested-with'
return response
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(65501), debug=False)