-
Notifications
You must be signed in to change notification settings - Fork 0
/
foxite
executable file
·236 lines (192 loc) · 6.19 KB
/
foxite
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/bin/python3
#
# Foxite
# Version v2.1
# Copyright (c) 2022-2023 FreedTapstry21
#
# Library imports
import socket, json, os, sys
# Defines STARTMSG and HELPMSG
STARTMSG = """
Foxite
Version v2.1
Copyright (c) 2022-2023 FreedTapstry21
"""
HELPMSG = """Foxite, version v2.1
Usage: """ + sys.argv[0] + """ [flags]
Available flags:
--help
-h [host]
-p [port]
-d [dir]"""
# notify class
# Used for logging info onto the Terminal display
class notify:
def __init__(self):
self.msg = []
def clear(self):
self.msg = []
def info(self, msg):
self.msg.append("[INFO] " + str(msg))
def error(self, msg):
self.msg.append("[ERROR] " + str(msg))
def raw_print(self, msg):
self.msg.append(str(msg))
def space(self):
self.msg.append("")
def print(self):
for x in self.msg:
print(x)
self.clear()
# server class
# Used for setting up and running the http server
class server:
def __init__(self):
self.host = "0.0.0.0"
self.port = "80"
self.listeners = "1"
self.dir = "./"
def connect(self):
# Waits untill client excepts
self.client_sock, self.client_addr = self.sock.accept()
def get_url(self):
request = self.client_sock.recv(1024).decode()
# Checks if it's a HTTP GET request is, if not then closes the connection
if request.split(' ')[0] != "GET":
self.client_sock.close()
# Splits the headers
headers = request.split('\n')
url = headers[0].split()[1]
adv_url = url.split("/")
adv_url.pop(0)
return url, adv_url
def get_file(self, dir, url):
found = False
if found == False:
try:
with open(dir + url + ".html", 'rb') as file:
contents = file.read()
found = True
except:
pass
if found == False:
try:
with open(dir + url, 'rb') as file:
contents = file.read()
found = True
found = True
except:
pass
if found == False:
return 1, "<html><head><title>404 - Not found!</title></head><body><h1>404 - Not found!</h1><h2>The file you where looking for was not found!</h2></body></html>"
else:
try:
contents = contents.decode()
except:
pass
return 0, contents
def get_settings(self):
if len(sys.argv) > 1:
i = 0
for x in sys.argv:
if x == sys.argv[0]:
pass
elif x == "--help":
Notify.raw_print(HELPMSG)
Notify.print()
sys.exit()
elif x == "-d":
try: self.dir = sys.argv[i + 1]
except: Notify.error("-d flag was specified but no arguments where given"); Notify.print(); sys.exit()
sys.argv[i + 1] = "pass"
elif x == "-h":
try: self.host = sys.argv[i + 1]
except: Notify.error("-h flag was specified but no arguments where given"); Notify.print(); sys.exit()
sys.argv[i + 1] = "pass"
elif x == "-p":
try: self.port = sys.argv[i + 1]
except: Notify.error("-p flag was specified but no arguments where given"); Notify.print(); sys.exit()
sys.argv[i + 1] = "pass"
elif x == "pass":
pass
else:
Notify.error("An invalid argument has been given"); Notify.print(); sys.exit()
i = i + 1
else:
pass
return 0
def bind(self, host, port):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.sock.bind((host, int(port)))
except:
Notify.error("Failed to bind to port!")
return 1
self.sock.listen(int(self.listeners))
return 0
# Web function
# Used to serve the page to the client
def Web(url):
# Web
if url == "/":
url = "/index.html"
status, data = Server.get_file(Server.dir, url)
if status == 0:
Notify.info(str(Server.client_addr) + " requested " + str(url))
elif status == 1:
Notify.info(str(Server.client_addr) + " requested " + str(url) + " but the file was not found!")
else:
Notify.error(str(Server.client_addr) + " requested " + str(url) + " but the get_file function returned an unknown status code.")
header = str("HTTP/1.1 200 OK\n\n").encode()
response = str(data).encode()
Server.client_sock.send(header)
Server.client_sock.send(response)
return
# Define classes
Notify = notify()
Server = server()
Notify = notify()
# Checks arguments
status = Server.get_settings()
if status == 0: pass
else: Notify.error("Foxite was unable to start. Please check your arguments."); Notify.print(); sys.exit()
#
# Server initialization
#
Notify.raw_print(STARTMSG)
Notify.info("Starting server...")
Notify.print()
Notify.space()
# Displays settings
Notify.info("Host: " + Server.host)
Notify.info("Port: " + Server.port)
Notify.info("Directory: " + Server.dir)
Notify.space()
Notify.print()
# Checks and setting variables
current_dir = os.getcwd()
try: os.chdir(Server.dir); os.chdir(current_dir)
except: os.mkdir(Server.dir)
host = Server.host
port = Server.port
status = Server.bind(host, port)
if status == 1: Notify.info("Unable to continue. Quitting."); Notify.print(); sys.exit()
else: Notify.print()
Notify.info("Now listening on " + str(host) + ":" + str(port))
Notify.info("Press Ctrl+C to exit!")
Notify.space()
Notify.print()
try:
while True:
Server.connect()
url, adv_url = Server.get_url()
Web(url)
Server.client_sock.close()
Notify.print()
except KeyboardInterrupt:
Notify.info("Quiting."); Notify.print(); sys.exit()
except Exception as err:
Notify.error("An internal error occured!"); Notify.error("Internal error: " + str(err)); Notify.print(); sys.exit()
#
# End of file
#