rest.py

Sat, 18 Jul 2015 16:59:44 +0300

author
Teemu Piippo <tsapii@utu.fi>
date
Sat, 18 Jul 2015 16:59:44 +0300
changeset 143
d86a81540a71
parent 124
7b2cd8b1ba86
child 146
c17b82b1f573
permissions
-rw-r--r--

Include timestamp in console outputs

'''
	Copyright 2014-2015 Teemu Piippo
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

	1. Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	3. The name of the author may not be used to endorse or promote products
	   derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''

import ssl
import socket
import errno
import select
import asyncore
import base64
import re
import json
import urllib
import irc as Irc
from datetime import datetime, timedelta
from configfile import Config
import hgpoll as HgPoll

g_credentials = None
g_portnumber = None
g_throttle = []

valid_repos = ['torr_samaho/zandronum', 'torr_samaho/zandronum-stable',
	'crimsondusk/zandronum-sandbox', 'crimsondusk/zandronum-sandbox-stable', 'crimsondusk/zfc9000', 'blzut3/doomseeker']

def is_throttled (address):
	i = 0

	while i < len (g_throttle):
		if g_throttle[i][1] <= datetime.utcnow():
			print 'Throttle of %s expired' % g_throttle[i][0]
			item = g_throttle.pop (i) # expired

			if item[0] == address:
				return False # this address expired

			continue

		if g_throttle[i][0] == address:
			return True # is throttled

		i += 1

	return False # not throttled

def throttle (address):
	tt = datetime.utcnow() + timedelta (0, 30)

	if address in g_throttle:
		g_throttle[g_throttle.index (address)][1] = tt
	else:
		g_throttle.append ([address, tt])

	Irc.broadcast ('Throttling %s for 30 seconds' % address)

def handle_rest_http (data, address):
	global g_credentials
	displayaddress = address
	authrex = re.compile (r'^authorization: Basic (.+)$')
	payloadrex = re.compile (r'^payload=(.+)$')
	authenticated = False
	payload = ''

	if not g_credentials:
		g_credentials = base64.b64encode (Config.get_node ('rest').get_value ('credentials'))

	# Authenticate and find the payload
	for line in data:
		match = authrex.match (line)

		if match and match.group (1) == g_credentials:
			authenticated = True
			continue

		match = payloadrex.match (line)
		if match:
			payload = match.group (1)

	if not authenticated:
		Irc.broadcast ('%s failed to authenticate' % address)
		throttle (address)
		return

	jsonstring = urllib.unquote_plus (payload).decode ('utf-8')

	try:
		jsondata = json.loads (jsonstring)
		repodata = jsondata['repository']
		repo_fullname = '%s/%s' % (repodata['owner'], repodata['name'])

		if repo_fullname.lower() not in valid_repos:
			raise ValueError ('unknown repository %s' % repo_fullname)

		commit_data = []

		if 'commits' in jsondata:
			for commit in jsondata['commits']:
				commit_data.append ([commit['node'], commit['message']])

			if len (commit_data) > 0:
				HgPoll.process_new_commits (repodata['name'], commit_data)
	except Exception as e:
		Irc.broadcast ('%s provided bad JSON: %s' % (address, e))

		try:
			with open ('rejected_json.txt', 'w') as fp:
				fp.write (str (e))
				fp.write (jsonstring)
			Irc.broadcast ('bad json written to rejected_json.txt')
		except Exception as e:
			Irc.broadcast ('failed to log json because %s' % e)
			pass

		throttle (address)
		return

class RESTConnection (asyncore.dispatcher):
	httpdata = ''
	writebuffer = ''
	address = None

	def __init__ (self, conn, address):
		asyncore.dispatcher.__init__ (self, conn)
		self.socket = ssl.wrap_socket (conn, server_side=True, keyfile='key.pem',
			certfile='cert.pem', do_handshake_on_connect=False)
		self.socket.settimeout (3)
		self.address = address

		while True:
			try:
				self.socket.do_handshake()
				break
			except ssl.SSLError, err:
				if err.args[0] == ssl.SSL_ERROR_WANT_READ:
					select.select([self.socket], [], [])
				elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:
					select.select([], [self.socket], [])
				else:
					Irc.broadcast ('%s:%s: SSL error: %s' % (self.address[0], err))
					throttle (self.address[0])
					self.close()
					return

	def write (self, msg):
		self.writebuffer += msg

	def readable (self):
		return True

	def writable (self):
		return self.writebuffer

	def handle_close (self):
		self.finish()

	def handle_read (self):
		while 1:
			try:
				data = self.recv (4096)
			except:
				# EOF
				self.finish()
				return

			self.httpdata += data.replace ('\r', '')

	def finish (self):
		handle_rest_http (self.httpdata.split ('\n'), self.address[0])
		self.close()

	def handle_write (self):
		self.send (self.writebuffer)
		self.writebuffer=''

	def handle_error (self):
		raise

class RESTServer (asyncore.dispatcher):
	def __init__ (self):
		global g_portnumber

		if g_portnumber == None:
			g_portnumber = Config.get_node ('rest').get_value ('port')

		asyncore.dispatcher.__init__ (self)
		self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
		self.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
		self.bind (('', g_portnumber))
		self.listen (5)
		print 'REST server initialized'

	def handle_accept (self):
		sock, address = self.accept()

		if is_throttled (address):
			# throttled
			sock.close()
			return

		conn = RESTConnection (sock, address)

	def handle_error (self):
		return

mercurial