// Package geckorelay provides a bounded TCP-only Wisp transport for Gecko. // It is not a public general-purpose proxy: the PagesWiki frontend authenticates // the user and signs each backend request, and every destination is checked here. package geckorelay import ( "context" "crypto/hmac" "crypto/sha256" "encoding/base64" "encoding/json" "errors" "net" "net/netip" "strconv" "strings" "time" ) const GrantHeader = "X-PagesWiki-Gecko-Grant" type Grant struct { Subject string `json:"subject"` Nonce string `json:"nonce"` Expires int64 `json:"expires"` } func Sign(key string, grant Grant) string { data, _ := json.Marshal(grant) body := base64.RawURLEncoding.EncodeToString(data) h := hmac.New(sha256.New, []byte(key)) h.Write([]byte(body)) return body + "." + base64.RawURLEncoding.EncodeToString(h.Sum(nil)) } func Verify(key, token string, now time.Time) (Grant, error) { var grant Grant parts := strings.Split(token, ".") if len(key) < 32 || len(token) > 2048 || len(parts) != 2 { return grant, errors.New("invalid grant") } sig, err := base64.RawURLEncoding.DecodeString(parts[1]) if err != nil { return grant, errors.New("invalid grant") } h := hmac.New(sha256.New, []byte(key)) h.Write([]byte(parts[0])) if !hmac.Equal(sig, h.Sum(nil)) { return grant, errors.New("invalid grant") } data, err := base64.RawURLEncoding.DecodeString(parts[0]) if err != nil { return grant, errors.New("invalid grant") } if json.Unmarshal(data, &grant) != nil || grant.Subject == "" || len(grant.Subject) > 160 || len(grant.Nonce) < 16 || len(grant.Nonce) > 80 || grant.Expires <= now.Unix() || grant.Expires > now.Add(time.Minute).Unix() { return Grant{}, errors.New("invalid grant") } return grant, nil } var denied = func() []netip.Prefix { var result []netip.Prefix for _, p := range []string{"0.0.0.0/8", "100.64.0.0/10", "192.0.0.0/24", "192.0.2.0/24", "192.88.99.0/24", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", "240.0.0.0/4", "2001::/23", "2001:db8::/32", "2002::/16", "3fff::/20"} { result = append(result, netip.MustParsePrefix(p)) } return result }() func PublicIP(ip netip.Addr) bool { ip = ip.Unmap() if !ip.IsValid() || ip.Zone() != "" || !ip.IsGlobalUnicast() || ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() { return false } // Only globally allocated IPv6 unicast; excludes NAT64, mapped/private and // future address classes rather than treating unknown ranges as public. if ip.Is6() && !netip.MustParsePrefix("2000::/3").Contains(ip) { return false } for _, p := range denied { if p.Contains(ip) { return false } } return true } func ValidDestination(host string, port uint16) bool { if port != 80 && port != 443 { return false } if ip, err := netip.ParseAddr(host); err == nil { return PublicIP(ip) } host = strings.TrimSuffix(strings.ToLower(host), ".") if len(host) == 0 || len(host) > 253 || host == "localhost" || strings.HasSuffix(host, ".localhost") || !strings.Contains(host, ".") { return false } for _, label := range strings.Split(host, ".") { if len(label) == 0 || len(label) > 63 || label[0] == '-' || label[len(label)-1] == '-' { return false } for _, c := range label { if !(c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '-') { return false } } } return true } type lookupFunc func(context.Context, string, string) ([]netip.Addr, error) type dialFunc func(context.Context, string, string) (net.Conn, error) func dialPublic(ctx context.Context, host string, port uint16, lookup lookupFunc, dial dialFunc) (net.Conn, error) { if !ValidDestination(host, port) { return nil, errors.New("blocked destination") } ips, err := lookup(ctx, "ip", host) if err != nil || len(ips) == 0 || len(ips) > 16 { return nil, errors.New("destination lookup failed") } for _, ip := range ips { if !PublicIP(ip) { return nil, errors.New("blocked destination") } } for _, ip := range ips { conn, e := dial(ctx, "tcp", net.JoinHostPort(ip.Unmap().String(), strconv.Itoa(int(port)))) if e == nil { return conn, nil } if ctx.Err() != nil { break } } return nil, errors.New("destination unavailable") }