Add projects

This commit is contained in:
Alois 2026-08-24 00:10:41 +02:00
commit 8b607dd700
Signed by: alois
SSH key fingerprint: SHA256:GBzT2DXvAuGV9XIV5W3WrzVpjU54FThmxHXdbz95J24
1802 changed files with 503346 additions and 2 deletions

View file

@ -0,0 +1,294 @@
package proxyutil
import (
"bufio"
"context"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"golang.org/x/net/proxy"
)
// Mode describes how a proxy setting should be interpreted.
type Mode int
const (
// ModeInherit means no explicit proxy behavior was configured.
ModeInherit Mode = iota
// ModeDirect means outbound requests must bypass proxies explicitly.
ModeDirect
// ModeProxy means a concrete proxy URL was configured.
ModeProxy
// ModeInvalid means the proxy setting is present but malformed or unsupported.
ModeInvalid
)
// Setting is the normalized interpretation of a proxy configuration value.
type Setting struct {
Raw string
Mode Mode
URL *url.URL
}
// Parse normalizes a proxy configuration value into inherit, direct, or proxy modes.
func Parse(raw string) (Setting, error) {
trimmed := strings.TrimSpace(raw)
setting := Setting{Raw: trimmed}
if trimmed == "" {
setting.Mode = ModeInherit
return setting, nil
}
if strings.EqualFold(trimmed, "direct") || strings.EqualFold(trimmed, "none") {
setting.Mode = ModeDirect
return setting, nil
}
parsedURL, errParse := url.Parse(trimmed)
if errParse != nil {
setting.Mode = ModeInvalid
return setting, fmt.Errorf("parse proxy URL failed")
}
if parsedURL.Scheme == "" || parsedURL.Host == "" {
setting.Mode = ModeInvalid
return setting, fmt.Errorf("proxy URL missing scheme/host")
}
switch parsedURL.Scheme {
case "socks5", "socks5h", "http", "https":
setting.Mode = ModeProxy
setting.URL = parsedURL
return setting, nil
default:
setting.Mode = ModeInvalid
return setting, fmt.Errorf("unsupported proxy scheme: %s", parsedURL.Scheme)
}
}
func cloneDefaultTransport() *http.Transport {
if transport, ok := http.DefaultTransport.(*http.Transport); ok && transport != nil {
return transport.Clone()
}
return &http.Transport{}
}
// NewDirectTransport returns a transport that bypasses environment proxies.
func NewDirectTransport() *http.Transport {
clone := cloneDefaultTransport()
clone.Proxy = nil
return clone
}
// BuildHTTPTransport constructs an HTTP transport for the provided proxy setting.
func BuildHTTPTransport(raw string) (*http.Transport, Mode, error) {
setting, errParse := Parse(raw)
if errParse != nil {
return nil, setting.Mode, errParse
}
switch setting.Mode {
case ModeInherit:
return nil, setting.Mode, nil
case ModeDirect:
return NewDirectTransport(), setting.Mode, nil
case ModeProxy:
if setting.URL.Scheme == "socks5" || setting.URL.Scheme == "socks5h" {
var proxyAuth *proxy.Auth
if setting.URL.User != nil {
username := setting.URL.User.Username()
password, _ := setting.URL.User.Password()
proxyAuth = &proxy.Auth{User: username, Password: password}
}
dialer, errSOCKS5 := proxy.SOCKS5("tcp", setting.URL.Host, proxyAuth, proxy.Direct)
if errSOCKS5 != nil {
return nil, setting.Mode, fmt.Errorf("create SOCKS5 dialer failed: %w", errSOCKS5)
}
transport := cloneDefaultTransport()
transport.Proxy = nil
transport.DialContext = func(_ context.Context, network, addr string) (net.Conn, error) {
return dialer.Dial(network, addr)
}
return transport, setting.Mode, nil
}
transport := cloneDefaultTransport()
transport.Proxy = http.ProxyURL(setting.URL)
return transport, setting.Mode, nil
default:
return nil, setting.Mode, nil
}
}
// BuildDialer constructs a proxy dialer for settings that operate at the connection layer.
func BuildDialer(raw string) (proxy.Dialer, Mode, error) {
setting, errParse := Parse(raw)
if errParse != nil {
return nil, setting.Mode, errParse
}
switch setting.Mode {
case ModeInherit:
return nil, setting.Mode, nil
case ModeDirect:
return proxy.Direct, setting.Mode, nil
case ModeProxy:
if setting.URL.Scheme == "http" || setting.URL.Scheme == "https" {
return &httpConnectDialer{proxyURL: setting.URL, dialer: proxy.Direct}, setting.Mode, nil
}
dialer, errDialer := proxy.FromURL(setting.URL, proxy.Direct)
if errDialer != nil {
return nil, setting.Mode, fmt.Errorf("create proxy dialer failed: %w", errDialer)
}
return dialer, setting.Mode, nil
default:
return nil, setting.Mode, nil
}
}
type httpConnectDialer struct {
proxyURL *url.URL
dialer proxy.Dialer
}
func (d *httpConnectDialer) Dial(network, addr string) (net.Conn, error) {
return d.DialContext(context.Background(), network, addr)
}
func (d *httpConnectDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
if ctx == nil {
ctx = context.Background()
}
contextDialer, ok := d.dialer.(proxy.ContextDialer)
if !ok {
return nil, errors.New("HTTP proxy base dialer does not support context cancellation")
}
proxyConn, errDial := contextDialer.DialContext(ctx, network, proxyDialAddr(d.proxyURL))
if errDial != nil {
return nil, fmt.Errorf("dial HTTP proxy failed: %w", errDial)
}
conn := proxyConn
cancelDone := make(chan struct{})
stopCancel := context.AfterFunc(ctx, func() {
_ = proxyConn.Close()
close(cancelDone)
})
defer func() {
if !stopCancel() {
<-cancelDone
}
}()
if d.proxyURL.Scheme == "https" {
tlsConn := tls.Client(conn, &tls.Config{ServerName: d.proxyURL.Hostname()})
if errHandshake := tlsConn.HandshakeContext(ctx); errHandshake != nil {
if errClose := conn.Close(); errClose != nil {
return nil, fmt.Errorf("HTTPS proxy TLS handshake failed: %w; close failed: %v", errHandshake, errClose)
}
return nil, fmt.Errorf("HTTPS proxy TLS handshake failed: %w", errHandshake)
}
conn = tlsConn
}
req := (&http.Request{
Method: http.MethodConnect,
URL: &url.URL{Host: addr},
Host: addr,
Header: make(http.Header),
}).WithContext(ctx)
if d.proxyURL.User != nil {
req.Header.Set("Proxy-Authorization", proxyAuthorization(d.proxyURL.User))
}
if errWrite := req.Write(conn); errWrite != nil {
if errClose := conn.Close(); errClose != nil {
return nil, fmt.Errorf("write CONNECT request failed: %w; close failed: %v", errWrite, errClose)
}
return nil, fmt.Errorf("write CONNECT request failed: %w", errWrite)
}
reader := bufio.NewReader(conn)
resp, errRead := http.ReadResponse(reader, req)
if errRead != nil {
if errClose := conn.Close(); errClose != nil {
return nil, fmt.Errorf("read CONNECT response failed: %w; close failed: %v", errRead, errClose)
}
return nil, fmt.Errorf("read CONNECT response failed: %w", errRead)
}
if resp.StatusCode != http.StatusOK {
if resp.Body != nil {
_ = resp.Body.Close()
}
if errClose := conn.Close(); errClose != nil {
return nil, fmt.Errorf("proxy CONNECT returned status %s; close failed: %v", resp.Status, errClose)
}
return nil, fmt.Errorf("proxy CONNECT returned status %s", resp.Status)
}
if errContext := ctx.Err(); errContext != nil {
if errClose := conn.Close(); errClose != nil && !errors.Is(errClose, net.ErrClosed) {
return nil, fmt.Errorf("HTTP proxy context ended: %w; close failed: %v", errContext, errClose)
}
return nil, errContext
}
if reader.Buffered() > 0 {
return &bufferedConn{Conn: conn, reader: reader}, nil
}
return conn, nil
}
func proxyDialAddr(proxyURL *url.URL) string {
port := proxyURL.Port()
if port == "" {
port = "80"
if proxyURL.Scheme == "https" {
port = "443"
}
}
return net.JoinHostPort(proxyURL.Hostname(), port)
}
func proxyAuthorization(user *url.Userinfo) string {
username := user.Username()
password, _ := user.Password()
encoded := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
return "Basic " + encoded
}
// Redact returns a log-safe proxy URL with credentials and path-like data removed.
func Redact(raw string) string {
trimmed := strings.TrimSpace(raw)
if trimmed == "" {
return ""
}
parsedURL, errParse := url.Parse(trimmed)
if errParse != nil || parsedURL.Scheme == "" || parsedURL.Host == "" {
return "<invalid proxy URL>"
}
redacted := &url.URL{
Scheme: parsedURL.Scheme,
Host: parsedURL.Host,
}
if parsedURL.User != nil {
redacted.User = url.User("redacted")
}
return redacted.String()
}
type bufferedConn struct {
net.Conn
reader *bufio.Reader
}
func (c *bufferedConn) Read(p []byte) (int, error) {
if c.reader.Buffered() > 0 {
return c.reader.Read(p)
}
return c.Conn.Read(p)
}

View file

@ -0,0 +1,397 @@
package proxyutil
import (
"bufio"
"context"
"encoding/base64"
"fmt"
"io"
"net"
"net/http"
"strings"
"testing"
"time"
)
func mustDefaultTransport(t *testing.T) *http.Transport {
t.Helper()
transport, ok := http.DefaultTransport.(*http.Transport)
if !ok || transport == nil {
t.Fatal("http.DefaultTransport is not an *http.Transport")
}
return transport
}
func TestParse(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
want Mode
wantErr bool
}{
{name: "inherit", input: "", want: ModeInherit},
{name: "direct", input: "direct", want: ModeDirect},
{name: "none", input: "none", want: ModeDirect},
{name: "http", input: "http://proxy.example.com:8080", want: ModeProxy},
{name: "https", input: "https://proxy.example.com:8443", want: ModeProxy},
{name: "socks5", input: "socks5://proxy.example.com:1080", want: ModeProxy},
{name: "socks5h", input: "socks5h://proxy.example.com:1080", want: ModeProxy},
{name: "invalid", input: "bad-value", want: ModeInvalid, wantErr: true},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
setting, errParse := Parse(tt.input)
if tt.wantErr && errParse == nil {
t.Fatal("expected error, got nil")
}
if !tt.wantErr && errParse != nil {
t.Fatalf("unexpected error: %v", errParse)
}
if setting.Mode != tt.want {
t.Fatalf("mode = %d, want %d", setting.Mode, tt.want)
}
})
}
}
func TestBuildHTTPTransportDirectBypassesProxy(t *testing.T) {
t.Parallel()
transport, mode, errBuild := BuildHTTPTransport("direct")
if errBuild != nil {
t.Fatalf("BuildHTTPTransport returned error: %v", errBuild)
}
if mode != ModeDirect {
t.Fatalf("mode = %d, want %d", mode, ModeDirect)
}
if transport == nil {
t.Fatal("expected transport, got nil")
}
if transport.Proxy != nil {
t.Fatal("expected direct transport to disable proxy function")
}
}
func TestBuildHTTPTransportHTTPProxy(t *testing.T) {
t.Parallel()
transport, mode, errBuild := BuildHTTPTransport("http://proxy.example.com:8080")
if errBuild != nil {
t.Fatalf("BuildHTTPTransport returned error: %v", errBuild)
}
if mode != ModeProxy {
t.Fatalf("mode = %d, want %d", mode, ModeProxy)
}
if transport == nil {
t.Fatal("expected transport, got nil")
}
req, errRequest := http.NewRequest(http.MethodGet, "https://example.com", nil)
if errRequest != nil {
t.Fatalf("http.NewRequest returned error: %v", errRequest)
}
proxyURL, errProxy := transport.Proxy(req)
if errProxy != nil {
t.Fatalf("transport.Proxy returned error: %v", errProxy)
}
if proxyURL == nil || proxyURL.String() != "http://proxy.example.com:8080" {
t.Fatalf("proxy URL = %v, want http://proxy.example.com:8080", proxyURL)
}
defaultTransport := mustDefaultTransport(t)
if transport.ForceAttemptHTTP2 != defaultTransport.ForceAttemptHTTP2 {
t.Fatalf("ForceAttemptHTTP2 = %v, want %v", transport.ForceAttemptHTTP2, defaultTransport.ForceAttemptHTTP2)
}
if transport.IdleConnTimeout != defaultTransport.IdleConnTimeout {
t.Fatalf("IdleConnTimeout = %v, want %v", transport.IdleConnTimeout, defaultTransport.IdleConnTimeout)
}
if transport.TLSHandshakeTimeout != defaultTransport.TLSHandshakeTimeout {
t.Fatalf("TLSHandshakeTimeout = %v, want %v", transport.TLSHandshakeTimeout, defaultTransport.TLSHandshakeTimeout)
}
}
func TestBuildHTTPTransportSOCKS5ProxyInheritsDefaultTransportSettings(t *testing.T) {
t.Parallel()
transport, mode, errBuild := BuildHTTPTransport("socks5://proxy.example.com:1080")
if errBuild != nil {
t.Fatalf("BuildHTTPTransport returned error: %v", errBuild)
}
if mode != ModeProxy {
t.Fatalf("mode = %d, want %d", mode, ModeProxy)
}
if transport == nil {
t.Fatal("expected transport, got nil")
}
if transport.Proxy != nil {
t.Fatal("expected SOCKS5 transport to bypass http proxy function")
}
defaultTransport := mustDefaultTransport(t)
if transport.ForceAttemptHTTP2 != defaultTransport.ForceAttemptHTTP2 {
t.Fatalf("ForceAttemptHTTP2 = %v, want %v", transport.ForceAttemptHTTP2, defaultTransport.ForceAttemptHTTP2)
}
if transport.IdleConnTimeout != defaultTransport.IdleConnTimeout {
t.Fatalf("IdleConnTimeout = %v, want %v", transport.IdleConnTimeout, defaultTransport.IdleConnTimeout)
}
if transport.TLSHandshakeTimeout != defaultTransport.TLSHandshakeTimeout {
t.Fatalf("TLSHandshakeTimeout = %v, want %v", transport.TLSHandshakeTimeout, defaultTransport.TLSHandshakeTimeout)
}
}
func TestBuildHTTPTransportSOCKS5HProxy(t *testing.T) {
t.Parallel()
transport, mode, errBuild := BuildHTTPTransport("socks5h://proxy.example.com:1080")
if errBuild != nil {
t.Fatalf("BuildHTTPTransport returned error: %v", errBuild)
}
if mode != ModeProxy {
t.Fatalf("mode = %d, want %d", mode, ModeProxy)
}
if transport == nil {
t.Fatal("expected transport, got nil")
}
if transport.Proxy != nil {
t.Fatal("expected SOCKS5H transport to bypass http proxy function")
}
if transport.DialContext == nil {
t.Fatal("expected SOCKS5H transport to have custom DialContext")
}
}
func TestBuildDialerHTTPProxyCONNECT(t *testing.T) {
t.Parallel()
listener, errListen := net.Listen("tcp", "127.0.0.1:0")
if errListen != nil {
t.Fatalf("net.Listen returned error: %v", errListen)
}
defer func() {
if errClose := listener.Close(); errClose != nil {
t.Errorf("listener.Close returned error: %v", errClose)
}
}()
done := make(chan error, 1)
go func() {
conn, errAccept := listener.Accept()
if errAccept != nil {
done <- errAccept
return
}
defer func() { _ = conn.Close() }()
if errDeadline := conn.SetDeadline(time.Now().Add(5 * time.Second)); errDeadline != nil {
done <- errDeadline
return
}
req, errRead := http.ReadRequest(bufio.NewReader(conn))
if errRead != nil {
done <- fmt.Errorf("read CONNECT request failed: %w", errRead)
return
}
if req.Method != http.MethodConnect {
done <- fmt.Errorf("method = %s, want CONNECT", req.Method)
return
}
if req.Host != "target.example.com:443" {
done <- fmt.Errorf("host = %s, want target.example.com:443", req.Host)
return
}
wantAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte("user:pass"))
if gotAuth := req.Header.Get("Proxy-Authorization"); gotAuth != wantAuth {
done <- fmt.Errorf("Proxy-Authorization = %q, want %q", gotAuth, wantAuth)
return
}
if _, errWrite := io.WriteString(conn, "HTTP/1.1 200 Connection Established\r\n\r\nok"); errWrite != nil {
done <- fmt.Errorf("write CONNECT response failed: %w", errWrite)
return
}
buf := make([]byte, 4)
n, errReadTunnel := io.ReadFull(conn, buf)
if errReadTunnel != nil {
done <- fmt.Errorf("read tunneled payload failed after %d bytes: %w", n, errReadTunnel)
return
}
if string(buf) != "ping" {
done <- fmt.Errorf("tunneled payload = %q, want ping", string(buf))
return
}
done <- nil
}()
dialer, mode, errBuild := BuildDialer("http://user:pass@" + listener.Addr().String())
if errBuild != nil {
t.Fatalf("BuildDialer returned error: %v", errBuild)
}
if mode != ModeProxy {
t.Fatalf("mode = %d, want %d", mode, ModeProxy)
}
if dialer == nil {
t.Fatal("expected dialer, got nil")
}
conn, errDial := dialer.Dial("tcp", "target.example.com:443")
if errDial != nil {
t.Fatalf("dialer.Dial returned error: %v", errDial)
}
defer func() {
if errClose := conn.Close(); errClose != nil {
t.Errorf("conn.Close returned error: %v", errClose)
}
}()
buf := make([]byte, 2)
n, errRead := io.ReadFull(conn, buf)
if errRead != nil {
t.Fatalf("conn.Read returned error after %d bytes: %v", n, errRead)
}
if string(buf) != "ok" {
t.Fatalf("buffered tunnel payload = %q, want ok", string(buf))
}
if _, errWrite := conn.Write([]byte("ping")); errWrite != nil {
t.Fatalf("conn.Write returned error: %v", errWrite)
}
if errServer := <-done; errServer != nil {
t.Fatalf("proxy server returned error: %v", errServer)
}
}
func TestBuildDialerHTTPProxyCONNECTCancellation(t *testing.T) {
t.Parallel()
listener, errListen := net.Listen("tcp", "127.0.0.1:0")
if errListen != nil {
t.Fatalf("net.Listen returned error: %v", errListen)
}
defer func() { _ = listener.Close() }()
requestRead := make(chan struct{})
serverDone := make(chan error, 1)
go func() {
connection, errAccept := listener.Accept()
if errAccept != nil {
serverDone <- errAccept
return
}
defer func() { _ = connection.Close() }()
if _, errRead := http.ReadRequest(bufio.NewReader(connection)); errRead != nil {
serverDone <- errRead
return
}
close(requestRead)
if errDeadline := connection.SetReadDeadline(time.Now().Add(5 * time.Second)); errDeadline != nil {
serverDone <- errDeadline
return
}
var buffer [1]byte
_, errRead := connection.Read(buffer[:])
serverDone <- errRead
}()
dialer, mode, errBuild := BuildDialer("http://" + listener.Addr().String())
if errBuild != nil || mode != ModeProxy {
t.Fatalf("BuildDialer mode=%d error=%v", mode, errBuild)
}
contextDialer, ok := dialer.(interface {
DialContext(context.Context, string, string) (net.Conn, error)
})
if !ok {
t.Fatal("HTTP CONNECT dialer does not support context cancellation")
}
ctx, cancel := context.WithCancel(context.Background())
dialDone := make(chan error, 1)
go func() {
connection, errDial := contextDialer.DialContext(ctx, "tcp", "20.42.0.20:443")
if connection != nil {
_ = connection.Close()
}
dialDone <- errDial
}()
select {
case <-requestRead:
case <-time.After(time.Second):
t.Fatal("proxy did not receive CONNECT request")
}
cancel()
select {
case errDial := <-dialDone:
if errDial == nil {
t.Fatal("canceled CONNECT dial returned nil error")
}
case <-time.After(time.Second):
t.Fatal("canceled CONNECT dial did not return")
}
select {
case errServer := <-serverDone:
if errServer == nil {
t.Fatal("proxy connection stayed open after cancellation")
}
case <-time.After(time.Second):
t.Fatal("proxy connection was not closed after cancellation")
}
}
func TestRedactProxyURL(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
want string
}{
{
name: "with credentials",
input: "http://user:pass@proxy.example.com:8080/path?token=secret",
want: "http://redacted@proxy.example.com:8080",
},
{
name: "without credentials",
input: "socks5://proxy.example.com:1080",
want: "socks5://proxy.example.com:1080",
},
{
name: "invalid",
input: "bad-value",
want: "<invalid proxy URL>",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := Redact(tt.input); got != tt.want {
t.Fatalf("Redact() = %q, want %q", got, tt.want)
}
})
}
}
func TestParseErrorDoesNotExposeProxyCredentials(t *testing.T) {
t.Parallel()
input := "http://user:secret%@proxy.example.com:8080"
_, errParse := Parse(input)
if errParse == nil {
t.Fatal("expected Parse to return an error")
}
if strings.Contains(errParse.Error(), input) ||
strings.Contains(errParse.Error(), "user") ||
strings.Contains(errParse.Error(), "secret") {
t.Fatalf("parse error exposes proxy credentials: %q", errParse.Error())
}
}