-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetBridge.py
More file actions
153 lines (111 loc) · 3.9 KB
/
NetBridge.py
File metadata and controls
153 lines (111 loc) · 3.9 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
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
import os
import socket
import threading
import tkinter as tk
from tkinter import filedialog
from http.server import BaseHTTPRequestHandler, HTTPServer
import urllib.parse
from datetime import datetime
PORT = 8000
BASE_DIR = ""
clients_log = []
# ---------------- IP ----------------
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
except:
return "127.0.0.1"
finally:
s.close()
# ---------------- HTTP SERVER ----------------
class Handler(BaseHTTPRequestHandler):
def log_client(self):
ip = self.client_address[0]
time = datetime.now().strftime("%H:%M:%S")
clients_log.append(f"[{time}] {ip} connected")
def do_GET(self):
self.log_client()
path = urllib.parse.unquote(self.path)
full_path = os.path.join(BASE_DIR, path.lstrip("/"))
if os.path.isdir(full_path):
try:
items = os.listdir(full_path)
except:
items = []
dirs = [i for i in items if os.path.isdir(os.path.join(full_path, i))]
files = [i for i in items if os.path.isfile(os.path.join(full_path, i))]
html = """
<html>
<body style="background:black;color:lime;font-family:monospace">
<h2>NetBridge Client</h2>
<hr>
<b>DIRECTORIES:</b><br>
"""
for d in dirs:
html += f'<a href="{d}">[DIR] {d}</a><br>'
html += "<br><b>FILES:</b><br>"
for f in files:
html += f'<a href="{f}">{f}</a><br>'
html += "</body></html>"
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.end_headers()
self.wfile.write(html.encode())
elif os.path.isfile(full_path):
self.send_response(200)
self.send_header("Content-Type", "application/octet-stream")
self.end_headers()
with open(full_path, "rb") as f:
self.wfile.write(f.read())
else:
self.send_response(404)
self.end_headers()
# ---------------- SERVER ----------------
def start_server(status_label):
def run():
global BASE_DIR
os.chdir(BASE_DIR)
server = HTTPServer(("0.0.0.0", PORT), Handler)
ip = get_ip()
status_label.config(
text=f"ONLINE → http://{ip}:{PORT}",
fg="green"
)
server.serve_forever()
threading.Thread(target=run, daemon=True).start()
# ---------------- GUI ----------------
def choose_folder(entry):
global BASE_DIR
folder = filedialog.askdirectory()
if folder:
BASE_DIR = folder
entry.delete(0, tk.END)
entry.insert(0, folder)
def refresh_log(listbox):
listbox.delete(0, tk.END)
for l in clients_log[-15:]:
listbox.insert(tk.END, l)
root.after(1000, lambda: refresh_log(listbox))
def build_gui():
global root
root = tk.Tk()
root.title("NetBridge Server (Powered by B@ss - http://www.basshp.msxall.com)")
root.geometry("600x350")
tk.Label(root, text="Shared folder:").pack()
frame = tk.Frame(root)
frame.pack()
entry = tk.Entry(frame, width=50)
entry.pack(side=tk.LEFT)
tk.Button(frame, text="...", command=lambda: choose_folder(entry)).pack(side=tk.LEFT)
status = tk.Label(root, text="OFFLINE", fg="red")
status.pack(pady=10)
tk.Button(root, text="START SERVER",
command=lambda: start_server(status)).pack()
tk.Label(root, text="Client log:").pack()
log = tk.Listbox(root, width=70, height=8)
log.pack()
refresh_log(log)
root.mainloop()
build_gui()