Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/andrioid/ublproxy. andrioid.github.io/ublproxy
Something went wrong. Try again.
ublproxy proxy.go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317package main
import ( "crypto/tls" "fmt" "io" "net/http" "os" "strings" "sync" "sync/atomic" "time"
"ublproxy/internal/blocklist" "ublproxy/internal/ca" "ublproxy/internal/store")
type proxyHandler struct { certs *ca.Cache caCertPEM []byte transport *http.Transport store *store.Store api *apiHandler sessions *sessionMap
portalOrigin string httpOrigin string
// activityLog records recent proxy events for the activity feed. activityLog *ActivityLog
// baselineRules are the always-active rules loaded from --blocklist // sources and --default-subscription lists. These apply to all traffic // regardless of user. baselineRules atomic.Pointer[blocklist.RuleSet]
// userRules caches per-user RuleSets keyed by credential ID. Each // user's RuleSet contains their custom rules and subscription lists. // Loaded lazily on first proxied request for that user. userRules sync.Map // credential ID -> *blocklist.RuleSet
// blocklistSources are the static blocklist file paths/URLs loaded at // startup (from CLI --blocklist flags). blocklistSources []string
// reloadMu serializes baseline rule reloads to prevent concurrent rebuilds. reloadMu sync.Mutex}
func newProxyHandler(certs *ca.Cache, caCertPEM []byte) *proxyHandler { return &proxyHandler{ certs: certs, caCertPEM: caCertPEM, // Force HTTP/1.1 upstream. Go's default HTTP/2 support causes hangs // with certain Cloudflare hosts in a MITM proxy scenario. transport: &http.Transport{ TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper), TLSHandshakeTimeout: 10 * time.Second, ResponseHeaderTimeout: 60 * time.Second, IdleConnTimeout: 90 * time.Second, }, }}
// getBaselineRules returns the baseline RuleSet, or nil if none is loaded.func (p *proxyHandler) getBaselineRules() *blocklist.RuleSet { return p.baselineRules.Load()}
// getUserRules returns the cached per-user RuleSet for the given credential,// loading it lazily from the database on first access. Returns nil if the// user has no custom rules or subscriptions, or if the store is unavailable.func (p *proxyHandler) getUserRules(credentialID string) *blocklist.RuleSet { if credentialID == "" || p.store == nil { return nil }
if cached, ok := p.userRules.Load(credentialID); ok { return cached.(*blocklist.RuleSet) }
rs := p.loadUserRules(credentialID) p.userRules.Store(credentialID, rs) return rs}
// loadUserRules builds a RuleSet from a user's DB rules and subscriptions.func (p *proxyHandler) loadUserRules(credentialID string) *blocklist.RuleSet { rs := blocklist.NewRuleSet()
subURLs, err := p.store.ListEnabledSubscriptionURLs(credentialID) if err != nil { fmt.Fprintf(os.Stderr, "user-rules: failed to load subscriptions for %s: %v\n", credentialID, err) } else { for _, url := range subURLs { if err := p.loadBlocklistSource(rs, url); err != nil { fmt.Fprintf(os.Stderr, "user-rules: failed to load subscription %s: %v\n", url, err) } } }
dbRules, err := p.store.ListEnabledRules(credentialID) if err != nil { fmt.Fprintf(os.Stderr, "user-rules: failed to load rules for %s: %v\n", credentialID, err) } else { for _, r := range dbRules { rs.AddLine(r.Rule) } }
return rs}
// invalidateUserRules evicts the cached RuleSet for a user so it will be// reloaded from the database on the next proxied request.func (p *proxyHandler) invalidateUserRules(credentialID string) { p.userRules.Delete(credentialID)}
// credentialForIP returns the credential ID for the authenticated session// on the given client IP, or empty string if not authenticated.func (p *proxyHandler) credentialForIP(clientIP string) string { if p.sessions == nil { return "" } entry := p.sessions.Get(clientIP) if entry == nil { return "" } return entry.CredentialID}
// shouldBlock checks whether a URL should be blocked, applying layered// evaluation: user exceptions override baseline blocks, then user blocks// are checked, then baseline blocks.func (p *proxyHandler) shouldBlock(clientIP, url string, ctx blocklist.MatchContext) bool { baseline := p.getBaselineRules() credID := p.credentialForIP(clientIP) userRS := p.getUserRules(credID)
// User exception overrides baseline block if userRS != nil && userRS.MatchesException(url, ctx) { return false }
// User-specific block if userRS != nil && userRS.ShouldBlockRequest(url, ctx) { return true }
// Baseline block if baseline != nil && baseline.ShouldBlockRequest(url, ctx) { return true }
return false}
// isHostExcepted checks whether a host has an active @@ exception rule.// Used at CONNECT time to decide between MITM and passthrough tunneling.func (p *proxyHandler) isHostExcepted(clientIP, host string) bool { credID := p.credentialForIP(clientIP) userRS := p.getUserRules(credID) if userRS != nil && userRS.MatchesExceptionHost(host) { return true }
baseline := p.getBaselineRules() if baseline != nil && baseline.MatchesExceptionHost(host) { return true }
return false}
// shouldBlockHost checks whether a host should be blocked at the CONNECT// level, applying layered evaluation like shouldBlock.func (p *proxyHandler) shouldBlockHost(clientIP, host string) bool { baseline := p.getBaselineRules() credID := p.credentialForIP(clientIP) userRS := p.getUserRules(credID)
// User exception overrides baseline block if userRS != nil && userRS.MatchesExceptionHost(host) { return false }
// User-specific block if userRS != nil && userRS.IsHostBlocked(host) { return true }
// Baseline block if baseline != nil && baseline.IsHostBlocked(host) { return true }
return false}
// blocklistCacheTTL is how long a cached blocklist download is considered// fresh. Stale entries are re-downloaded on next reload.const blocklistCacheTTL = 24 * time.Hour
// reloadBaseline rebuilds the baseline RuleSet from --blocklist sources// (static files/URLs). Default subscriptions are per-user (provisioned on// auth) and loaded via getUserRules, not included here.func (p *proxyHandler) reloadBaseline() { p.reloadMu.Lock() defer p.reloadMu.Unlock()
rs := blocklist.NewRuleSet()
// Load static blocklists (CLI --blocklist flags) for _, src := range p.blocklistSources { if err := p.loadBlocklistSource(rs, src); err != nil { fmt.Fprintf(os.Stderr, "baseline: failed to load blocklist %s: %v\n", src, err) } }
p.baselineRules.Store(rs) if rs.HostCount() > 0 || rs.RuleCount() > 0 { fmt.Fprintf(os.Stderr, "baseline: loaded %d hostnames, %d URL rules\n", rs.HostCount(), rs.RuleCount()) }}
// loadBlocklistSource loads a blocklist from a file path or URL into the// RuleSet. Remote URLs are cached in the database with a 24-hour TTL.func (p *proxyHandler) loadBlocklistSource(rs *blocklist.RuleSet, src string) error { if !strings.HasPrefix(src, "http://") && !strings.HasPrefix(src, "https://") { return rs.LoadFile(src) } return p.loadBlocklistURL(rs, src)}
// loadBlocklistURL loads a remote blocklist, using the DB cache when// available and fresh. Downloads and caches when stale or missing.func (p *proxyHandler) loadBlocklistURL(rs *blocklist.RuleSet, url string) error { // Try cache first if p.store != nil { cached, err := p.store.GetCachedBlocklist(url) if err == nil && cached != nil && time.Since(cached.FetchedAt) < blocklistCacheTTL { return rs.LoadReader(strings.NewReader(string(cached.Content))) } }
// Download fresh content, err := downloadBlocklist(url) if err != nil { // Fall back to stale cache if download fails if p.store != nil { cached, cacheErr := p.store.GetCachedBlocklist(url) if cacheErr == nil && cached != nil { fmt.Fprintf(os.Stderr, "reload: using stale cache for %s (download failed: %v)\n", url, err) return rs.LoadReader(strings.NewReader(string(cached.Content))) } } return err }
// Save to cache if p.store != nil { if cacheErr := p.store.SetCachedBlocklist(url, content); cacheErr != nil { fmt.Fprintf(os.Stderr, "reload: failed to cache %s: %v\n", url, cacheErr) } }
return rs.LoadReader(strings.NewReader(string(content)))}
// downloadBlocklist fetches a blocklist URL and returns its raw content.func downloadBlocklist(url string) ([]byte, error) { client := &http.Client{Timeout: 5 * time.Second} resp, err := client.Get(url) if err != nil { return nil, fmt.Errorf("fetch %s: %w", url, err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("fetch %s: HTTP %d", url, resp.StatusCode) } // Limit to 50MB to prevent memory exhaustion body := http.MaxBytesReader(nil, resp.Body, 50<<20) var buf strings.Builder if _, err := io.Copy(&buf, body); err != nil { return nil, fmt.Errorf("read %s: %w", url, err) } return []byte(buf.String()), nil}
// logActivity records a proxy event to the activity log if available.func (p *proxyHandler) logActivity(entryType, host, url, rule string) { if p.activityLog == nil { return } p.activityLog.Add(ActivityEntry{ Type: entryType, Host: host, URL: url, Rule: rule, })}
func (p *proxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodConnect { p.handleConnect(w, r) return } // Direct request to the proxy itself, not a proxy request if r.URL.Host == "" { p.handlePortal(w, r) return } p.handleHTTP(w, r)}