-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrexec.go
More file actions
334 lines (282 loc) · 9.39 KB
/
Copy pathrexec.go
File metadata and controls
334 lines (282 loc) · 9.39 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
// Package rexec provides a Go SDK for interacting with Rexec - Terminal as a Service.
//
// The SDK allows you to programmatically create, manage, and interact with
// sandboxed Linux environments through the Rexec API.
//
// Basic usage:
//
// client := rexec.NewClient("https://rexec.sh", "your-api-token")
//
// // Create a sandbox (preferred)
// sandbox, err := client.Sandboxes.Create(ctx, &rexec.CreateSandboxRequest{
// Image: "ubuntu",
// Name: "my-sandbox",
// })
// // Legacy: client.Containers is the same service
//
// term, err := client.Terminal.Connect(ctx, sandbox.ID)
// term.Write([]byte("echo hello\n"))
package rexec
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/gorilla/websocket"
)
// Client is the main Rexec API client.
type Client struct {
baseURL string
token string
httpClient *http.Client
// Sandboxes is the preferred accessor for sandbox lifecycle APIs.
Sandboxes *SandboxService
// Containers is a deprecated alias for Sandboxes (same pointer). Prefer Sandboxes.
Containers *SandboxService
Files *FileService
Terminal *TerminalService
}
// NewClient creates a new Rexec client.
func NewClient(baseURL, token string) *Client {
baseURL = strings.TrimSuffix(baseURL, "/")
c := &Client{
baseURL: baseURL,
token: token,
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
}
svc := &SandboxService{client: c}
c.Sandboxes = svc
c.Containers = svc
c.Files = &FileService{client: c}
c.Terminal = &TerminalService{client: c}
return c
}
// SetHTTPClient sets a custom HTTP client.
func (c *Client) SetHTTPClient(httpClient *http.Client) {
c.httpClient = httpClient
}
func (c *Client) doRequest(ctx context.Context, method, path string, body interface{}, result interface{}) error {
var bodyReader io.Reader
if body != nil {
jsonBody, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("failed to marshal request body: %w", err)
}
bodyReader = bytes.NewReader(jsonBody)
}
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, bodyReader)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+c.token)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
var errResp ErrorResponse
if err := json.NewDecoder(resp.Body).Decode(&errResp); err != nil {
return fmt.Errorf("request failed with status %d", resp.StatusCode)
}
return &APIError{
StatusCode: resp.StatusCode,
Message: errResp.Error,
}
}
if result != nil {
if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
return fmt.Errorf("failed to decode response: %w", err)
}
}
return nil
}
func (c *Client) websocketURL(path string) string {
u, _ := url.Parse(c.baseURL)
if u.Scheme == "https" {
u.Scheme = "wss"
} else {
u.Scheme = "ws"
}
u.Path = path
return u.String()
}
// ErrorResponse represents an API error response.
type ErrorResponse struct {
Error string `json:"error"`
}
// APIError represents an API error.
type APIError struct {
StatusCode int
Message string
}
func (e *APIError) Error() string {
return fmt.Sprintf("API error %d: %s", e.StatusCode, e.Message)
}
// Sandbox represents a Rexec sandbox (isolated Linux environment).
// Wire protocol still uses the /api/containers resource.
type Sandbox struct {
ID string `json:"id"`
Name string `json:"name"`
Image string `json:"image"`
Status string `json:"status"`
CreatedAt time.Time `json:"created_at"`
StartedAt *time.Time `json:"started_at,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Environment map[string]string `json:"environment,omitempty"`
}
// Container is a deprecated alias for Sandbox.
// Deprecated: use Sandbox.
type Container = Sandbox
// CreateSandboxRequest represents a request to create a sandbox.
// Prefer image aliases such as "ubuntu".
type CreateSandboxRequest struct {
Name string `json:"name,omitempty"`
Image string `json:"image"`
Environment map[string]string `json:"environment,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}
// CreateContainerRequest is a deprecated alias for CreateSandboxRequest.
// Deprecated: use CreateSandboxRequest.
type CreateContainerRequest = CreateSandboxRequest
// SandboxService handles sandbox lifecycle operations.
// HTTP paths remain /api/containers for backend compatibility.
type SandboxService struct {
client *Client
}
// ContainerService is a deprecated alias for SandboxService.
// Deprecated: use SandboxService.
type ContainerService = SandboxService
// containerListResponse matches the production API list payload.
type containerListResponse struct {
Containers []Sandbox `json:"containers"`
Count int `json:"count"`
Limit int `json:"limit"`
}
// List returns all sandboxes for the authenticated user.
func (s *SandboxService) List(ctx context.Context) ([]Sandbox, error) {
var wrapped containerListResponse
if err := s.client.doRequest(ctx, http.MethodGet, "/api/containers", nil, &wrapped); err != nil {
return nil, err
}
if wrapped.Containers == nil {
return []Sandbox{}, nil
}
return wrapped.Containers, nil
}
// Get returns a sandbox by ID.
func (s *SandboxService) Get(ctx context.Context, id string) (*Sandbox, error) {
var sandbox Sandbox
err := s.client.doRequest(ctx, http.MethodGet, "/api/containers/"+id, nil, &sandbox)
return &sandbox, err
}
// Create creates a new sandbox.
func (s *SandboxService) Create(ctx context.Context, req *CreateSandboxRequest) (*Sandbox, error) {
var sandbox Sandbox
err := s.client.doRequest(ctx, http.MethodPost, "/api/containers", req, &sandbox)
return &sandbox, err
}
// Delete deletes a sandbox.
func (s *SandboxService) Delete(ctx context.Context, id string) error {
return s.client.doRequest(ctx, http.MethodDelete, "/api/containers/"+id, nil, nil)
}
// Start starts a stopped sandbox.
func (s *SandboxService) Start(ctx context.Context, id string) error {
return s.client.doRequest(ctx, http.MethodPost, "/api/containers/"+id+"/start", nil, nil)
}
// Stop stops a running sandbox.
func (s *SandboxService) Stop(ctx context.Context, id string) error {
return s.client.doRequest(ctx, http.MethodPost, "/api/containers/"+id+"/stop", nil, nil)
}
// FileInfo represents file metadata.
type FileInfo struct {
Name string `json:"name"`
Path string `json:"path"`
Size int64 `json:"size"`
Mode string `json:"mode"`
ModTime time.Time `json:"mod_time"`
IsDir bool `json:"is_dir"`
}
// FileService handles file operations.
type FileService struct {
client *Client
}
// List lists files in a container directory.
func (s *FileService) List(ctx context.Context, containerID, path string) ([]FileInfo, error) {
var files []FileInfo
endpoint := fmt.Sprintf("/api/containers/%s/files/list?path=%s", containerID, url.QueryEscape(path))
err := s.client.doRequest(ctx, http.MethodGet, endpoint, nil, &files)
return files, err
}
// Download downloads a file from a container.
func (s *FileService) Download(ctx context.Context, containerID, path string) ([]byte, error) {
endpoint := fmt.Sprintf("/api/containers/%s/files?path=%s", containerID, url.QueryEscape(path))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.client.baseURL+endpoint, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+s.client.token)
resp, err := s.client.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("download failed with status %d", resp.StatusCode)
}
return io.ReadAll(resp.Body)
}
// Mkdir creates a directory in a container.
func (s *FileService) Mkdir(ctx context.Context, containerID, path string) error {
return s.client.doRequest(ctx, http.MethodPost, "/api/containers/"+containerID+"/files/mkdir", map[string]string{"path": path}, nil)
}
// Terminal represents a WebSocket terminal connection.
type Terminal struct {
conn *websocket.Conn
}
// TerminalService handles terminal connections.
type TerminalService struct {
client *Client
}
// Connect establishes a WebSocket terminal connection to a container.
func (s *TerminalService) Connect(ctx context.Context, containerID string) (*Terminal, error) {
wsURL := s.client.websocketURL("/ws/terminal/" + containerID)
header := http.Header{}
header.Set("Authorization", "Bearer "+s.client.token)
conn, _, err := websocket.DefaultDialer.DialContext(ctx, wsURL, header)
if err != nil {
return nil, fmt.Errorf("failed to connect to terminal: %w", err)
}
return &Terminal{conn: conn}, nil
}
// Write sends data to the terminal.
func (t *Terminal) Write(data []byte) error {
return t.conn.WriteMessage(websocket.BinaryMessage, data)
}
// Read reads data from the terminal.
func (t *Terminal) Read() ([]byte, error) {
_, data, err := t.conn.ReadMessage()
return data, err
}
// Resize resizes the terminal.
func (t *Terminal) Resize(cols, rows int) error {
msg := map[string]interface{}{
"type": "resize",
"cols": cols,
"rows": rows,
}
return t.conn.WriteJSON(msg)
}
// Close closes the terminal connection.
func (t *Terminal) Close() error {
return t.conn.Close()
}