Changeset 182
- Timestamp:
- 07/27/12 03:05:02 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
etherws/trunk/etherws.py
r181 r182 55 55 import tornado.web 56 56 import tornado.ioloop 57 import tornado.websocket58 57 import tornado.httpserver 59 58 59 from tornado.websocket import WebSocketHandler 60 60 from pytun import TunTapDevice, IFF_TAP, IFF_NO_PI 61 61 … … 237 237 238 238 239 class BasicAuthMixIn(object): 240 def _execute(self, transforms, *args, **kwargs): 241 def do_execute(): 242 sp = super(BasicAuthMixIn, self) 243 return sp._execute(transforms, *args, **kwargs) 244 245 def auth_required(): 246 self.stream.write(tornado.escape.utf8( 247 'HTTP/1.1 401 Authorization Required\r\n' 248 'WWW-Authenticate: Basic realm=etherws\r\n\r\n' 249 )) 250 self.stream.close() 251 252 try: 253 if not self._htpasswd: 254 return do_execute() 255 256 creds = self.request.headers.get('Authorization') 257 258 if not creds or not creds.startswith('Basic '): 259 return auth_required() 260 261 name, passwd = base64.b64decode(creds[6:]).split(':', 1) 262 263 if self._htpasswd.load().auth(name, passwd): 264 return do_execute() 265 except: 266 traceback.print_exc() 267 268 return auth_required() 269 270 239 271 class TapHandler(DebugMixIn): 240 272 READ_SIZE = 65535 … … 296 328 297 329 298 class EtherWebSocketHandler( tornado.websocket.WebSocketHandler, DebugMixIn):330 class EtherWebSocketHandler(DebugMixIn, BasicAuthMixIn, WebSocketHandler): 299 331 def __init__(self, app, req, switch, htpasswd=None, debug=False): 300 332 super(EtherWebSocketHandler, self).__init__(app, req) … … 316 348 self._switch.unregister_port(self) 317 349 self.dprintf('disconnected: %s\n', lambda: self.request.remote_ip) 318 319 def _execute(self, transforms, *args, **kwargs):320 def do_execute():321 sp = super(EtherWebSocketHandler, self)322 return sp._execute(transforms, *args, **kwargs)323 324 def auth_required():325 self.stream.write(tornado.escape.utf8(326 'HTTP/1.1 401 Authorization Required\r\n'327 'WWW-Authenticate: Basic realm=etherws\r\n\r\n'328 ))329 self.stream.close()330 331 try:332 if not self._htpasswd:333 return do_execute()334 335 creds = self.request.headers.get('Authorization')336 337 if not creds or not creds.startswith('Basic '):338 return auth_required()339 340 name, passwd = base64.b64decode(creds[6:]).split(':', 1)341 342 if self._htpasswd.load().auth(name, passwd):343 return do_execute()344 except:345 traceback.print_exc()346 347 return auth_required()348 350 349 351
Note: See TracChangeset
for help on using the changeset viewer.