Changeset 40:2bcb10fb4da4 for wokkel/component.py
- Timestamp:
- Oct 18, 2008, 12:18:02 PM (14 years ago)
- Branch:
- default
- Convert:
- svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@112
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
wokkel/component.py
r39 r40 134 134 """ 135 135 Authenticator for accepting components. 136 """ 136 137 @ivar secret: The shared used to authorized incoming component connections. 138 @type secret: C{str}. 139 """ 140 137 141 namespace = NS_COMPONENT_ACCEPT 138 142 … … 143 147 144 148 def associateWithStream(self, xs): 149 """ 150 Associate the authenticator with a stream. 151 152 This sets the stream's version to 0.0, because the XEP-0114 component 153 protocol was not designed for XMPP 1.0. 154 """ 145 155 xs.version = (0, 0) 146 156 xmlstream.ListenAuthenticator.associateWithStream(self, xs) … … 148 158 149 159 def streamStarted(self, rootElement): 160 """ 161 Called by the stream when it has started. 162 163 This examines the default namespace of the incoming stream and whether 164 there is a requested hostname for the component. Then it generates a 165 stream identifier, sends a response header and adds an observer for 166 the first incoming element, triggering L{onElement}. 167 """ 168 150 169 xmlstream.ListenAuthenticator.streamStarted(self, rootElement) 170 171 # Compatibility fix 172 if not self.xmlstream.sid: 173 from twisted.python import randbytes 174 self.xmlstream.sid = randbytes.secureRandom(8).encode('hex') 151 175 152 176 if rootElement.defaultUri != self.namespace: … … 156 180 157 181 # self.xmlstream.thisEntity is set to the address the component 158 # wants to assume. This should probably be checked.182 # wants to assume. 159 183 if not self.xmlstream.thisEntity: 160 184 exc = error.StreamError('improper-addressing') … … 162 186 return 163 187 164 self.xmlstream.sid = 'random' # FIXME165 166 188 self.xmlstream.sendHeader() 167 189 self.xmlstream.addOnetimeObserver('/*', self.onElement) … … 169 191 170 192 def onElement(self, element): 193 """ 194 Called on incoming XML Stanzas. 195 196 The very first element received should be a request for handshake. 197 Otherwise, the stream is dropped with a 'not-authorized' error. If a 198 handshake request was received, the hash is extracted and passed to 199 L{onHandshake}. 200 """ 171 201 if (element.uri, element.name) == (self.namespace, 'handshake'): 172 202 self.onHandshake(unicode(element)) 173 203 else: 174 exc = error. streamError('not-authorized')204 exc = error.StreamError('not-authorized') 175 205 self.xmlstream.sendStreamError(exc) 176 206 177 207 178 208 def onHandshake(self, handshake): 209 """ 210 Called upon receiving the handshake request. 211 212 This checks that the given hash in C{handshake} is equal to a 213 calculated hash, responding with a handshake reply or a stream error. 214 If the handshake was ok, the stream is authorized, and XML Stanzas may 215 be exchanged. 216 """ 179 217 calculatedHash = xmlstream.hashPassword(self.xmlstream.sid, self.secret) 180 218 if handshake != calculatedHash: … … 188 226 189 227 190 class Router Service(service.Service):191 """ 192 XMPP Server's Router Service.193 194 This service connects the different components of the XMPP service and195 routesmessages between them based on the given routing table.228 class Router(object): 229 """ 230 XMPP Server's Router. 231 232 A router connects the different components of the XMPP service and routes 233 messages between them based on the given routing table. 196 234 197 235 Connected components are trusted to have correct addressing in the … … 251 289 @type stanza: L{domish.Element}. 252 290 """ 253 if not list(stanza.elements()):254 return255 256 291 destination = JID(stanza['to']) 257 292 … … 265 300 266 301 267 class ComponentServer(service.Service):268 """ 269 XMPP Component Server service.270 271 This serviceaccepts XMPP external component connections and makes302 class XMPPComponentServerFactory(xmlstream.XmlStreamServerFactory): 303 """ 304 XMPP Component Server factory. 305 306 This factory accepts XMPP external component connections and makes 272 307 the router service route traffic for a component's bound domain 273 308 to that component. … … 276 311 logTraffic = False 277 312 278 def __init__(self, router, port=5347,secret='secret'):313 def __init__(self, router, secret='secret'): 279 314 self.router = router 280 self.port = port281 315 self.secret = secret 282 316 … … 284 318 return ListenComponentAuthenticator(self.secret) 285 319 286 self.factory = xmlstream.XmlStreamServerFactory(authenticatorFactory)287 self. factory.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT,288 289 self. factory.addBootstrap(xmlstream.STREAM_AUTHD_EVENT,290 320 xmlstream.XmlStreamServerFactory.__init__(self, authenticatorFactory) 321 self.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT, 322 self.makeConnection) 323 self.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, 324 self.connectionInitialized) 291 325 292 326 self.serial = 0 293 294 295 def startService(self):296 service.Service.startService(self)297 reactor.listenTCP(self.port, self.factory)298 327 299 328
Note: See TracChangeset
for help on using the changeset viewer.