99 lines
3.0 KiB
Go
99 lines
3.0 KiB
Go
// Package sourcehttp provides DNS-pinned, proxy-free source connections.
|
|
package sourcehttp
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"net/http"
|
|
"net/netip"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Resolver interface {
|
|
LookupNetIP(context.Context, string, string) ([]netip.Addr, error)
|
|
}
|
|
type DialFunc func(context.Context, string, string) (net.Conn, error)
|
|
|
|
var errUnavailable = errors.New("source connection unavailable")
|
|
var forbidden = prefixes(
|
|
"0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", "192.0.0.0/24", "192.0.2.0/24", "192.88.99.0/24", "192.168.0.0/16", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", "224.0.0.0/4", "240.0.0.0/4",
|
|
"2001::/23", "2001:db8::/32", "2002::/16", "3fff::/20",
|
|
)
|
|
|
|
func prefixes(values ...string) []netip.Prefix {
|
|
out := make([]netip.Prefix, 0, len(values))
|
|
for _, v := range values {
|
|
out = append(out, netip.MustParsePrefix(v))
|
|
}
|
|
return out
|
|
}
|
|
|
|
// IsPublicAddress rejects special-use and non-global destinations.
|
|
func IsPublicAddress(ip netip.Addr) bool {
|
|
if !ip.IsValid() || ip.Zone() != "" {
|
|
return false
|
|
}
|
|
ip = ip.Unmap()
|
|
if !ip.IsGlobalUnicast() || ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() {
|
|
return false
|
|
}
|
|
// Only currently allocated global IPv6 unicast, excluding transition/special ranges.
|
|
if ip.Is6() && !netip.MustParsePrefix("2000::/3").Contains(ip) {
|
|
return false
|
|
}
|
|
for _, p := range forbidden {
|
|
if p.Contains(ip) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// NewTransport resolves exactly once per new connection, validates the entire
|
|
// answer set, and dials only numeric addresses. TLS still uses the request host.
|
|
// Nil dependencies select the production resolver and dialer; fixtures inject both.
|
|
func NewTransport(resolver Resolver, dial DialFunc) *http.Transport {
|
|
if resolver == nil {
|
|
resolver = net.DefaultResolver
|
|
}
|
|
if dial == nil {
|
|
dial = (&net.Dialer{Timeout: 15 * time.Second, KeepAlive: 30 * time.Second}).DialContext
|
|
}
|
|
return &http.Transport{
|
|
Proxy: nil, TLSHandshakeTimeout: 10 * time.Second, ResponseHeaderTimeout: 15 * time.Second,
|
|
IdleConnTimeout: 90 * time.Second, MaxIdleConns: 20, ForceAttemptHTTP2: true,
|
|
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
host, port, err := net.SplitHostPort(address)
|
|
if err != nil || port != "443" {
|
|
return nil, errUnavailable
|
|
}
|
|
switch strings.ToLower(host) {
|
|
case "finance.yahoo.com", "feeds.finance.yahoo.com", "www.cnbc.com", "feed.quanwenrss.com":
|
|
default:
|
|
return nil, errUnavailable
|
|
}
|
|
ips, err := resolver.LookupNetIP(ctx, "ip", host)
|
|
if err != nil || len(ips) == 0 {
|
|
return nil, errUnavailable
|
|
}
|
|
for _, ip := range ips {
|
|
if !IsPublicAddress(ip) {
|
|
return nil, errUnavailable
|
|
}
|
|
}
|
|
for _, ip := range ips {
|
|
conn, err := dial(ctx, network, net.JoinHostPort(ip.Unmap().String(), port))
|
|
if err == nil {
|
|
return conn, nil
|
|
}
|
|
if ctx.Err() != nil {
|
|
break
|
|
}
|
|
}
|
|
return nil, errUnavailable
|
|
},
|
|
}
|
|
}
|