- 851FE9967EA2562F0BD30255F3E9B1CA1487E7FD56A649E93A0EFBA4D6DE28A70F0FF6C1B3DDBEF90EE4D6370403D18A27A31CDCD062A46E7116DB5F4479F7A3
+ 44B1B3516F89AD8B7E7B9769A5C6089E22E9D459D012C1B72232926E0ABBDF42FBF74D2CD38F1344FE29143386C21432A61DC1DF454BF1CBA19ABDD8910C05CF
blatta/lib/server.py
(1 . 4)(1 . 4)
218 VERSION = "9992"
219 VERSION = "9991"
220
221 import os
222 import select
(8 . 12)(8 . 18)
224 import tempfile
225 import time
226 import string
227 import binascii
228 import datetime
229 from datetime import datetime
230 from lib.client import Client
231 from lib.state import State
232 from lib.channel import Channel
233 from lib.infosec import PACKET_SIZE
234 from lib.infosec import MAX_BOUNCES
235 from lib.infosec import STALE_PACKET
236 from lib.infosec import DUPLICATE_PACKET
237 from lib.infosec import MALFORMED_PACKET
238 from lib.infosec import INVALID_SIGNATURE
239 from lib.infosec import Infosec
240 from lib.peer import Peer
241 from lib.ringbuffer import Ringbuffer
(25 . 10)(31 . 9)
243
244 class Server(object):
245 def __init__(self, options):
246 self.ports = options.ports
247 self.irc_ports = options.irc_ports
248 self.udp_port = options.udp_port
249 self.password = options.password
250 self.ssl_pem_file = options.ssl_pem_file
251 self.motdfile = options.motd
252 self.verbose = options.verbose
253 self.debug = options.debug
(112 . 7)(117 . 7)
255
256 def print_debug(self, msg):
257 if self.debug:
258 print(msg)
259 print("%s %s" % (datetime.now(), msg))
260 sys.stdout.flush()
261
262 def print_error(self, msg):
(144 . 11)(149 . 16)
264 def handle_udp_data(self, bytes_address_pair):
265 data = bytes_address_pair[0]
266 address = bytes_address_pair[1]
267 packet_info = (address[0],
268 address[1],
269 binascii.hexlify(data)[0:16])
270 self.print_debug("[%s:%d] -> %s" % packet_info)
271 for peer in self.state.get_peers():
272 if peer.get_key() != None:
273 message = self.infosec.unpack(peer, data)
274 if(message != None):
275 self.print_debug("valid message from peer: %s" % peer.handles[0])
276 error_code = message["error_code"]
277 if(error_code == None):
278 self.print_debug("[%s] -> %s" % (peer.handles[0], message["body"]))
279
280 # we only update the address table if the speaker is same as peer
281
(159 . 8)(169 . 8)
283
284 if idx != None:
285 self.state.update_address_table({"handle": message["speaker"],
286 "address": address[0],
287 "port": address[1]
288 "address": address[0],
289 "port": address[1]
290 })
291 # send the message to all clients
292 for c in self.clients:
(171 . 7)(181 . 18)
294 if(message["command"] == BROADCAST) and message["bounces"] < MAX_BOUNCES:
295 self.rebroadcast(peer, message)
296 return
297 self.print_debug("Unknown peer: %s %d" % (address[0], address[1]))
298 elif error_code == STALE_PACKET:
299 self.print_debug("[%s:%d] -> stale packet: %s" % packet_info)
300 return
301 elif error_code == DUPLICATE_PACKET:
302 self.print_debug("[%s:%d] -> duplicate packet: %s" % packet_info)
303 return
304 elif error_code == MALFORMED_PACKET:
305 self.print_debug("[%s:%d] -> malformed packet: %s" % packet_info)
306 return
307 elif error_code == INVALID_SIGNATURE:
308 pass
309 self.print_debug("[%s:%d] -> martian packet: %s" % packet_info)
310
311 def peer_message(self, message):
312 message["original"] = True
(201 . 9)(222 . 10)
314 # Setup UDP first
315 self.udp_server_socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
316 self.udp_server_socket.bind((self.address, self.udp_port))
317 self.print_info("Listening for Pest packets on udp port %d." % self.udp_port)
318
319 serversockets = []
320 for port in self.ports:
321 for port in self.irc_ports:
322 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
323 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
324 try:
(214 . 7)(236 . 7)
326 s.listen(5)
327 serversockets.append(s)
328 del s
329 self.print_info("Listening on port %d." % port)
330 self.print_info("Listening for IRC connections on port %d." % port)
331 if self.chroot:
332 os.chdir(self.chroot)
333 os.chroot(self.chroot)
(232 . 7)(254 . 7)
335 [x.socket for x in self.clients.values()
336 if x.write_queue_size() > 0],
337 [],
338 0)
339 1)
340 for x in inputready:
341 if x == self.udp_server_socket:
342 bytes_address_pair = self.udp_server_socket.recvfrom(PACKET_SIZE)
(242 . 19)(264 . 6)
344 self.clients[x].socket_readable_notification()
345 else:
346 (conn, addr) = x.accept()
347 if self.ssl_pem_file:
348 import ssl
349 try:
350 conn = ssl.wrap_socket(
351 conn,
352 server_side=True,
353 certfile=self.ssl_pem_file,
354 keyfile=self.ssl_pem_file)
355 except ssl.SSLError as e:
356 self.print_error(
357 "SSL error for connection from %s:%s: %s" % (
358 addr[0], addr[1], e))
359 continue
360 self.clients[conn] = Client(self, conn)
361 self.print_info("Accepted connection from %s:%s." % (
362 addr[0], addr[1]))