-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinjector.go
More file actions
220 lines (190 loc) · 5.22 KB
/
Copy pathinjector.go
File metadata and controls
220 lines (190 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
/*
#include <linux/uinput.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
int create_uinput_device() {
int fd = open("/dev/uinput", O_RDWR | O_NONBLOCK);
if (fd < 0) return -1;
ioctl(fd, UI_SET_EVBIT, EV_KEY);
ioctl(fd, UI_SET_EVBIT, EV_REL);
ioctl(fd, UI_SET_EVBIT, EV_SYN);
// Keyboard keys
for (int i = 1; i < 249; i++)
ioctl(fd, UI_SET_KEYBIT, i);
// Mouse buttons
ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
ioctl(fd, UI_SET_KEYBIT, BTN_RIGHT);
ioctl(fd, UI_SET_KEYBIT, BTN_MIDDLE);
// Mouse axes
ioctl(fd, UI_SET_RELBIT, REL_X);
ioctl(fd, UI_SET_RELBIT, REL_Y);
struct uinput_setup usetup;
memset(&usetup, 0, sizeof(usetup));
strncpy(usetup.name, "gamepad-osk", sizeof(usetup.name) - 1);
usetup.id.bustype = BUS_USB;
usetup.id.vendor = 0x1234;
usetup.id.product = 0x5678;
usetup.id.version = 1;
if (ioctl(fd, UI_DEV_SETUP, &usetup) < 0) {
close(fd);
return -2;
}
if (ioctl(fd, UI_DEV_CREATE) < 0) {
close(fd);
return -3;
}
return fd;
}
void destroy_uinput_device(int fd) {
ioctl(fd, UI_DEV_DESTROY);
close(fd);
}
*/
import "C"
import (
"encoding/binary"
"errors"
"fmt"
"os"
"time"
)
const (
uinputPath = "/dev/uinput"
evKey = 0x01
evRel = 0x02
evSyn = 0x00
relX = 0x00
relY = 0x01
synReport = 0x00
)
type Injector struct {
fd *os.File
}
func NewInjector() (*Injector, error) {
cfd := C.create_uinput_device()
if cfd < 0 {
return nil, uinputError(int(cfd))
}
fd := os.NewFile(uintptr(cfd), uinputPath)
time.Sleep(50 * time.Millisecond)
return &Injector{fd: fd}, nil
}
// uinputError returns a short single-line error for the injector failure.
// The caller is responsible for logging fix steps via logPermissionFix().
func uinputError(code int) error {
if _, err := os.Stat(uinputPath); os.IsNotExist(err) {
return errors.New("/dev/uinput not found - load the module: sudo modprobe uinput")
}
f, err := os.OpenFile(uinputPath, os.O_RDWR, 0) //nolint:gosec // G304: path is constant
if err != nil && os.IsPermission(err) {
return errors.New("cannot open /dev/uinput - permission denied")
}
if f != nil {
_ = f.Close()
}
return fmt.Errorf("uinput device setup failed (code %d)", code)
}
// keyHoldDuration is the press-to-release delay. Some native Linux games
// (notably SDL apps) drop sub-millisecond key events for action keys like
// Enter, Backspace, and Esc. Letter keys often slip through via the
// IME/TextInput path, but action keys go through KeyDown/KeyUp only and
// need a real hold to register. xdotool defaults to 12ms for the same
// reason.
const keyHoldDuration = 12 * time.Millisecond
func (inj *Injector) PressKey(code int, modifiers []int) {
Debugf("Inject key=%d mods=%v", code, modifiers)
for _, m := range modifiers {
inj.writeEvent(evKey, uint16(m), 1)
}
inj.syn()
inj.writeEvent(evKey, uint16(code), 1)
inj.syn()
time.Sleep(keyHoldDuration)
inj.writeEvent(evKey, uint16(code), 0)
inj.syn()
for _, m := range modifiers {
inj.writeEvent(evKey, uint16(m), 0)
}
inj.syn()
}
func (inj *Injector) TypeUnicode(codepoint int) {
// Ctrl+Shift+U, hex digits, Enter (GTK/Qt Unicode input)
inj.writeEvent(evKey, KEY_LEFTCTRL, 1)
inj.writeEvent(evKey, KEY_LEFTSHIFT, 1)
inj.syn()
inj.writeEvent(evKey, KEY_U, 1)
inj.syn()
inj.writeEvent(evKey, KEY_U, 0)
inj.syn()
inj.writeEvent(evKey, KEY_LEFTSHIFT, 0)
inj.writeEvent(evKey, KEY_LEFTCTRL, 0)
inj.syn()
time.Sleep(20 * time.Millisecond)
hexStr := fmt.Sprintf("%04x", codepoint)
hexKeys := map[byte]uint16{
'0': KEY_0, '1': KEY_1, '2': KEY_2, '3': KEY_3, '4': KEY_4,
'5': KEY_5, '6': KEY_6, '7': KEY_7, '8': KEY_8, '9': KEY_9,
'a': KEY_A, 'b': KEY_B, 'c': KEY_C, 'd': KEY_D, 'e': KEY_E, 'f': KEY_F,
}
for _, ch := range []byte(hexStr) {
code := hexKeys[ch]
inj.writeEvent(evKey, code, 1)
inj.syn()
inj.writeEvent(evKey, code, 0)
inj.syn()
}
inj.writeEvent(evKey, KEY_ENTER, 1)
inj.syn()
inj.writeEvent(evKey, KEY_ENTER, 0)
inj.syn()
}
func (inj *Injector) HoldKey(code int) {
Debugf("Inject hold key=%d", code)
inj.writeEvent(evKey, uint16(code), 1)
inj.syn()
}
func (inj *Injector) ReleaseKey(code int) {
Debugf("Inject release key=%d", code)
inj.writeEvent(evKey, uint16(code), 0)
inj.syn()
}
func (inj *Injector) ClickMouse(button uint16, pressed bool) {
val := int32(0)
if pressed {
val = 1
}
inj.writeEvent(evKey, button, val)
inj.syn()
}
func (inj *Injector) MoveMouse(dx, dy int) {
if dx != 0 {
inj.writeEvent(evRel, relX, int32(dx))
}
if dy != 0 {
inj.writeEvent(evRel, relY, int32(dy))
}
if dx != 0 || dy != 0 {
inj.syn()
}
}
func (inj *Injector) Close() {
if inj.fd != nil {
C.destroy_uinput_device(C.int(inj.fd.Fd()))
inj.fd = nil
}
}
func (inj *Injector) writeEvent(evType uint16, code uint16, value int32) {
// struct input_event on x86_64: sec(8) + usec(8) + type(2) + code(2) + value(4) = 24 bytes
var buf [24]byte
binary.LittleEndian.PutUint16(buf[16:], evType)
binary.LittleEndian.PutUint16(buf[18:], code)
binary.LittleEndian.PutUint32(buf[20:], uint32(value))
_, _ = inj.fd.Write(buf[:])
}
func (inj *Injector) syn() {
inj.writeEvent(evSyn, synReport, 0)
}