Skip to content

Commit

Permalink
Merge pull request usb-tools#8 from BenGardiner/rate-limit-inject
Browse files Browse the repository at this point in the history
Rate limit inject
  • Loading branch information
pojungc committed Nov 23, 2015
2 parents 5a5f9ad + e4d0de9 commit 96a6571
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
103 changes: 103 additions & 0 deletions src/bindings/python/UDPKeyboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# UDPKeyboard.py
#
# Contains class definitions to implement a USB keyboard.

from USB import *
from USBDevice import *
from USBConfiguration import *
from USBInterface import *
from USBEndpoint import *
from keymap import get_keycode
from evdev import InputDevice, categorize, ecodes
from select import select
from socket import *
import os

class UDPKeyboardInterface(USBInterface):
name = "USB keyboard interface"

hid_descriptor = b'\x09\x21\x10\x01\x00\x01\x22\x2b\x00'
report_descriptor = b'\x05\x01\x09\x06\xA1\x01\x05\x07\x19\xE0\x29\xE7\x15\x00\x25\x01\x75\x01\x95\x08\x81\x02\x95\x01\x75\x08\x81\x01\x19\x00\x29\x65\x15\x00\x25\x65\x75\x08\x95\x06\x81\x00\xC0'

def __init__(self, verbose=0):
descriptors = {
USB.desc_type_hid : self.hid_descriptor,
USB.desc_type_report : self.report_descriptor
}

self.endpoint = USBEndpoint(
3, # endpoint number
USBEndpoint.direction_in,
USBEndpoint.transfer_type_interrupt,
USBEndpoint.sync_type_none,
USBEndpoint.usage_type_data,
16384, # max packet size
10, # polling interval, see USB 2.0 spec Table 9-13
self.handle_buffer_available # handler function
)

# TODO: un-hardcode string index (last arg before "verbose")
USBInterface.__init__(
self,
0, # interface number
0, # alternate setting
3, # interface class
0, # subclass
0, # protocol
0, # string index
verbose,
[ self.endpoint ],
descriptors
)

#TODO: change to socket open
self.udp = socket(AF_INET, SOCK_DGRAM)
self.udp.bind(('',32008))
self.devices =[self.udp]

self.last_send_was_nil = 0

def handle_buffer_available(self):
send_keys = []
r, w, x = select(self.devices, [], [])
for fd in r:
if fd == self.udp:
send_keys = list(self.udp.recv(6))
if not send_keys:
if self.last_send_was_nil == 1:
return
self.last_send_was_nil = 1
else:
self.last_send_was_nil = 0

self.endpoint.send(bytes([0,0] + send_keys + [0]*(6-len(send_keys)) ))
#always return after send
return

class UDPKeyboardDevice(USBDevice):
name = "USB keyboard device"

def __init__(self, maxusb_app, verbose=0):
config = USBConfiguration(
1, # index
"Emulated Keyboard", # string desc
[ UDPKeyboardInterface() ] # interfaces
)

USBDevice.__init__(
self,
maxusb_app,
0, # device class
0, # device subclass
0, # protocol release number
64, # max packet size for endpoint 0
0x610b, # vendor id
0x4653, # product id
0x3412, # device revision
"Move along", # manufacturer string
"This is not the USB device you're looking for", # product string
"00001", # serial number string
[ config ],
verbose=verbose
)

18 changes: 18 additions & 0 deletions src/bindings/python/usbproxy-udp-keyboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python3
#
# usbproxy-fd-keyboard.py

from USBProxyApp import USBProxyApp
from UDPKeyboard import UDPKeyboardDevice
import sys

u = USBProxyApp(verbose=0)
d = UDPKeyboardDevice(u, verbose=0)

d.connect()

try:
d.run()
# SIGINT raises KeyboardInterrupt
except KeyboardInterrupt:
d.disconnect()
10 changes: 10 additions & 0 deletions src/tools/send-a-udp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
#press a
echo -ne "\x04\x00\x00\x00\x00\x00" > /dev/udp/localhost/32008
#also works, but results in long keypress due to -q1 one second delay on exit
#echo -ne "\x04\x00\x00\x00\x00\x00" | nc localhost -q 1 -u 32008

#release a
echo -ne "\x00" | nc localhost -q 1 -u 32008
#also works
#echo -ne "\x00" > /dev/udp/localhost/32008

0 comments on commit 96a6571

Please sign in to comment.