package proxy import ( "bufio" "crypto/tls" "io" "net" "os" "path/filepath" "strconv" "strings" "sync" "time" "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
CONNECT example.com:443 HTTP/1.1") 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("
Incoming HTTPS connections support is disabled on this server.
" + t.s.infoString() + "