2023-12-23 07:18:30 +01:00
|
|
|
//go:build linux
|
2023-12-06 02:09:43 +01:00
|
|
|
|
|
|
|
package autotype
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"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")
|
|
|
|
|
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 {
|
|
|
|
select {
|
|
|
|
case message := <-signals:
|
|
|
|
if state == 0 {
|
2024-01-19 08:55:37 +01:00
|
|
|
log.Info("Selecting Devices")
|
2023-12-06 02:09:43 +01:00
|
|
|
result := message.Body[1].(map[string]dbus.Variant)
|
|
|
|
resultSessionHandle := result["session_handle"]
|
|
|
|
sessionHandle = dbus.ObjectPath(resultSessionHandle.String()[1 : len(resultSessionHandle.String())-1])
|
2024-01-19 08:55:37 +01:00
|
|
|
res := obj.Call("org.freedesktop.portal.RemoteDesktop.SelectDevices", 0, sessionHandle, map[string]dbus.Variant{})
|
|
|
|
if res.Err != nil {
|
|
|
|
log.Error("Error selecting devices: %s", res.Err.Error())
|
|
|
|
}
|
2023-12-06 02:09:43 +01:00
|
|
|
state = 1
|
|
|
|
} else if state == 1 {
|
2024-01-19 08:55:37 +01:00
|
|
|
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())
|
|
|
|
}
|
2023-12-06 02:09:43 +01:00
|
|
|
state = 2
|
|
|
|
} else if state == 2 {
|
2024-01-19 08:55:37 +01:00
|
|
|
log.Info("Performing Typing")
|
2023-12-06 02:09:43 +01:00
|
|
|
state = 3
|
2023-12-06 02:39:36 +01:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2023-12-06 02:09:43 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bus.Close()
|
|
|
|
return
|
2024-01-19 08:55:37 +01:00
|
|
|
} else {
|
|
|
|
log.Info("State 3")
|
|
|
|
return
|
2023-12-06 02:09:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|