fe61503e6had a Remote magpie whose address didn't dial look for it on the old address's /24 and this computer's private /24s, and sent GET /v1/models, signed with the provider's gateway key (Authorization, x-api-key and the provider's own headers), to every address that took the connection at the port. On a café's or hotel's network, or with the other computer off, anything listening there got the key in clear and, by answering {"data":[]}, became the provider: the failed request and every later one went to it.18a9a2d88turned the look off. Now an address that takes the connection is first asked to prove it holds the key, with nothing keyed sent: - B POSTs {"nonce": 32 random bytes hex, "key_id": sha256(key) hex} to /v1/magpie/prove (provider.RemoteProvePath), no key, no provider headers, no proxy, no redirects (proves in remote_moved.go). - A (gateway's remote_prove.go) finds the enabled gateway key with that sha256 (access.ProveSecret: constant-time compares over every key, the legacy settings sharing key too, then Check, so an off, expired or orphaned issued key has none) and answers HMAC-SHA256(key, "magpie-prove-v1\n" nonce "\n" address), the address being the one the connection reached it at (http.LocalAddrContextKey); 404 when no key matches. A host that relays B's nonce to A elsewhere gets a proof for A's address, which B, checking against the address it dialled with hmac.Equal, refuses. - Only after that does isRemote ask /v1/models with the key, as before. - The route goes through lanGuard like OIDC's trade (proveAsked: POST, exact path, no gateway key): from another machine only while shared, past the allow list and rebinding check; corsGuard still judges its Origin, the handler refuses anything fromBrowser, and an unknown id from another machine counts against tooManyAuthFails. readOnlyRoutes lists it (it carries no key and answers none). seekAt is lanNeighbours again: the old /24 and this computer's own private /24s, both behind the proof. Not found, and so safe: a remote magpie from before this route (404), one not shared (403), one reached through a port forward, NAT or a Docker port (it sees another local address than the one dialled), and one behind a proxy that needs the provider's headers. Each fails as beforefe61503e6and the user types the new address. Tests (local httptest servers only): - TestRemoteMagpieKeyGoesToNoNeighbour, rewritten from 18a9a2d88's "no address looked at": four impostors that list 200 for anything (no proof route; a well-formed proof under another key; a garbled proof; one relaying B's ask to the real magpie and handing back its proof) are asked only the proof, never see the key or the provider's custom header, and aren't saved; then the real magpie, last in the list, is found and saved. - TestRemoteProof: another key, nonce, address or port is another proof; an IPv4-mapped IPv6 address is the same address. - TestRemoteProveAnswersOnlyTheKeysHolder (gateway): from another machine while shared, the key's holder answers the right HMAC and never the key; an unknown id and a key turned off get 404; Origin, a Mozilla User-Agent and Sec-Fetch-Mode are refused; not shared, 403. - TestRemoteMagpieMovedIsFound now uses a real gateway key on A. Run on mote at this tree (base18a9a2d88): - gofmt -l internal cmd: clean - go vet ./internal/provider ./internal/gateway ./internal/access (darwin, with and without -tags nogui), GOOS=windows go vet, GOOS=linux go vet -tags nogui: ok - go test -tags nogui -count=1 -timeout 45m ./internal/provider ./internal/gateway ./internal/access: provider ok (206s), access ok, gateway FAIL on TestUnroutedHeldReportSaved only ("(under way)"), the open #4300, a file this change doesn't touch; it passes alone with -count=3 on mote. Rebased onto2000ae439(its gateway.go change, the Antigravity format check in models(), sits beside this one's route line). Rerun on mote at the rebased head: gofmt clean; the same four vets ok; go test -tags nogui -count=1 -run 'RemoteMagpie|RemoteProof|RemoteProve|LANNeighbours| Dialable|ReadOnlyKeyEveryRoute|OIDCToken|Antigravity|KeyPage' over the three packages: ok. The full suite was not run again. Broken by hand, each red: - isRemote without the proves check: the impostor with no proof route is sent the key and the header, is saved, and the real magpie is never reached. - RemoteProof without the address: the relaying impostor passes, is sent the key and saved; TestRemoteProof red too. Not tried: a real look across two computers on a LAN (A on mote, B in WSL asfe61503e6did); a real Windows A; IPv6 link-local addresses (not looked at). Residual: a user-chosen weak gateway key (8+ characters) can be guessed offline by an impostor from its sha256; magpie's own keys are 24 random bytes. Push rejected once; rebased ontof4068ccc2(proc only, none of this change's files). Rerun on mote there: gofmt, the same vets and the -run tests above: ok.
400 lines
12 KiB
Go
400 lines
12 KiB
Go
package provider
|
|
|
|
// A Remote magpie whose computer came back at another address
|
|
// (luckymanjc on Discord): computer A's router hands it a new IP now and
|
|
// then, a boot often enough, and B's Remote magpie, saved at the old
|
|
// one, reached nothing until the user typed the new one in. So when B
|
|
// can't connect to it at all, B looks for it on the networks it shares
|
|
// with it: every address of the old one's /24 and of each of B's own
|
|
// private IPv4 networks, at the port A served on.
|
|
//
|
|
// Whatever takes the connection there may be anyone's: on a café's or a
|
|
// hotel's network, or with A simply off, any host listening on that port
|
|
// would have been handed the provider's key in clear and, answering a
|
|
// list, become the provider, given the user's prompts and files. So the
|
|
// key, and the provider's own headers, go to no address that hasn't
|
|
// proved first that it holds the key (proves): B sends a nonce of its
|
|
// own and the key's id (sha256 of the key, RemoteKeyID) to
|
|
// RemoteProvePath, which needs no key, and only a magpie with an enabled
|
|
// gateway key of that id answers the HMAC of the nonce and the address
|
|
// it was reached at (RemoteProof) under the key. Another magpie, an older
|
|
// one without the route, and anything else answer nothing B can check,
|
|
// and are passed over without a keyed request. An address that proved
|
|
// is then asked its model list with the key; one whose list this magpie
|
|
// made (its magpie_via ends with catalog.Self, remote_self.go) is this
|
|
// magpie and is passed over. The address found is saved as the
|
|
// provider's, as if the user had typed it, and the request or list that
|
|
// failed is asked there.
|
|
//
|
|
// An address given by name (a-pc.local, a Tailscale name) is the name's
|
|
// to follow, and a public address isn't looked around: neither is
|
|
// looked for. A look is made at most once a minute for each provider.
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/hmac"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"io"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"regexp"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
)
|
|
|
|
// seekDial is how long each address is given to take the connection.
|
|
var seekDial = 500 * time.Millisecond
|
|
|
|
// seekAnswer is how long an address that took it is given to list.
|
|
var seekAnswer = 5 * time.Second
|
|
|
|
// seekFor is how long a list that failed waits for a look and the list
|
|
// asked again.
|
|
var seekFor = 20 * time.Second
|
|
|
|
// seekEvery is how soon after one look another is made for a provider.
|
|
var seekEvery = time.Minute
|
|
|
|
// seekWide is how many addresses are dialled at once.
|
|
const seekWide = 128
|
|
|
|
// seekNets is how many /24 networks one look covers at most.
|
|
const seekNets = 4
|
|
|
|
var seekMu sync.Mutex
|
|
var seekLast = map[string]time.Time{}
|
|
|
|
// seekAt is the addresses (host:port) a Remote magpie last at host:port
|
|
// may be at now, nearest first. Tests give their own. Whatever takes the
|
|
// connection at one is sent nothing keyed until it proves it holds the
|
|
// key (isRemote, proves).
|
|
var seekAt = lanNeighbours
|
|
|
|
// SeekHere has every look made at ats alone, as if any look made before
|
|
// were long ago, until restore: for the gateway's tests, whose Remote
|
|
// magpie is on loopback.
|
|
func SeekHere(ats []string) (restore func()) {
|
|
seekMu.Lock()
|
|
old := seekAt
|
|
seekAt = func(string, string) []string { return ats }
|
|
clear(seekLast)
|
|
seekMu.Unlock()
|
|
return func() {
|
|
seekMu.Lock()
|
|
seekAt = old
|
|
clear(seekLast)
|
|
seekMu.Unlock()
|
|
}
|
|
}
|
|
|
|
// unconnected says err is a connection that was never made: what a
|
|
// computer that moved, or is off, leaves behind.
|
|
var unconnected = regexp.MustCompile(`dial tcp |connectex|no route to host|host is down|network is unreachable`)
|
|
|
|
// SeekMoved is p, a Remote magpie a request found no one at (err), at
|
|
// the address it was found at on this computer's network, saved there:
|
|
// the request is made again at it. Requests failing at once wait for one
|
|
// look, and one is made at most every seekEvery.
|
|
func SeekMoved(ctx context.Context, p Provider, err error) (Provider, bool) {
|
|
if !p.IsRemoteMagpie() || err == nil || !unconnected.MatchString(err.Error()) {
|
|
return p, false
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, seekFor)
|
|
defer cancel()
|
|
q := seekOnce(ctx, p.ID)
|
|
if q == nil || strings.TrimSpace(q.Chat) == strings.TrimSpace(p.Chat) {
|
|
return p, false
|
|
}
|
|
p.Chat, p.Responses, p.Anthropic, p.Decide = q.Chat, q.Responses, q.Anthropic, q.Decide
|
|
return p, true
|
|
}
|
|
|
|
// a look under way, for those who wait for it
|
|
type seekRun struct {
|
|
done chan struct{}
|
|
found *Provider
|
|
}
|
|
|
|
var seekRunning = map[string]*seekRun{}
|
|
|
|
// seekOnce is seekMoved, made once for those asking at once, and not
|
|
// again within seekEvery.
|
|
func seekOnce(ctx context.Context, id string) *Provider {
|
|
seekMu.Lock()
|
|
if r := seekRunning[id]; r != nil {
|
|
seekMu.Unlock()
|
|
select {
|
|
case <-r.done:
|
|
return r.found
|
|
case <-ctx.Done():
|
|
return nil
|
|
}
|
|
}
|
|
if time.Since(seekLast[id]) < seekEvery {
|
|
seekMu.Unlock()
|
|
return nil
|
|
}
|
|
r := &seekRun{done: make(chan struct{})}
|
|
seekRunning[id], seekLast[id] = r, time.Now()
|
|
seekMu.Unlock()
|
|
r.found, _ = seekMoved(ctx, id)
|
|
seekMu.Lock()
|
|
delete(seekRunning, id)
|
|
seekMu.Unlock()
|
|
close(r.done)
|
|
return r.found
|
|
}
|
|
|
|
// seekMoved looks for provider id at another address, and is it as saved
|
|
// at the address it was found at, if it was.
|
|
func seekMoved(ctx context.Context, id string) (*Provider, bool) {
|
|
p, err := Find(id)
|
|
if err != nil || p.ID != id || !p.IsRemoteMagpie() {
|
|
return nil, false
|
|
}
|
|
u, err := url.Parse(p.Chat)
|
|
if err != nil || u.Port() == "" {
|
|
return nil, false
|
|
}
|
|
if p.Key == "" {
|
|
if on := p.KeysOn(); len(on) > 0 {
|
|
p.Key = on[0].Key
|
|
}
|
|
}
|
|
if p.Key == "" {
|
|
return nil, false
|
|
}
|
|
seekMu.Lock()
|
|
look := seekAt
|
|
seekMu.Unlock()
|
|
open := dialable(ctx, look(u.Hostname(), u.Port()))
|
|
for _, at := range open {
|
|
if ctx.Err() != nil {
|
|
return nil, false
|
|
}
|
|
if !isRemote(ctx, *p, u.Scheme, at) {
|
|
continue
|
|
}
|
|
q, err := Find(id)
|
|
if err != nil || q.Chat != p.Chat {
|
|
return nil, false // changed meanwhile: the user's word stands
|
|
}
|
|
was := q.Chat
|
|
q.Chat, q.Responses, q.Anthropic, q.Decide = u.Scheme+"://"+at, "", "", ""
|
|
if err := Save(*q); err != nil {
|
|
log.Printf("%s: found at %s, not saved: %v", id, at, err)
|
|
return nil, false
|
|
}
|
|
log.Printf("%s: %s didn't answer; found at %s, saved there", id, was, at)
|
|
q, err = Find(id)
|
|
return q, err == nil
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
// isRemote says the magpie at at is p's: it proves it holds p's key, then
|
|
// lists for it, and isn't this magpie. The key goes out only after the
|
|
// proof.
|
|
func isRemote(ctx context.Context, p Provider, scheme, at string) bool {
|
|
if !proves(ctx, scheme, at, p.Key) {
|
|
return false
|
|
}
|
|
p.Chat, p.Responses, p.Anthropic, p.Decide = scheme+"://"+at, "", "", ""
|
|
p.remoteMagpieEndpoints()
|
|
b, err := remoteBody(ctx, p, "GET", "/v1/models", seekAnswer)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
var list struct {
|
|
Data []struct {
|
|
Via []string `json:"magpie_via"`
|
|
} `json:"data"`
|
|
}
|
|
if json.Unmarshal(b, &list) != nil {
|
|
return false
|
|
}
|
|
for _, m := range list.Data {
|
|
if len(m.Via) > 0 {
|
|
// a test's own gateway serves its Remote magpie (LeaveOutSelf)
|
|
return !LeaveOutSelf || m.Via[len(m.Via)-1] != catalog.Self()
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// RemoteProvePath is where a magpie proves, with no key sent to it, that
|
|
// it holds a gateway key: POST {"nonce": hex, "key_id": RemoteKeyID(key)}
|
|
// is answered {"proof": hex of RemoteProof(key, nonce, the address it was
|
|
// reached at)}, or 404 when no enabled gateway key there has that id
|
|
// (the gateway's remote_prove.go).
|
|
const RemoteProvePath = "/v1/magpie/prove"
|
|
|
|
// RemoteKeyID is the id a proof names key by: its sha256, in hex.
|
|
func RemoteKeyID(key string) string {
|
|
sum := sha256.Sum256([]byte(key))
|
|
return hex.EncodeToString(sum[:])
|
|
}
|
|
|
|
// RemoteProof is what a magpie holding key, reached at at (host:port),
|
|
// answers nonce with: HMAC-SHA256 under key of the nonce and the address.
|
|
// The address is in it so that a host passing B's nonce on to the real
|
|
// magpie elsewhere gets a proof for that magpie's address, not its own.
|
|
func RemoteProof(key, nonce, at string) []byte {
|
|
m := hmac.New(sha256.New, []byte(key))
|
|
io.WriteString(m, "magpie-prove-v1\n"+nonce+"\n"+proofAt(at))
|
|
return m.Sum(nil)
|
|
}
|
|
|
|
// proofAt is at with its IP written one way (an IPv4 address mapped into
|
|
// IPv6 as plain IPv4), as both sides of a proof write it.
|
|
func proofAt(at string) string {
|
|
host, port, err := net.SplitHostPort(at)
|
|
if err != nil {
|
|
return at
|
|
}
|
|
if ip := net.ParseIP(host); ip != nil {
|
|
if v4 := ip.To4(); v4 != nil {
|
|
ip = v4
|
|
}
|
|
host = ip.String()
|
|
}
|
|
return net.JoinHostPort(host, port)
|
|
}
|
|
|
|
// proveClient asks for a proof straight at the address dialled, as
|
|
// dialable dialled it (no proxy), and follows no redirect.
|
|
var proveClient = func() *http.Client {
|
|
t := http.DefaultTransport.(*http.Transport).Clone()
|
|
t.Proxy = nil
|
|
return &http.Client{Transport: t, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
|
|
}()
|
|
|
|
// proves says the magpie at at holds key: it answered a nonce of ours with
|
|
// the proof only key's holder, reached at at, can make. Neither key nor
|
|
// any header of the provider's own goes with the ask.
|
|
func proves(ctx context.Context, scheme, at, key string) bool {
|
|
if key == "" {
|
|
return false
|
|
}
|
|
n := make([]byte, 32)
|
|
if _, err := rand.Read(n); err != nil {
|
|
return false
|
|
}
|
|
nonce := hex.EncodeToString(n)
|
|
body, _ := json.Marshal(map[string]string{"nonce": nonce, "key_id": RemoteKeyID(key)})
|
|
ctx, cancel := context.WithTimeout(ctx, seekAnswer)
|
|
defer cancel()
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, scheme+"://"+at+RemoteProvePath, bytes.NewReader(body))
|
|
if err != nil {
|
|
return false
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Accept", "application/json")
|
|
res, err := proveClient.Do(req)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusOK {
|
|
return false
|
|
}
|
|
var out struct {
|
|
Proof string `json:"proof"`
|
|
}
|
|
if json.NewDecoder(io.LimitReader(res.Body, 4<<10)).Decode(&out) != nil {
|
|
return false
|
|
}
|
|
got, err := hex.DecodeString(out.Proof)
|
|
return err == nil && hmac.Equal(got, RemoteProof(key, nonce, at))
|
|
}
|
|
|
|
// dialable is the addresses of ats that take a connection, in ats' order.
|
|
func dialable(ctx context.Context, ats []string) []string {
|
|
ok := make([]bool, len(ats))
|
|
slots := make(chan struct{}, seekWide)
|
|
var wg sync.WaitGroup
|
|
d := net.Dialer{Timeout: seekDial}
|
|
for i, at := range ats {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
select {
|
|
case slots <- struct{}{}:
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
defer func() { <-slots }()
|
|
if c, err := d.DialContext(ctx, "tcp", at); err == nil {
|
|
c.Close()
|
|
ok[i] = true
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
var out []string
|
|
for i, at := range ats {
|
|
if ok[i] {
|
|
out = append(out, at)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// lanNeighbours is every address at port of host's /24 and of each of
|
|
// this computer's own private IPv4 networks' /24, this computer's own
|
|
// addresses and host itself left out. A host that is a name, or not a
|
|
// private IPv4 address, has none.
|
|
func lanNeighbours(host, port string) []string {
|
|
old := net.ParseIP(host).To4()
|
|
if old == nil || !old.IsPrivate() {
|
|
return nil
|
|
}
|
|
own := map[string]bool{old.String(): true}
|
|
nets := []net.IP{old.Mask(net.CIDRMask(24, 32))}
|
|
if addrs, err := net.InterfaceAddrs(); err == nil {
|
|
for _, a := range addrs {
|
|
n, ok := a.(*net.IPNet)
|
|
if !ok {
|
|
continue
|
|
}
|
|
ip := n.IP.To4()
|
|
if ip == nil || !ip.IsPrivate() {
|
|
continue
|
|
}
|
|
own[ip.String()] = true
|
|
base := ip.Mask(net.CIDRMask(24, 32))
|
|
if !containsIP(nets, base) && len(nets) < seekNets {
|
|
nets = append(nets, base)
|
|
}
|
|
}
|
|
}
|
|
var out []string
|
|
for _, base := range nets {
|
|
for i := 1; i < 255; i++ {
|
|
ip := net.IPv4(base[0], base[1], base[2], byte(i)).String()
|
|
if !own[ip] {
|
|
out = append(out, net.JoinHostPort(ip, port))
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func containsIP(ips []net.IP, ip net.IP) bool {
|
|
for _, x := range ips {
|
|
if x.Equal(ip) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|