|
6 | 6 | After obtaining an auth code, the web server will automatically shut down. |
7 | 7 | """ |
8 | 8 | import logging |
| 9 | +import os |
9 | 10 | import socket |
10 | 11 | import sys |
11 | 12 | from string import Template |
@@ -38,6 +39,20 @@ def obtain_auth_code(listen_port, auth_uri=None): # Historically only used in t |
38 | 39 | ).get("code") |
39 | 40 |
|
40 | 41 |
|
| 42 | +def _is_inside_docker(): |
| 43 | + try: |
| 44 | + with open("/proc/1/cgroup") as f: # https://stackoverflow.com/a/20012536/728675 |
| 45 | + # Search keyword "/proc/pid/cgroup" in this link for the file format |
| 46 | + # https://man7.org/linux/man-pages/man7/cgroups.7.html |
| 47 | + for line in f.readlines(): |
| 48 | + cgroup_path = line.split(":", 2)[2].strip() |
| 49 | + if cgroup_path.strip() != "/": |
| 50 | + return True |
| 51 | + except IOError: |
| 52 | + pass # We are probably not running on Linux |
| 53 | + return os.path.exists("/.dockerenv") # Docker on Mac will run this line |
| 54 | + |
| 55 | + |
41 | 56 | def is_wsl(): |
42 | 57 | # "Official" way of detecting WSL: https://github.com/Microsoft/WSL/issues/423#issuecomment-221627364 |
43 | 58 | # Run `uname -a` to get 'release' without python |
@@ -165,7 +180,7 @@ def __init__(self, port=None, scheduled_actions=None): |
165 | 180 | then the receiver would call that lambda function after |
166 | 181 | waiting the response for 10 seconds. |
167 | 182 | """ |
168 | | - address = "127.0.0.1" # Hardcode, for now, Not sure what to expose, yet. |
| 183 | + address = "0.0.0.0" if _is_inside_docker() else "127.0.0.1" # Hardcode |
169 | 184 | # Per RFC 8252 (https://tools.ietf.org/html/rfc8252#section-8.3): |
170 | 185 | # * Clients should listen on the loopback network interface only. |
171 | 186 | # (It is not recommended to use "" shortcut to bind all addr.) |
@@ -283,13 +298,15 @@ def _get_auth_response(self, result, auth_uri=None, timeout=None, state=None, |
283 | 298 | logger.warning( |
284 | 299 | "Found no browser in current environment. " |
285 | 300 | "If this program is being run inside a container " |
286 | | - "which has access to host network " |
| 301 | + "which either (1) has access to host network " |
287 | 302 | "(i.e. started by `docker run --net=host -it ...`), " |
| 303 | + "or (2) published port {port} to host network " |
| 304 | + "(i.e. started by `docker run -p 127.0.0.1:{port}:{port} -it ...`), " |
288 | 305 | "you can use browser on host to visit the following link. " |
289 | 306 | "Otherwise, this auth attempt would either timeout " |
290 | 307 | "(current timeout setting is {timeout}) " |
291 | 308 | "or be aborted by CTRL+C. Auth URI: {auth_uri}".format( |
292 | | - auth_uri=_uri, timeout=timeout)) |
| 309 | + auth_uri=_uri, timeout=timeout, port=self.get_port())) |
293 | 310 | else: # Then it is the auth_uri_callback()'s job to inform the user |
294 | 311 | auth_uri_callback(_uri) |
295 | 312 |
|
|
0 commit comments