- 543B50FA952DEA77A3A28AC4166697877605D330DD65B74B50F2E27393F7AA0C3CC08585515661194D65F23F208818A32D2DFF7C3C81385A9473803B4FEE61E6+ 81527BDB79D3F7C5F42234E6355A530D86FCDC3974A0461782DCD4FE5FC245F8CDCA423F4F033CF207224544525EC222A647F8BF56E70BF5A0F04FBB73120D89blatta/lib/server.py(1 . 10)(1 . 9)
194 VERSION = "9986"
195 VERSION = "9985"
196
197 import os
198 import select
199 import socket
200 import sys
201 import sys
202 import tempfile
203 import time
204 import string
(12 . 11)(11 . 13)
206 import sqlite3
207 from datetime import datetime
208 from funcs import *
209 from lib.client import Client
210 from lib.channel import Channel
211 from lib.station import Station
212 from lib.message import Message
213 from lib.infosec import PACKET_SIZE
214 from client import Client
215 from channel import Channel
216 from station import Station
217 from station import EMBARGO_INTERVAL
218 from station import RUBBISH_INTERVAL
219 from message import Message
220 from infosec import PACKET_SIZE
221 import imp
222 import pprint
223 import logging
(28 . 7)(29 . 6)
225 self.channel_name = options.channel_name
226 self.password = options.password
227 self.motdfile = options.motd
228 self.verbose = options.verbose
229 self.logdir = options.logdir
230 self.chroot = options.chroot
231 self.setuid = options.setuid
(37 . 13)(37 . 14)
233 self.pp = pprint.PrettyPrinter(indent=4)
234 self.db_path = options.db_path
235 self.address_table_path = options.address_table_path
236 self.irc_server_address = "127.0.0.1"
237
238 if options.listen:
239 self.address = socket.gethostbyname(options.listen)
240 self.udp_address = socket.gethostbyname(options.listen)
241 else:
242 self.address = ""
243 self.udp_address = ""
244 server_name_limit = 63 # From the RFC.
245 self.name = socket.getfqdn(self.address)[:server_name_limit]
246 self.name = socket.getfqdn(self.udp_address)[:server_name_limit]
247
248 self.channels = {} # irc_lower(Channel name) --> Channel instance.
249 self.client = None
(124 . 13)(125 . 11)
251 def start(self):
252 # Setup UDP first
253 self.udp_server_socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
254 self.udp_server_socket.bind((self.address, self.udp_port))
255 self.udp_server_socket.bind((self.udp_address, self.udp_port))
256 self.station = Station({ "socket": self.udp_server_socket,
257 "db_path": self.db_path,
258 "address_table_path": self.address_table_path
259 })
260 self.station.start_embargo_queue_checking()
261 self.station.start_rubbish()
262 logging.info("Listening for Pest packets on udp port %d." % self.udp_port)
263
264 serversockets = []
(138 . 7)(137 . 7)
266 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
267 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
268 try:
269 s.bind((self.address, port))
270 s.bind((self.irc_server_address, port))
271 except socket.error as e:
272 logging.error("Could not bind port %s: %s." % (port, e))
273 sys.exit(1)
(155 . 36)(154 . 62)
275 os.setuid(self.setuid[0])
276 logging.info("Setting uid:gid to %s:%s"
277 % (self.setuid[0], self.setuid[1]))
278
279 # event loop setup
280 last_aliveness_check = time.time()
281 last_embargo_queue_check = time.time()
282 last_rubbish_dispatch = time.time()
283 while True:
284 (inputready,outputready,exceptready) = select.select([self.udp_server_socket],[],[],0)
285 (iwtd, owtd, ewtd) = select.select(
286 serversockets + ([self.client.socket] if self.client else []),
287 [self.client.socket] if self.client and self.client.write_queue_size() > 0 else [],
288 [],
289 .2)
290 for x in inputready:
291 if x == self.udp_server_socket:
292 bytes_address_pair = self.udp_server_socket.recvfrom(PACKET_SIZE)
293 self.station.handle_udp_data(bytes_address_pair)
294 # we don't want to be listening for client connections if there's already a client connected
295 if self.client == None:
296 input_sockets = serversockets
297 else:
298 input_sockets = [self.client.socket]
299 output_sockets = ([self.client.socket]
300 if self.client and self.client.write_queue_size() > 0 else [])
301
302 # handle tcp socket events
303 (iwtd, owtd, ewtd) = select.select(input_sockets, output_sockets, [], .2)
304 for x in iwtd:
305 if self.client != None:
306 self.client.socket_readable_notification()
307 else:
308 (conn, addr) = x.accept()
309 self.client = Client(self, conn)
310 self.station.client = self.client
311 logging.info("Accepted connection from %s:%s." % (
312 addr[0], addr[1]))
313 try:
314 (conn, addr) = x.accept()
315 self.client = Client(self, conn)
316 self.station.client = self.client
317 logging.info("Accepted connection from %s:%s." % (
318 addr[0], addr[1]))
319 except socket.error as e:
320 logging.error("Failed to accept new client connection: %s" % e)
321 for x in owtd:
322 if self.client and x == self.client.socket: # client may have been disconnected
323 self.client.socket_writable_notification()
324
325 # handle udp socket events
326 (inputready,outputready,exceptready) = select.select([self.udp_server_socket],[],[],0)
327 for x in inputready:
328 if x == self.udp_server_socket:
329 bytes_address_pair = self.udp_server_socket.recvfrom(PACKET_SIZE)
330 self.station.handle_udp_data(bytes_address_pair)
331
332 # ping pong
333 now = time.time()
334 if last_aliveness_check + 10 < now:
335 if self.client:
336 self.client.check_aliveness()
337 if self.client:
338 self.client.check_aliveness()
339 last_aliveness_check = now
340
341 # clear embargo queue if enough time has elapsed
342 if last_embargo_queue_check + EMBARGO_INTERVAL < now:
343 self.station.check_embargo_queue()
344 last_embargo_queue_check = now
345
346 # spray rubbish
347 if last_rubbish_dispatch + RUBBISH_INTERVAL < now:
348 self.station.send_rubbish()
349 last_rubbish_dispatch = now
350
351 def create_directory(path):
352 if not os.path.isdir(path):
353 os.makedirs(path)