From c65b37c5c40a373926781de9a2f1ac32ecca2361 Mon Sep 17 00:00:00 2001 From: Solderpunk Date: Tue, 13 Aug 2019 13:04:07 +0300 Subject: [PATCH] Make Gopher proxy configurable via 'set'. --- av98.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/av98.py b/av98.py index 6ed045d..136a50a 100755 --- a/av98.py +++ b/av98.py @@ -89,9 +89,6 @@ _ITEMTYPE_COLORS = { CRLF = '\r\n' -_GOPHER_PROXY_HOST = "localhost" -_GOPHER_PROXY_PORT = 1965 - # Lightweight representation of an item in Geminispace GeminiItem = collections.namedtuple("GeminiItem", ("scheme", "host", "port", "path", "name")) @@ -199,6 +196,7 @@ class GeminiClient(cmd.Cmd): "debug" : False, "ipv6" : False, "timeout" : 10, + "gopher_proxy" : "localhost:1965", } self.log = { @@ -345,9 +343,13 @@ Slow internet connection? Use 'set timeout' to be more patient.""") """Send a selector to a given host and port. Returns the resolved address and binary file with the reply.""" if gi.scheme == "gemini": - addresses = self._get_addresses(gi.host, gi.port) + # For Gemini requests, connect to the host and port specified in the URL + host, port = gi.host, gi.port elif gi.scheme == "gopher": - addresses = self._get_addresses(_GOPHER_PROXY_HOST, _GOPHER_PROXY_PORT) + # For Gopher requests, use the configured proxy + host, port = self.options["gopher_proxy"].rsplit(":", 1) + self._debug("Using gopher proxy: " + self.options["gopher_proxy"]) + addresses = self._get_addresses(host, port) # Connect to remote host by any address possible err = None for address in addresses: @@ -553,16 +555,27 @@ Slow internet connection? Use 'set timeout' to be more patient.""") for option in sorted(self.options.keys()): print("%s %s" % (option, self.options[option])) elif len(line.split()) == 1: + # Show current value of one specific setting option = line.strip() if option in self.options: print("%s %s" % (option, self.options[option])) else: print("Unrecognised option %s" % option) else: + # Set value of one specific setting option, value = line.split(" ", 1) if option not in self.options: print("Unrecognised option %s" % option) return + # Validate / convert values + if option == "gopher_proxy": + if ":" not in value: + value += ":1965" + else: + host, port = value.rsplit(":",1) + if not port.isnumeric(): + print("Invalid proxy port %s" % port) + return elif value.isnumeric(): value = int(value) elif value.lower() == "false":