4
4
from socket import gethostname
5
5
6
6
import requests
7
+ from requests .exceptions import ConnectionError
7
8
8
9
import monkeyfs
9
10
import tunnel
@@ -24,59 +25,77 @@ class ControlClient(object):
24
25
proxies = {}
25
26
26
27
@staticmethod
27
- def wakeup (parent = None , default_tunnel = None , has_internet_access = None ):
28
- LOG . debug ( "Trying to wake up with Monkey Island servers list: %r" % WormConfiguration . command_servers )
29
- if parent or default_tunnel :
30
- LOG . debug ( "parent: %s, default_tunnel: %s" % ( parent , default_tunnel ))
28
+ def wakeup (parent = None , has_internet_access = None ):
29
+ if parent :
30
+ LOG . debug ( "parent: %s" % ( parent ,))
31
+
31
32
hostname = gethostname ()
32
33
if not parent :
33
34
parent = GUID
34
35
35
36
if has_internet_access is None :
36
37
has_internet_access = check_internet_access (WormConfiguration .internet_services )
37
38
38
- for server in WormConfiguration .command_servers :
39
- try :
40
- WormConfiguration .current_server = server
39
+ monkey = {'guid' : GUID ,
40
+ 'hostname' : hostname ,
41
+ 'ip_addresses' : local_ips (),
42
+ 'description' : " " .join (platform .uname ()),
43
+ 'internet_access' : has_internet_access ,
44
+ 'config' : WormConfiguration .as_dict (),
45
+ 'parent' : parent }
41
46
42
- monkey = {'guid' : GUID ,
43
- 'hostname' : hostname ,
44
- 'ip_addresses' : local_ips (),
45
- 'description' : " " .join (platform .uname ()),
46
- 'internet_access' : has_internet_access ,
47
- 'config' : WormConfiguration .as_dict (),
48
- 'parent' : parent }
47
+ if ControlClient .proxies :
48
+ monkey ['tunnel' ] = ControlClient .proxies .get ('https' )
49
49
50
- if ControlClient .proxies :
51
- monkey ['tunnel' ] = ControlClient .proxies .get ('https' )
50
+ requests .post ("https://%s/api/monkey" % (WormConfiguration .current_server ,),
51
+ data = json .dumps (monkey ),
52
+ headers = {'content-type' : 'application/json' },
53
+ verify = False ,
54
+ proxies = ControlClient .proxies ,
55
+ timeout = 20 )
56
+
57
+ @staticmethod
58
+ def find_server (default_tunnel = None ):
59
+ LOG .debug ("Trying to wake up with Monkey Island servers list: %r" % WormConfiguration .command_servers )
60
+ if default_tunnel :
61
+ LOG .debug ("default_tunnel: %s" % (default_tunnel ,))
62
+
63
+ current_server = ""
64
+
65
+ for server in WormConfiguration .command_servers :
66
+ try :
67
+ current_server = server
52
68
53
69
debug_message = "Trying to connect to server: %s" % server
54
70
if ControlClient .proxies :
55
71
debug_message += " through proxies: %s" % ControlClient .proxies
56
72
LOG .debug (debug_message )
57
- reply = requests .post ("https://%s/api/monkey" % (server ,),
58
- data = json .dumps (monkey ),
59
- headers = {'content-type' : 'application/json' },
60
- verify = False ,
61
- proxies = ControlClient .proxies ,
62
- timeout = 20 )
73
+ requests .get ("https://%s/api?action=is-up" % (server ,),
74
+ verify = False ,
75
+ proxies = ControlClient .proxies )
76
+ WormConfiguration .current_server = current_server
63
77
break
64
78
65
- except Exception as exc :
66
- WormConfiguration . current_server = ""
79
+ except ConnectionError as exc :
80
+ current_server = ""
67
81
LOG .warn ("Error connecting to control server %s: %s" , server , exc )
68
82
69
- if not WormConfiguration .current_server :
70
- if not ControlClient .proxies :
83
+ if current_server :
84
+ return True
85
+ else :
86
+ if ControlClient .proxies :
87
+ return False
88
+ else :
71
89
LOG .info ("Starting tunnel lookup..." )
72
90
proxy_find = tunnel .find_tunnel (default = default_tunnel )
73
91
if proxy_find :
74
92
proxy_address , proxy_port = proxy_find
75
93
LOG .info ("Found tunnel at %s:%s" % (proxy_address , proxy_port ))
76
94
ControlClient .proxies ['https' ] = 'https://%s:%s' % (proxy_address , proxy_port )
77
- ControlClient .wakeup ( parent = parent , has_internet_access = has_internet_access )
95
+ return ControlClient .find_server ( )
78
96
else :
79
97
LOG .info ("No tunnel found" )
98
+ return False
80
99
81
100
@staticmethod
82
101
def keepalive ():
@@ -249,7 +268,6 @@ def get_monkey_exe_filename_and_size_by_host_dict(host_dict):
249
268
data = json .dumps (host_dict ),
250
269
headers = {'content-type' : 'application/json' },
251
270
verify = False , proxies = ControlClient .proxies )
252
-
253
271
if 200 == reply .status_code :
254
272
result_json = reply .json ()
255
273
filename = result_json .get ('filename' )
0 commit comments