X-Git-Url: https://git.frykholm.com/friends.git/blobdiff_plain/ca027dea1d5dcb07c4ee3b248da64da8d761ccb4..a0cc65b15945a2cc4db03ed9a0ed1b618b76c4d5:/friends/server.py?ds=inline diff --git a/friends/server.py b/friends/server.py old mode 100644 new mode 100755 index 853e78c..cf31ed8 --- a/friends/server.py +++ b/friends/server.py @@ -1,29 +1,34 @@ +#!/usr/bin/python3 import tornado.ioloop import tornado.web -import os, os.path +import os +import os.path import tornado.httpserver import tornado.httpclient as httpclient import sqlite3 import arrow import datetime from rd import RD, Link -import hashlib import hmac +from tornado.options import options, define +import logging db = None -#insert into user (name,email) values('mikael','mikael@frykholm.com'); -#insert into entry (userid,text) values (1,'My thoughts on ostatus'); -import tornado.options +# insert into user (name,email) values('mikael','mikael@frykholm.com'); +# insert into entry (userid,text) values (1,'My thoughts on ostatus'); + settings = { "static_path": os.path.join(os.path.dirname(__file__), "static"), - "cookie_secret": "__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__", + "cookie_secret": "supersecret123", "login_url": "/login", "xsrf_cookies": False, "domain":"https://ronin.frykholm.com", } -class PushHandler(tornado.web.RequestHandler): + #curl -v -k "https://ronin.frykholm.com/hub" -d "hub.callback=a" -d "hub.mode=b" -d "hub.topic=c" -d "hub.verify=d" + +class PushHandler(tornado.web.RequestHandler): def post(self): """ Someone wants to subscribe to hub_topic feed""" hub_callback = self.get_argument('hub.callback') @@ -93,7 +98,7 @@ class FingerHandler(tornado.web.RequestHandler): class UserHandler(tornado.web.RequestHandler): def get(self, user): entries = db.execute("select entry.id,text,ts from user,entry where user.id=entry.userid and user.name=?",(user,)) - #import pdb;pdb.set_trace() + # import pdb;pdb.set_trace() self.set_header("Content-Type", 'application/atom+xml') out = self.render("templates/feed.xml", user=user, @@ -103,6 +108,25 @@ class UserHandler(tornado.web.RequestHandler): arrow=arrow ) #digest = hmac.new() + def post(self, user): + entries = db.execute("select entry.id,text,ts from user,entry where user.id=entry.userid and user.name=?",(user,)) + + self.set_header("Content-Type", 'application/atom+xml') + out = self.render_string("templates/feed.xml", + user=user, + feed_url="{}/user/{}".format(self.settings['domain'], user), + hub_url="{}/hub".format(self.settings['domain']), + entries=entries, + arrow=arrow) + #import pdb;pdb.set_trace() + subscribers = db.execute("select callback, secret from subscriptions, user where user.id=subscriptions.userid and user.name=?",(user,)) + for url,secret in subscribers: + digest = hmac.new(secret.encode('utf8'), out, digestmod='sha1').hexdigest() + + req = httpclient.HTTPRequest(url=url, allow_nonstandard_methods=True,method='POST', body=out, headers={"X-Hub-Signature":"sha1={}".format(digest),"Content-Type": 'application/atom+xml',"Content-Length":len(out)}) + apa = httpclient.HTTPClient() + apa.fetch(req) + application = tornado.web.Application([ (r"/.well-known/host-meta", XrdHandler), (r"/.well-known/webfinger", FingerHandler), @@ -110,8 +134,10 @@ application = tornado.web.Application([ (r"/hub", PushHandler), ],debug=True,**settings) srv = tornado.httpserver.HTTPServer(application, ) + def setup_db(path): - print("No db found, creating in {}".format(path)) + gen_log = logging.getLogger("tornado.general") + gen_log.warn("No db found, creating in {}".format(path)) con = sqlite3.connect(path) con.execute(""" create table user (id integer primary key, name varchar, @@ -130,14 +156,21 @@ def setup_db(path): FOREIGN KEY(userid) REFERENCES user(id));""") con.commit() + +options.define("config_file", default="/etc/friends/friends.conf", type=str) +options.define("webroot", default="/srv/friends/", type=str) + if __name__ == "__main__": dbPath = 'friends.db' +# options.log_file_prefix="/tmp/friends" + tornado.options.parse_config_file(options.config_file) tornado.options.parse_command_line() + gen_log = logging.getLogger("tornado.general") + gen_log.info("Reading config from: %s", options.config_file,) if not os.path.exists(dbPath): setup_db(dbPath) db = sqlite3.connect(dbPath, detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES) db.row_factory = sqlite3.Row - srv.listen(8080) + srv.listen(80) tornado.ioloop.IOLoop.instance().start() - #TODO hmac.new(b'5cc324285ece71e21e9554f4056563806f6ce0b7e4ab18d0133b602f8ba7e87a',open("apa.xml","rb").read(),digestmod='sha1').hexdigest() -'b75d4733e0802629a0c15d0faa8de8fc9778cd05' queue runner +