Files
Claude 25a0fe0eb1 Fetch over HTTP/3 and pass gRPC through CONNECT tunnels
HTTP/3: sites that offer h3 in Alt-Svc are reached over QUIC (quic-go),
falling back to TCP for ten minutes when QUIC fails. RemoteHttpVersion
takes 3.0 (=3.0 forces it, <3.0 turns it off). Not used through an
UpperProxy or without TLS 1.3.

gRPC: in decrypted CONNECT tunnels to 443, clients that offer only h2 in
ALPN get HTTP/2 and are reverse-proxied to the site over HTTP/2, streamed
both ways with trailers. NonHttpSslServers tunnels now mirror ALPN to the
server, so h2 works through them too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRSqXQt9VziEWCfxcMo
2026-11-11 13:30:15 +00:00

324 lines
9.1 KiB
Go

package proxy
import (
"bufio"
"crypto/tls"
"io"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"github.com/D-Modem/retrossl"
"github.com/D-Modem/webone-go/internal/logx"
"github.com/D-Modem/webone-go/internal/rx"
)
// bufConn is a connection whose first bytes may already sit in a reader.
type bufConn struct {
net.Conn
r io.Reader
}
func (c *bufConn) Read(p []byte) (int, error) { return c.r.Read(p) }
// connect handles CONNECT host:port (the HTTPS proxy part of
// ProcessTransit, with HttpSecureServer, HttpSecurePassthroughServer,
// HttpSecureNonHttpServer and HttpSecureNonHttpDecryptServer).
func (t *transit) connect() {
raw := t.req.RawURL
if !strings.Contains(raw, ":") {
t.log.Printf(" Invalid CONNECT target: %s", raw)
t.sendError(400, "Invalid request. Correct format is <pre>CONNECT example.com:443 HTTP/1.1</pre>")
return
}
switch {
case rx.AnyMatches(raw, t.cfg.ConnectPassThrough):
t.connectPassThrough()
case t.cfg.NonHttpSslDecrypt[raw] != "":
t.connectDecrypt(t.cfg.NonHttpSslDecrypt[raw])
case strings.HasSuffix(raw, ":443"):
t.connectSecure()
default:
t.connectNonHttp(containsAny(strings.ToLower(raw), t.cfg.NonHttpSslServers))
}
}
// established answers "200 Connection established" and gives the
// connection over to the tunnel.
func (t *transit) established(via bool) net.Conn {
w := t.resp
w.SetVersion(1, 1)
w.Status = 200
w.StatusMessage = "Connection established"
if via {
w.Header.Add("Via", "1.1 WebOne/"+t.cfg.Vars.Value("WOVer"))
}
w.Header.Set("Connection", "Keep-Alive")
w.KeepAlive = true
w.SendHeaders(Version)
t.hijacked = true
return &bufConn{Conn: t.req.conn, r: t.req.br}
}
// onlyHTTPS refuses a tunnel to a port other than 443.
func (t *transit) onlyHTTPS() {
const msg = "This proxy is performing only HTTP and HTTPS tunneling."
t.resp.SetVersion(1, 1)
t.resp.KeepAlive = false
t.resp.Header.Set("Connection", "Close")
t.sendBytes(502, "text/plain", []byte(msg))
t.log.Printf("<Not a HTTPS CONNECT, goodbye.")
}
// sslDisabled answers a secure CONNECT when SslEnable is off.
func (t *transit) sslDisabled() {
t.log.Printf("<Secure CONNECT is disabled.")
html := "<HTML><HEAD><TITLE>WebOne: SSL is not supported</TITLE></HEAD>" +
"<BODY><P>Incoming HTTPS connections support is disabled on this server.</P>" + t.s.infoString() + "</BODY></HTML>"
if b, err := os.ReadFile(filepath.Join(t.cfg.ContentDirectory, "Err-SslDisabled.htm")); err == nil {
html = string(b)
}
t.resp.SetVersion(1, 1)
t.sendBytes(501, "text/html", []byte(html))
}
func (t *transit) sslAvailable() bool { return t.cfg.SslEnable && t.s.CA != nil }
// connectSecure is the HTTPS proxy: the proxy answers TLS with a
// certificate for the host and handles the requests inside the tunnel as
// any other (HttpSecureServer).
func (t *transit) connectSecure() {
if !t.sslAvailable() {
t.sslDisabled()
return
}
host := t.req.RawURL[:strings.LastIndexByte(t.req.RawURL, ':')]
cfg, rc, err := t.s.tlsFor(host)
if err != nil {
t.log.Printf(" Cannot made SSL connection: %v", err)
t.sendError(501, "Sorry, an error occured on creating client SSL tunnel: "+err.Error())
return
}
c := t.established(true)
tc, agreed, err := t.s.serveSecure(c, withH2(cfg), rc)
if err != nil {
t.log.Printf("!SSL Handshake failed: %s", handshakeError(err))
c.Close()
return
}
log := logx.New()
if negotiated(tc) == "h2" {
log.Printf(">SSL %s: %s, HTTP/2", host, agreed)
t.s.serveH2(tc, t.req.RawURL)
tc.Close()
log.Printf("<Close SSL.")
return
}
log.Printf(">SSL %s: %s", host, agreed)
t.s.handleConn(tc, bufio.NewReaderSize(tc, 16*1024), true, host, log)
tc.Close()
log.Printf("<Close SSL.")
}
// handshakeError makes Go's TLS errors say what old clients do wrong.
func handshakeError(err error) string {
msg := err.Error()
if strings.Contains(msg, "unsupported SSLv2 handshake") || strings.Contains(msg, "oversized record") ||
strings.Contains(msg, "which is off") {
return msg + " (the client speaks SSL 2.0 or 3.0: see SslProtocols)"
}
return msg
}
// connectPassThrough connects the client straight to the server
// (HttpSecurePassthroughServer).
func (t *transit) connectPassThrough() {
raw := t.req.RawURL
if !strings.HasSuffix(raw, ":443") && !t.cfg.AllowNonHttpsCONNECT {
t.onlyHTTPS()
return
}
c := t.established(false)
t.log.Printf(">Passthrough: %s", raw)
remote, err := t.s.up.dialer.Dial("tcp", raw)
if err != nil {
t.log.Printf(" PT Connection failed: %v.", err)
io.WriteString(c, "The proxy server is unable to connect pass-though: "+err.Error()+"\r\n")
c.Close()
return
}
t.log.Printf(" PT tunnel established.")
pipe(c, remote)
t.log.Printf(" PT connection to %s closed.", raw)
}
// connectNonHttp is a tunnel to some other protocol, decrypting the
// client's TLS and re-encrypting to the server when useTLS
// (HttpSecureNonHttpServer).
func (t *transit) connectNonHttp(useTLS bool) {
if !t.cfg.AllowNonHttpsCONNECT {
t.onlyHTTPS()
return
}
target := t.req.RawURL
parts := strings.Split(target, ":")
if len(parts) != 2 {
t.sendError(400, "Invalid `domain:port` pair supplied for CONNECT method.")
return
}
if _, err := strconv.Atoi(parts[1]); err != nil {
t.sendError(400, "Invalid port number supplied for CONNECT method.")
return
}
var cfg *tls.Config
var rc *retrossl.Config
if useTLS && t.sslAvailable() {
var err error
if cfg, rc, err = t.s.tlsFor(parts[0]); err != nil {
t.log.Printf(" Cannot made SSL connection: %v", err)
t.sendError(501, "Sorry, an error occured on creating client SSL tunnel: "+err.Error())
return
}
}
c := t.established(false)
t.log.Printf(">Non-HTTP: %s", target)
defer c.Close()
if containsAny(target, t.cfg.UrlBlackList) {
t.log.Printf(" Blacklisted target, goodbye.")
return
}
if len(t.cfg.UrlWhiteList) > 0 && !containsAny(target, t.cfg.UrlWhiteList) {
t.log.Printf(" Non-allowed target, goodbye.")
return
}
for src, dst := range t.cfg.NonHttpConnectRedirect {
if strings.Contains(t.req.RawURL, src) {
target = dst
t.log.Printf(" Redirect connection to %s.", dst)
}
}
var remote net.Conn
var err error
if useTLS {
if cfg == nil {
t.log.Printf("<SSL is disabled, goodbye.")
return
}
// The server is reached during the client's handshake, offering
// what the client offers in ALPN, so they agree on the same
// protocol (gRPC needs "h2").
base := cfg
cfg = base.Clone()
cfg.GetConfigForClient = func(h *tls.ClientHelloInfo) (*tls.Config, error) {
if len(h.SupportedProtos) == 0 {
return nil, nil
}
if remote, err = t.dialTLS(target, parts[0], h.SupportedProtos); err != nil {
return nil, nil // the handshake ends; the error is told after
}
cc := base.Clone()
if p := negotiated(remote); p != "" {
cc.NextProtos = []string{p}
}
return cc, nil
}
tc, agreed, herr := t.s.serveSecure(c, cfg, rc)
if herr != nil {
t.log.Printf("!SSL client handshake failed: %s", handshakeError(herr))
if remote != nil {
remote.Close()
}
return
}
if p := negotiated(tc); p != "" {
agreed += ", " + p
}
t.log.Printf(" SSL: %s", agreed)
c = tc
if remote == nil && err == nil {
remote, err = t.dialTLS(target, parts[0], nil)
}
} else {
remote, err = t.s.up.dialer.Dial("tcp", target)
}
if err != nil {
t.log.Printf(" Connection failed: %v.", err)
io.WriteString(c, "The proxy server is unable to connect: "+err.Error()+"\r\n")
return
}
if useTLS {
t.log.Printf(" Secure tunnel established.")
} else {
t.log.Printf(" Tunnel established.")
}
pipe(c, remote)
t.log.Printf(" Connection to %s closed.", t.req.RawURL)
}
// connectDecrypt is a plain tunnel from the client that the proxy
// encrypts toward the server (HttpSecureNonHttpDecryptServer).
func (t *transit) connectDecrypt(server string) {
if !t.cfg.AllowNonHttpsCONNECT {
t.onlyHTTPS()
return
}
host := server
if i := strings.LastIndexByte(server, ':'); i >= 0 {
host = server[:i]
}
c := t.established(false)
defer c.Close()
t.log.Printf(">Decrypt: %s", server)
remote, err := t.dialTLS(server, host, nil)
if err != nil {
t.log.Printf(" D connection failed: %v.", err)
io.WriteString(c, "The proxy server is unable to connect with decryption: "+err.Error()+"\r\n")
return
}
t.log.Printf(" D tunnel established.")
pipe(c, remote)
t.log.Printf(" D connection to %s closed.", t.req.RawURL)
}
// dialTLS connects to a server over TLS, offering protos in ALPN.
func (t *transit) dialTLS(addr, host string, protos []string) (net.Conn, error) {
raw, err := t.s.up.dialer.Dial("tcp", addr)
if err != nil {
return nil, err
}
cfg := t.upstreamTLS(host)
cfg.NextProtos = protos
tc := tls.Client(raw, cfg)
if err := tc.Handshake(); err != nil {
raw.Close()
return nil, err
}
return tc, nil
}
// upstreamTLS is the TLS client configuration toward a server.
func (t *transit) upstreamTLS(host string) *tls.Config {
c := t.s.up.tlsCfg.Clone()
c.ServerName = host
return c
}
// pipe copies both ways until one side ends, then closes both.
func pipe(a, b net.Conn) {
var once sync.Once
done := make(chan struct{})
closeBoth := func() {
once.Do(func() {
a.Close()
b.Close()
close(done)
})
}
go func() { io.Copy(b, a); closeBoth() }()
go func() { io.Copy(a, b); closeBoth() }()
<-done
}