forked from joedurbak/asdetector-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_client.py
108 lines (87 loc) · 2.85 KB
/
server_client.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
import argparse
import socket
import sys
import threading
import time
import queue
from asdetector.utils.logging import get_request_message, request_sendall
from asdetector.utils.files import load_settings
parser = argparse.ArgumentParser()
parser.add_argument('COMMAND', type=str, nargs='+')
def add_input(input_queue):
while True:
input_queue.put(sys.stdin.read(1))
class MySocket:
"""demonstration class only
- coded for clarity, not efficiency
"""
def __init__(self, sock=None):
if sock is None:
self.sock = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
else:
self.sock = sock
def connect(self, host, port):
self.sock.connect((host, port))
def mysend(self, msg):
totalsent = 0
MSGLEN = len(msg)
while totalsent < MSGLEN:
sent = self.sock.send(msg[totalsent:])
if sent == 0:
raise RuntimeError("socket connection broken")
totalsent = totalsent + sent
def myreceive(self):
chunks = []
bytes_recd = 0
MSGLEN = 60
while bytes_recd < MSGLEN:
chunk = self.sock.recv(min(MSGLEN - bytes_recd, 2048))
if chunk == b'':
raise RuntimeError("socket connection broken")
chunks.append(chunk)
bytes_recd = bytes_recd + len(chunk)
return b''.join(chunks)
def receive_beef(self):
complete = False
while not complete:
message = get_request_message(self.sock)
if message == '\x03' or message == '\x15':
complete = True
else:
print(message)
def send_beef(self, message):
request_sendall(self.sock, message)
def main():
ms = MySocket()
ms.connect(load_settings()['HOST'], load_settings()['PORT'])
input_queue = queue.Queue()
input_thread = threading.Thread(target=add_input, args=(input_queue,))
input_thread.daemon = True
input_thread.start()
last_update = time.time()
command = ''
while True:
if time.time()-last_update > 0.5:
# sys.stdout.write(".")
last_update = time.time()
if not input_queue.empty():
while not input_queue.empty():
command += input_queue.get_nowait()
if command.endswith('\n'):
print("\ninput:", command)
ms.send_beef(command)
ms.receive_beef()
command = ''
# ms = MySocket()
# ms.connect(load_settings()['HOST'], load_settings()['PORT'])
# ms.mysend(str.encode('TEST'))
# print(ms.myreceive().decode())
# args = parser.parse_args()
# args_joined = (' '.join(args.COMMAND))
# commands = args_joined.split(',')
# for command in commands:
# ms.send_beef(command)
# ms.receive_beef()
# foobar()
main()