Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

407 řádky
16 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 signal
  9. import ssl
  10. import sys
  11. import toml
  12. import types
  13. logging.basicConfig(level = logging.DEBUG, format = '{asctime} {levelname} {message}', style = '{')
  14. SSL_CONTEXTS = {'yes': True, 'no': False, 'insecure': ssl.SSLContext()}
  15. class InvalidConfig(Exception):
  16. '''Error in configuration file'''
  17. def _mapping_to_namespace(d):
  18. '''Converts a mapping (e.g. dict) to a types.SimpleNamespace, recursively'''
  19. return types.SimpleNamespace(**{key: _mapping_to_namespace(value) if isinstance(value, collections.abc.Mapping) else value for key, value in d.items()})
  20. class Config:
  21. def __init__(self, filename):
  22. self._filename = filename
  23. # Set below:
  24. self.irc = None
  25. self.web = None
  26. self.maps = None
  27. with open(self._filename, 'r') as fp:
  28. obj = toml.load(fp)
  29. logging.info(repr(obj))
  30. # Sanity checks
  31. if any(x not in ('irc', 'web', 'maps') for x in obj.keys()):
  32. raise InvalidConfig('Unknown sections found in base object')
  33. if any(not isinstance(x, collections.abc.Mapping) for x in obj.values()):
  34. raise InvalidConfig('Invalid section type(s), expected objects/dicts')
  35. if 'irc' in obj:
  36. if any(x not in ('host', 'port', 'ssl', 'nick', 'real') for x in obj['irc']):
  37. raise InvalidConfig('Unknown key found in irc section')
  38. if 'host' in obj['irc'] and not isinstance(obj['irc']['host'], str): #TODO: Check whether it's a valid hostname
  39. raise InvalidConfig('Invalid IRC host')
  40. if 'port' in obj['irc'] and (not isinstance(obj['irc']['port'], int) or not 1 <= obj['irc']['port'] <= 65535):
  41. raise InvalidConfig('Invalid IRC port')
  42. if 'ssl' in obj['irc'] and obj['irc']['ssl'] not in ('yes', 'no', 'insecure'):
  43. raise InvalidConfig(f'Invalid IRC SSL setting: {obj["irc"]["ssl"]!r}')
  44. if 'nick' in obj['irc'] and not isinstance(obj['irc']['nick'], str): #TODO: Check whether it's a valid nickname
  45. raise InvalidConfig('Invalid IRC nick')
  46. if 'real' in obj['irc'] and not isinstance(obj['irc']['real'], str):
  47. raise InvalidConfig('Invalid IRC realname')
  48. if 'web' in obj:
  49. if any(x not in ('host', 'port') for x in obj['web']):
  50. raise InvalidConfig('Unknown key found in web section')
  51. if 'host' in obj['web'] and not isinstance(obj['web']['host'], str): #TODO: Check whether it's a valid hostname (must resolve I guess?)
  52. raise InvalidConfig('Invalid web hostname')
  53. if 'port' in obj['web'] and (not isinstance(obj['web']['port'], int) or not 1 <= obj['web']['port'] <= 65535):
  54. raise InvalidConfig('Invalid web port')
  55. if 'maps' in obj:
  56. for key, map_ in obj['maps'].items():
  57. # Ensure that the key is a valid Python identifier since it will be set as an attribute in the namespace.
  58. #TODO: Support for fancier identifiers (PEP 3131)?
  59. if not isinstance(key, str) or not key or key.strip('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_') != '' or key[0].strip('0123456789') == '':
  60. raise InvalidConfig(f'Invalid map key {key!r}')
  61. if not isinstance(map_, collections.abc.Mapping):
  62. raise InvalidConfig(f'Invalid map for {key!r}')
  63. if any(x not in ('webpath', 'ircchannel', 'auth') for x in map_):
  64. raise InvalidConfig(f'Unknown key(s) found in map {key!r}')
  65. #TODO: Check values
  66. # Default values
  67. self._obj = {'irc': {'host': 'irc.hackint.org', 'port': 6697, 'ssl': 'yes', 'nick': 'h2ibot', 'real': 'I am an http2irc bot.'}, 'web': {'host': '127.0.0.1', 'port': 8080}, 'maps': {}}
  68. # Fill in default values for the maps
  69. for key, map_ in obj['maps'].items():
  70. if 'webpath' not in map_:
  71. map_['webpath'] = f'/{key}'
  72. if 'ircchannel' not in map_:
  73. map_['ircchannel'] = f'#{key}'
  74. if 'auth' not in map_:
  75. map_['auth'] = False
  76. # Merge in what was read from the config file and convert to SimpleNamespace
  77. for key in ('irc', 'web', 'maps'):
  78. if key in obj:
  79. self._obj[key].update(obj[key])
  80. setattr(self, key, _mapping_to_namespace(self._obj[key]))
  81. def __repr__(self):
  82. return f'Config(irc={self.irc!r}, web={self.web!r}, maps={self.maps!r})'
  83. def reread(self):
  84. return Config(self._filename)
  85. class MessageQueue:
  86. # An object holding onto the messages received from nodeping
  87. # This is effectively a reimplementation of parts of asyncio.Queue with some specific additional code.
  88. # Unfortunately, asyncio.Queue's extensibility (_init, _put, and _get methods) is undocumented, so I don't want to rely on that.
  89. # Differences to asyncio.Queue include:
  90. # - No maxsize
  91. # - No put coroutine (not necessary since the queue can never be full)
  92. # - Only one concurrent getter
  93. # - putleft_nowait to put to the front of the queue (so that the IRC client can put a message back when delivery fails)
  94. def __init__(self):
  95. self._getter = None # None | asyncio.Future
  96. self._queue = collections.deque()
  97. async def get(self):
  98. if self._getter is not None:
  99. raise RuntimeError('Cannot get concurrently')
  100. if len(self._queue) == 0:
  101. self._getter = asyncio.get_running_loop().create_future()
  102. logging.debug('Awaiting getter')
  103. try:
  104. await self._getter
  105. except asyncio.CancelledError:
  106. logging.debug('Cancelled getter')
  107. self._getter = None
  108. raise
  109. logging.debug('Awaited getter')
  110. self._getter = None
  111. # For testing the cancellation/putting back onto the queue
  112. #logging.debug('Delaying message queue get')
  113. #await asyncio.sleep(3)
  114. #logging.debug('Done delaying')
  115. return self.get_nowait()
  116. def get_nowait(self):
  117. if len(self._queue) == 0:
  118. raise asyncio.QueueEmpty
  119. return self._queue.popleft()
  120. def put_nowait(self, item):
  121. self._queue.append(item)
  122. if self._getter is not None:
  123. self._getter.set_result(None)
  124. def putleft_nowait(self, *item):
  125. self._queue.extendleft(reversed(item))
  126. if self._getter is not None:
  127. self._getter.set_result(None)
  128. def qsize(self):
  129. return len(self._queue)
  130. class IRCClientProtocol(asyncio.Protocol):
  131. def __init__(self, messageQueue, connectionClosedEvent, loop, config, channels):
  132. logging.debug(f'Protocol init {id(self)}: {messageQueue} {id(messageQueue)}, {connectionClosedEvent}, {loop}')
  133. self.messageQueue = messageQueue
  134. self.connectionClosedEvent = connectionClosedEvent
  135. self.loop = loop
  136. self.config = config
  137. self.buffer = b''
  138. self.connected = False
  139. self.channels = channels # Currently joined/supposed-to-be-joined channels; set(str)
  140. self.unconfirmedMessages = []
  141. self.pongReceivedEvent = asyncio.Event()
  142. def connection_made(self, transport):
  143. logging.info('Connected')
  144. self.transport = transport
  145. self.connected = True
  146. nickb = self.config.irc.nick.encode('utf-8')
  147. self.send(b'NICK ' + nickb)
  148. self.send(b'USER ' + nickb + b' ' + nickb + b' ' + nickb + b' :' + self.config.irc.real.encode('utf-8'))
  149. self.send(b'JOIN ' + ','.join(self.channels).encode('utf-8')) #TODO: Split if too long
  150. asyncio.create_task(self.send_messages())
  151. asyncio.create_task(self.confirm_messages())
  152. def update_channels(self, channels: set):
  153. channelsToPart = self.channels - channels
  154. channelsToJoin = channels - self.channels
  155. self.channels = channels
  156. if self.connected:
  157. if channelsToPart:
  158. #TODO: Split if too long
  159. self.send(b'PART ' + ','.join(channelsToPart).encode('utf-8'))
  160. if channelsToJoin:
  161. self.send(b'JOIN ' + ','.join(channelsToJoin).encode('utf-8'))
  162. def send(self, data):
  163. logging.info(f'Send: {data!r}')
  164. self.transport.write(data + b'\r\n')
  165. async def _get_message(self):
  166. logging.debug(f'Message queue {id(self.messageQueue)} length: {self.messageQueue.qsize()}')
  167. messageFuture = asyncio.create_task(self.messageQueue.get())
  168. done, pending = await asyncio.wait((messageFuture, self.connectionClosedEvent.wait()), return_when = concurrent.futures.FIRST_COMPLETED)
  169. if self.connectionClosedEvent.is_set():
  170. if messageFuture in pending:
  171. logging.debug('Cancelling messageFuture')
  172. messageFuture.cancel()
  173. try:
  174. await messageFuture
  175. except asyncio.CancelledError:
  176. logging.debug('Cancelled messageFuture')
  177. pass
  178. else:
  179. # messageFuture is already done but we're stopping, so put the result back onto the queue
  180. self.messageQueue.putleft_nowait(messageFuture.result())
  181. return None, None
  182. assert messageFuture in done, 'Invalid state: messageFuture not in done futures'
  183. return messageFuture.result()
  184. async def send_messages(self):
  185. while self.connected:
  186. logging.debug(f'{id(self)}: trying to get a message')
  187. channel, message = await self._get_message()
  188. logging.debug(f'{id(self)}: got message: {message!r}')
  189. if message is None:
  190. break
  191. #TODO Split if the message is too long.
  192. self.unconfirmedMessages.append((channel, message))
  193. self.send(b'PRIVMSG ' + channel.encode('utf-8') + b' :' + message.encode('utf-8'))
  194. await asyncio.sleep(1) # Rate limit
  195. async def confirm_messages(self):
  196. while self.connected:
  197. await asyncio.wait((asyncio.sleep(60), self.connectionClosedEvent.wait()), return_when = concurrent.futures.FIRST_COMPLETED) # Confirm once per minute
  198. if not self.connected: # Disconnected while sleeping, can't confirm unconfirmed messages, requeue them directly
  199. self.messageQueue.putleft_nowait(*self.unconfirmedMessages)
  200. self.unconfirmedMessages = []
  201. break
  202. if not self.unconfirmedMessages:
  203. logging.debug(f'{id(self)}: no messages to confirm')
  204. continue
  205. logging.debug(f'{id(self)}: trying to confirm message delivery')
  206. self.pongReceivedEvent.clear()
  207. self.send(b'PING :42')
  208. await asyncio.wait((asyncio.sleep(5), self.pongReceivedEvent.wait()), return_when = concurrent.futures.FIRST_COMPLETED)
  209. logging.debug(f'{id(self)}: message delivery success: {self.pongReceivedEvent.is_set()}')
  210. if not self.pongReceivedEvent.is_set():
  211. # No PONG received in five seconds, assume connection's dead
  212. self.messageQueue.putleft_nowait(*self.unconfirmedMessages)
  213. self.transport.close()
  214. self.unconfirmedMessages = []
  215. def data_received(self, data):
  216. logging.debug(f'Data received: {data!r}')
  217. # Split received data on CRLF. If there's any data left in the buffer, prepend it to the first message and process that.
  218. # Then, process all messages except the last one (since data might not end on a CRLF) and keep the remainder in the buffer.
  219. # If data does end with CRLF, all messages will have been processed and the buffer will be empty again.
  220. messages = data.split(b'\r\n')
  221. if self.buffer:
  222. self.message_received(self.buffer + messages[0])
  223. messages = messages[1:]
  224. for message in messages[:-1]:
  225. self.message_received(message)
  226. self.buffer = messages[-1]
  227. def message_received(self, message):
  228. logging.info(f'Message received: {message!r}')
  229. if message.startswith(b'PING '):
  230. self.send(b'PONG ' + message[5:])
  231. elif message.startswith(b'PONG '):
  232. self.pongReceivedEvent.set()
  233. def connection_lost(self, exc):
  234. logging.info('The server closed the connection')
  235. self.connected = False
  236. self.connectionClosedEvent.set()
  237. class IRCClient:
  238. def __init__(self, messageQueue, config):
  239. self.messageQueue = messageQueue
  240. self.config = config
  241. self.channels = {map_.ircchannel for map_ in config.maps.__dict__.values()}
  242. self._transport = None
  243. self._protocol = None
  244. def update_config(self, config):
  245. needReconnect = (self.config.irc.host, self.config.irc.port, self.config.irc.ssl) != (config.irc.host, config.irc.port, config.irc.ssl)
  246. self.config = config
  247. if self._transport: # if currently connected:
  248. if needReconnect:
  249. self._transport.close()
  250. else:
  251. self.channels = {map_.ircchannel for map_ in config.maps.__dict__.values()}
  252. self._protocol.update_channels(self.channels)
  253. async def run(self, loop, sigintEvent):
  254. connectionClosedEvent = asyncio.Event()
  255. while True:
  256. connectionClosedEvent.clear()
  257. try:
  258. 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 = SSL_CONTEXTS[self.config.irc.ssl])
  259. try:
  260. await asyncio.wait((connectionClosedEvent.wait(), sigintEvent.wait()), return_when = concurrent.futures.FIRST_COMPLETED)
  261. finally:
  262. 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?
  263. except (ConnectionRefusedError, asyncio.TimeoutError) as e:
  264. logging.error(str(e))
  265. await asyncio.wait((asyncio.sleep(5), sigintEvent.wait()), return_when = concurrent.futures.FIRST_COMPLETED)
  266. if sigintEvent.is_set():
  267. break
  268. class WebServer:
  269. def __init__(self, messageQueue, config):
  270. self.messageQueue = messageQueue
  271. self.config = config
  272. self._paths = {} # '/path' => ('#channel', auth) where auth is either False (no authentication) or the HTTP header value for basic auth
  273. self._app = aiohttp.web.Application()
  274. self._app.add_routes([aiohttp.web.post('/{path:.+}', self.post)])
  275. self.update_config(config)
  276. def update_config(self, config):
  277. 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.__dict__.values()}
  278. needRebind = (self.config.web.host, self.config.web.port) != (config.web.host, config.web.port)
  279. self.config = config
  280. if needRebind:
  281. #TODO
  282. logging.error('Webserver host or port changes while running are currently not supported')
  283. async def run(self, stopEvent):
  284. runner = aiohttp.web.AppRunner(self._app)
  285. await runner.setup()
  286. site = aiohttp.web.TCPSite(runner, self.config.web.host, self.config.web.port)
  287. await site.start()
  288. await stopEvent.wait()
  289. await runner.cleanup()
  290. async def post(self, request):
  291. logging.info(f'Received request for {request.path!r}')
  292. try:
  293. channel, auth = self._paths[request.path]
  294. except KeyError:
  295. logging.info(f'Bad request: no path {request.path!r}')
  296. raise aiohttp.web.HTTPNotFound()
  297. if auth:
  298. authHeader = request.headers.get('Authorization')
  299. if not authHeader or authHeader != auth:
  300. logging.info(f'Bad request: authentication failed: {authHeader!r} != {auth}')
  301. raise aiohttp.web.HTTPForbidden()
  302. try:
  303. message = await request.text()
  304. except Exception as e:
  305. logging.info(f'Bad request: exception while reading request data: {e!s}')
  306. raise aiohttp.web.HTTPBadRequest() # Yes, it's always the client's fault. :-)
  307. logging.debug(f'Request payload: {message!r}')
  308. # Strip optional [CR] LF at the end of the payload
  309. if message.endswith('\r\n'):
  310. message = message[:-2]
  311. elif message.endswith('\n'):
  312. message = message[:-1]
  313. if '\r' in message or '\n' in message:
  314. logging.info('Bad request: linebreaks in message')
  315. raise aiohttp.web.HTTPBadRequest()
  316. logging.debug(f'Putting message {message!r} for {channel} into message queue')
  317. self.messageQueue.put_nowait((channel, message))
  318. raise aiohttp.web.HTTPOk()
  319. async def main():
  320. if len(sys.argv) != 2:
  321. print('Usage: http2irc.py CONFIGFILE', file = sys.stderr)
  322. sys.exit(1)
  323. configFile = sys.argv[1]
  324. config = Config(configFile)
  325. loop = asyncio.get_running_loop()
  326. messageQueue = MessageQueue()
  327. irc = IRCClient(messageQueue, config)
  328. webserver = WebServer(messageQueue, config)
  329. sigintEvent = asyncio.Event()
  330. def sigint_callback():
  331. logging.info('Got SIGINT')
  332. nonlocal sigintEvent
  333. sigintEvent.set()
  334. loop.add_signal_handler(signal.SIGINT, sigint_callback)
  335. def sigusr1_callback():
  336. logging.info('Got SIGUSR1, reloading config')
  337. nonlocal config, irc, webserver
  338. newConfig = config.reread()
  339. config = newConfig
  340. irc.update_config(config)
  341. webserver.update_config(config)
  342. loop.add_signal_handler(signal.SIGUSR1, sigusr1_callback)
  343. await asyncio.gather(irc.run(loop, sigintEvent), webserver.run(sigintEvent))
  344. if __name__ == '__main__':
  345. asyncio.run(main())