Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

635 righe
26 KiB

  1. import aiohttp
  2. import aiohttp.web
  3. import asyncio
  4. import base64
  5. import collections
  6. import concurrent.futures
  7. import importlib.util
  8. import inspect
  9. import logging
  10. import os.path
  11. import signal
  12. import ssl
  13. import string
  14. import sys
  15. import toml
  16. logger = logging.getLogger('http2irc')
  17. SSL_CONTEXTS = {'yes': True, 'no': False, 'insecure': ssl.SSLContext()}
  18. class InvalidConfig(Exception):
  19. '''Error in configuration file'''
  20. def is_valid_pem(path, withCert):
  21. '''Very basic check whether something looks like a valid PEM certificate'''
  22. try:
  23. with open(path, 'rb') as fp:
  24. contents = fp.read()
  25. # All of these raise exceptions if something's wrong...
  26. if withCert:
  27. assert contents.startswith(b'-----BEGIN CERTIFICATE-----\n')
  28. endCertPos = contents.index(b'-----END CERTIFICATE-----\n')
  29. base64.b64decode(contents[28:endCertPos].replace(b'\n', b''), validate = True)
  30. assert contents[endCertPos + 26:].startswith(b'-----BEGIN PRIVATE KEY-----\n')
  31. else:
  32. assert contents.startswith(b'-----BEGIN PRIVATE KEY-----\n')
  33. endCertPos = -26 # Please shoot me.
  34. endKeyPos = contents.index(b'-----END PRIVATE KEY-----\n')
  35. base64.b64decode(contents[endCertPos + 26 + 28: endKeyPos].replace(b'\n', b''), validate = True)
  36. assert contents[endKeyPos + 26:] == b''
  37. return True
  38. except: # Yes, really
  39. return False
  40. class Config(dict):
  41. def __init__(self, filename):
  42. super().__init__()
  43. self._filename = filename
  44. with open(self._filename, 'r') as fp:
  45. obj = toml.load(fp)
  46. # Sanity checks
  47. if any(x not in ('logging', 'irc', 'web', 'maps') for x in obj.keys()):
  48. raise InvalidConfig('Unknown sections found in base object')
  49. if any(not isinstance(x, collections.abc.Mapping) for x in obj.values()):
  50. raise InvalidConfig('Invalid section type(s), expected objects/dicts')
  51. if 'logging' in obj:
  52. if any(x not in ('level', 'format') for x in obj['logging']):
  53. raise InvalidConfig('Unknown key found in log section')
  54. if 'level' in obj['logging'] and obj['logging']['level'] not in ('DEBUG', 'INFO', 'WARNING', 'ERROR'):
  55. raise InvalidConfig('Invalid log level')
  56. if 'format' in obj['logging']:
  57. if not isinstance(obj['logging']['format'], str):
  58. raise InvalidConfig('Invalid log format')
  59. try:
  60. #TODO: Replace with logging.Formatter's validate option (3.8+); this test does not cover everything that could be wrong (e.g. invalid format spec or conversion)
  61. # This counts the number of replacement fields. Formatter.parse yields tuples whose second value is the field name; if it's None, there is no field (e.g. literal text).
  62. assert sum(1 for x in string.Formatter().parse(obj['logging']['format']) if x[1] is not None) > 0
  63. except (ValueError, AssertionError) as e:
  64. raise InvalidConfig('Invalid log format: parsing failed') from e
  65. if 'irc' in obj:
  66. if any(x not in ('host', 'port', 'ssl', 'nick', 'real', 'certfile', 'certkeyfile') for x in obj['irc']):
  67. raise InvalidConfig('Unknown key found in irc section')
  68. if 'host' in obj['irc'] and not isinstance(obj['irc']['host'], str): #TODO: Check whether it's a valid hostname
  69. raise InvalidConfig('Invalid IRC host')
  70. if 'port' in obj['irc'] and (not isinstance(obj['irc']['port'], int) or not 1 <= obj['irc']['port'] <= 65535):
  71. raise InvalidConfig('Invalid IRC port')
  72. if 'ssl' in obj['irc'] and obj['irc']['ssl'] not in ('yes', 'no', 'insecure'):
  73. raise InvalidConfig(f'Invalid IRC SSL setting: {obj["irc"]["ssl"]!r}')
  74. if 'nick' in obj['irc'] and not isinstance(obj['irc']['nick'], str): #TODO: Check whether it's a valid nickname
  75. raise InvalidConfig('Invalid IRC nick')
  76. if 'real' in obj['irc'] and not isinstance(obj['irc']['real'], str):
  77. raise InvalidConfig('Invalid IRC realname')
  78. if ('certfile' in obj['irc']) != ('certkeyfile' in obj['irc']):
  79. raise InvalidConfig('Invalid IRC cert config: needs both certfile and certkeyfile')
  80. if 'certfile' in obj['irc']:
  81. if not isinstance(obj['irc']['certfile'], str):
  82. raise InvalidConfig('Invalid certificate file: not a string')
  83. if not os.path.isfile(obj['irc']['certfile']):
  84. raise InvalidConfig('Invalid certificate file: not a regular file')
  85. if not is_valid_pem(obj['irc']['certfile'], True):
  86. raise InvalidConfig('Invalid certificate file: not a valid PEM cert')
  87. if 'certkeyfile' in obj['irc']:
  88. if not isinstance(obj['irc']['certkeyfile'], str):
  89. raise InvalidConfig('Invalid certificate key file: not a string')
  90. if not os.path.isfile(obj['irc']['certkeyfile']):
  91. raise InvalidConfig('Invalid certificate key file: not a regular file')
  92. if not is_valid_pem(obj['irc']['certkeyfile'], False):
  93. raise InvalidConfig('Invalid certificate key file: not a valid PEM key')
  94. if 'web' in obj:
  95. if any(x not in ('host', 'port') for x in obj['web']):
  96. raise InvalidConfig('Unknown key found in web section')
  97. if 'host' in obj['web'] and not isinstance(obj['web']['host'], str): #TODO: Check whether it's a valid hostname (must resolve I guess?)
  98. raise InvalidConfig('Invalid web hostname')
  99. if 'port' in obj['web'] and (not isinstance(obj['web']['port'], int) or not 1 <= obj['web']['port'] <= 65535):
  100. raise InvalidConfig('Invalid web port')
  101. if 'maps' in obj:
  102. seenWebPaths = {}
  103. for key, map_ in obj['maps'].items():
  104. if not isinstance(key, str) or not key:
  105. raise InvalidConfig(f'Invalid map key {key!r}')
  106. if not isinstance(map_, collections.abc.Mapping):
  107. raise InvalidConfig(f'Invalid map for {key!r}')
  108. if any(x not in ('webpath', 'ircchannel', 'auth', 'module', 'moduleargs') for x in map_):
  109. raise InvalidConfig(f'Unknown key(s) found in map {key!r}')
  110. if 'webpath' not in map_:
  111. map_['webpath'] = f'/{key}'
  112. if not isinstance(map_['webpath'], str):
  113. raise InvalidConfig(f'Invalid map {key!r} web path: not a string')
  114. if not map_['webpath'].startswith('/'):
  115. raise InvalidConfig(f'Invalid map {key!r} web path: does not start at the root')
  116. if map_['webpath'] in seenWebPaths:
  117. raise InvalidConfig(f'Invalid map {key!r} web path: collides with map {seenWebPaths[map_["webpath"]]!r}')
  118. seenWebPaths[map_['webpath']] = key
  119. if 'ircchannel' not in map_:
  120. map_['ircchannel'] = f'#{key}'
  121. if not isinstance(map_['ircchannel'], str):
  122. raise InvalidConfig(f'Invalid map {key!r} IRC channel: not a string')
  123. if not map_['ircchannel'].startswith('#') and not map_['ircchannel'].startswith('&'):
  124. raise InvalidConfig(f'Invalid map {key!r} IRC channel: does not start with # or &')
  125. if any(x in map_['ircchannel'][1:] for x in (' ', '\x00', '\x07', '\r', '\n', ',')):
  126. raise InvalidConfig(f'Invalid map {key!r} IRC channel: contains forbidden characters')
  127. if 'auth' in map_:
  128. if map_['auth'] is not False and not isinstance(map_['auth'], str):
  129. raise InvalidConfig(f'Invalid map {key!r} auth: must be false or a string')
  130. if isinstance(map_['auth'], str) and ':' not in map_['auth']:
  131. raise InvalidConfig(f'Invalid map {key!r} auth: must contain a colon')
  132. if 'module' in map_ and not os.path.isfile(map_['module']):
  133. raise InvalidConfig(f'Module {map_["module"]!r} in map {key!r} is not a file')
  134. if 'moduleargs' in map_:
  135. if not isinstance(map_['moduleargs'], list):
  136. raise InvalidConfig(f'Invalid module args for {key!r}: not an array')
  137. if 'module' not in map_:
  138. raise InvalidConfig(f'Module args cannot be specified without a module for {key!r}')
  139. # Default values
  140. finalObj = {'logging': {'level': 'INFO', 'format': '{asctime} {levelname} {name} {message}'}, 'irc': {'host': 'irc.hackint.org', 'port': 6697, 'ssl': 'yes', 'nick': 'h2ibot', 'real': 'I am an http2irc bot.', 'certfile': None, 'certkeyfile': None}, 'web': {'host': '127.0.0.1', 'port': 8080}, 'maps': {}}
  141. # Fill in default values for the maps
  142. for key, map_ in obj['maps'].items():
  143. # webpath is already set above for duplicate checking
  144. # ircchannel is set above for validation
  145. if 'auth' not in map_:
  146. map_['auth'] = False
  147. if 'module' not in map_:
  148. map_['module'] = None
  149. if 'moduleargs' not in map_:
  150. map_['moduleargs'] = []
  151. # Load modules
  152. modulePaths = {} # path: str -> (extraargs: int, key: str)
  153. for key, map_ in obj['maps'].items():
  154. if map_['module'] is not None:
  155. if map_['module'] not in modulePaths:
  156. modulePaths[map_['module']] = (len(map_['moduleargs']), key)
  157. elif modulePaths[map_['module']][0] != len(map_['moduleargs']):
  158. raise InvalidConfig(f'Module {map_["module"]!r} process function extra argument inconsistency between {key!r} and {modulePaths[map_["module"]][1]!r}')
  159. modules = {} # path: str -> module: module
  160. for i, (path, (extraargs, _)) in enumerate(modulePaths.items()):
  161. try:
  162. # Build a name that is virtually guaranteed to be unique across a process.
  163. # Although importlib does not seem to perform any caching as of CPython 3.8, this is not guaranteed by spec.
  164. spec = importlib.util.spec_from_file_location(f'http2irc-module-{id(self)}-{i}', path)
  165. module = importlib.util.module_from_spec(spec)
  166. spec.loader.exec_module(module)
  167. except Exception as e: # This is ugly, but exec_module can raise virtually any exception
  168. raise InvalidConfig(f'Loading module {path!r} failed: {e!s}')
  169. if not hasattr(module, 'process'):
  170. raise InvalidConfig(f'Module {path!r} does not have a process function')
  171. if not inspect.iscoroutinefunction(module.process):
  172. raise InvalidConfig(f'Module {path!r} process attribute is not a coroutine function')
  173. nargs = len(inspect.signature(module.process).parameters)
  174. if nargs != 1 + extraargs:
  175. raise InvalidConfig(f'Module {path!r} process function takes {nargs} parameter{"s" if nargs > 1 else ""}, not {1 + extraargs}')
  176. modules[path] = module
  177. # Replace module value in maps
  178. for map_ in obj['maps'].values():
  179. if 'module' in map_ and map_['module'] is not None:
  180. map_['module'] = modules[map_['module']]
  181. # Merge in what was read from the config file and set keys on self
  182. for key in ('logging', 'irc', 'web', 'maps'):
  183. if key in obj:
  184. finalObj[key].update(obj[key])
  185. self[key] = finalObj[key]
  186. def __repr__(self):
  187. return f'<Config(logging={self["logging"]!r}, irc={self["irc"]!r}, web={self["web"]!r}, maps={self["maps"]!r})>'
  188. def reread(self):
  189. return Config(self._filename)
  190. class MessageQueue:
  191. # An object holding onto the messages received from nodeping
  192. # This is effectively a reimplementation of parts of asyncio.Queue with some specific additional code.
  193. # Unfortunately, asyncio.Queue's extensibility (_init, _put, and _get methods) is undocumented, so I don't want to rely on that.
  194. # Differences to asyncio.Queue include:
  195. # - No maxsize
  196. # - No put coroutine (not necessary since the queue can never be full)
  197. # - Only one concurrent getter
  198. # - putleft_nowait to put to the front of the queue (so that the IRC client can put a message back when delivery fails)
  199. logger = logging.getLogger('http2irc.MessageQueue')
  200. def __init__(self):
  201. self._getter = None # None | asyncio.Future
  202. self._queue = collections.deque()
  203. async def get(self):
  204. if self._getter is not None:
  205. raise RuntimeError('Cannot get concurrently')
  206. if len(self._queue) == 0:
  207. self._getter = asyncio.get_running_loop().create_future()
  208. self.logger.debug('Awaiting getter')
  209. try:
  210. await self._getter
  211. except asyncio.CancelledError:
  212. self.logger.debug('Cancelled getter')
  213. self._getter = None
  214. raise
  215. self.logger.debug('Awaited getter')
  216. self._getter = None
  217. # For testing the cancellation/putting back onto the queue
  218. #self.logger.debug('Delaying message queue get')
  219. #await asyncio.sleep(3)
  220. #self.logger.debug('Done delaying')
  221. return self.get_nowait()
  222. def get_nowait(self):
  223. if len(self._queue) == 0:
  224. raise asyncio.QueueEmpty
  225. return self._queue.popleft()
  226. def put_nowait(self, item):
  227. self._queue.append(item)
  228. if self._getter is not None and not self._getter.cancelled():
  229. self._getter.set_result(None)
  230. def putleft_nowait(self, *item):
  231. self._queue.extendleft(reversed(item))
  232. if self._getter is not None and not self._getter.cancelled():
  233. self._getter.set_result(None)
  234. def qsize(self):
  235. return len(self._queue)
  236. class IRCClientProtocol(asyncio.Protocol):
  237. logger = logging.getLogger('http2irc.IRCClientProtocol')
  238. def __init__(self, messageQueue, connectionClosedEvent, loop, config, channels):
  239. self.messageQueue = messageQueue
  240. self.connectionClosedEvent = connectionClosedEvent
  241. self.loop = loop
  242. self.config = config
  243. self.buffer = b''
  244. self.connected = False
  245. self.channels = channels # Currently joined/supposed-to-be-joined channels; set(str)
  246. self.unconfirmedMessages = []
  247. self.pongReceivedEvent = asyncio.Event()
  248. self.sasl = bool(self.config['irc']['certfile'] and self.config['irc']['certkeyfile'])
  249. self.authenticated = False
  250. def connection_made(self, transport):
  251. self.logger.info('IRC connected')
  252. self.transport = transport
  253. self.connected = True
  254. nickb = self.config['irc']['nick'].encode('utf-8')
  255. if self.sasl:
  256. self.send(b'CAP REQ :sasl')
  257. self.send(b'NICK ' + nickb)
  258. self.send(b'USER ' + nickb + b' ' + nickb + b' ' + nickb + b' :' + self.config['irc']['real'].encode('utf-8'))
  259. def update_channels(self, channels: set):
  260. channelsToPart = self.channels - channels
  261. channelsToJoin = channels - self.channels
  262. self.channels = channels
  263. if self.connected:
  264. if channelsToPart:
  265. #TODO: Split if too long
  266. self.send(b'PART ' + ','.join(channelsToPart).encode('utf-8'))
  267. if channelsToJoin:
  268. self.send(b'JOIN ' + ','.join(channelsToJoin).encode('utf-8'))
  269. def send(self, data):
  270. self.logger.debug(f'Send: {data!r}')
  271. self.transport.write(data + b'\r\n')
  272. async def _get_message(self):
  273. self.logger.debug(f'Message queue {id(self.messageQueue)} length: {self.messageQueue.qsize()}')
  274. messageFuture = asyncio.create_task(self.messageQueue.get())
  275. done, pending = await asyncio.wait((messageFuture, self.connectionClosedEvent.wait()), return_when = concurrent.futures.FIRST_COMPLETED)
  276. if self.connectionClosedEvent.is_set():
  277. if messageFuture in pending:
  278. self.logger.debug('Cancelling messageFuture')
  279. messageFuture.cancel()
  280. try:
  281. await messageFuture
  282. except asyncio.CancelledError:
  283. self.logger.debug('Cancelled messageFuture')
  284. pass
  285. else:
  286. # messageFuture is already done but we're stopping, so put the result back onto the queue
  287. self.messageQueue.putleft_nowait(messageFuture.result())
  288. return None, None
  289. assert messageFuture in done, 'Invalid state: messageFuture not in done futures'
  290. return messageFuture.result()
  291. async def send_messages(self):
  292. while self.connected:
  293. self.logger.debug(f'Trying to get a message')
  294. channel, message = await self._get_message()
  295. self.logger.debug(f'Got message: {message!r}')
  296. if message is None:
  297. break
  298. self.logger.info(f'Sending {message!r} to {channel!r}')
  299. #TODO Split if the message is too long.
  300. self.unconfirmedMessages.append((channel, message))
  301. self.send(b'PRIVMSG ' + channel.encode('utf-8') + b' :' + message.encode('utf-8'))
  302. await asyncio.sleep(1) # Rate limit
  303. async def confirm_messages(self):
  304. while self.connected:
  305. await asyncio.wait((asyncio.sleep(60), self.connectionClosedEvent.wait()), return_when = concurrent.futures.FIRST_COMPLETED) # Confirm once per minute
  306. if not self.connected: # Disconnected while sleeping, can't confirm unconfirmed messages, requeue them directly
  307. self.messageQueue.putleft_nowait(*self.unconfirmedMessages)
  308. self.unconfirmedMessages = []
  309. break
  310. if not self.unconfirmedMessages:
  311. self.logger.debug('No messages to confirm')
  312. continue
  313. self.logger.debug('Trying to confirm message delivery')
  314. self.pongReceivedEvent.clear()
  315. self.send(b'PING :42')
  316. await asyncio.wait((asyncio.sleep(5), self.pongReceivedEvent.wait()), return_when = concurrent.futures.FIRST_COMPLETED)
  317. self.logger.debug(f'Message delivery successful: {self.pongReceivedEvent.is_set()}')
  318. if not self.pongReceivedEvent.is_set():
  319. # No PONG received in five seconds, assume connection's dead
  320. self.logger.warning(f'Message delivery confirmation failed, putting {len(self.unconfirmedMessages)} messages back into the queue')
  321. self.messageQueue.putleft_nowait(*self.unconfirmedMessages)
  322. self.transport.close()
  323. self.unconfirmedMessages = []
  324. def data_received(self, data):
  325. self.logger.debug(f'Data received: {data!r}')
  326. # Split received data on CRLF. If there's any data left in the buffer, prepend it to the first message and process that.
  327. # Then, process all messages except the last one (since data might not end on a CRLF) and keep the remainder in the buffer.
  328. # If data does end with CRLF, all messages will have been processed and the buffer will be empty again.
  329. messages = data.split(b'\r\n')
  330. if self.buffer:
  331. self.message_received(self.buffer + messages[0])
  332. messages = messages[1:]
  333. for message in messages[:-1]:
  334. self.message_received(message)
  335. self.buffer = messages[-1]
  336. def message_received(self, message):
  337. self.logger.debug(f'Message received: {message!r}')
  338. if message.startswith(b':'):
  339. # Prefixed message, extract command + parameters (the prefix cannot contain a space)
  340. message = message.split(b' ', 1)[1]
  341. # PING/PONG
  342. if message.startswith(b'PING '):
  343. self.send(b'PONG ' + message[5:])
  344. elif message.startswith(b'PONG '):
  345. self.pongReceivedEvent.set()
  346. # SASL
  347. elif message.startswith(b'CAP ') and self.sasl:
  348. if message[message.find(b' ', 4) + 1:] == b'ACK :sasl':
  349. self.send(b'AUTHENTICATE EXTERNAL')
  350. else:
  351. self.logger.error(f'Received unexpected CAP reply {message!r}, terminating connection')
  352. self.transport.close()
  353. elif message == b'AUTHENTICATE +':
  354. self.send(b'AUTHENTICATE +')
  355. elif message.startswith(b'903 '): # SASL auth successful
  356. self.authenticated = True
  357. self.send(b'CAP END')
  358. elif any(message.startswith(x) for x in (b'902 ', b'904 ', b'905 ', b'906 ', b'908 ')):
  359. self.logger.error('SASL error, terminating connection')
  360. self.transport.close()
  361. # NICK errors
  362. elif any(message.startswith(x) for x in (b'431 ', b'432 ', b'433 ', b'436 ')):
  363. self.logger.error(f'Failed to set nickname: {message!r}, terminating connection')
  364. self.transport.close()
  365. # USER errors
  366. elif any(message.startswith(x) for x in (b'461 ', b'462 ')):
  367. self.logger.error(f'Failed to register: {message!r}, terminating connection')
  368. self.transport.close()
  369. # JOIN errors
  370. elif any(message.startswith(x) for x in (b'405 ', b'471 ', b'473 ', b'474 ', b'475 ')):
  371. self.logger.error(f'Failed to join channel: {message!r}, terminating connection')
  372. self.transport.close()
  373. # PART errors
  374. elif message.startswith(b'442 '):
  375. self.logger.error(f'Failed to part channel: {message!r}')
  376. # JOIN/PART errors
  377. elif message.startswith(b'403 '):
  378. self.logger.error(f'Failed to join or part channel: {message!r}')
  379. # PRIVMSG errors
  380. elif any(message.startswith(x) for x in (b'401 ', b'404 ', b'407 ', b'411 ', b'412 ', b'413 ', b'414 ')):
  381. self.logger.error(f'Failed to send message: {message!r}')
  382. # Connection registration reply
  383. elif message.startswith(b'001 '):
  384. self.logger.info('IRC connection registered')
  385. if self.sasl and not self.authenticated:
  386. self.logger.error('IRC connection registered but not authenticated, terminating connection')
  387. self.transport.close()
  388. return
  389. self.send(b'JOIN ' + ','.join(self.channels).encode('utf-8')) #TODO: Split if too long
  390. asyncio.create_task(self.send_messages())
  391. asyncio.create_task(self.confirm_messages())
  392. def connection_lost(self, exc):
  393. self.logger.info('IRC connection lost')
  394. self.connected = False
  395. self.connectionClosedEvent.set()
  396. class IRCClient:
  397. logger = logging.getLogger('http2irc.IRCClient')
  398. def __init__(self, messageQueue, config):
  399. self.messageQueue = messageQueue
  400. self.config = config
  401. self.channels = {map_['ircchannel'] for map_ in config['maps'].values()}
  402. self._transport = None
  403. self._protocol = None
  404. def update_config(self, config):
  405. needReconnect = self.config['irc'] != config['irc']
  406. self.config = config
  407. if self._transport: # if currently connected:
  408. if needReconnect:
  409. self._transport.close()
  410. else:
  411. self.channels = {map_['ircchannel'] for map_ in config['maps'].values()}
  412. self._protocol.update_channels(self.channels)
  413. def _get_ssl_context(self):
  414. ctx = SSL_CONTEXTS[self.config['irc']['ssl']]
  415. if self.config['irc']['certfile'] and self.config['irc']['certkeyfile']:
  416. if ctx is True:
  417. ctx = ssl.create_default_context()
  418. if isinstance(ctx, ssl.SSLContext):
  419. ctx.load_cert_chain(self.config['irc']['certfile'], keyfile = self.config['irc']['certkeyfile'])
  420. return ctx
  421. async def run(self, loop, sigintEvent):
  422. connectionClosedEvent = asyncio.Event()
  423. while True:
  424. connectionClosedEvent.clear()
  425. try:
  426. self._transport, self._protocol = await loop.create_connection(lambda: IRCClientProtocol(self.messageQueue, connectionClosedEvent, loop, self.config, self.channels), self.config['irc']['host'], self.config['irc']['port'], ssl = self._get_ssl_context())
  427. try:
  428. await asyncio.wait((connectionClosedEvent.wait(), sigintEvent.wait()), return_when = concurrent.futures.FIRST_COMPLETED)
  429. finally:
  430. self._transport.close() #TODO BaseTransport.close is asynchronous and then triggers the protocol's connection_lost callback; need to wait for connectionClosedEvent again perhaps to correctly handle ^C?
  431. except (ConnectionRefusedError, asyncio.TimeoutError) as e:
  432. self.logger.error(str(e))
  433. await asyncio.wait((asyncio.sleep(5), sigintEvent.wait()), return_when = concurrent.futures.FIRST_COMPLETED)
  434. if sigintEvent.is_set():
  435. break
  436. class WebServer:
  437. logger = logging.getLogger('http2irc.WebServer')
  438. def __init__(self, messageQueue, config):
  439. self.messageQueue = messageQueue
  440. self.config = config
  441. self._paths = {} # '/path' => ('#channel', auth, module, moduleargs) where auth is either False (no authentication) or the HTTP header value for basic auth
  442. self._app = aiohttp.web.Application()
  443. self._app.add_routes([aiohttp.web.post('/{path:.+}', self.post)])
  444. self.update_config(config)
  445. self._configChanged = asyncio.Event()
  446. def update_config(self, config):
  447. self._paths = {map_['webpath']: (map_['ircchannel'], f'Basic {base64.b64encode(map_["auth"].encode("utf-8")).decode("utf-8")}' if map_['auth'] else False, map_['module'], map_['moduleargs']) for map_ in config['maps'].values()}
  448. needRebind = self.config['web'] != config['web']
  449. self.config = config
  450. if needRebind:
  451. self._configChanged.set()
  452. async def run(self, stopEvent):
  453. while True:
  454. runner = aiohttp.web.AppRunner(self._app)
  455. await runner.setup()
  456. site = aiohttp.web.TCPSite(runner, self.config['web']['host'], self.config['web']['port'])
  457. await site.start()
  458. await asyncio.wait((stopEvent.wait(), self._configChanged.wait()), return_when = concurrent.futures.FIRST_COMPLETED)
  459. await runner.cleanup()
  460. if stopEvent.is_set():
  461. break
  462. self._configChanged.clear()
  463. async def post(self, request):
  464. self.logger.info(f'Received request {id(request)} from {request.remote!r} for {request.path!r}')
  465. try:
  466. channel, auth, module, moduleargs = self._paths[request.path]
  467. except KeyError:
  468. self.logger.info(f'Bad request {id(request)}: no path {request.path!r}')
  469. raise aiohttp.web.HTTPNotFound()
  470. if auth:
  471. authHeader = request.headers.get('Authorization')
  472. if not authHeader or authHeader != auth:
  473. self.logger.info(f'Bad request {id(request)}: authentication failed: {authHeader!r} != {auth}')
  474. raise aiohttp.web.HTTPForbidden()
  475. if module is not None:
  476. self.logger.debug(f'Processing request {id(request)} using {module!r}')
  477. try:
  478. message = await module.process(request, *moduleargs)
  479. except aiohttp.web.HTTPException as e:
  480. raise e
  481. except Exception as e:
  482. self.logger.error(f'Bad request {id(request)}: exception in module process function: {e!s}')
  483. raise aiohttp.web.HTTPBadRequest()
  484. if '\r' in message or '\n' in message:
  485. self.logger.error(f'Bad request {id(request)}: module process function returned message with linebreaks: {message!r}')
  486. raise aiohttp.web.HTTPBadRequest()
  487. else:
  488. self.logger.debug(f'Processing request {id(request)} using default processor')
  489. message = await self._default_process(request)
  490. self.logger.info(f'Accepted request {id(request)}, putting message {message!r} for {channel} into message queue')
  491. self.messageQueue.put_nowait((channel, message))
  492. raise aiohttp.web.HTTPOk()
  493. async def _default_process(self, request):
  494. try:
  495. message = await request.text()
  496. except Exception as e:
  497. self.logger.info(f'Bad request {id(request)}: exception while reading request data: {e!s}')
  498. raise aiohttp.web.HTTPBadRequest() # Yes, it's always the client's fault. :-)
  499. self.logger.debug(f'Request {id(request)} payload: {message!r}')
  500. # Strip optional [CR] LF at the end of the payload
  501. if message.endswith('\r\n'):
  502. message = message[:-2]
  503. elif message.endswith('\n'):
  504. message = message[:-1]
  505. if '\r' in message or '\n' in message:
  506. self.logger.info('Bad request {id(request)}: linebreaks in message')
  507. raise aiohttp.web.HTTPBadRequest()
  508. return message
  509. def configure_logging(config):
  510. #TODO: Replace with logging.basicConfig(..., force = True) (Py 3.8+)
  511. root = logging.getLogger()
  512. root.setLevel(getattr(logging, config['logging']['level']))
  513. root.handlers = [] #FIXME: Undocumented attribute of logging.Logger
  514. formatter = logging.Formatter(config['logging']['format'], style = '{')
  515. stderrHandler = logging.StreamHandler()
  516. stderrHandler.setFormatter(formatter)
  517. root.addHandler(stderrHandler)
  518. async def main():
  519. if len(sys.argv) != 2:
  520. print('Usage: http2irc.py CONFIGFILE', file = sys.stderr)
  521. sys.exit(1)
  522. configFile = sys.argv[1]
  523. config = Config(configFile)
  524. configure_logging(config)
  525. loop = asyncio.get_running_loop()
  526. messageQueue = MessageQueue()
  527. irc = IRCClient(messageQueue, config)
  528. webserver = WebServer(messageQueue, config)
  529. sigintEvent = asyncio.Event()
  530. def sigint_callback():
  531. global logger
  532. nonlocal sigintEvent
  533. logger.info('Got SIGINT, stopping')
  534. sigintEvent.set()
  535. loop.add_signal_handler(signal.SIGINT, sigint_callback)
  536. def sigusr1_callback():
  537. global logger
  538. nonlocal config, irc, webserver
  539. logger.info('Got SIGUSR1, reloading config')
  540. try:
  541. newConfig = config.reread()
  542. except InvalidConfig as e:
  543. logger.error(f'Config reload failed: {e!s} (old config remains active)')
  544. return
  545. config = newConfig
  546. configure_logging(config)
  547. irc.update_config(config)
  548. webserver.update_config(config)
  549. loop.add_signal_handler(signal.SIGUSR1, sigusr1_callback)
  550. await asyncio.gather(irc.run(loop, sigintEvent), webserver.run(sigintEvent))
  551. if __name__ == '__main__':
  552. asyncio.run(main())