| 1 | |
| 2 | |
| 3 | |
| 4 | package cagemobile |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "encoding/json" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/sylvinus/agent-vm/internal/mobilelink" |
| 12 | "github.com/sylvinus/agent-vm/internal/mobilelink/nativeclient" |
| 13 | ) |
| 14 | |
| 15 | func DefaultRelay() string { return mobilelink.OfficialRelay } |
| 16 | |
| 17 | type DeviceSigner interface { |
| 18 | PublicKeyDER() ([]byte, error) |
| 19 | SignSHA256(digest []byte) ([]byte, error) |
| 20 | } |
| 21 | |
| 22 | func Fingerprint(device DeviceSigner) (string, error) { return nativeclient.Fingerprint(device) } |
| 23 | |
| 24 | type Client struct{ native *nativeclient.Client } |
| 25 | |
| 26 | func Open(relayOrigin, roomID, clientToken, trustedHostPin string, device DeviceSigner) (*Client, error) { |
| 27 | client, err := nativeclient.Open(relayOrigin, roomID, clientToken, trustedHostPin, device) |
| 28 | if err != nil { |
| 29 | return nil, err |
| 30 | } |
| 31 | return &Client{native: client}, nil |
| 32 | } |
| 33 | |
| 34 | func OpenSaved(relayOrigin, lookupToken, trustedHostPin string, device DeviceSigner) (*Client, error) { |
| 35 | client, err := nativeclient.OpenSaved(relayOrigin, lookupToken, trustedHostPin, device) |
| 36 | if err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | return &Client{native: client}, nil |
| 40 | } |
| 41 | |
| 42 | func (c *Client) StartUI(directory string) (string, error) { return c.native.StartUI(directory) } |
| 43 | func (c *Client) Close() error { return c.native.Close() } |
| 44 | |
| 45 | |
| 46 | |
| 47 | func (c *Client) ReportDevice(platform, model string) { |
| 48 | body, _ := json.Marshal(mobilelink.DeviceInfo{Platform: platform, Model: mobilelink.DeviceModel(platform, model)}) |
| 49 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 50 | defer cancel() |
| 51 | _, _ = c.native.Do(ctx, "POST", "/api/mobile/device", body) |
| 52 | } |
| 53 | |
| 54 | func Pair(code, relay string, device DeviceSigner) (string, error) { |
| 55 | pin, err := Fingerprint(device) |
| 56 | if err != nil { |
| 57 | return "", err |
| 58 | } |
| 59 | ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second) |
| 60 | defer cancel() |
| 61 | return mobilelink.RedeemPair(ctx, code, relay, pin) |
| 62 | } |