Simplify codebase per audit of 0139564
This commit is contained in:
@@ -21,27 +21,25 @@ import (
|
||||
)
|
||||
|
||||
func (s *Server) ConnectClient(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.Header.Get("Byre-Client")
|
||||
host := clientHost(r)
|
||||
if id == "" || host == "" {
|
||||
c := requestClient(r, actor(r))
|
||||
if c.Id == "" || c.Host == "" {
|
||||
writeError(w, http.StatusBadRequest, errors.New("missing Byre-Client or Byre-Host, or Byre-Host isn't percent-encoded"))
|
||||
return
|
||||
}
|
||||
|
||||
ws, err := websocket.Accept(w, r, nil)
|
||||
if err != nil {
|
||||
slog.Warn("accept client websocket", "host", host, "err", err)
|
||||
slog.Warn("accept client websocket", "host", c.Host, "err", err)
|
||||
return
|
||||
}
|
||||
defer ws.CloseNow()
|
||||
|
||||
s.clients.Serve(r.Context(), ws, id, actor(r), host, r.Header.Get("Byre-Commit"), r.Header.Get("Byre-Build"), r.Header.Get("Byre-Device"))
|
||||
s.clients.Serve(r.Context(), ws, c)
|
||||
}
|
||||
|
||||
func (s *Server) SetClientSync(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.Header.Get("Byre-Client")
|
||||
host := clientHost(r)
|
||||
if id == "" || host == "" {
|
||||
c := requestClient(r, actor(r))
|
||||
if c.Id == "" || c.Host == "" {
|
||||
writeError(w, http.StatusBadRequest, errors.New("missing Byre-Client or Byre-Host, or Byre-Host isn't percent-encoded"))
|
||||
return
|
||||
}
|
||||
@@ -56,9 +54,9 @@ func (s *Server) SetClientSync(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
slog.Warn("client icloud sync failing", "id", c.Id, "host", c.Host, "error", *sync.Error)
|
||||
}
|
||||
s.clients.Synced(id, actor(r), host, r.Header.Get("Byre-Commit"), r.Header.Get("Byre-Build"), sync)
|
||||
s.clients.Synced(c, sync)
|
||||
s.clientsChanged()
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
@@ -86,7 +84,7 @@ func (s *Server) ExecClient(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusServiceUnavailable, errors.New("no client Macs are connected"))
|
||||
return
|
||||
}
|
||||
name = recent[0].ID
|
||||
name = recent[0].Id
|
||||
}
|
||||
|
||||
timeout := time.Duration(req.TimeoutMs) * time.Millisecond
|
||||
|
||||
+13
-20
@@ -203,11 +203,22 @@ func (s *Server) identify(next http.Handler) http.Handler {
|
||||
}
|
||||
|
||||
func (s *Server) acted(login string, r *http.Request) {
|
||||
if s.clients.Acted(r.Header.Get("Byre-Client"), login, clientHost(r), r.Header.Get("Byre-Commit"), r.Header.Get("Byre-Build")) {
|
||||
if s.clients.Acted(requestClient(r, login)) {
|
||||
s.clientsChanged()
|
||||
}
|
||||
}
|
||||
|
||||
func requestClient(r *http.Request, login string) api.ConnectedClient {
|
||||
return api.ConnectedClient{
|
||||
Id: r.Header.Get("Byre-Client"),
|
||||
Login: login,
|
||||
Host: clientHost(r),
|
||||
Device: r.Header.Get("Byre-Device"),
|
||||
Commit: r.Header.Get("Byre-Commit"),
|
||||
Build: r.Header.Get("Byre-Build"),
|
||||
}
|
||||
}
|
||||
|
||||
func clientHost(r *http.Request) string {
|
||||
host, err := url.PathUnescape(r.Header.Get("Byre-Host"))
|
||||
if err != nil {
|
||||
@@ -216,24 +227,6 @@ func clientHost(r *http.Request) string {
|
||||
return host
|
||||
}
|
||||
|
||||
func (s *Server) connectedClients() []api.ConnectedClient {
|
||||
out := []api.ConnectedClient{}
|
||||
for _, c := range s.clients.List() {
|
||||
out = append(out, api.ConnectedClient{
|
||||
Id: c.ID,
|
||||
Login: c.Login,
|
||||
Host: c.Host,
|
||||
Device: c.Device,
|
||||
Commit: c.Commit,
|
||||
Build: c.Build,
|
||||
ConnectedAt: c.ConnectedAt,
|
||||
LastActionAt: c.LastActionAt,
|
||||
Sync: c.Sync,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *Server) clientsChanged() {
|
||||
s.store.Broadcast(api.Event{Kind: "admin.clients", Actor: "byre", Payload: map[string]any{}})
|
||||
}
|
||||
@@ -720,7 +713,7 @@ func (s *Server) status(ctx context.Context) (api.Status, error) {
|
||||
Projects: c.Projects,
|
||||
ActiveTasks: c.ActiveTasks,
|
||||
SseClients: s.hub.count(),
|
||||
Clients: s.connectedClients(),
|
||||
Clients: s.clients.List(),
|
||||
LiveAgents: s.agents.LiveCount(),
|
||||
RunningTurns: usage.Running,
|
||||
RateLimits: usage.RateLimits,
|
||||
|
||||
Reference in New Issue
Block a user