feat(http client): Add environment variable to use a proxy (http, socks5, socks5h, etc...)
All checks were successful
CI / build (push) Successful in 1m12s

This commit is contained in:
Fijxu 2025-01-06 22:11:29 -03:00
parent 3fc14dd18b
commit f66ae3187f
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4

12
main.go
View file

@ -9,6 +9,7 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"net/url"
"os" "os"
"regexp" "regexp"
"runtime" "runtime"
@ -29,6 +30,8 @@ var (
rl = flag.Int("r", 8000, "Read limit in Kbps") rl = flag.Int("r", 8000, "Read limit in Kbps")
) )
// QUIC doesn't seem to support HTTP nor SOCKS5 proxies due to how it's made.
// (Since it's UDP)
var h3client = &http.Client{ var h3client = &http.Client{
Transport: &http3.Transport{}, Transport: &http3.Transport{},
Timeout: 10 * time.Second, Timeout: 10 * time.Second,
@ -39,6 +42,8 @@ var dialer = &net.Dialer{
KeepAlive: 30 * time.Second, KeepAlive: 30 * time.Second,
} }
var proxy string
// http/2 client // http/2 client
var h2client = &http.Client{ var h2client = &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
@ -60,6 +65,12 @@ var h2client = &http.Client{
MaxConnsPerHost: 0, MaxConnsPerHost: 0,
MaxIdleConnsPerHost: 10, MaxIdleConnsPerHost: 10,
MaxIdleConns: 0, MaxIdleConns: 0,
Proxy: func(r *http.Request) (*url.URL, error) {
if proxy != "" {
return url.Parse(proxy)
}
return nil, nil
},
}, },
} }
@ -405,6 +416,7 @@ func main() {
if host == "" { if host == "" {
host = defaultHost host = defaultHost
} }
proxy = os.Getenv("PROXY")
flag.BoolVar(&https, "https", https, "Use built-in https server (recommended)") flag.BoolVar(&https, "https", https, "Use built-in https server (recommended)")
flag.BoolVar(&h3c, "h3c", h3c, "Use HTTP/3 for client requests (high CPU usage)") flag.BoolVar(&h3c, "h3c", h3c, "Use HTTP/3 for client requests (high CPU usage)")