GHC 2020-04-03

1 comment.

, https://git.io/JvFFP in jarun/googler
Very interesting observation. Now that you mentioned it I observe the same thing on my perma-blocked Linode instances.

Thanks to googler sticking to PSL and operating low-level (as low as `socket`), we can fairly easily provide curl-style `-4/-6` switches without much effort, and without even monkeypatching `sock.getaddrinfo` or something (although I personally always welcome judicial uses of monkeypatching). I'll post a patch for the options later.

And below is a crude patch to ensure we only connect over IPv4, so no need to disable anything system-wide:

```diff
diff --git a/googler b/googler
index 90ad482..033da2e 100755
--- a/googler
+++ b/googler
@@ -1517,8 +1517,7 @@ class HardenedHTTPSConnection(HTTPSConnection):
         HTTPSConnection.__init__(self, host, **kwargs)
 
     def connect(self, notweak=False):
-        sock = socket.create_connection((self.host, self.port),
-                                        self.timeout, self.source_address)
+        sock = self.create_socket_connection()
 
         # Optimizations not available on OS X
         if not notweak and sys.platform.startswith('linux'):
@@ -1554,6 +1553,36 @@ class HardenedHTTPSConnection(HTTPSConnection):
         # Fallback
         HTTPSConnection.connect(self)
 
+    def create_socket_connection(self):
+        err = None
+        for res in socket.getaddrinfo(self.host, self.port, socket.AF_INET, socket.SOCK_STREAM):
+            af, socktype, proto, canonname, sa = res
+            sock = None
+            try:
+                sock = socket.socket(af, socktype, proto)
+                if self.timeout is not None:
+                    sock.settimeout(self.timeout)
+                if self.source_address:
+                    sock.bind(self.source_address)
+                sock.connect(sa)
+                # Break explicitly a reference cycle
+                err = None
+                return sock
+
+            except socket.error as _:
+                err = _
+                if sock is not None:
+                    sock.close()
+
+        if err is not None:
+            try:
+                raise err
+            finally:
+                # Break explicitly a reference cycle
+                err = None
+        else:
+            raise socket.error("getaddrinfo returns an empty list")
+
 
 class GoogleUrl(object):
     """
```

Note: If you connect over a proxy, this only affects your connection to the proxy server, not the proxy server to Google.