Files
bbs/internal/api/proxy.go
T
Claude 67eecc7b4e Image boards at /chan/, after kusaba x and 4chan; trusted proxies
Boards by directory with anonymous posts and images: thumbnails made
in Go (JPEG, PNG, GIF, WebP, told by their bytes), JPEG metadata
stripped, duplicates refused by MD5. Threads bump until a bump limit,
sage, image limits, pruning off the last page, stickies and closed
threads. Tripcodes are 2channel's (a DES crypt(3) of our own, checked
against libc's) and secure ones; greentext, >>quotes with backlinks,
cross-board links, spoilers and code. Index, thread pages, catalog,
password deletion, reports; staff (sysops, a board's mods) delete,
ban by address or range, and post with capcodes. Yotsuba, Yotsuba B,
Futaba, Burichan, Photon and Tomorrow styles, and 4chan's read-only
JSON. bbs chan board|ban on the command line; [chan] in bbs.conf.

[http] trusted_proxies: requests from those take their caller's
address from X-Forwarded-For (Forwarded, X-Real-IP), the last hop that
is not a trusted proxy, for rate limits, logs, the deny lists and the
image boards' bans; a proxy's connections are not held to max_per_ip.
secure_cookies for TLS at the proxy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
2026-10-14 00:53:47 +00:00

129 lines
3.6 KiB
Go

package api
import (
"net"
"net/http"
"strings"
)
// Behind a reverse proxy (nginx, Caddy, a load balancer) every request
// comes from the proxy's address. The proxies named in trusted_proxies
// are believed when they say whose request it is, by X-Forwarded-For (or
// Forwarded, or X-Real-IP); no one else is, since anyone can send those
// headers.
// ParseProxies reads trusted_proxies: addresses and CIDR ranges, by spaces
// or commas.
func ParseProxies(s string) ([]*net.IPNet, error) {
var out []*net.IPNet
for _, f := range strings.FieldsFunc(s, func(r rune) bool { return r == ',' || r == ' ' || r == '\t' }) {
if _, n, err := net.ParseCIDR(f); err == nil {
out = append(out, n)
continue
}
ip := net.ParseIP(f)
if ip == nil {
return nil, &net.ParseError{Type: "address or CIDR range", Text: f}
}
bits := 128
if ip.To4() != nil {
ip, bits = ip.To4(), 32
}
out = append(out, &net.IPNet{IP: ip, Mask: net.CIDRMask(bits, bits)})
}
return out, nil
}
// trusted says whether an address is a trusted proxy's.
func (s *Server) trusted(ip net.IP) bool {
for _, n := range s.S.TrustedProxies {
if n.Contains(ip) {
return true
}
}
return false
}
// parseHopIP is an address as a forwarding header gives it: bare,
// with a port, or in brackets ("[2001:db8::1]:443").
func parseHopIP(s string) net.IP {
s = strings.Trim(strings.TrimSpace(s), `"`)
if h, _, err := net.SplitHostPort(s); err == nil {
s = h
}
return net.ParseIP(strings.Trim(s, "[]"))
}
// forwardedFor is the hops a request came by, as its proxies say, the
// first (the client) first.
func forwardedFor(r *http.Request) []string {
var hops []string
for _, h := range r.Header.Values("X-Forwarded-For") {
hops = append(hops, strings.Split(h, ",")...)
}
if len(hops) > 0 {
return hops
}
// RFC 7239: Forwarded: for=192.0.2.60;proto=http, for="[2001:db8::1]"
for _, h := range r.Header.Values("Forwarded") {
for _, elem := range strings.Split(h, ",") {
for _, pair := range strings.Split(elem, ";") {
k, v, ok := strings.Cut(strings.TrimSpace(pair), "=")
if ok && strings.EqualFold(k, "for") {
hops = append(hops, v)
}
}
}
}
if len(hops) > 0 {
return hops
}
if v := r.Header.Get("X-Real-IP"); v != "" {
return []string{v}
}
return nil
}
// clientIP is whom a trusted proxy's request is for: the last hop, from
// the end, that is not a trusted proxy (the ones before it could be
// anyone's invention); nil if the headers do not say.
func (s *Server) clientIP(r *http.Request) net.IP {
hops := forwardedFor(r)
var last net.IP
for i := len(hops) - 1; i >= 0; i-- {
ip := parseHopIP(hops[i])
if ip == nil {
break // "unknown", an obfuscated name: no further back
}
last = ip
if !s.trusted(ip) {
return ip
}
}
return last
}
// realAddr has requests from trusted proxies come from whom they are
// for, before anything else sees them: rate limits, logins, the logs,
// the forum and the image boards, the web terminal. The caller's
// address is then held to the [security] deny lists as a connection's
// is.
func (s *Server) realAddr(h http.Handler) http.Handler {
if len(s.S.TrustedProxies) == 0 {
return h
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
host, port, err := net.SplitHostPort(r.RemoteAddr)
if ip := net.ParseIP(host); err == nil && ip != nil && s.trusted(ip) {
if c := s.clientIP(r); c != nil {
if s.B.Refuse("http", &net.TCPAddr{IP: c}) {
http.Error(w, "refused", http.StatusForbidden)
return
}
r.RemoteAddr = net.JoinHostPort(c.String(), port)
}
}
h.ServeHTTP(w, r)
})
}