def onrequest(self, line, addr, build): if addr[0] == "127.0.0.1": forceupdategui = line == "forceupdategui" if forceupdategui or line == "updategui": succ, msg = self.gui.update(force=forceupdategui) return ("gui updated successfully: %s" if succ else "could not update gui: %s") % msg, True elif line == "updateservers": succ, msg = self.additionalservers.updateservers() return ("servers updated successfully: %s" if succ else "could not update servers: %s") % msg, True elif line == "updatebanned": succ, msg = self.additionalservers.updatebanned() return ("banned servers updated successfully: %s" if succ else "could not update banned servers: %s") % msg, True elif line == "exc": (0 + "s") return "exception has been raised", False elif line == "catchexc": try: (0 + "s") except: debug.exc() return "raised exception has been caught", False return None, False
def run(self): while True: try: self.updatefrommaster() except: debug.exc(self) time.sleep(self.interval)
def run(self, funcs): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind((self.addr, self.port)) sock.listen(5) while True: try: i = sock.accept() if self.accesscache.permit(i[1][0]): tt = threading.Thread(target=self.processconnection, args=(i, funcs)) tt.start() else: # connection not permitted by accesscache debug.msg(netserver.DBGTAG_REJECT, "rejecting connection from %s:%d (not permitted by accesscache)" % i[1]) try: i[0].shutdown(socket.SHUT_RDWR) except: pass try: i[0].close() except: pass except: debug.exc(self)
def run(self, funcs): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind((self.addr, self.port)) sock.listen(5) while True: try: i = sock.accept() if self.accesscache.permit(i[1][0]): tt = threading.Thread(target=self.processconnection, args=(i, funcs)) tt.start() else: # connection not permitted by accesscache debug.msg( netserver.DBGTAG_REJECT, "rejecting connection from %s:%d (not permitted by accesscache)" % i[1]) try: i[0].shutdown(socket.SHUT_RDWR) except: pass try: i[0].close() except: pass except: debug.exc(self)
def receive(self): while True: try: (data, addr) = self.socket.recvfrom(4096) except: break try: self.processdata(packet(data, addr)) except: debug.exc(self)
def run(self): while True: self.t = time.time() try: self.pingservers() self.sendqueue() self.receive() except: debug.exc(self) now = time.time() time.sleep(max(0, servercommunicator.LOOP_INTERVAL - (now-self.t)))
def run(self): while True: self.t = time.time() try: self.pingservers() self.sendqueue() self.receive() except: debug.exc(self) now = time.time() time.sleep( max(0, servercommunicator.LOOP_INTERVAL - (now - self.t)))
def update(self, catch=True, force=False): # update gui (read it from file) try: f = open(self.path, "r") newguilines = [] errors = [] warnings = [] n = 0 # line number for line in f: n += 1 self.curline = n gl = guiline( self.g, line[:-1] if (len(line) > 0 and line[-1] == "\n") else l) for w in gl.warnings: warnings.append("warning in line %d: %s" % (n, w)) if gl.error: errors.append("skipping line %d: %s" % (n, gl.error)) else: newguilines.append(gl) f.close() exchange = force or (not len(errors)) for w in warnings: debug.msg(gui.DBGTAG_WARNING, w) debug.msg( gui.DBGTAG_WARNINGS, "%s warnings while reading gui" % (str(len(warnings)) if len(warnings) else "no")) for e in errors: debug.msg(gui.DBGTAG_ERROR, e) debug.msg( gui.DBGTAG_ERRORS, "%s errors while reading gui%s, %s" % (str(len(errors)) if len(errors) else "no", " (ignoring these lines)" if len(errors) else "", "applying new guilines list" if exchange else "cancelling update")) if exchange: self.guilines.exchange(newguilines) return exchange, "%d lines read, %d errors (skipping lines with errors), %d warnings" % ( len(newguilines), len(errors), len(warnings)) except: try: f.close() except: pass if catch: debug.exc(self) else: raise return False, "internal errors occurred"
def update(self, catch=True, force=False): # update gui (read it from file) try: f = open(self.path, "r") newguilines = [] errors = [] warnings = [] n = 0 # line number for line in f: n += 1 self.curline = n gl = guiline(self.g, line[:-1] if (len(line) > 0 and line[-1]=="\n") else l) for w in gl.warnings: warnings.append("warning in line %d: %s" % (n, w)) if gl.error: errors.append("skipping line %d: %s" % (n, gl.error)) else: newguilines.append(gl) f.close() exchange = force or (not len(errors)) for w in warnings: debug.msg(gui.DBGTAG_WARNING, w) debug.msg(gui.DBGTAG_WARNINGS, "%s warnings while reading gui" % (str(len(warnings)) if len(warnings) else "no")) for e in errors: debug.msg(gui.DBGTAG_ERROR, e) debug.msg(gui.DBGTAG_ERRORS, "%s errors while reading gui%s, %s" % (str(len(errors)) if len(errors) else "no", " (ignoring these lines)" if len(errors) else "", "applying new guilines list" if exchange else "cancelling update")) if exchange: self.guilines.exchange(newguilines) return exchange, "%d lines read, %d errors (skipping lines with errors), %d warnings" % (len(newguilines), len(errors), len(warnings)) except: try: f.close() except: pass if catch: debug.exc(self) else: raise return False, "internal errors occurred"
def fetch(self, catch, path, synclists, oldlist, needport=True, add_once=False): # sync synclist (read it from file, add unexisting servers, remove servers no more in file (compare read items with oldlist)) and return a list of fetched items (which will be used as oldlist next time debug.msg(additionalservers.DBGTAG_UPDATING, "updating %d server list%s with servers from file %s" % (len(synclists), "s" if len(synclists) > 1 else "", path)) try: f = open(path, "r") newlist = [] addlist = [] n = 0 # line number errors = 0 for line_ in f: n += 1 if line_[-1] == "\n": line = line_[:-1] else: line = line_ if len(line) <= 0 or line.startswith(additionalservers.COMMENT_STR): continue p = line.split(" ") if len(p) < 2 and needport: debug.msg(additionalservers.DBGTAG_INVALID, "can't fetch server from line %d in file %s" % (n, path)) errors += 1 else: whost = p[0] if len(p) >= 2: try: port = int(p[1]) except: debug.msg(additionalservers.DBGTAG_INVALID, "can't fetch port for server at %s from line %d in file %s" % (whost, n, path)) errors += 1 continue else: port = "*" try: host = socket.gethostbyname(whost) except: debug.msg(additionalservers.DBGTAG_INVALID, "can't resolve host %s for server at %s:%s from line %d in file %s" % (whost, whost, str(port), n, path)) errors += 1 continue item = (host, port) newlist.append(item) addlist.append(item) f.close() dellist = [] for old in oldlist: if addlist.count(old): addlist.remove(old) # was added already else: dellist.append(old) # was removed from file for (synclist, listname) in synclists: for (host, port) in dellist: s = synclist.find(host, port) if s and s.fromfile == path: synclist.removeall(s) debug.msg(additionalservers.DBGTAG_REMOVED, "removed server at %s:%s from %s servers list" % (host, str(port), listname)) for (host, port) in addlist: if add_once: # don't add this anywhere if it already exists in any of the supplied synclists skip = False for (list_, name) in synclists: if list_.find(host, port): skip = True break if skip: debug.msg(additionalservers.DBGTAG_SKIP, "skipping server %s:%s from file %s because it already exists in the supplied synclists" % (host, str(port), path)) continue debug.msg(additionalservers.DBGTAG_ADD, "adding server at %s:%s to %s servers list" % (host, str(port), synclists[0][1])) synclists[0][0].add_from_file(host, (port if (type(port) == types.IntType) else None), file=path) debug.msg(additionalservers.DBGTAG_UPDATED, "updated %d server list%s with servers from file %s: %d items fetched from %d lines, %d errors, %d items added, %d items deleted" % (len(synclists), "s" if len(synclists) > 1 else "", path, len(newlist), n, errors, len(addlist), len(dellist))) return True, "%d items fetched from %d lines, %d errors (skipping lines with errors), %d items added, %d items deleted" % (len(newlist), n, errors, len(addlist), len(dellist)), newlist except: if catch: debug.exc(self) try: f.close() except: pass else: try: f.close() except: pass raise return False, "internal errors occurred", oldlist
def fetch(self, catch, path, synclists, oldlist, needport=True, add_once=False): # sync synclist (read it from file, add unexisting servers, remove servers no more in file (compare read items with oldlist)) and return a list of fetched items (which will be used as oldlist next time debug.msg( additionalservers.DBGTAG_UPDATING, "updating %d server list%s with servers from file %s" % (len(synclists), "s" if len(synclists) > 1 else "", path)) try: f = open(path, "r") newlist = [] addlist = [] n = 0 # line number errors = 0 for line_ in f: n += 1 if line_[-1] == "\n": line = line_[:-1] else: line = line_ if len(line) <= 0 or line.startswith( additionalservers.COMMENT_STR): continue p = line.split(" ") if len(p) < 2 and needport: debug.msg( additionalservers.DBGTAG_INVALID, "can't fetch server from line %d in file %s" % (n, path)) errors += 1 else: whost = p[0] if len(p) >= 2: try: port = int(p[1]) except: debug.msg( additionalservers.DBGTAG_INVALID, "can't fetch port for server at %s from line %d in file %s" % (whost, n, path)) errors += 1 continue else: port = "*" try: host = socket.gethostbyname(whost) except: debug.msg( additionalservers.DBGTAG_INVALID, "can't resolve host %s for server at %s:%s from line %d in file %s" % (whost, whost, str(port), n, path)) errors += 1 continue item = (host, port) newlist.append(item) addlist.append(item) f.close() dellist = [] for old in oldlist: if addlist.count(old): addlist.remove(old) # was added already else: dellist.append(old) # was removed from file for (synclist, listname) in synclists: for (host, port) in dellist: s = synclist.find(host, port) if s and s.fromfile == path: synclist.removeall(s) debug.msg( additionalservers.DBGTAG_REMOVED, "removed server at %s:%s from %s servers list" % (host, str(port), listname)) for (host, port) in addlist: if add_once: # don't add this anywhere if it already exists in any of the supplied synclists skip = False for (list_, name) in synclists: if list_.find(host, port): skip = True break if skip: debug.msg( additionalservers.DBGTAG_SKIP, "skipping server %s:%s from file %s because it already exists in the supplied synclists" % (host, str(port), path)) continue debug.msg( additionalservers.DBGTAG_ADD, "adding server at %s:%s to %s servers list" % (host, str(port), synclists[0][1])) synclists[0][0].add_from_file( host, (port if (type(port) == types.IntType) else None), file=path) debug.msg( additionalservers.DBGTAG_UPDATED, "updated %d server list%s with servers from file %s: %d items fetched from %d lines, %d errors, %d items added, %d items deleted" % (len(synclists), "s" if len(synclists) > 1 else "", path, len(newlist), n, errors, len(addlist), len(dellist))) return True, "%d items fetched from %d lines, %d errors (skipping lines with errors), %d items added, %d items deleted" % ( len(newlist), n, errors, len(addlist), len(dellist)), newlist except: if catch: debug.exc(self) try: f.close() except: pass else: try: f.close() except: pass raise return False, "internal errors occurred", oldlist
def processconnection(self, i, funcs): (sock, addr) = i (host, port) = addr debug.msg(netserver.DBGTAG, "connection from %s:%d" % (host, port)) sock.settimeout(2) closecon = False try: while True: lines, err = self.recv(sock) if err: debug.msg(netserver.DBGTAG_RECVFAIL, "an error occured while receiving request from %s:%d" % (host, port)) break for line in lines: if len(line) <= 0: debug.msg(netserver.DBGTAG_EMPTYLINE, "empty line received from %s:%d, skipping" % (host, port)) continue debug.msg(netserver.DBGTAG_TRAFFIC, "received from %s:%d: %s" % (host, port, line)) reply = None close = False cachedreply = self.cache.find(line) build = not bool(cachedreply) for func in funcs: r = func(line, addr, build) if not r: continue (creply, cclose) = r if build: (reply, close) = r if creply or cclose: break if cachedreply: (reply, close) = cachedreply closecon = closecon or close if reply: replytext, n, err = self.send(sock, reply, (debug.tagenabled(netserver.DBGTAG_REPLYTEXT) or ((not cachedreply) and replycache.enabled(line)))) if not err: debug.msg(netserver.DBGTAG_REPLY, "sent %sreply with length %d to %s:%d" % (("cached " if cachedreply else ""), n, host, port)) if replytext != None: debug.msg(netserver.DBGTAG_REPLYTEXT, "sent %sreply to %s:%d: (starts next line)\n%s\n --- end of reply ---" % (("cached " if cachedreply else ""), host, port, replytext)) if (not cachedreply) and replycache.enabled(line): self.cache.cache(line, replytext, close) else: debug.msg(netserver.DBGTAG_SENDFAIL, "an error occured while sending reply to %s:%d" % (host, port)) elif close: # no reply, but close - forceclose debug.msg(netserver.DBGTAG_FORCECLOSE, "force-closing connection to %s:%d without reply" % (host, port)) break else: debug.msg(netserver.DBGTAG_UNKNOWN, "no reply available for line received from %s:%d: %s" % (host, port, line)) if closecon: break except: debug.msg(netserver.DBGTAG, "an error occured while processing connection with %s:%d" % (host, port)) debug.exc() self.accesscache.end(host) shut = False try: sock.shutdown(socket.SHUT_RDWR) shut = True except: pass # remote has already shut down connection try: sock.close() except: pass debug.msg(netserver.DBGTAG, "connection %s:%d closed (shutdown %s)" % (host, port, "succeeded" if shut else "failed"))
def processconnection(self, i, funcs): (sock, addr) = i (host, port) = addr debug.msg(netserver.DBGTAG, "connection from %s:%d" % (host, port)) sock.settimeout(2) closecon = False try: while True: lines, err = self.recv(sock) if err: debug.msg( netserver.DBGTAG_RECVFAIL, "an error occured while receiving request from %s:%d" % (host, port)) break for line in lines: if len(line) <= 0: debug.msg( netserver.DBGTAG_EMPTYLINE, "empty line received from %s:%d, skipping" % (host, port)) continue debug.msg(netserver.DBGTAG_TRAFFIC, "received from %s:%d: %s" % (host, port, line)) reply = None close = False cachedreply = self.cache.find(line) build = not bool(cachedreply) for func in funcs: r = func(line, addr, build) if not r: continue (creply, cclose) = r if build: (reply, close) = r if creply or cclose: break if cachedreply: (reply, close) = cachedreply closecon = closecon or close if reply: replytext, n, err = self.send( sock, reply, (debug.tagenabled(netserver.DBGTAG_REPLYTEXT) or ((not cachedreply) and replycache.enabled(line)))) if not err: debug.msg( netserver.DBGTAG_REPLY, "sent %sreply with length %d to %s:%d" % (("cached " if cachedreply else ""), n, host, port)) if replytext != None: debug.msg( netserver.DBGTAG_REPLYTEXT, "sent %sreply to %s:%d: (starts next line)\n%s\n --- end of reply ---" % (("cached " if cachedreply else ""), host, port, replytext)) if (not cachedreply) and replycache.enabled(line): self.cache.cache(line, replytext, close) else: debug.msg( netserver.DBGTAG_SENDFAIL, "an error occured while sending reply to %s:%d" % (host, port)) elif close: # no reply, but close - forceclose debug.msg( netserver.DBGTAG_FORCECLOSE, "force-closing connection to %s:%d without reply" % (host, port)) break else: debug.msg( netserver.DBGTAG_UNKNOWN, "no reply available for line received from %s:%d: %s" % (host, port, line)) if closecon: break except: debug.msg( netserver.DBGTAG, "an error occured while processing connection with %s:%d" % (host, port)) debug.exc() self.accesscache.end(host) shut = False try: sock.shutdown(socket.SHUT_RDWR) shut = True except: pass # remote has already shut down connection try: sock.close() except: pass debug.msg( netserver.DBGTAG, "connection %s:%d closed (shutdown %s)" % (host, port, "succeeded" if shut else "failed"))