-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelay_api.go
More file actions
494 lines (424 loc) · 14.3 KB
/
Copy pathrelay_api.go
File metadata and controls
494 lines (424 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/gorilla/websocket"
)
// RelayClient handles API calls to the relay server
type RelayClient struct {
baseURL string
httpClient *http.Client
pcConfig *PCConfig
}
// NewRelayClient creates a new relay API client
func NewRelayClient(relayURL string, pcConfig *PCConfig) *RelayClient {
// Convert WebSocket URL to HTTP URL
baseURL := relayURL
baseURL = strings.Replace(baseURL, "wss://", "https://", 1)
baseURL = strings.Replace(baseURL, "ws://", "http://", 1)
return &RelayClient{
baseURL: baseURL,
httpClient: &http.Client{
Timeout: HTTPClientTimeout,
},
pcConfig: pcConfig,
}
}
// setPCHeaders sets X-PC-ID and Authorization headers on a request
func (c *RelayClient) setPCHeaders(req *http.Request) {
req.Header.Set("X-PC-ID", c.pcConfig.PCID)
if c.pcConfig.Secret != "" {
req.Header.Set("Authorization", "Bearer "+c.pcConfig.Secret)
}
}
// --- Pairing API ---
// PairingInitRequest is the request body for POST /api/pairing/init
type PairingInitRequest struct {
PCID string `json:"pc_id"`
PCName string `json:"pc_name"`
PublicKey string `json:"public_key"`
}
// PairingInitResponse is the response from POST /api/pairing/init
type PairingInitResponse struct {
Token string `json:"token"`
ExpiresAt string `json:"expires_at"`
Secret string `json:"secret,omitempty"`
}
// InitPairing initiates a pairing request and returns a token
func (c *RelayClient) InitPairing() (*PairingInitResponse, error) {
req := PairingInitRequest{
PCID: c.pcConfig.PCID,
PCName: c.pcConfig.PCName,
PublicKey: c.pcConfig.PublicKey,
}
body, err := json.Marshal(req)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Post(
c.baseURL+"/api/pairing/init",
"application/json",
bytes.NewReader(body),
)
if err != nil {
return nil, fmt.Errorf("failed to connect to relay: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("pairing init failed: %s (failed to read response: %v)", resp.Status, err)
}
return nil, fmt.Errorf("pairing init failed: %s - %s", resp.Status, string(respBody))
}
var result PairingInitResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
// Save secret if provided by relay (first registration or re-registration)
if result.Secret != "" && c.pcConfig.Secret == "" {
c.pcConfig.Secret = result.Secret
savePCConfig(c.pcConfig)
}
return &result, nil
}
// PairingStatusResponse is the response when checking pairing status
type PairingStatusResponse struct {
Status string `json:"status"` // "pending", "completed", "expired"
MobileID string `json:"mobile_id,omitempty"`
MobileName string `json:"mobile_name,omitempty"`
PublicKey string `json:"public_key,omitempty"`
}
// CheckPairingStatus checks if a pairing has been completed (HTTP polling - kept for fallback)
func (c *RelayClient) CheckPairingStatus(token string) (*PairingStatusResponse, error) {
resp, err := c.httpClient.Get(c.baseURL + "/api/pairing/status?token=" + token)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("pairing status check failed: %s (failed to read response: %v)", resp.Status, err)
}
return nil, fmt.Errorf("pairing status check failed: %s - %s", resp.Status, string(respBody))
}
var result PairingStatusResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}
// WaitPairingViaWebSocket connects to the relay WebSocket and waits for pairing completion.
// This replaces HTTP polling (~150 requests per pairing → 1 WebSocket connection).
// Returns the pairing result or an error. Falls back to HTTP polling on WS failure.
func (c *RelayClient) WaitPairingViaWebSocket(token string, timeout time.Duration) (*PairingStatusResponse, error) {
// Build WebSocket URL from HTTP base URL
wsURL := c.baseURL
wsURL = strings.Replace(wsURL, "https://", "wss://", 1)
wsURL = strings.Replace(wsURL, "http://", "ws://", 1)
wsURL += "/ws/pairing/" + token
conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
if err != nil {
// Fallback to HTTP polling
return c.pollPairingStatus(token, timeout)
}
defer conn.Close()
// Set read deadline for timeout
conn.SetReadDeadline(time.Now().Add(timeout))
// Start ping goroutine to keep connection alive
done := make(chan struct{})
defer close(done)
go func() {
ticker := time.NewTicker(PingInterval)
defer ticker.Stop()
for {
select {
case <-done:
return
case <-ticker.C:
conn.WriteJSON(map[string]string{"type": "ping"})
}
}
}()
for {
_, message, err := conn.ReadMessage()
if err != nil {
return nil, fmt.Errorf("pairing WebSocket error: %w", err)
}
var result PairingStatusResponse
if err := json.Unmarshal(message, &result); err != nil {
continue // Ignore non-JSON messages (e.g. pong)
}
switch result.Status {
case "completed":
return &result, nil
case "expired":
return &result, nil
case "":
// Ignore messages without status (e.g. pong)
continue
}
}
}
// pollPairingStatus is the HTTP polling fallback for pairing status
func (c *RelayClient) pollPairingStatus(token string, timeout time.Duration) (*PairingStatusResponse, error) {
timeoutCh := time.After(timeout)
ticker := time.NewTicker(PairingPollInterval)
defer ticker.Stop()
for {
select {
case <-timeoutCh:
return &PairingStatusResponse{Status: "expired"}, nil
case <-ticker.C:
status, err := c.CheckPairingStatus(token)
if err != nil {
continue
}
if status.Status != "pending" {
return status, nil
}
}
}
}
// --- Session API ---
// CreateSessionRequest is the request body for POST /api/sessions
type CreateSessionRequest struct {
PCID string `json:"pc_id"`
AgentType string `json:"agent_type"`
WorkingDir string `json:"working_dir"`
DisplayName string `json:"display_name"` // Short name for display
Token string `json:"token,omitempty"` // Session token for E2E encryption
EncryptedTokens map[string]string `json:"encrypted_tokens"` // mobile_id -> encrypted token
// SSH info for auto-setup
SSHAvailable bool `json:"ssh_available,omitempty"`
SSHPort int `json:"ssh_port,omitempty"`
Hostname string `json:"hostname,omitempty"`
Username string `json:"username,omitempty"`
IPs []string `json:"ips,omitempty"` // Local network IPs
}
// CreateSessionResponse is the response from POST /api/sessions
type CreateSessionResponse struct {
SessionID string `json:"session_id"`
Token string `json:"token"` // Session token for WebSocket auth
}
// SSHInfo contains SSH availability information for a session
type SSHInfo struct {
Available bool
Port int
Hostname string
Username string
IPs []string
}
// CreateSession registers a new session on the relay
// It encrypts the session token for each paired mobile device
func (c *RelayClient) CreateSession(agentType, workDir, displayName string, sshInfo *SSHInfo) (*CreateSessionResponse, error) {
// Get the PC's private key for encryption
pcPrivateKey, err := GetPrivateKeyFromHex(c.pcConfig.PrivateKey)
if err != nil {
return nil, fmt.Errorf("failed to get private key: %w", err)
}
// Generate a session token (will be returned by relay, but we need to encrypt it for each mobile)
// For now, we'll create the session first and then update with encrypted tokens
// Actually, let's generate a token locally and encrypt it before sending
sessionToken := generateRandomToken()
// Encrypt token for each paired mobile
encryptedTokens := make(map[string]string)
for _, mobile := range c.pcConfig.PairedMobiles {
if mobile.PublicKey == "" {
// Skip mobiles without public key (legacy pairing)
continue
}
encrypted, err := EncryptForMobile(sessionToken, mobile.PublicKey, pcPrivateKey)
if err != nil {
// Log but don't fail - mobile might not be able to connect directly
fmt.Printf("Warning: Could not encrypt token for %s: %v\n", mobile.Name, err)
continue
}
encryptedTokens[mobile.ID] = encrypted
}
req := CreateSessionRequest{
PCID: c.pcConfig.PCID,
AgentType: agentType,
WorkingDir: workDir,
DisplayName: displayName,
Token: sessionToken,
EncryptedTokens: encryptedTokens,
}
// Add SSH info if available
if sshInfo != nil {
req.SSHAvailable = sshInfo.Available
req.SSHPort = sshInfo.Port
req.Hostname = sshInfo.Hostname
req.Username = sshInfo.Username
req.IPs = sshInfo.IPs
}
body, err := json.Marshal(req)
if err != nil {
return nil, err
}
httpReq, err := http.NewRequest("POST", c.baseURL+"/api/sessions", bytes.NewReader(body))
if err != nil {
return nil, err
}
httpReq.Header.Set("Content-Type", "application/json")
c.setPCHeaders(httpReq)
resp, err := c.httpClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("failed to create session: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("session creation failed: %s (failed to read response: %v)", resp.Status, err)
}
return nil, fmt.Errorf("session creation failed: %s - %s", resp.Status, string(respBody))
}
var result CreateSessionResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
// Override the token with our locally generated one that matches the encrypted versions
result.Token = sessionToken
return &result, nil
}
// AddSessionTokenForMobile adds an encrypted token for a newly paired mobile
func (c *RelayClient) AddSessionTokenForMobile(sessionID, mobileID, encryptedToken string) error {
payload := map[string]string{
"mobile_id": mobileID,
"encrypted_token": encryptedToken,
}
body, err := json.Marshal(payload)
if err != nil {
return err
}
httpReq, err := http.NewRequest("POST", c.baseURL+"/api/sessions/"+sessionID+"/tokens", bytes.NewReader(body))
if err != nil {
return err
}
httpReq.Header.Set("Content-Type", "application/json")
c.setPCHeaders(httpReq)
resp, err := c.httpClient.Do(httpReq)
if err != nil {
return fmt.Errorf("failed to add session token: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("add session token failed: %s (failed to read response: %v)", resp.Status, err)
}
return fmt.Errorf("add session token failed: %s - %s", resp.Status, string(respBody))
}
return nil
}
// DeleteSession removes a session from the relay
func (c *RelayClient) DeleteSession(sessionID string) error {
httpReq, err := http.NewRequest("DELETE", c.baseURL+"/api/sessions/"+sessionID, nil)
if err != nil {
return err
}
c.setPCHeaders(httpReq)
resp, err := c.httpClient.Do(httpReq)
if err != nil {
return fmt.Errorf("failed to delete session: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("session deletion failed: %s (failed to read response: %v)", resp.Status, err)
}
return fmt.Errorf("session deletion failed: %s - %s", resp.Status, string(respBody))
}
return nil
}
// PurgeAllSessions removes all sessions for this PC from the relay
func (c *RelayClient) PurgeAllSessions() (int, error) {
httpReq, err := http.NewRequest("DELETE", c.baseURL+"/api/sessions", nil)
if err != nil {
return 0, err
}
c.setPCHeaders(httpReq)
resp, err := c.httpClient.Do(httpReq)
if err != nil {
return 0, fmt.Errorf("failed to purge sessions: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("session purge failed: %s (failed to read response: %v)", resp.Status, err)
}
return 0, fmt.Errorf("session purge failed: %s - %s", resp.Status, string(respBody))
}
var result struct {
Success bool `json:"success"`
DeletedCount int `json:"deleted_count"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return 0, err
}
return result.DeletedCount, nil
}
// --- Mobile Management API ---
// SessionInfo represents a session returned by the relay for CLI queries
type SessionInfo struct {
ID string `json:"id"`
AgentType string `json:"agent_type"`
WorkingDir string `json:"working_dir"`
DisplayName string `json:"display_name"`
Status string `json:"status"`
Token string `json:"token,omitempty"`
CreatedAt string `json:"created_at"`
}
// ListAllSessions returns all sessions for this PC
func (c *RelayClient) ListAllSessions() ([]SessionInfo, error) {
reqURL := c.baseURL + "/api/sessions?for_cli=true"
httpReq, err := http.NewRequest("GET", reqURL, nil)
if err != nil {
return nil, err
}
c.setPCHeaders(httpReq)
resp, err := c.httpClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("failed to list sessions: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
respBody, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("list sessions failed: %s - %s", resp.Status, string(respBody))
}
var sessions []SessionInfo
if err := json.NewDecoder(resp.Body).Decode(&sessions); err != nil {
return nil, err
}
return sessions, nil
}
// UnpairMobile removes a paired mobile
func (c *RelayClient) UnpairMobile(mobileID string) error {
httpReq, err := http.NewRequest("DELETE", c.baseURL+"/api/pairing/mobiles/"+mobileID, nil)
if err != nil {
return err
}
c.setPCHeaders(httpReq)
resp, err := c.httpClient.Do(httpReq)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to unpair mobile: %s (failed to read response: %v)", resp.Status, err)
}
return fmt.Errorf("failed to unpair mobile: %s - %s", resp.Status, string(respBody))
}
return nil
}