mirror of
https://github.com/fish-shell/fish-shell.git
synced 2025-03-12 05:15:24 +08:00
Make fish_config work correctly when IPv6 is disabled in the kernel
Fixes #1754
This commit is contained in:
parent
49b49d7ed4
commit
a83323705d
@ -6,6 +6,7 @@ import sys
|
|||||||
import multiprocessing.pool
|
import multiprocessing.pool
|
||||||
import os
|
import os
|
||||||
import operator
|
import operator
|
||||||
|
import socket
|
||||||
IS_PY2 = sys.version_info[0] == 2
|
IS_PY2 = sys.version_info[0] == 2
|
||||||
|
|
||||||
if IS_PY2:
|
if IS_PY2:
|
||||||
@ -18,6 +19,14 @@ else:
|
|||||||
import socketserver as SocketServer
|
import socketserver as SocketServer
|
||||||
from urllib.parse import parse_qs
|
from urllib.parse import parse_qs
|
||||||
|
|
||||||
|
# Check to see if IPv6 is enabled in the kernel
|
||||||
|
HAS_IPV6 = True
|
||||||
|
try:
|
||||||
|
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
|
||||||
|
s.close()
|
||||||
|
except:
|
||||||
|
HAS_IPV6 = False
|
||||||
|
|
||||||
# Disable CLI web browsers
|
# Disable CLI web browsers
|
||||||
term = os.environ.pop('TERM', None)
|
term = os.environ.pop('TERM', None)
|
||||||
import webbrowser
|
import webbrowser
|
||||||
@ -419,7 +428,7 @@ class FishConfigTCPServer(SocketServer.TCPServer):
|
|||||||
"""TCPServer that only accepts connections from localhost (IPv4/IPv6)."""
|
"""TCPServer that only accepts connections from localhost (IPv4/IPv6)."""
|
||||||
WHITELIST = set(['::1', '::ffff:127.0.0.1', '127.0.0.1'])
|
WHITELIST = set(['::1', '::ffff:127.0.0.1', '127.0.0.1'])
|
||||||
|
|
||||||
address_family = socket.AF_INET6
|
address_family = socket.AF_INET6 if HAS_IPV6 else socket.AF_INET
|
||||||
|
|
||||||
def verify_request(self, request, client_address):
|
def verify_request(self, request, client_address):
|
||||||
return client_address[0] in FishConfigTCPServer.WHITELIST
|
return client_address[0] in FishConfigTCPServer.WHITELIST
|
||||||
@ -910,16 +919,18 @@ authkey = binascii.b2a_hex(os.urandom(16)).decode('ascii')
|
|||||||
|
|
||||||
# Try to find a suitable port
|
# Try to find a suitable port
|
||||||
PORT = 8000
|
PORT = 8000
|
||||||
|
HOST = "::" if HAS_IPV6 else "localhost"
|
||||||
while PORT <= 9000:
|
while PORT <= 9000:
|
||||||
try:
|
try:
|
||||||
Handler = FishConfigHTTPRequestHandler
|
Handler = FishConfigHTTPRequestHandler
|
||||||
httpd = FishConfigTCPServer(("::", PORT), Handler)
|
httpd = FishConfigTCPServer((HOST, PORT), Handler)
|
||||||
# Success
|
# Success
|
||||||
break
|
break
|
||||||
except socket.error:
|
except socket.error:
|
||||||
err_type, err_value = sys.exc_info()[:2]
|
err_type, err_value = sys.exc_info()[:2]
|
||||||
# str(err_value) handles Python3 correctly
|
# str(err_value) handles Python3 correctly
|
||||||
if 'Address already in use' not in str(err_value):
|
if 'Address already in use' not in str(err_value):
|
||||||
|
print(str(err_value))
|
||||||
break
|
break
|
||||||
PORT += 1
|
PORT += 1
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user