| 1 | package mobilelink |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/ed25519" |
| 6 | "crypto/rand" |
| 7 | "crypto/tls" |
| 8 | "encoding/base64" |
| 9 | "encoding/json" |
| 10 | "errors" |
| 11 | "io" |
| 12 | "strings" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/sylvinus/agent-vm/internal/relay" |
| 16 | "github.com/sylvinus/agent-vm/internal/remotelink" |
| 17 | ) |
| 18 | |
| 19 | |
| 20 | |
| 21 | type PairCode struct { |
| 22 | Relay string `json:"r,omitempty"` |
| 23 | Room string `json:"i"` |
| 24 | Token string `json:"t"` |
| 25 | Seed []byte `json:"s"` |
| 26 | Host string `json:"h"` |
| 27 | Expires int64 `json:"e"` |
| 28 | } |
| 29 | type PairedHost struct { |
| 30 | Version int `json:"version"` |
| 31 | Relay string `json:"relay"` |
| 32 | HostPin string `json:"host_pin"` |
| 33 | Lookup string `json:"lookup_token"` |
| 34 | } |
| 35 | |
| 36 | func NewPairCode(origin string, room relay.Invitation, host string) (PairCode, string, error) { |
| 37 | seed := make([]byte, ed25519.SeedSize) |
| 38 | if _, err := rand.Read(seed); err != nil { |
| 39 | return PairCode{}, "", err |
| 40 | } |
| 41 | code := PairCode{Relay: origin, Room: room.ID, Token: room.ClientToken, Seed: seed, Host: host, Expires: room.ExpiresAt.Unix()} |
| 42 | if origin == OfficialRelay { |
| 43 | code.Relay = "" |
| 44 | } |
| 45 | b, err := json.Marshal(code) |
| 46 | return code, "CAGE1:" + string(b), err |
| 47 | } |
| 48 | func (p PairCode) origin() string { |
| 49 | if p.Relay == "" { |
| 50 | return OfficialRelay |
| 51 | } |
| 52 | return p.Relay |
| 53 | } |
| 54 | func pairPin(pin string) bool { |
| 55 | b, e := base64.StdEncoding.DecodeString(strings.TrimPrefix(pin, "sha256:")) |
| 56 | return e == nil && len(b) == 32 && pin == "sha256:"+base64.StdEncoding.EncodeToString(b) |
| 57 | } |
| 58 | func (p PairCode) identity() (*remotelink.Identity, error) { |
| 59 | if len(p.Seed) != ed25519.SeedSize || !pairPin(p.Host) || !validCapability(p.Room) || !validCapability(p.Token) || !time.Now().Before(time.Unix(p.Expires, 0)) { |
| 60 | return nil, errors.New("配对码无效或已过期") |
| 61 | } |
| 62 | return remotelink.IdentityFromKey(ed25519.NewKeyFromSeed(p.Seed)) |
| 63 | } |
| 64 | func pairDeadline(ctx context.Context, c *tls.Conn) func() { |
| 65 | deadline, _ := ctx.Deadline() |
| 66 | _ = c.SetDeadline(deadline) |
| 67 | stop := context.AfterFunc(ctx, func() { c.NetConn().Close() }) |
| 68 | return func() { stop(); c.NetConn().Close() } |
| 69 | } |
| 70 | |
| 71 | |
| 72 | |
| 73 | func AcceptPair(ctx context.Context, p PairCode, hostToken string, host *remotelink.Identity, save func(string) (PairedHost, error)) error { |
| 74 | temp, err := p.identity() |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | ws, err := join(ctx, p.origin(), p.Room, "host", hostToken) |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | conn, err := remotelink.ServerAfterJoin(ctx, ws, host, temp.Pin) |
| 83 | if err != nil { |
| 84 | return err |
| 85 | } |
| 86 | defer pairDeadline(ctx, conn)() |
| 87 | var pin string |
| 88 | if err = json.NewDecoder(io.LimitReader(conn, 256)).Decode(&pin); err != nil || !pairPin(pin) { |
| 89 | return errors.New("手机身份无效") |
| 90 | } |
| 91 | record, err := save(pin) |
| 92 | if err != nil { |
| 93 | return err |
| 94 | } |
| 95 | return json.NewEncoder(conn).Encode(record) |
| 96 | } |
| 97 | func RedeemPair(ctx context.Context, raw, selectedRelay, phonePin string) (string, error) { |
| 98 | var p PairCode |
| 99 | raw = strings.TrimSpace(raw) |
| 100 | if len(raw) <= 4096 && strings.HasPrefix(raw, "CAGE-") { |
| 101 | b, err := base64.RawURLEncoding.DecodeString(raw[5:]) |
| 102 | if err != nil { |
| 103 | return "", errors.New("配对码格式无效") |
| 104 | } |
| 105 | raw = string(b) |
| 106 | } |
| 107 | if len(raw) > 4096 || !strings.HasPrefix(raw, "CAGE1:") || json.Unmarshal([]byte(raw[6:]), &p) != nil || !pairPin(phonePin) { |
| 108 | return "", errors.New("请扫描 Cage 电脑端的配对码") |
| 109 | } |
| 110 | if p.origin() != selectedRelay || !ValidRelayOrigin(selectedRelay) { |
| 111 | return "", errors.New("中继与当前设置不符,请检查自建中继地址") |
| 112 | } |
| 113 | id, err := p.identity() |
| 114 | if err != nil { |
| 115 | return "", err |
| 116 | } |
| 117 | ctx, cancel := context.WithDeadline(ctx, time.Unix(p.Expires, 0)) |
| 118 | defer cancel() |
| 119 | ws, err := join(ctx, selectedRelay, p.Room, "client", p.Token) |
| 120 | if err != nil { |
| 121 | return "", err |
| 122 | } |
| 123 | conn, err := remotelink.Client(ctx, ws, id, p.Host) |
| 124 | if err != nil { |
| 125 | return "", err |
| 126 | } |
| 127 | defer pairDeadline(ctx, conn)() |
| 128 | if err = json.NewEncoder(conn).Encode(phonePin); err != nil { |
| 129 | return "", err |
| 130 | } |
| 131 | var record PairedHost |
| 132 | if json.NewDecoder(io.LimitReader(conn, 4096)).Decode(&record) != nil || record.Version != 1 || record.HostPin != p.Host || record.Relay != selectedRelay || !validCapability(record.Lookup) { |
| 133 | return "", errors.New("电脑未完成配对,请刷新二维码") |
| 134 | } |
| 135 | b, err := json.Marshal(record) |
| 136 | return string(b), err |
| 137 | } |