-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
80 lines (56 loc) · 2.05 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import logging
import os
import ssl
import threading
try:
import http.client as http_client
except ImportError:
import httplib as http_client
try:
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
except ImportError:
from http.server import HTTPServer, BaseHTTPRequestHandler
try:
import Queue
except ImportError:
import queue as Queue
try:
from urlparse import urlparse, parse_qs
except ImportError:
from urllib.parse import urlparse, parse_qs
def enable_requests_logging():
http_client.HTTPConnection.debuglevel = 4
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger('requests.packages.urllib3')
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
def is_remote_session():
return os.environ.get('SSH_TTY', os.environ.get('SSH_CONNECTION'))
class RedirectHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b'You\'re all set, you can close this window!')
code = parse_qs(urlparse(self.path).query).get('code', [''])[0]
self.server.return_code(code)
def log_message(self, format, *args):
return
class RedirectHTTPServer(HTTPServer, object):
def __init__(self, listen, handler_class, https=False):
super(RedirectHTTPServer, self).__init__(listen, handler_class)
self._auth_code_queue = Queue.Queue()
if https:
self.socket = ssl.wrap_socket(
self.socket, certfile='./ssl/server.pem', server_side=True)
def return_code(self, code):
self._auth_code_queue.put_nowait(code)
def wait_for_code(self):
return self._auth_code_queue.get(block=True)
def start_local_server(listen=('', 4443)):
server = RedirectHTTPServer(listen, RedirectHandler)
thread = threading.Thread(target=server.serve_forever)
thread.daemon = True
thread.start()
return server