-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·106 lines (85 loc) · 3.6 KB
/
server.py
File metadata and controls
executable file
·106 lines (85 loc) · 3.6 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# python server.py
#
import sys, os
import socket
import subprocess
import threading
import urllib.request
import urllib.parse
import http.client
from datetime import datetime
class Server:
def __init__(self, host='0.0.0.0', port=31522):
self.host = host
self.port = port
def start(self):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
server_socket.bind((self.host, self.port))
server_socket.listen(65535)
print(f"[+] Listening on port {self.port}")
while True:
client_socket, addr = server_socket.accept()
client_thread = threading.Thread(target=self.handle_client, args=(client_socket, addr))
client_thread.start()
def handle_client(self, client_socket, addr):
try:
command = client_socket.recv(4096).decode('utf-8').strip()
if not command: return
print(f"[+] {addr[0]} -> {command}")
response = self.parse_command(command)
client_socket.sendall(response.encode('utf-8'))
client_socket.close()
except Exception as e:
print(f"[-] {addr[0]} -- {e}")
client_socket.close()
def parse_command(self, command):
parts = command.split(maxsplit=1)
cmd = parts[0]
if cmd == "get": return self.get_file_content(parts[1] if len(parts) > 1 else "")
elif cmd == "http": return self.fetch_url(parts[1] if len(parts) > 1 else "")
elif cmd == "post": return self.post_request(parts[1] if len(parts) > 1 else "")
elif cmd == "fetch": os.system("git pull"); return "200 OK"
else: return "Invalid API request\n"
def get_file_content(self, filename):
if not filename:
return "Missing filename\n"
elif not os.path.isfile(filename):
return f"File '{filename}' not found.\n"
elif filename.startswith("/") or ".." in filename :
return "Permission Error\n"
else:
try:
with open(filename, "rt") as f: return f.read()
except Exception as e: return f"{e}"
def fetch_url(self, url):
if not url:
return "Missing URL\n"
try:
with urllib.request.urlopen(url if url.startswith("http://") or url.startswith("https://") else "http://" + url) as response:
return response.read().decode('utf-8')
except Exception as e:
return f"{e}"
def post_request(self, post_command):
try:
parts = post_command.split(maxsplit=1)
if len(parts) < 2:
return "URL or data is missing.\n"
url, data = parts
data_dict = dict(param.split('=', 1) for param in data.split('&') if '=' in param)
encoded_data = urllib.parse.urlencode(data_dict).encode('utf-8')
parsed_url = urllib.parse.urlparse(url)
connection_class = http.client.HTTPSConnection if parsed_url.scheme == "https" else http.client.HTTPConnection
connection = connection_class(parsed_url.netloc)
connection.request("POST", parsed_url.path, body=encoded_data,
headers={"Content-Type": "application/x-www-form-urlencoded"})
response = connection.getresponse()
response_data = response.read().decode('utf-8')
connection.close()
return response_data
except Exception as e:
return f"{e}"
if __name__ == '__main__':
Server().start()