2026-10-17 23:45:31 -07:00
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2026-10-21 10:48:59 -07:00
|
|
|
"context"
|
2026-10-20 08:55:37 -07:00
|
|
|
"encoding/json"
|
2026-10-17 23:45:31 -07:00
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2026-10-18 00:13:37 -07:00
|
|
|
"io"
|
2026-10-17 23:45:31 -07:00
|
|
|
"log/slog"
|
|
|
|
|
"net/http"
|
2026-10-18 00:13:37 -07:00
|
|
|
"os"
|
2026-10-17 23:45:31 -07:00
|
|
|
"os/exec"
|
2026-10-18 00:13:37 -07:00
|
|
|
"path/filepath"
|
2026-10-21 10:48:59 -07:00
|
|
|
"time"
|
2026-10-17 23:45:31 -07:00
|
|
|
|
|
|
|
|
"github.com/coder/websocket"
|
2026-10-20 08:55:37 -07:00
|
|
|
|
|
|
|
|
"g.fc.run/ai/byre/internal/api"
|
2026-10-21 10:48:59 -07:00
|
|
|
"g.fc.run/ai/byre/internal/clients"
|
2026-10-17 23:45:31 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (s *Server) ConnectClient(w http.ResponseWriter, r *http.Request) {
|
2026-10-18 09:16:31 -07:00
|
|
|
id := r.Header.Get("Byre-Client")
|
2026-10-17 23:45:31 -07:00
|
|
|
host := r.Header.Get("Byre-Host")
|
2026-10-18 09:16:31 -07:00
|
|
|
if id == "" || host == "" {
|
|
|
|
|
writeError(w, http.StatusBadRequest, errors.New("missing Byre-Client or Byre-Host"))
|
2026-10-17 23:45:31 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ws, err := websocket.Accept(w, r, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Warn("accept client websocket", "host", host, "err", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer ws.CloseNow()
|
|
|
|
|
|
2026-10-18 09:16:31 -07:00
|
|
|
s.clients.Serve(r.Context(), ws, id, actor(r), host, r.Header.Get("Byre-Commit"))
|
2026-10-17 23:45:31 -07:00
|
|
|
}
|
|
|
|
|
|
2026-10-20 08:55:37 -07:00
|
|
|
func (s *Server) SetClientSync(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
id := r.Header.Get("Byre-Client")
|
|
|
|
|
host := r.Header.Get("Byre-Host")
|
|
|
|
|
if id == "" || host == "" {
|
|
|
|
|
writeError(w, http.StatusBadRequest, errors.New("missing Byre-Client or Byre-Host"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
sync := api.ClientSync{}
|
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&sync)
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeError(w, http.StatusBadRequest, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !sync.Ok {
|
|
|
|
|
if sync.Error == nil {
|
|
|
|
|
writeError(w, http.StatusBadRequest, errors.New("a failed sync needs an error"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
slog.Warn("client icloud sync failing", "id", id, "host", host, "error", *sync.Error)
|
|
|
|
|
}
|
|
|
|
|
s.clients.Synced(id, actor(r), host, r.Header.Get("Byre-Commit"), sync)
|
|
|
|
|
s.clientsChanged()
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-21 10:48:59 -07:00
|
|
|
const execGrace = 30 * time.Second
|
|
|
|
|
|
|
|
|
|
func (s *Server) ExecClient(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if !s.bearer(r) {
|
|
|
|
|
writeError(w, http.StatusUnauthorized, errors.New("exec needs the loopback token as a bearer token"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
req := api.ClientExec{}
|
|
|
|
|
if !readJSON(w, r, &req) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if req.TimeoutMs <= 0 {
|
|
|
|
|
writeError(w, http.StatusBadRequest, errors.New("timeout_ms must be positive"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
name := ""
|
|
|
|
|
if req.Client != nil {
|
|
|
|
|
name = *req.Client
|
|
|
|
|
}
|
|
|
|
|
if name == "" {
|
|
|
|
|
recent := s.clients.Recent()
|
|
|
|
|
if len(recent) == 0 {
|
|
|
|
|
writeError(w, http.StatusServiceUnavailable, errors.New("no client Macs are connected"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
name = recent[0].ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timeout := time.Duration(req.TimeoutMs) * time.Millisecond
|
|
|
|
|
ctx, cancel := context.WithTimeout(r.Context(), timeout+execGrace)
|
|
|
|
|
defer cancel()
|
|
|
|
|
start := time.Now()
|
|
|
|
|
reply, err := s.clients.Call(ctx, name, api.ClientRequest{Op: api.Exec, Command: &req.Command, TimeoutMs: &req.TimeoutMs})
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Info("client exec", "client", name, "command", req.Command, "err", err, "duration", time.Since(start))
|
|
|
|
|
status := http.StatusInternalServerError
|
|
|
|
|
if errors.Is(err, clients.ErrUnavailable) {
|
|
|
|
|
status = http.StatusServiceUnavailable
|
|
|
|
|
}
|
|
|
|
|
writeError(w, status, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if reply.ExitCode == nil || reply.Stdout == nil || reply.Stderr == nil {
|
|
|
|
|
writeError(w, http.StatusInternalServerError, fmt.Errorf("%s's reply has no exit_code, stdout or stderr", name))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
slog.Info("client exec", "client", name, "command", req.Command, "exit", *reply.ExitCode, "duration", time.Since(start))
|
|
|
|
|
writeJSON(w, http.StatusOK, api.ClientExecResult{Client: name, ExitCode: *reply.ExitCode, Stdout: *reply.Stdout, Stderr: *reply.Stderr})
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-17 23:45:31 -07:00
|
|
|
func (s *Server) GetClientTransfer(w http.ResponseWriter, r *http.Request, transferID string) {
|
|
|
|
|
t, ok := s.clients.Transfer(transferID)
|
2026-10-18 00:13:37 -07:00
|
|
|
if !ok || t.Receive {
|
|
|
|
|
writeError(w, http.StatusNotFound, fmt.Errorf("no transfer %s to send", transferID))
|
2026-10-17 23:45:31 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/gzip")
|
|
|
|
|
cmd := exec.CommandContext(r.Context(), "tar", "-czf", "-", "-C", t.Dir, "--", t.Name)
|
|
|
|
|
cmd.Stdout = w
|
|
|
|
|
stderr := &bytes.Buffer{}
|
|
|
|
|
cmd.Stderr = stderr
|
|
|
|
|
err := cmd.Run()
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error("stream client transfer", "transfer", transferID, "dir", t.Dir, "name", t.Name, "err", err, "stderr", stderr.String())
|
|
|
|
|
panic(http.ErrAbortHandler)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-18 00:13:37 -07:00
|
|
|
|
|
|
|
|
func (s *Server) PutClientTransfer(w http.ResponseWriter, r *http.Request, transferID string) {
|
|
|
|
|
t, ok := s.clients.Transfer(transferID)
|
|
|
|
|
if !ok || !t.Receive {
|
|
|
|
|
writeError(w, http.StatusNotFound, fmt.Errorf("no transfer %s to receive", transferID))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f, err := os.OpenFile(filepath.Join(t.Dir, t.Name), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeError(w, http.StatusNotFound, fmt.Errorf("transfer %s: %w", transferID, err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
_, err = io.Copy(f, r.Body)
|
|
|
|
|
cerr := f.Close()
|
|
|
|
|
if err == nil {
|
|
|
|
|
err = cerr
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeError(w, http.StatusInternalServerError, fmt.Errorf("receive transfer %s: %w", transferID, err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
}
|