Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

9.3 KB
Raw Blame
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
	}
	return fetchIcon(ctx, iconClient(), 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; that A answer is judged in
// their place), or 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) {
			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()
}