-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_base.py
More file actions
146 lines (121 loc) · 5.42 KB
/
server_base.py
File metadata and controls
146 lines (121 loc) · 5.42 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
"""
Summary: Reciever to pair wih the sender.
Instructions:
Replace "your key" below with the same key used in your sender. Share the updated "sender_base.py" and "server_base.py" to people you want to allow to send/recieve files from.
"""
import os
import threading
import socket
from cryptography.fernet import Fernet
import tkinter as tk
from tkinter import filedialog, messagebox, scrolledtext
from twisted.internet import reactor, protocol, endpoints
from zeroconf import ServiceInfo, Zeroconf
# Hard-coded Fernet key
key = your key # Replace with your generated key
fernet = Fernet(key)
TEMP_DIR = os.path.join(os.path.expanduser("~"), "Documents", "AirDropped Files")
os.makedirs(TEMP_DIR, exist_ok=True)
class FileReceiver(protocol.Protocol):
def __init__(self, factory):
self.factory = factory
self.receiving_file = False
self.filename = ""
self.file_data = b""
def dataReceived(self, data):
if not self.receiving_file:
if data.startswith(b"FILE "):
self.filename = data[5:].decode().strip()
self.factory.app.status_text.insert(tk.END, f"Receiving file: {self.filename}\n")
self.receiving_file = True
else:
self.file_data += data
# Assuming the file is sent in one chunk
if len(self.file_data) > 0:
self.process_received_file()
self.receiving_file = False
self.filename = ""
self.file_data = b""
def process_received_file(self):
try:
decrypted_data = fernet.decrypt(self.file_data)
file_path = os.path.join(TEMP_DIR, self.filename)
with open(file_path, "wb") as f:
f.write(decrypted_data)
self.factory.app.status_text.insert(tk.END, f"Received and decrypted file: {self.filename}\n")
copy_yn = messagebox.askyesno("Copy File", "Copy file to another folder?")
if copy_yn:
new_dir = filedialog.askdirectory(initialdir=os.path.join(os.path.expanduser("~"), "Documents"))
if new_dir:
saveto = os.path.join(new_dir, self.filename)
with open(saveto, "wb") as f:
f.write(decrypted_data)
self.factory.app.status_text.insert(tk.END, "Copied!\n")
else:
self.factory.app.status_text.insert(tk.END, "File saved in AirDropped Files folder.\n")
except Exception as e:
self.factory.app.status_text.insert(tk.END, f"Decryption error: {e}\n")
class FileReceiverFactory(protocol.Factory):
def __init__(self, app):
self.app = app
def buildProtocol(self, addr):
return FileReceiver(self)
class SignalingClientProtocol(protocol.Protocol):
def __init__(self, factory):
self.factory = factory
def connectionMade(self):
self.factory.app.status_text.insert(tk.END, "Connected to signaling server\n")
self.factory.signaling_connected = True
self.transport.write(f"RECEIVER {self.factory.ip}:{self.factory.port}\n".encode())
class SignalingClientFactory(protocol.Factory):
def __init__(self, app, ip, port):
self.app = app
self.signaling_connected = False
self.ip = ip
self.port = port
def buildProtocol(self, addr):
return SignalingClientProtocol(self)
class ReceiverApp:
def __init__(self, root):
self.root = root
self.root.title("AirDrop Receiver")
self.status_text = scrolledtext.ScrolledText(root, width=60, height=15, wrap=tk.WORD)
self.status_text.pack(pady=20)
# Zeroconf for advertising
self.zeroconf = Zeroconf()
ip_address = socket.gethostbyname(socket.gethostname())
self.info = ServiceInfo(
"_airdrop._tcp.local.",
"AirDropReceiver._airdrop._tcp.local.",
addresses=[socket.inet_aton(ip_address)],
port=6001,
properties={},
server=f"{socket.gethostname()}.local."
)
self.zeroconf.register_service(self.info)
# Signaling server connection
self.ip = ip_address
self.port = 6001
self.signaling_client = SignalingClientFactory(self, self.ip, self.port)
endpoint = endpoints.TCP4ClientEndpoint(reactor, "127.0.0.1", 0) # Use port 0 to let the system choose an available port
d = endpoint.connect(self.signaling_client)
d.addCallback(self.handle_signaling_connection)
d.addErrback(self.handle_error)
# Twisted server for receiving files
endpoint = endpoints.TCP4ServerEndpoint(reactor, 6001)
endpoint.listen(FileReceiverFactory(self))
self.status_text.insert(tk.END, f"Listening on {self.ip}:{self.port}\n")
def handle_signaling_connection(self, protocol):
self.status_text.insert(tk.END, "Connected to signaling server\n")
def handle_error(self, error):
self.status_text.insert(tk.END, f"Connection error: {error}\n")
def __del__(self):
self.zeroconf.close()
if __name__ == "__main__":
root = tk.Tk()
app = ReceiverApp(root)
def start_reactor():
reactor.run(installSignalHandlers=False)
reactor_thread = threading.Thread(target=start_reactor)
reactor_thread.start()
root.mainloop()