From 0816001b240c759480f74440a1df9fa038ddfacb Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 27 Jun 2022 13:25:31 +0100 Subject: [PATCH] Add option to disable ipv6. --- main.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 3d86c8d..07040de 100644 --- a/main.go +++ b/main.go @@ -24,13 +24,20 @@ var h3client = &http.Client{ Transport: &http3.RoundTripper{}, } +var dialer = &net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, +} + // http/2 client var h2client = &http.Client{ Transport: &http.Transport{ - Dial: (&net.Dialer{ - Timeout: 30 * time.Second, - KeepAlive: 30 * time.Second, - }).Dial, + Dial: func(network, addr string) (net.Conn, error) { + if disable_ipv6 { + network = "tcp4" + } + return dialer.Dial(network, addr) + }, TLSHandshakeTimeout: 10 * time.Second, ResponseHeaderTimeout: 20 * time.Second, ExpectContinueTimeout: 1 * time.Second, @@ -70,6 +77,8 @@ var path_prefix = "" var manifest_re = regexp.MustCompile(`(?m)URI="([^"]+)"`) +var disable_ipv6 = false + type requesthandler struct{} func (*requesthandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { @@ -292,6 +301,8 @@ func RelativeUrl(in string) (newurl string) { func main() { path_prefix = os.Getenv("PREFIX_PATH") + disable_ipv6 = os.Getenv("DISABLE_IPV6") == "1" + socket := "socket" + string(os.PathSeparator) + "http-proxy.sock" syscall.Unlink(socket) listener, err := net.Listen("unix", socket)