[96] | 1 | # Copyright (c) Ralph Meijer. |
---|
[91] | 2 | # See LICENSE for details. |
---|
| 3 | |
---|
| 4 | """ |
---|
| 5 | XMPP Component Service. |
---|
| 6 | |
---|
| 7 | This provides an XMPP server that accepts External Components connections |
---|
| 8 | and accepts and initiates server-to-server connections for the specified |
---|
| 9 | domain(s). |
---|
| 10 | """ |
---|
| 11 | |
---|
[209] | 12 | from __future__ import division, absolute_import |
---|
| 13 | |
---|
[91] | 14 | from twisted.application import service, strports |
---|
| 15 | from twisted.python import usage |
---|
| 16 | from twisted.words.protocols.jabber import component |
---|
| 17 | from wokkel import server |
---|
| 18 | |
---|
| 19 | class Options(usage.Options): |
---|
| 20 | |
---|
| 21 | optParameters = [ |
---|
| 22 | ('component-port', None, 'tcp:5347:interface=127.0.0.1', |
---|
| 23 | 'Port components connect to'), |
---|
| 24 | ('component-secret', None, 'secret', |
---|
| 25 | 'Secret components use to connect'), |
---|
| 26 | ('server-port', None, 'tcp:5269', |
---|
| 27 | 'Port other servers connect to'), |
---|
| 28 | ('server-secret', None, None, |
---|
| 29 | 'Shared secret for dialback verification'), |
---|
| 30 | ] |
---|
| 31 | |
---|
| 32 | optFlags = [ |
---|
| 33 | ('verbose', 'v', 'Log traffic'), |
---|
| 34 | ] |
---|
| 35 | |
---|
| 36 | def __init__(self): |
---|
| 37 | usage.Options.__init__(self) |
---|
| 38 | self['domains'] = set() |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | def opt_domain(self, domain): |
---|
| 42 | """ |
---|
| 43 | Domain to accept server connections for. Repeat for more domains. |
---|
| 44 | """ |
---|
| 45 | self['domains'].add(domain) |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | def postOptions(self): |
---|
| 49 | if not self['domains']: |
---|
| 50 | raise usage.UsageError('Need at least one domain') |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | def makeService(config): |
---|
| 55 | s = service.MultiService() |
---|
| 56 | |
---|
| 57 | router = component.Router() |
---|
| 58 | |
---|
| 59 | # Set up the XMPP server service |
---|
| 60 | |
---|
| 61 | serverService = server.ServerService(router, secret=config['server-secret']) |
---|
| 62 | serverService.domains = config['domains'] |
---|
| 63 | serverService.logTraffic = config['verbose'] |
---|
| 64 | |
---|
| 65 | # Hook up XMPP server-to-server service |
---|
| 66 | s2sFactory = server.XMPPS2SServerFactory(serverService) |
---|
| 67 | s2sFactory.logTraffic = config['verbose'] |
---|
| 68 | s2sService = strports.service(config['server-port'], s2sFactory) |
---|
| 69 | s2sService.setServiceParent(s) |
---|
| 70 | |
---|
| 71 | # Hook up XMPP external server-side component service |
---|
| 72 | cFactory = component.XMPPComponentServerFactory(router, |
---|
| 73 | config['component-secret']) |
---|
| 74 | |
---|
| 75 | cFactory.logTraffic = config['verbose'] |
---|
| 76 | cServer = strports.service(config['component-port'], cFactory) |
---|
| 77 | cServer.setServiceParent(s) |
---|
| 78 | |
---|
| 79 | return s |
---|