Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

483 рядки
19 KiB

  1. import aiohttp
  2. import aiohttp.web
  3. import asyncio
  4. import base64
  5. import collections
  6. import concurrent.futures
  7. import logging
  8. import os.path
  9. import signal
  10. import ssl
  11. import string
  12. import sys
  13. import toml
  14. SSL_CONTEXTS = {'yes': True, 'no': False, 'insecure': ssl.SSLContext()}
  15. class InvalidConfig(Exception):
  16. '''Error in configuration file'''
  17. def is_valid_pem(path, withCert):
  18. '''Very basic check whether something looks like a valid PEM certificate'''
  19. try:
  20. with open(path, 'rb') as fp:
  21. contents = fp.read()
  22. # All of these raise exceptions if something's wrong...
  23. if withCert:
  24. assert contents.startswith(b'-----BEGIN CERTIFICATE-----\n')
  25. endCertPos = contents.index(b'-----END CERTIFICATE-----\n')
  26. base64.b64decode(contents[28:endCertPos].replace(b'\n', b''), validate = True)
  27. assert contents[endCertPos + 26:].startswith(b'-----BEGIN PRIVATE KEY-----\n')
  28. else:
  29. assert contents.startswith(b'-----BEGIN PRIVATE KEY-----\n')
  30. endCertPos = -26 # Please shoot me.
  31. endKeyPos = contents.index(b'-----END PRIVATE KEY-----\n')
  32. base64.b64decode(contents[endCertPos + 26 + 28: endKeyPos].replace(b'\n', b''), validate = True)
  33. assert contents[endKeyPos + 26:] == b''
  34. return True
  35. except: # Yes, really
  36. return False
  37. class Config(dict):
  38. def __init__(self, filename):
  39. super().__init__()
  40. self._filename = filename
  41. with open(self._filename, 'r') as fp:
  42. obj = toml.load(fp)
  43. logging.info(repr(obj))
  44. # Sanity checks
  45. if any(x not in ('logging', 'irc', 'web', 'maps') for x in obj.keys()):
  46. raise InvalidConfig('Unknown sections found in base object')
  47. if any(not isinstance(x, collections.abc.Mapping) for x in obj.values()):
  48. raise InvalidConfig('Invalid section type(s), expected objects/dicts')
  49. if 'logging' in obj:
  50. if any(x not in ('level', 'format') for x in obj['logging']):
  51. raise InvalidConfig('Unknown key found in log section')
  52. if 'level' in obj['logging'] and obj['logging']['level'] not in ('DEBUG', 'INFO', 'WARNING', 'ERROR'):
  53. raise InvalidConfig('Invalid log level')
  54. if 'format' in obj['logging']:
  55. if not isinstance(obj['logging']['format'], str):
  56. raise InvalidConfig('Invalid log format')
  57. try:
  58. #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)
  59. # 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).
  60. assert sum(1 for x in string.Formatter().parse(obj['logging']['format']) if x[1] is not None) > 0
  61. except (ValueError, AssertionError) as e:
  62. raise InvalidConfig('Invalid log format: parsing failed') from e
  63. if 'irc' in obj:
  64. if any(x not in ('host', 'port', 'ssl', 'nick', 'real', 'certfile', 'certkeyfile') for x in obj['irc']):
  65. raise InvalidConfig('Unknown key found in irc section')
  66. if 'host' in obj['irc'] and not isinstance(obj['irc']['host'], str): #TODO: Check whether it's a valid hostname
  67. raise InvalidConfig('Invalid IRC host')
  68. if 'port' in obj['irc'] and (not isinstance(obj['irc']['port'], int) or not 1 <= obj['irc']['port'] <= 65535):
  69. raise InvalidConfig('Invalid IRC port')
  70. if 'ssl' in obj['irc'] and obj['irc']['ssl'] not in ('yes', 'no', 'insecure'):
  71. raise InvalidConfig(f'Invalid IRC SSL setting: {obj["irc"]["ssl"]!r}')
  72. if 'nick' in obj['irc'] and not isinstance(obj['irc']['nick'], str): #TODO: Check whether it's a valid nickname
  73. raise InvalidConfig('Invalid IRC nick')
  74. if 'real' in obj['irc'] and not isinstance(obj['irc']['real'], str):
  75. raise InvalidConfig('Invalid IRC realname')
  76. if ('certfile' in obj['irc']) != ('certkeyfile' in obj['irc']):
  77. raise InvalidConfig('Invalid IRC cert config: needs both certfile and certkeyfile')
  78. if 'certfile' in obj['irc']:
  79. if not isinstance(obj['irc']['certfile'], str):
  80. raise InvalidConfig('Invalid certificate file: not a string')
  81. if not os.path.isfile(obj['irc']['certfile']):
  82. raise InvalidConfig('Invalid certificate file: not a regular file')
  83. if not is_valid_pem(obj['irc']['certfile'], True):
  84. raise InvalidConfig('Invalid certificate file: not a valid PEM cert')
  85. if 'certkeyfile' in obj['irc']:
  86. if not isinstance(obj['irc']['certkeyfile'], str):
  87. raise InvalidConfig('Invalid certificate key file: not a string')
  88. if not os.path.isfile(obj['irc']['certkeyfile']):
  89. raise InvalidConfig('Invalid certificate key file: not a regular file')
  90. if not is_valid_pem(obj['irc']['certkeyfile'], False):
  91. raise InvalidConfig('Invalid certificate key file: not a valid PEM key')
  92. if 'web' in obj:
  93. if any(x not in ('host', 'port') for x in obj['web']):
  94. raise InvalidConfig('Unknown key found in web section')
  95. if 'host' in obj['web'] and not isinstance(obj['web']['host'], str): #TODO: Check whether it's a valid hostname (must resolve I guess?)
  96. raise InvalidConfig('Invalid web hostname')
  97. if 'port' in obj['web'] and (not isinstance(obj['web']['port'], int) or not 1 <= obj['web']['port'] <= 65535):
  98. raise InvalidConfig('Invalid web port')
  99. if 'maps' in obj:
  100. for key, map_ in obj['maps'].items():
  101. if not isinstance(key, str) or not key:
  102. raise InvalidConfig(f'Invalid map key {key!r}')
  103. if not isinstance(map_, collections.abc.Mapping):
  104. raise InvalidConfig(f'Invalid map for {key!r}')
  105. if any(x not in ('webpath', 'ircchannel', 'auth') for x in map_):
  106. raise InvalidConfig(f'Unknown key(s) found in map {key!r}')
  107. #TODO: Check values
  108. # Default values
  109. finalObj = {'logging': {'level': 'INFO', 'format': '{asctime} {levelname} {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': {}}
  110. # Fill in default values for the maps
  111. for key, map_ in obj['maps'].items():
  112. if 'webpath' not in map_:
  113. map_['webpath'] = f'/{key}'
  114. if 'ircchannel' not in map_:
  115. map_['ircchannel'] = f'#{key}'
  116. if 'auth' not in map_:
  117. map_['auth'] = False
  118. # Merge in what was read from the config file and set keys on self
  119. for key in ('logging', 'irc', 'web', 'maps'):
  120. if key in obj:
  121. finalObj[key].update(obj[key])
  122. self[key] = finalObj[key]
  123. def __repr__(self):
  124. return f'<Config(logging={self["logging"]!r}, irc={self["irc"]!r}, web={self["web"]!r}, maps={self["maps"]!r})>'
  125. def reread(self):
  126. return Config(self._filename)
  127. class MessageQueue:
  128. # An object holding onto the messages received from nodeping
  129. # This is effectively a reimplementation of parts of asyncio.Queue with some specific additional code.
  130. # Unfortunately, asyncio.Queue's extensibility (_init, _put, and _get methods) is undocumented, so I don't want to rely on that.
  131. # Differences to asyncio.Queue include:
  132. # - No maxsize
  133. # - No put coroutine (not necessary since the queue can never be full)
  134. # - Only one concurrent getter
  135. # - putleft_nowait to put to the front of the queue (so that the IRC client can put a message back when delivery fails)
  136. def __init__(self):
  137. self._getter = None # None | asyncio.Future
  138. self._queue = collections.deque()
  139. async def get(self):
  140. if self._getter is not None:
  141. raise RuntimeError('Cannot get concurrently')
  142. if len(self._queue) == 0:
  143. self._getter = asyncio.get_running_loop().create_future()
  144. logging.debug('Awaiting getter')
  145. try:
  146. await self._getter
  147. except asyncio.CancelledError:
  148. logging.debug('Cancelled getter')
  149. self._getter = None
  150. raise
  151. logging.debug('Awaited getter')
  152. self._getter = None
  153. # For testing the cancellation/putting back onto the queue
  154. #logging.debug('Delaying message queue get')
  155. #await asyncio.sleep(3)
  156. #logging.debug('Done delaying')
  157. return self.get_nowait()
  158. def get_nowait(self):
  159. if len(self._queue) == 0:
  160. raise asyncio.QueueEmpty
  161. return self._queue.popleft()
  162. def put_nowait(self, item):
  163. self._queue.append(item)
  164. if self._getter is not None and not self._getter.cancelled():
  165. self._getter.set_result(None)
  166. def putleft_nowait(self, *item):
  167. self._queue.extendleft(reversed(item))
  168. if self._getter is not None and not self._getter.cancelled():
  169. self._getter.set_result(None)
  170. def qsize(self):
  171. return len(self._queue)
  172. class IRCClientProtocol(asyncio.Protocol):
  173. def __init__(self, messageQueue, connectionClosedEvent, loop, config, channels):
  174. logging.debug(f'Protocol init {id(self)}: {messageQueue} {id(messageQueue)}, {connectionClosedEvent}, {loop}')
  175. self.messageQueue = messageQueue
  176. self.connectionClosedEvent = connectionClosedEvent
  177. self.loop = loop
  178. self.config = config
  179. self.buffer = b''
  180. self.connected = False
  181. self.channels = channels # Currently joined/supposed-to-be-joined channels; set(str)
  182. self.unconfirmedMessages = []
  183. self.pongReceivedEvent = asyncio.Event()
  184. def connection_made(self, transport):
  185. logging.info('Connected')
  186. self.transport = transport
  187. self.connected = True
  188. nickb = self.config['irc']['nick'].encode('utf-8')
  189. self.send(b'NICK ' + nickb)
  190. self.send(b'USER ' + nickb + b' ' + nickb + b' ' + nickb + b' :' + self.config['irc']['real'].encode('utf-8'))
  191. def update_channels(self, channels: set):
  192. channelsToPart = self.channels - channels
  193. channelsToJoin = channels - self.channels
  194. self.channels = channels
  195. if self.connected:
  196. if channelsToPart:
  197. #TODO: Split if too long
  198. self.send(b'PART ' + ','.join(channelsToPart).encode('utf-8'))
  199. if channelsToJoin:
  200. self.send(b'JOIN ' + ','.join(channelsToJoin).encode('utf-8'))
  201. def send(self, data):
  202. logging.info(f'Send: {data!r}')
  203. self.transport.write(data + b'\r\n')
  204. async def _get_message(self):
  205. logging.debug(f'Message queue {id(self.messageQueue)} length: {self.messageQueue.qsize()}')
  206. messageFuture = asyncio.create_task(self.messageQueue.get())
  207. done, pending = await asyncio.wait((messageFuture, self.connectionClosedEvent.wait()), return_when = concurrent.futures.FIRST_COMPLETED)
  208. if self.connectionClosedEvent.is_set():
  209. if messageFuture in pending:
  210. logging.debug('Cancelling messageFuture')
  211. messageFuture.cancel()
  212. try:
  213. await messageFuture
  214. except asyncio.CancelledError:
  215. logging.debug('Cancelled messageFuture')
  216. pass
  217. else:
  218. # messageFuture is already done but we're stopping, so put the result back onto the queue
  219. self.messageQueue.putleft_nowait(messageFuture.result())
  220. return None, None
  221. assert messageFuture in done, 'Invalid state: messageFuture not in done futures'
  222. return messageFuture.result()
  223. async def send_messages(self):
  224. while self.connected:
  225. logging.debug(f'{id(self)}: trying to get a message')
  226. channel, message = await self._get_message()
  227. logging.debug(f'{id(self)}: got message: {message!r}')
  228. if message is None:
  229. break
  230. #TODO Split if the message is too long.
  231. self.unconfirmedMessages.append((channel, message))
  232. self.send(b'PRIVMSG ' + channel.encode('utf-8') + b' :' + message.encode('utf-8'))
  233. await asyncio.sleep(1) # Rate limit
  234. async def confirm_messages(self):
  235. while self.connected:
  236. await asyncio.wait((asyncio.sleep(60), self.connectionClosedEvent.wait()), return_when = concurrent.futures.FIRST_COMPLETED) # Confirm once per minute
  237. if not self.connected: # Disconnected while sleeping, can't confirm unconfirmed messages, requeue them directly
  238. self.messageQueue.putleft_nowait(*self.unconfirmedMessages)
  239. self.unconfirmedMessages = []
  240. break
  241. if not self.unconfirmedMessages:
  242. logging.debug(f'{id(self)}: no messages to confirm')
  243. continue
  244. logging.debug(f'{id(self)}: trying to confirm message delivery')
  245. self.pongReceivedEvent.clear()
  246. self.send(b'PING :42')
  247. await asyncio.wait((asyncio.sleep(5), self.pongReceivedEvent.wait()), return_when = concurrent.futures.FIRST_COMPLETED)
  248. logging.debug(f'{id(self)}: message delivery success: {self.pongReceivedEvent.is_set()}')
  249. if not self.pongReceivedEvent.is_set():
  250. # No PONG received in five seconds, assume connection's dead
  251. self.messageQueue.putleft_nowait(*self.unconfirmedMessages)
  252. self.transport.close()
  253. self.unconfirmedMessages = []
  254. def data_received(self, data):
  255. logging.debug(f'Data received: {data!r}')
  256. # Split received data on CRLF. If there's any data left in the buffer, prepend it to the first message and process that.
  257. # Then, process all messages except the last one (since data might not end on a CRLF) and keep the remainder in the buffer.
  258. # If data does end with CRLF, all messages will have been processed and the buffer will be empty again.
  259. messages = data.split(b'\r\n')
  260. if self.buffer:
  261. self.message_received(self.buffer + messages[0])
  262. messages = messages[1:]
  263. for message in messages[:-1]:
  264. self.message_received(message)
  265. self.buffer = messages[-1]
  266. def message_received(self, message):
  267. logging.info(f'Message received: {message!r}')
  268. if message.startswith(b':'):
  269. # Prefixed message, extract command + parameters (the prefix cannot contain a space)
  270. message = message.split(b' ', 1)[1]
  271. if message.startswith(b'PING '):
  272. self.send(b'PONG ' + message[5:])
  273. elif message.startswith(b'PONG '):
  274. self.pongReceivedEvent.set()
  275. elif message.startswith(b'001 '):
  276. # Connection registered
  277. self.send(b'JOIN ' + ','.join(self.channels).encode('utf-8')) #TODO: Split if too long
  278. asyncio.create_task(self.send_messages())
  279. asyncio.create_task(self.confirm_messages())
  280. def connection_lost(self, exc):
  281. logging.info('The server closed the connection')
  282. self.connected = False
  283. self.connectionClosedEvent.set()
  284. class IRCClient:
  285. def __init__(self, messageQueue, config):
  286. self.messageQueue = messageQueue
  287. self.config = config
  288. self.channels = {map_['ircchannel'] for map_ in config['maps'].values()}
  289. self._transport = None
  290. self._protocol = None
  291. def update_config(self, config):
  292. needReconnect = self.config['irc'] != config['irc']
  293. self.config = config
  294. if self._transport: # if currently connected:
  295. if needReconnect:
  296. self._transport.close()
  297. else:
  298. self.channels = {map_['ircchannel'] for map_ in config['maps'].values()}
  299. self._protocol.update_channels(self.channels)
  300. def _get_ssl_context(self):
  301. ctx = SSL_CONTEXTS[self.config['irc']['ssl']]
  302. if self.config['irc']['certfile'] and self.config['irc']['certkeyfile']:
  303. if ctx is True:
  304. ctx = ssl.create_default_context()
  305. if isinstance(ctx, ssl.SSLContext):
  306. ctx.load_cert_chain(self.config['irc']['certfile'], keyfile = self.config['irc']['certkeyfile'])
  307. return ctx
  308. async def run(self, loop, sigintEvent):
  309. connectionClosedEvent = asyncio.Event()
  310. while True:
  311. connectionClosedEvent.clear()
  312. try:
  313. 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())
  314. try:
  315. await asyncio.wait((connectionClosedEvent.wait(), sigintEvent.wait()), return_when = concurrent.futures.FIRST_COMPLETED)
  316. finally:
  317. 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?
  318. except (ConnectionRefusedError, asyncio.TimeoutError) as e:
  319. logging.error(str(e))
  320. await asyncio.wait((asyncio.sleep(5), sigintEvent.wait()), return_when = concurrent.futures.FIRST_COMPLETED)
  321. if sigintEvent.is_set():
  322. break
  323. class WebServer:
  324. def __init__(self, messageQueue, config):
  325. self.messageQueue = messageQueue
  326. self.config = config
  327. self._paths = {} # '/path' => ('#channel', auth) where auth is either False (no authentication) or the HTTP header value for basic auth
  328. self._app = aiohttp.web.Application()
  329. self._app.add_routes([aiohttp.web.post('/{path:.+}', self.post)])
  330. self.update_config(config)
  331. self._configChanged = asyncio.Event()
  332. def update_config(self, config):
  333. self._paths = {map_['webpath']: (map_['ircchannel'], f'Basic {base64.b64encode(map_["auth"].encode("utf-8")).decode("utf-8")}' if map_['auth'] else False) for map_ in config['maps'].values()}
  334. needRebind = self.config['web'] != config['web']
  335. self.config = config
  336. if needRebind:
  337. self._configChanged.set()
  338. async def run(self, stopEvent):
  339. while True:
  340. runner = aiohttp.web.AppRunner(self._app)
  341. await runner.setup()
  342. site = aiohttp.web.TCPSite(runner, self.config['web']['host'], self.config['web']['port'])
  343. await site.start()
  344. await asyncio.wait((stopEvent.wait(), self._configChanged.wait()), return_when = concurrent.futures.FIRST_COMPLETED)
  345. await runner.cleanup()
  346. if stopEvent.is_set():
  347. break
  348. self._configChanged.clear()
  349. async def post(self, request):
  350. logging.info(f'Received request for {request.path!r}')
  351. try:
  352. channel, auth = self._paths[request.path]
  353. except KeyError:
  354. logging.info(f'Bad request: no path {request.path!r}')
  355. raise aiohttp.web.HTTPNotFound()
  356. if auth:
  357. authHeader = request.headers.get('Authorization')
  358. if not authHeader or authHeader != auth:
  359. logging.info(f'Bad request: authentication failed: {authHeader!r} != {auth}')
  360. raise aiohttp.web.HTTPForbidden()
  361. try:
  362. message = await request.text()
  363. except Exception as e:
  364. logging.info(f'Bad request: exception while reading request data: {e!s}')
  365. raise aiohttp.web.HTTPBadRequest() # Yes, it's always the client's fault. :-)
  366. logging.debug(f'Request payload: {message!r}')
  367. # Strip optional [CR] LF at the end of the payload
  368. if message.endswith('\r\n'):
  369. message = message[:-2]
  370. elif message.endswith('\n'):
  371. message = message[:-1]
  372. if '\r' in message or '\n' in message:
  373. logging.info('Bad request: linebreaks in message')
  374. raise aiohttp.web.HTTPBadRequest()
  375. logging.debug(f'Putting message {message!r} for {channel} into message queue')
  376. self.messageQueue.put_nowait((channel, message))
  377. raise aiohttp.web.HTTPOk()
  378. def configure_logging(config):
  379. #TODO: Replace with logging.basicConfig(..., force = True) (Py 3.8+)
  380. root = logging.getLogger()
  381. root.setLevel(getattr(logging, config['logging']['level']))
  382. root.handlers = [] #FIXME: Undocumented attribute of logging.Logger
  383. formatter = logging.Formatter(config['logging']['format'], style = '{')
  384. stderrHandler = logging.StreamHandler()
  385. stderrHandler.setFormatter(formatter)
  386. root.addHandler(stderrHandler)
  387. async def main():
  388. if len(sys.argv) != 2:
  389. print('Usage: http2irc.py CONFIGFILE', file = sys.stderr)
  390. sys.exit(1)
  391. configFile = sys.argv[1]
  392. config = Config(configFile)
  393. configure_logging(config)
  394. loop = asyncio.get_running_loop()
  395. messageQueue = MessageQueue()
  396. irc = IRCClient(messageQueue, config)
  397. webserver = WebServer(messageQueue, config)
  398. sigintEvent = asyncio.Event()
  399. def sigint_callback():
  400. logging.info('Got SIGINT')
  401. nonlocal sigintEvent
  402. sigintEvent.set()
  403. loop.add_signal_handler(signal.SIGINT, sigint_callback)
  404. def sigusr1_callback():
  405. logging.info('Got SIGUSR1, reloading config')
  406. nonlocal config, irc, webserver
  407. try:
  408. newConfig = config.reread()
  409. except InvalidConfig as e:
  410. logging.error(f'Config reload failed: {e!s}')
  411. return
  412. config = newConfig
  413. configure_logging(config)
  414. irc.update_config(config)
  415. webserver.update_config(config)
  416. loop.add_signal_handler(signal.SIGUSR1, sigusr1_callback)
  417. await asyncio.gather(irc.run(loop, sigintEvent), webserver.run(sigintEvent))
  418. if __name__ == '__main__':
  419. asyncio.run(main())