jiezhengj (Discord, 11-08): under Clash Verge Rev's TUN (mihomo
1.19.33, enhanced-mode fake-ip, fake-ip-range 198.18.0.1/16) every
web_fetch of `magpie mcp search` answered "it is at a private address".
#2593 already lets a name whose every answer is in 198.18.0.0/15 through,
so that wasn't it. Clash Verge Rev writes dns.ipv6: true and
fake-ip-range6: fdfe:dcba:9876::1/64 into its default config (and fills
in 2001:2::/64 when IPv6 is on without one), and mihomo then answers
AAAA from that pool too (dns/middleware.go withFakeIP, read at
v1.19.33). The resolver's answer was 198.18.x.y beside fdfe:dcba:9876::z,
the ULA is private, a fake answer beside a private one is refused, so
every name failed.
netguard.FakePaired names that AAAA: an IPv6 address in fc00::/7 or
2001:2::/48 while the same answer holds an IPv4 one in 198.18.0.0/15.
It is dropped where Loon's paired AAAA is dropped (dropLoon in the
gateway's publicTransport, iconIPs for a provider's icon, the two
places that share netguard), and the fake A is judged and pinned in its
place. 2001:2::/48 (RFC 5180 benchmarking) joins Public's special ranges;
it wasn't covered by 2001::/32 and passed as public.
Why this opens nothing new: the AAAA is never dialled. What is dialled
is the fake A, exactly as when the resolver answered the A alone, which
#2593 already allows: an address in 198.18.0.0/15, which no host on a
normal network uses, so without a TUN it goes to the default route and
reaches the internet or nothing. A hostile DNS that pairs 198.18.0.9
with fd00::<a LAN host> gets the 198.18 address dialled, never the LAN
one. Through a proxy on this computer the proxy gets the name, as for a
pure fake answer; a hostile resolver could already give a proxy another
answer for that name than it gave magpie, so the pairing adds no route.
What stays refused (tests below): a ULA or 2001:2:: alone or beside a
real A, a fake pair beside 10.0.0.1 or ::1, a fake A beside a public
address, and ULA / 2001:2:: / 198.18 address literals.
Ran (on mote; whole packages at base f1a4b5751, the -run set, vet and
gofmt again after the rebase onto the next main):
- go test ./internal/netguard ./internal/provider ./internal/gateway
-run 'FakePaired|FakeIP|Loon|Icon|Public|Fetch|Guard|Private': ok
- go test -timeout 45m ./internal/netguard ./internal/provider
./internal/gateway (whole packages): ok (netguard 0.2s, provider
130s, gateway 838s)
- go vet for darwin, GOOS=linux and GOOS=windows on those packages,
gofmt -l internal cmd: clean
Broken by hand: with netguard.go, image_fetch_guard.go and icon.go back
at origin/main, TestFakeIPWithItsIPv6Answer fails with the reporter's
own message ("couldn't fetch http://example.com/: it is at a private
address ..."), TestFakeIPRefusesTheRest lets http://[2001:2::7]/
through, and TestIconIPsLoon refuses fdfe:dcba:9876::5 + 198.18.0.5 and
keeps 2001:2::5. Green with the fix.
Not tried: a real Clash Verge Rev / mihomo TUN on mote or the Mac (the
answers are a fake resolver in the tests, nothing reaches the network);
sing-box's inet6_range is covered only as far as it is a ULA. Not run:
the full ./... suite (mote's suite.lock had five full runs queued).
Reported-by: jiezhengj (Discord)
312 lines
9.6 KiB
Go
312 lines
9.6 KiB
Go
package provider
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/netip"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/iconproxy"
|
|
"github.com/yetone/magpie/internal/netguard"
|
|
)
|
|
|
|
// A provider's own picture lives in the icons folder beside providers.json,
|
|
// named after its content, and the provider's Icon reads "file:<name>". The
|
|
// built-in icons are plain names ("deepseek-color").
|
|
const iconPrefix = "file:"
|
|
|
|
// MaxIcon is the largest picture magpie keeps for a provider.
|
|
const MaxIcon = 1 << 20
|
|
|
|
var iconName = regexp.MustCompile(`^[0-9a-f]{16}\.(png|jpg|gif|webp|ico|svg)$`)
|
|
|
|
// IconDir is where pictures given to providers are kept.
|
|
func IconDir() string { return filepath.Join(filepath.Dir(Path()), "icons") }
|
|
|
|
// IconFile is the path of a stored picture, for a name as StoreIcon made it;
|
|
// "" for anything else, so a request can't reach outside the folder.
|
|
func IconFile(name string) string {
|
|
if !iconName.MatchString(name) {
|
|
return ""
|
|
}
|
|
return filepath.Join(IconDir(), name)
|
|
}
|
|
|
|
// StoreIcon keeps a picture (PNG, JPEG, GIF, WebP, ICO or SVG) and returns
|
|
// the Icon value that points at it. The same picture is stored once.
|
|
func StoreIcon(data []byte) (string, error) {
|
|
if len(data) == 0 {
|
|
return "", errors.New("the picture is empty")
|
|
}
|
|
if len(data) > MaxIcon {
|
|
return "", errors.New("the picture is over 1 MB; pick a smaller one")
|
|
}
|
|
ext := iconExt(data)
|
|
if ext == "" {
|
|
return "", errors.New("not a picture magpie can show: use PNG, JPEG, GIF, WebP, ICO or SVG")
|
|
}
|
|
sum := sha256.Sum256(data)
|
|
name := hex.EncodeToString(sum[:8]) + "." + ext
|
|
if err := os.MkdirAll(IconDir(), 0o755); err != nil {
|
|
return "", err
|
|
}
|
|
if err := os.WriteFile(filepath.Join(IconDir(), name), data, 0o644); err != nil {
|
|
return "", err
|
|
}
|
|
return iconPrefix + name, nil
|
|
}
|
|
|
|
// FetchIcon downloads the picture an import link named and keeps it like
|
|
// any other, returning the Icon value that points at it. It runs when the
|
|
// user confirms the import, never at parse time; the URL is re-checked here
|
|
// so a crafted request can't point magpie at anything but a public https
|
|
// picture, and the read stops one byte past MaxIcon so a huge body is
|
|
// refused rather than buffered. The picture is asked for through
|
|
// usemagpie.ai's icon proxy, so its host never sees the user's IP.
|
|
func FetchIcon(ctx context.Context, rawURL string) (string, error) {
|
|
u, err := iconURL(rawURL)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
c := iconClient()
|
|
// the client is this fetch's alone: the connection it made, left idle
|
|
// in the transport's pool, is dropped by nobody else (#4824)
|
|
defer c.CloseIdleConnections()
|
|
return fetchIcon(ctx, c, iconproxy.URL(u))
|
|
}
|
|
|
|
// iconClient is the client FetchIcon uses; tests point it at a local proxy.
|
|
var iconClient = guardClient
|
|
|
|
// guardClient is the http.Client icon fetches use: the default transport —
|
|
// so the proxy settings still apply — but with dialing wrapped in publicDial.
|
|
//
|
|
// Through a proxy the connection goes to the proxy, often on this computer
|
|
// (Clash's 127.0.0.1:7890), so the proxy's own address is dialed as it is;
|
|
// the site's name is judged before the proxy is asked for, since the proxy
|
|
// resolves it where magpie can't see.
|
|
func guardClient() *http.Client {
|
|
c := *http.DefaultClient
|
|
t := http.DefaultTransport.(*http.Transport).Clone()
|
|
dial := t.DialContext
|
|
var proxies sync.Map // host:port of the proxies this client was sent to
|
|
if proxy := t.Proxy; proxy != nil {
|
|
t.Proxy = func(req *http.Request) (*url.URL, error) {
|
|
u, err := proxy(req)
|
|
if err != nil || u == nil {
|
|
return u, err
|
|
}
|
|
if err := publicHost(req.Context(), req.URL.Hostname()); err != nil {
|
|
return nil, err
|
|
}
|
|
proxies.Store(proxyAddr(u), true)
|
|
return u, nil
|
|
}
|
|
}
|
|
t.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
if _, ok := proxies.Load(addr); ok {
|
|
return dial(ctx, network, addr)
|
|
}
|
|
return publicDial(ctx, dial, network, addr)
|
|
}
|
|
c.Transport = t
|
|
return &c
|
|
}
|
|
|
|
// publicHost checks a site's answers with the same rules as publicDial,
|
|
// for a fetch the proxy will make. A name that doesn't resolve
|
|
// here is left to the proxy, which may be the only way to reach it.
|
|
func publicHost(ctx context.Context, host string) error {
|
|
ips, err := net.DefaultResolver.LookupIPAddr(ctx, host)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
_, err = iconIPs(host, ips)
|
|
return err
|
|
}
|
|
|
|
// iconIPs is ips without the AAAA records Loon synthesizes for an A
|
|
// answer beside them (netguard.LoonPaired), nor a fake-IP resolver's
|
|
// fake AAAA beside its fake A (netguard.FakePaired); that A answer is
|
|
// judged in their place. It is an error when any other is not public.
|
|
func iconIPs(host string, ips []net.IPAddr) ([]net.IPAddr, error) {
|
|
all := make([]netip.Addr, len(ips))
|
|
for i, ip := range ips {
|
|
all[i], _ = netip.AddrFromSlice(ip.IP)
|
|
}
|
|
var allowed []net.IPAddr
|
|
for i, ip := range ips {
|
|
if publicIP(ip.IP) {
|
|
allowed = append(allowed, ip)
|
|
continue
|
|
}
|
|
if netguard.LoonPaired(all[i].Unmap(), all) || netguard.FakePaired(all[i], all) {
|
|
continue
|
|
}
|
|
return nil, errorf("the icon host %s resolves to %s, which is not public", host, ip.IP)
|
|
}
|
|
return allowed, nil
|
|
}
|
|
|
|
// proxyAddr is the host:port the transport dials to reach proxy u.
|
|
func proxyAddr(u *url.URL) string {
|
|
port := u.Port()
|
|
if port == "" {
|
|
port = map[string]string{"https": "443", "socks5": "1080", "socks5h": "1080"}[u.Scheme]
|
|
if port == "" {
|
|
port = "80"
|
|
}
|
|
}
|
|
return net.JoinHostPort(u.Hostname(), port)
|
|
}
|
|
|
|
// publicDial resolves the host and refuses to connect when any address it
|
|
// maps to is not public (except a paired Loon AAAA discarded by iconIPs).
|
|
// Checking the resolved address here, not just the URL's hostname, closes
|
|
// the door on a name that answers public once and
|
|
// private the next time (DNS rebinding): the same resolution this dial uses
|
|
// is the one that is judged.
|
|
func publicDial(ctx context.Context, dial func(context.Context, string, string) (net.Conn, error), network, addr string) (net.Conn, error) {
|
|
host, port, err := net.SplitHostPort(addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ips, err := net.DefaultResolver.LookupIPAddr(ctx, host)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(ips) == 0 {
|
|
return nil, errorf("could not resolve %s", host)
|
|
}
|
|
ips, err = iconIPs(host, ips)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// Dial each resolved address by IP, so nothing resolves twice.
|
|
var lastErr error
|
|
for _, ip := range ips {
|
|
conn, err := dial(ctx, network, net.JoinHostPort(ip.IP.String(), port))
|
|
if err == nil {
|
|
return conn, nil
|
|
}
|
|
lastErr = err
|
|
}
|
|
return nil, lastErr
|
|
}
|
|
|
|
// publicIP is true for an address an icon may be fetched from: one on the
|
|
// open internet (netguard.Public, the gateway's rule), or a fake-IP
|
|
// resolver's answer in 198.18.0.0/15 (netguard.FakeIP): Clash's TUN mode
|
|
// with fake-ip and Surge's enhanced mode answer every name with an
|
|
// address there and carry the connection to the real host through the
|
|
// proxy (#252). No machine on the user's network sits in that range, so a
|
|
// fetch aimed at it reaches the internet host the name stands for, not
|
|
// this computer or the LAN.
|
|
func publicIP(ip net.IP) bool {
|
|
a, ok := netip.AddrFromSlice(ip)
|
|
return ok && (netguard.Public(a) || netguard.FakeIP(a))
|
|
}
|
|
|
|
// fetchIcon is the download itself, split out so tests can point it at a
|
|
// local server; the URL has already passed iconURL.
|
|
func fetchIcon(ctx context.Context, c *http.Client, u string) (string, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
|
defer cancel()
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
req.Header.Set("Accept", "image/*")
|
|
req.Header.Set("User-Agent", iconUA)
|
|
res, err := c.Do(req)
|
|
if err != nil {
|
|
return "", errorf("could not fetch the icon: %v", err)
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusOK {
|
|
return "", errorf("the icon URL answered %s", res.Status)
|
|
}
|
|
b, err := io.ReadAll(io.LimitReader(res.Body, MaxIcon+1))
|
|
if err != nil {
|
|
return "", errorf("could not read the icon: %v", err)
|
|
}
|
|
return StoreIcon(b)
|
|
}
|
|
|
|
// StoreIconFile is StoreIcon for a picture on disk.
|
|
func StoreIconFile(path string) (string, error) {
|
|
st, err := os.Stat(path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if st.Size() > MaxIcon {
|
|
return "", errors.New("the picture is over 1 MB; pick a smaller one")
|
|
}
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return StoreIcon(b)
|
|
}
|
|
|
|
func iconExt(b []byte) string {
|
|
switch http.DetectContentType(b) {
|
|
case "image/png":
|
|
return "png"
|
|
case "image/jpeg":
|
|
return "jpg"
|
|
case "image/gif":
|
|
return "gif"
|
|
case "image/webp":
|
|
return "webp"
|
|
case "image/x-icon", "image/vnd.microsoft.icon":
|
|
return "ico"
|
|
}
|
|
head := bytes.ToLower(b[:min(len(b), 1024)])
|
|
if bytes.Contains(head, []byte("<svg")) {
|
|
return "svg"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// pruneIcons removes pictures no provider points at any more, nor a plugin
|
|
// gave its provider (PluginIcon), nor a partner listed now shows. One
|
|
// picked in the editor is stored before its provider is saved, so a fresh
|
|
// one stays.
|
|
func pruneIcons(f file) {
|
|
used := map[string]bool{}
|
|
for _, p := range f.Providers {
|
|
if name, ok := strings.CutPrefix(p.Icon, iconPrefix); ok {
|
|
used[name] = true
|
|
}
|
|
}
|
|
for _, name := range keptPluginIcons() {
|
|
used[name] = true
|
|
}
|
|
for _, name := range keptPartnerIcons() {
|
|
used[name] = true
|
|
}
|
|
ents, _ := os.ReadDir(IconDir())
|
|
for _, e := range ents {
|
|
if !iconName.MatchString(e.Name()) || used[e.Name()] {
|
|
continue
|
|
}
|
|
if info, err := e.Info(); err == nil && time.Since(info.ModTime()) > time.Hour {
|
|
os.Remove(filepath.Join(IconDir(), e.Name()))
|
|
}
|
|
}
|
|
pruneSmallIcons()
|
|
}
|