mirror of
https://github.com/D-Modem/WebOne-go.git
synced 2026-11-28 12:38:05 -08:00
A Go translation of WebOne (github.com/atauenis/webone, commit e940b43), the HTTP 1.x proxy that lets old browsers use today's web: config loader with sets of edits and .NET regular expressions, the HTTP/1.0-1.1 server with keep-alive, HTTPS fetching and patching, code pages, the CONNECT proxy (MITM, pass-through, non-HTTP tunnels), the CA and site certificates (MD5/SHA-1 signing included), internal and error pages, Web Archive fallback, converters, web FTP client and online video. WebOne's licence, configuration files and html pages are kept. Go-only options: SslCaDirectory signs with the D-Modem BBS's proxy sub-CA (or its root), SslTrustOwnCA, and DnsServer. README lists the differences: no SSL 2.0/3.0 in Go's TLS, and the upstream bugs fixed on purpose. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRRH4kntyPfrY1bFWC3
111 lines
3.0 KiB
Go
111 lines
3.0 KiB
Go
package proxy
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/D-Modem/webone-go/internal/config"
|
|
)
|
|
|
|
// Kind is how a request names its target (HttpUtil.RequestKind).
|
|
//
|
|
// GET /123, Host: example.com -> http://example.com/123 StandardRemote
|
|
// GET /123, Host: (the proxy) -> the proxy's own page StandardHttp
|
|
// GET /http://example.com/123 -> http://example.com/123 AlternateProxy
|
|
// GET http://example.com/123 -> http://example.com/123 StandardProxy
|
|
// GET ftp://example.com/pub -> ftp://example.com/pub StandardProxy
|
|
// CONNECT example.com:443 -> (a tunnel) StandardSslProxy
|
|
type Kind int
|
|
|
|
const (
|
|
StandardRemote Kind = iota
|
|
StandardHttp
|
|
AlternateProxy
|
|
DirtyAlternateProxy
|
|
StandardProxy
|
|
StandardSslProxy
|
|
)
|
|
|
|
func (k Kind) String() string {
|
|
return [...]string{"StandardRemote", "StandardHttp", "AlternateProxy", "DirtyAlternateProxy", "StandardProxy", "StandardSslProxy"}[k]
|
|
}
|
|
|
|
// kindOf works out a request's Kind (HttpUtil.GetKindOfRequest).
|
|
func (s *Server) kindOf(rawURL, hostHeader string, isConnect bool) (Kind, error) {
|
|
host := hostHeader
|
|
if host == "" {
|
|
host = "127.0.0.1"
|
|
}
|
|
port := 80
|
|
if !strings.HasPrefix(host, "/") && strings.Contains(host, ":") && !strings.HasSuffix(host, "]") {
|
|
i := strings.LastIndexByte(host, ':')
|
|
if p, err := strconv.Atoi(host[i+1:]); err == nil {
|
|
port = p
|
|
}
|
|
host = host[:i]
|
|
}
|
|
lower := strings.ToLower(rawURL)
|
|
if strings.HasPrefix(rawURL, "/") {
|
|
if s.isLocalhost(host, port) {
|
|
if strings.HasPrefix(lower, "/http:") || strings.HasPrefix(lower, "/https:") || strings.HasPrefix(lower, "/ftp:") {
|
|
return AlternateProxy, nil
|
|
}
|
|
// (WebOne's DirtyAlternateProxy test never succeeds; it is not ported.)
|
|
return StandardHttp, nil
|
|
}
|
|
return StandardRemote, nil
|
|
}
|
|
self := "http://" + host + ":" + strconv.Itoa(port) + "/"
|
|
if strings.HasPrefix(rawURL, self) && strings.Contains(rawURL[len(self):], ":") {
|
|
return AlternateProxy, nil
|
|
}
|
|
if s.isLocalhost(host, port) {
|
|
return StandardHttp, nil
|
|
}
|
|
if strings.Contains(rawURL, "://") {
|
|
return StandardProxy, nil
|
|
}
|
|
if isConnect {
|
|
return StandardSslProxy, nil
|
|
}
|
|
return 0, errors.New("cannot guess kind of requested target")
|
|
}
|
|
|
|
// isLocalhost: host:port is this proxy.
|
|
func (s *Server) isLocalhost(host string, port int) bool {
|
|
if port != s.cfg.Port {
|
|
return false
|
|
}
|
|
h := strings.ToLower(host)
|
|
for _, n := range s.localHostNames() {
|
|
if strings.Contains(h, strings.ToLower(n)) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// localHostNames is the names and addresses the proxy answers to.
|
|
func (s *Server) localHostNames() []string {
|
|
var names []string
|
|
for _, ip := range config.LocalIPs() {
|
|
if ip.To4() == nil {
|
|
names = append(names, "["+ip.String()+"]")
|
|
} else {
|
|
names = append(names, ip.String())
|
|
}
|
|
}
|
|
host, _ := os.Hostname()
|
|
names = append(names, "wpad", "localhost", host, s.cfg.DefaultHostName)
|
|
names = append(names, s.cfg.HostNames...)
|
|
var out []string
|
|
for _, n := range names {
|
|
if n != "" {
|
|
out = append(out, n)
|
|
}
|
|
}
|
|
return out
|
|
}
|