Class: FingerPoken::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/fingerpoken/target.rb

Direct Known Subclasses

Tivo, VNC, Xdo

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Target

Returns a new instance of Target.



6
7
8
9
10
11
12
13
# File 'lib/fingerpoken/target.rb', line 6

def initialize(config)
  @channel = config[:channel]
  @logger = Logger.new(STDERR)
  @logger.level = ($DEBUG ? Logger::DEBUG: Logger::WARN)
  @passphrase = config[:passphrase] # OK if this is nil

  @last_sequence = -1
end

Instance Method Details

#click(button) ⇒ Object



105
106
107
108
# File 'lib/fingerpoken/target.rb', line 105

def click(button)
  mousedown(button)
  mouseup(button)
end

#keydown(key) ⇒ Object



118
119
120
# File 'lib/fingerpoken/target.rb', line 118

def keydown(key)
  @logger.info("keydown not supported")
end

#keypress(key) ⇒ Object



114
115
116
# File 'lib/fingerpoken/target.rb', line 114

def keypress(key)
  @logger.info("keypress not supported")
end

#keyup(key) ⇒ Object



122
123
124
# File 'lib/fingerpoken/target.rb', line 122

def keyup(key)
  @logger.info("keyup not supported")
end

#mousedown(button) ⇒ Object



97
98
99
# File 'lib/fingerpoken/target.rb', line 97

def mousedown(button)
  @logger.info("mousedown not supported")
end

#mousemove_relative(x, y) ⇒ Object

Subclasses should implement this.



93
94
95
# File 'lib/fingerpoken/target.rb', line 93

def mousemove_relative(x, y)
  @logger.info("mousemove not supported")
end

#mouseup(button) ⇒ Object



101
102
103
# File 'lib/fingerpoken/target.rb', line 101

def mouseup(button)
  @logger.info("mouseup not supported")
end

#move_endObject



126
127
128
# File 'lib/fingerpoken/target.rb', line 126

def move_end()
  @logger.info("move_end not supported")
end

#registerObject

def verify



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
# File 'lib/fingerpoken/target.rb', line 42

def register
  if @registered
    @logger.warn("Ignoring extra call to #{self.class.name}#register. Trace:\n#{caller[0..3].join("\n")}")
    return
  end

  @registered = true
  @logger.debug(:register => self.class.name)
  @channel.subscribe do |obj|
    request = obj[:request]
    callback = obj[:callback]
    @logger.debug(:request => request)

    if @passphrase
      verified = verify(request, callback)
      if !verified
        @logger.warn("Dropping corrupt/forged request")
        next
      end
    end

    @last_sequence = request["sequence"].to_i

    response = case request["action"]
      when "mousemove_relative"
        mousemove_relative(request["rel_x"], request["rel_y"])
      when "mousemove_absolute"
        mousemove_absolute(request["percent_x"], request["percent_y"])
      when "move_end"
        move_end()
      when "click"
        click(request["button"])
      when "mousedown"
        mousedown(request["button"])
      when "mouseup"
        mouseup(request["button"])
      when "type"
        type(request["string"])
      when "keypress"
        keypress(request["key"])
      else
        p ["Unsupported action", request]
    end

    if response.is_a?(Hash)
      callback.call(response)
    end
  end # @channel.subscribe
end

#type(string) ⇒ Object



110
111
112
# File 'lib/fingerpoken/target.rb', line 110

def type(string)
  @logger.info("typing not supported")
end

#verify(request, callback) ⇒ Object



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
# File 'lib/fingerpoken/target.rb', line 15

def verify(request, callback)
  if !request["signature"]
    # TODO(sissel): Send callback saying "passphrase required"
    @logger.warn("Message with no signature")
    return false
  end

  if request["sequence"] < @last_sequence
    # Reject out of sequence or replayed messages
    # TODO(sissel): Report replay attack detected
    @logger.warn("Sequence #{request["sequence"]} < #{@last_sequence} "\
                 "(last sequence). Replay attack or bug?")
    return false
  end

  hmac = HMAC::MD5.new(@passphrase)
  hmac.update(request["sequence"].to_s)
  digest_bytes = hmac.digest.bytes.to_a
  if request["signature"] == digest_bytes
    return true
  else
    # TODO(sissel): Report verification failed
    @logger.warn("Signature verify failed. Server:#{digest_bytes.inspect}, Client:#{request["signature"].inspect}")
    return false
  end
end