-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
executable file
·164 lines (112 loc) · 4.48 KB
/
Copy pathrun.py
File metadata and controls
executable file
·164 lines (112 loc) · 4.48 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
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/python
import struct
import json
import time
from twisted.internet.protocol import DatagramProtocol, ClientFactory, Protocol, connectionDone
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet.task import LoopingCall
from twisted.internet import reactor
from service import airTelnetFactory
from air import airDataManager, airBundle
dataManager = airDataManager()
class airCargo(object):
def __init__(self, data):
self.__file_id = data['file_id']
self.__filename = data['filename']
self.__size = data['size']
self.__sender = data['sender']
self.__ready = 0
self.__buff = ''
self.__connection = None
self.__fp = None
def download(self):
self.__fp = open('/tmp/' + str(time.time()) + self.__filename, 'wb')
reactor.connectTCP(self.__sender, 9999, airCargoFactory({
'onReceive': self.onReceive,
'onConnect': self.onConnect,
'onClose': self.onClose,
}))
def onReceive(self, data):
self.__ready += len(data)
self.__buff += data
if self.__ready >= self.__size and self.__connection is not None:
self.__connection.transport.loseConnection()
def onClose(self, data):
jsonSize = struct.unpack('>h', self.__buff[:2])
head = jsonSize[0] + 2
self.__fp.write(self.__buff[head:])
self.__fp.close()
def onConnect(self, connection):
jsonData = json.dumps({'fileid': self.__file_id, 'from': 0, 'to': self.__size})
raw = struct.pack('>h', len(jsonData)) + jsonData
connection.transport.write(raw)
def getID(self):
return self.__file_id
def getSender(self):
return self.__sender
def getName(self):
return self.__filename
class airCargoDownloader(Protocol):
def __init__(self, callbacks):
self.__callbacks = callbacks
def dataReceived(self, data):
self.__callbacks['onReceive'].__call__(data)
def connectionMade(self):
self.__callbacks['onConnect'].__call__(self)
def connectionLost(self, reason=connectionDone):
self.__callbacks['onClose'].__call__(reason)
class airCargoFactory(ClientFactory):
def __init__(self, callbacks):
self.__callbacks = callbacks
def buildProtocol(self, addr):
return airCargoDownloader(self.__callbacks)
class airReceiver(DatagramProtocol):
def __init__(self):
self.__loopHeartBeat = None
self.__loopResponse = None
def datagramReceived(self, data, (host, port)):
pack = airBundle(data)
if pack.is_valid():
if 'iamalive' == pack.type:
found = False
for user in dataManager.users:
if pack.name == user['name']:
user['up'] = time.time()
found = True
elif time.time() - user['up'] > 5000:
dataManager.users.remove(user)
if not found:
dataManager.users.append({'name': pack.name, 'up': time.time(), 'ip': host})
elif 'send-file' == pack.type:
found = False
for f in dataManager.files:
if pack.file_id == f.getID():
found = True
break
if not found:
dataManager.files.append(airCargo({
'file_id': pack.file_id,
'filename': pack.filename,
'sender': host,
'size': pack.size
}))
def sendHeartBeat(self):
beatPack = airBundle({'type': 'iamalive', 'name': 'or.n0t'})
self.transport.write(beatPack.compile(), ('<broadcast>', 9999))
def sendResponse(self):
while len(dataManager.bundles) > 0:
response = dataManager.bundles.pop()
self.transport.write(response.compile(), (response.getSender(), 9999))
response.afterSend.__call__()
def startProtocol(self):
self.transport.setBroadcastAllowed(True)
self.__loopHeartBeat = LoopingCall(self.sendHeartBeat)
self.__loopHeartBeat.start(1)
self.__loopResponse = LoopingCall(self.sendResponse)
self.__loopResponse.start(1)
def stopProtocol(self):
pass
reactor.listenUDP(9999, airReceiver())
endpoint = TCP4ServerEndpoint(reactor, 2233)
endpoint.listen(airTelnetFactory(dataManager))
reactor.run()