Skip to content

Commit 7ed4808

Browse files
author
TheNaterz
committed
making api improvements
1 parent c4b49bc commit 7ed4808

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

core/commands/api.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,24 @@ def execute(shell, cmd):
3535
sw = splitted[1].lower()
3636
if sw == "on":
3737
if not shell.rest_thread:
38+
import socket
39+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
40+
try:
41+
s.bind(('127.0.0.1', int(port)))
42+
except OSError as e:
43+
if e.errno == 98:
44+
shell.print_error("Port %s is already bound!" % (port))
45+
elif e.errno == 13:
46+
shell.print_error("Port %s bind permission denied!" % (port))
47+
s.close()
48+
return
49+
s.close()
50+
3851
rest_server = core.rest_server.RestServer(shell, port, username, password)
3952
def thread_rest_server():
4053
try:
4154
rest_server.run()
42-
except SystemExit as e:
55+
except SystemExit:
4356
pass
4457

4558
stdout = sys.stdout
@@ -50,13 +63,18 @@ def thread_rest_server():
5063
shell.rest_thread.start()
5164
time.sleep(1)
5265
sys.stdout = stdout
53-
if shell.rest_thread:
66+
# ok, now THIS is the most embarassing thing i've ever done.
67+
# i don't know how to pass exceptions from the thread to the caller.
68+
# so here we are.
69+
if "started" in shell.rest_thread.localtrace(0,0,0).__str__():
5470
shell.print_good("Rest server running on port %s" % port)
5571
shell.print_status("Username: %s" % username)
5672
shell.print_status("Password: %s" % password)
5773
shell.print_status("API Token: %s" % rest_server.token)
5874
else:
59-
shell.print_error("Could not start rest server: ")
75+
shell.rest_thread.kill()
76+
shell.rest_thread = ""
77+
shell.print_error("Could not start rest server.")
6078

6179
else:
6280
shell.print_error("Rest server already running")
@@ -67,3 +85,6 @@ def thread_rest_server():
6785
shell.print_good("Rest server shutdown")
6886
else:
6987
shell.print_error("Rest server not running")
88+
89+
else:
90+
help(shell)

core/rest_server.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -609,10 +609,7 @@ def implants(i_type, i_name):
609609
else:
610610
return jsonify(success=False, error="Couldn't dispatch jobs")
611611

612-
613-
614-
615-
rest_api.run(host='0.0.0.0', port=int(self.port), threaded=True)
612+
rest_api.run(host='127.0.0.1', port=int(self.port), threaded=True)
616613

617614
def condense_creds(self):
618615
bad_keys = []

0 commit comments

Comments
 (0)