def recv_json(self, data): self.emit("test", data) self.emit_to_room("test", data) if self.user: log("Received JSON from %s: %s" % (self.user.username, str(data))) else: log("Received JSON: %s" % str(data))
def get(url, user_agent, user=None): """ Return a Revision of a url. Check for the presence of a URL At its original location on the web. In the database. In the DHT. """ revision = Revision() # Ignore for now if the addr is on our LAN, VLAN or localhost. url = urlparse.urlparse(url) domain = url.netloc path = url.path or '/' try: host = socket.gethostbyname(domain) except: host = '' # Making deep assumptions about the future of ipv4 here.. log(host) if any([host.startswith(subnet) for subnet in local_subnets]): revision.status = 403 revision.mimetype = "text" revision.data = "We're not currently proxying to local subnets." return revision # Check the web response = None try: response = requests.get(url.geturl(), headers={'User-Agent': user_agent} ) except Exception, e: log("Error retrieving %s: %s" % (url.geturl(), e.message))
def initialize(self): self.buffer = [] self.inbox = None self.user = None # Make the channels a set self.channel = None log("init global activity stream")
def index(self): user = auth(session, required=True) current_session = session['session'] [db.session.delete(s) for s in user.sessions if s.session_id == current_session] session['session'] = '' log("%s logged out." % user.username) return redirect('/')
def put(self, username): "Add a session for a user and return the session cookie" parser = reqparse.RequestParser() parser.add_argument("password", type=str, help="password.", required=True) args = parser.parse_args() user = User.query.filter(User.username == username).first() if not user: return{}, 404 if not user.verify_password(args.password): return {}, 401 s = Session() s.from_request(request) user.sessions.append(s) db.session.add(user) db.session.add(s) db.session.commit() session['session'] = s.session_id log("%s logged in." % user.username) response = user.jsonify() response['session'] = s.jsonify() return response
def recv_connect(self): user = auth(self.request) if not user: self.request_reconnect() # self.user = AnonUser() else: log("Received chat connection from %s" % user.username) self.user = user
def on_join(self, channel): # Keep everyone on the same stream generally. channel = "public" if self.user: # if can(self.user.username, "rx_stream"): log('%s joined activity stream "%s"' % (self.user.username, channel)) self.channel = channel self.join(channel)
def on_msg(self, msg): if self.user and self.channel: if self.user.created: body = {"u":self.user.username,"m":escape(msg)} else: body = {"u":self.user.username,"m":escape(msg),"a": True} log("Message to %s from %s: %s" % (self.channel, self.user.username, msg)) self.emit_to_room(self.channel, "privmsg", body) self.emit("privmsg", body)
def recv_connect(self): user = auth(self.request) if user: log("Received activity stream connection from %s" % user.username) self.user = user # if not can(user.username, "chat"): # body = {"message":"Your user group doesn't have permission to chat"} # self.emit("disconnect", body) # self.send("disconnect") # log("%s isn't permitted to chat." % user.username) # else: self.user = AnonUser()
def initialize(self): self.user = None # Access to app.routes for # admin users to access via /eval self.routes = app.routes # TODO: Make the channels a set self.channel = None # This lets us cycle through stream connections on # the httpd and easily determine the session type. self.socket.socket_type = "chat" self.socket.appearing_offline = False log("init chat stream")
def delete(self, username): "Delete a session for a user" user = auth(session, required=True) parser = reqparse.RequestParser() parser.add_argument("timestamp", type=int, help="session timestamp", required=True) args = parser.parse_args() for s in user.sessions: if time.mktime(s.created.timetuple()) == args.timestamp: db.session.delete(s) db.session.commit() log("%s logged out." % user.username) return {}, 204
def post(self): """ Unserialise the data and accept the following calls: PING CHAT APPEND LEAVING FIND_NODE FIND_VALUE APPEND signifies the requesting host has data for the given hash. """ parser = restful.reqparse.RequestParser() parser.add_argument("data", type=str, help="The RPC body.", default=None) args = parser.parse_args() if not args.data: return {}, 400 response = [] data = json.loads(args.data) if not validate_signature(data): log("Received message from %s with an invalid signature." % request.remote_addr) return "Invalid message signature.", 400 if not 'node' in data: log("%s didn't provide useable information about themselves." % request.remote_addr, "warning") return {}, 400 # Validate the node field for internet hosts if not any([request.remote_addr.startswith(subnet) for subnet in local_subnets]): stated_addr = data['node'].split()[1] if stated_addr != request.remote_addr: log("Request made from %s stated it originated from %s" % (request.remote_addr, stated_addr), "warning") return "sicillian shrug", 418 # Execute the corresponding RPC handler for field in data.keys(): if field.startswith('rpc_'): rpc_name = 'handle_%s' % field.replace('rpc_', '') # data[rpc_name] = data[field] # del data[field] rpc_method = getattr(app.routes.protocol, rpc_name, None) if not rpc_method: log("%s tried to call unknown procedure %s." % (request.remote_addr, rpc_name), "warning") return {}, 400 response = rpc_method(data) break return response
def parse(html, url): "" domain = urlparse.urlparse(url).netloc append_text = '<script src="/static/uroko.js"></script>\n<link rel="stylesheet" type="text/css" href="/static/uroko.css" />' appendage = BeautifulSoup(append_text) soup = BeautifulSoup(html) # log('Requested %s (%s)' % (url, domain)) request_endpoint = "/request/" for a in soup.findAll('a'): try: log(str(a['href'])) if a['href'].startswith('https'): a['href'] = a['href'].replace("https://", request_endpoint) elif a['href'].startswith('http'): a['href'] = a['href'].replace("http://", request_endpoint) elif a['href'].startswith('/'): a['href'] = '%s%s%s' % (request_endpoint, domain, link['href']) else: a['href'] = '%s%s/%s' % (request_endpoint,domain, a['href']) except: continue for link in soup.findAll('link'): try: log(str(link['href'])) if link['href'].startswith('https'): link['href'] = link['href'].replace("https://", request_endpoint) elif link['href'].startswith('http'): link['href'] = link['href'].replace("http://", request_endpoint) elif link['href'].startswith('/'): link['href'] = '%s%s%s' % (request_endpoint, domain, link['href']) else: link['href'] = '%s%s/%s' % (request_endpoint,domain, link['href']) except: continue log('Should have cycled through urls by now.') try: soup.head.append(appendage) log('Appended:\n%s' % append_text) except: pass return unicode(soup)
def recv_message(self, data): log(data) self.emit("test", data) self.socket.send_packet({"test":"hello"})
def request_reconnect(self): """ Shortcut for asking a client to reconnect. """ log("Received chat connection before authentication. Requesting client reconnects.") self.emit("reconnect", {"m":"Reconnecting.."})
if pid > 0: sys.exit(0) # parent except OSError, e: sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror)) sys.exit(-2) os.setsid() os.umask(0) try: pid = os.fork() if pid > 0: try: f = file(pidfile, 'w') f.write(str(pid)) f.close() except IOError, err: log(err,'error') sys.exit(0) # parent except OSError, e: sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror)) sys.exit(-2) for fd in (0, 1, 2): try: os.close(fd) except OSError: pass if __name__ == "__main__": epilog = "Available test suites: %s" % ','.join([test for test in maps.keys()]) parser = optparse.OptionParser(epilog=epilog) parser.add_option("-p", "--port", dest="port", action="store", default=8080, help="(defaults to 8080)") parser.add_option("--key", dest="key", action="store", default="id_rsa", help="(defaults to id_rsa)") parser.add_option("-c", "--config", dest="config", action="store", default='uroko.config')
def on_join(self, channel): if self.user and self.user.username: # if can(self.user.username, "chat"): log("%s joined %s" % (self.user.username, channel)) self.channel = channel self.join(channel)
response = None try: response = requests.get(url.geturl(), headers={'User-Agent': user_agent} ) except Exception, e: log("Error retrieving %s: %s" % (url.geturl(), e.message)) if response: revision.add(response) if 'content-type' in response.headers: revision.mimetype = response.headers['content-type'] revision.save(user, domain, path) return revision # Check the database domain_from_db = Domain.query.filter_by(name=domain).first() rev = Revision.query.filter( and_(Revision.original == True, Revision.resource.has(domain=domain_from_db), Revision.resource.has(path=path)) ).order_by(desc(Revision.created)).first() log(domain_from_db) if rev: return rev # Last but not least, check the overlay network if user and user.public: revision = app.routes.protocol.rpc_find_value(url) return revision