2023-12-23 07:18:30 +01:00
|
|
|
//go:build linux
|
2023-12-06 02:09:43 +01:00
|
|
|
|
|
|
|
package autotype
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-04-28 21:50:22 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2023-12-06 02:09:43 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/godbus/dbus/v5"
|
2024-01-19 08:55:37 +01:00
|
|
|
"github.com/quexten/goldwarden/logging"
|
2023-12-06 02:09:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var globalID = 0
|
|
|
|
|
|
|
|
const autoTypeDelay = 1 * time.Millisecond
|
|
|
|
|
2024-01-19 08:55:37 +01:00
|
|
|
var log = logging.GetLogger("Goldwarden", "Autotype")
|
|
|
|
|
2024-04-28 21:50:22 +02:00
|
|
|
// todo need to store this encrypted. will be done when migrating this file to python
|
|
|
|
func persistToken(token string) error {
|
|
|
|
tokenPath := ""
|
|
|
|
userHome, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat("/.flatpak-info"); err == nil {
|
|
|
|
tokenPath = userHome + "/.var/app/com.quexten.Goldwarden/config/autotype_token.txt"
|
|
|
|
} else {
|
|
|
|
tokenPath = userHome + "/.config/goldwarden/autotype_token.txt"
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(tokenPath, []byte(token), 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readToken() (string, error) {
|
|
|
|
tokenPath := ""
|
|
|
|
userHome, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat("/.flatpak-info"); err == nil {
|
|
|
|
tokenPath = userHome + "/.var/app/com.quexten.Goldwarden/config/autotype_token.txt"
|
|
|
|
} else {
|
|
|
|
tokenPath = userHome + "/.config/goldwarden/autotype_token.txt"
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := ioutil.ReadFile(tokenPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(token), nil
|
|
|
|
}
|
|
|
|
|
2023-12-23 07:18:30 +01:00
|
|
|
func TypeString(textToType string) {
|
2024-01-19 08:55:37 +01:00
|
|
|
log.Info("Starting to Type String")
|
2023-12-06 02:09:43 +01:00
|
|
|
bus, err := dbus.SessionBus()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := bus.Object("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop")
|
|
|
|
obj.AddMatchSignal("org.freedesktop.portal.Request", "Response")
|
|
|
|
|
|
|
|
globalID++
|
2024-01-19 08:55:37 +01:00
|
|
|
res0 := obj.Call("org.freedesktop.portal.RemoteDesktop.CreateSession", 0, map[string]dbus.Variant{
|
2023-12-06 02:09:43 +01:00
|
|
|
"session_handle_token": dbus.MakeVariant("u" + fmt.Sprint(globalID)),
|
|
|
|
})
|
2024-01-19 08:55:37 +01:00
|
|
|
if res0.Err != nil {
|
|
|
|
log.Error("Error creating session: %s", res0.Err.Error())
|
|
|
|
return
|
|
|
|
}
|
2023-12-06 02:09:43 +01:00
|
|
|
|
|
|
|
signals := make(chan *dbus.Signal, 10)
|
|
|
|
bus.Signal(signals)
|
|
|
|
|
|
|
|
var state = 0
|
|
|
|
var sessionHandle dbus.ObjectPath
|
|
|
|
|
|
|
|
for {
|
2024-03-15 16:23:11 +01:00
|
|
|
message := <-signals
|
|
|
|
switch state {
|
|
|
|
case 0:
|
|
|
|
log.Info("Selecting Devices")
|
|
|
|
result := message.Body[1].(map[string]dbus.Variant)
|
|
|
|
resultSessionHandle := result["session_handle"]
|
|
|
|
sessionHandle = dbus.ObjectPath(resultSessionHandle.String()[1 : len(resultSessionHandle.String())-1])
|
2024-04-28 21:50:22 +02:00
|
|
|
options := map[string]dbus.Variant{
|
|
|
|
"types": dbus.MakeVariant(uint32(1)),
|
|
|
|
"persist_mode": dbus.MakeVariant(uint32(2)),
|
|
|
|
}
|
|
|
|
if token, err := readToken(); err == nil {
|
|
|
|
log.Info("Restoring token, no confirmation prompt")
|
|
|
|
options["restore_token"] = dbus.MakeVariant(token)
|
|
|
|
}
|
|
|
|
|
|
|
|
res := obj.Call("org.freedesktop.portal.RemoteDesktop.SelectDevices", 0, sessionHandle, options)
|
2024-03-15 16:23:11 +01:00
|
|
|
if res.Err != nil {
|
|
|
|
log.Error("Error selecting devices: %s", res.Err.Error())
|
|
|
|
}
|
|
|
|
state = 1
|
|
|
|
case 1:
|
|
|
|
log.Info("Starting Session")
|
|
|
|
res := obj.Call("org.freedesktop.portal.RemoteDesktop.Start", 0, sessionHandle, "", map[string]dbus.Variant{})
|
|
|
|
if res.Err != nil {
|
|
|
|
log.Error("Error starting session: %s", res.Err.Error())
|
|
|
|
}
|
|
|
|
state = 2
|
|
|
|
case 2:
|
2024-04-28 21:50:22 +02:00
|
|
|
// try to cast to interface array
|
|
|
|
if len(message.Body) == 2 {
|
|
|
|
if resMap, ok := message.Body[1].(map[string]dbus.Variant); ok {
|
|
|
|
// check if restore token in response
|
|
|
|
if restoreToken, ok := resMap["restore_token"]; ok {
|
|
|
|
token := restoreToken.Value().(string)
|
|
|
|
if err := persistToken(token); err != nil {
|
|
|
|
log.Error("Error persisting token: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-15 16:23:11 +01:00
|
|
|
log.Info("Performing Typing")
|
|
|
|
time.Sleep(1000 * time.Millisecond)
|
|
|
|
for _, char := range textToType {
|
|
|
|
if char == '\t' {
|
|
|
|
obj.Call("org.freedesktop.portal.RemoteDesktop.NotifyKeyboardKeycode", 0, sessionHandle, map[string]dbus.Variant{}, 15, uint32(1))
|
|
|
|
time.Sleep(autoTypeDelay)
|
|
|
|
obj.Call("org.freedesktop.portal.RemoteDesktop.NotifyKeyboardKeycode", 0, sessionHandle, map[string]dbus.Variant{}, 15, uint32(0))
|
|
|
|
time.Sleep(autoTypeDelay)
|
|
|
|
} else {
|
|
|
|
obj.Call("org.freedesktop.portal.RemoteDesktop.NotifyKeyboardKeysym", 0, sessionHandle, map[string]dbus.Variant{}, int32(char), uint32(1))
|
|
|
|
time.Sleep(autoTypeDelay)
|
|
|
|
obj.Call("org.freedesktop.portal.RemoteDesktop.NotifyKeyboardKeysym", 0, sessionHandle, map[string]dbus.Variant{}, int32(char), uint32(0))
|
|
|
|
time.Sleep(autoTypeDelay)
|
2023-12-06 02:09:43 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-15 16:23:11 +01:00
|
|
|
bus.Close()
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
return
|
2023-12-06 02:09:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|