Class: Net::VNC

Inherits:
Object show all
Defined in:
lib/net/vnc/vnc.rb

Overview

The VNC class provides for simple rfb-protocol based control of a VNC server. This can be used, eg, to automate applications.

Sample usage:

# launch xclock on localhost. note that there is an xterm in the top-left
Net::VNC.open 'localhost:0', :shared => true, :password => 'mypass' do |vnc|

vnc.pointer_move 10, 10 vnc.type 'xclock' vnc.key_press :return

end

TODO

  • The server read loop seems a bit iffy. Not sure how best to do it.

  • Should probably be changed to be more of a lower-level protocol wrapping thing, with the actual VNCClient sitting on top of that. all it should do is read/write the packets over the socket.

Defined Under Namespace

Classes: PointerState

Constant Summary collapse

BASE_PORT =
5900
CHALLENGE_SIZE =
16
DEFAULT_OPTIONS =
{
  :shared => false,
  :wait => 0.1
}
KEY_MAP =
YAML.load_file(keys_file).inject({}) { |h, (k, v)| h.update k.to_sym => v }
SHIFTED_CHARS =
'[email protected]#$%^&*()_+{}|:"<>?'
KEY_PRESS_CHARS =
{
  "\n" => :return,
  "\t" => :tab
}
BUTTON_MAP =
{
  :left => 0
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(display = ':0', options = {}) ⇒ VNC

Returns a new instance of VNC


75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/net/vnc/vnc.rb', line 75

def initialize display=':0', options={}
  @server = '127.0.0.1'
  if display =~ /^(.*)(:\d+)$/
    @server, display = $1, $2
  end
  @display = display[1..-1].to_i
  @options = DEFAULT_OPTIONS.merge options
  @clipboard = nil
  @pointer = PointerState.new self
  @mutex = Mutex.new
  connect
  @packet_reading_state = nil
  @packet_reading_thread = Thread.new { packet_reading_thread }
end

Instance Attribute Details

#displayObject (readonly)

Returns the value of attribute display


73
74
75
# File 'lib/net/vnc/vnc.rb', line 73

def display
  @display
end

#optionsObject (readonly)

Returns the value of attribute options


73
74
75
# File 'lib/net/vnc/vnc.rb', line 73

def options
  @options
end

#pointerObject (readonly)

Returns the value of attribute pointer


73
74
75
# File 'lib/net/vnc/vnc.rb', line 73

def pointer
  @pointer
end

#serverObject (readonly)

Returns the value of attribute server


73
74
75
# File 'lib/net/vnc/vnc.rb', line 73

def server
  @server
end

#socketObject (readonly)

Returns the value of attribute socket


73
74
75
# File 'lib/net/vnc/vnc.rb', line 73

def socket
  @socket
end

Class Method Details

.open(display = ':0', options = {}) ⇒ Object


90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/net/vnc/vnc.rb', line 90

def self.open display=':0', options={}
  vnc = new display, options
  if block_given?
    begin
      yield vnc
    ensure
      vnc.close
    end
  else
    vnc
  end
end

Instance Method Details

#button_down(which = :left, options = {}) ⇒ Object

Raises:

  • (ArgumentError)

266
267
268
269
270
271
# File 'lib/net/vnc/vnc.rb', line 266

def button_down which=:left, options={}
  button = BUTTON_MAP[which] || which
  raise ArgumentError, 'Invalid button - %p' % which unless (0..2) === button
  pointer.button |= 1 << button
  wait options
end

#button_press(button = :left, options = {}) ⇒ Object


257
258
259
260
261
262
263
264
# File 'lib/net/vnc/vnc.rb', line 257

def button_press button=:left, options={}
  begin
    button_down button, options
    yield if block_given?
  ensure
    button_up button, options
  end
end

#button_up(which = :left, options = {}) ⇒ Object

Raises:

  • (ArgumentError)

273
274
275
276
277
278
# File 'lib/net/vnc/vnc.rb', line 273

def button_up which=:left, options={}
  button = BUTTON_MAP[which] || which
  raise ArgumentError, 'Invalid button - %p' % which unless (0..2) === button
  pointer.button &= ~(1 << button)
  wait options
end

#clipboardObject


295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/net/vnc/vnc.rb', line 295

def clipboard
  if block_given?
    @clipboard = nil
    yield
    60.times do
      clipboard = @mutex.synchronize { @clipboard }
      return clipboard if clipboard
      sleep 0.5
    end
    warn 'clipboard still empty after 30s'
    nil
  else
    @mutex.synchronize { @clipboard }
  end
end

#closeObject


284
285
286
287
288
289
290
291
292
293
# File 'lib/net/vnc/vnc.rb', line 284

def close
  # destroy packet reading thread
  if @packet_reading_state == :loop
    @packet_reading_state = :stop
    while @packet_reading_state
      # do nothing
    end
  end
  socket.close
end

#connectObject


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
# File 'lib/net/vnc/vnc.rb', line 107

def connect
  @socket = TCPSocket.open server, port
  unless socket.read(12) =~ /^RFB (\d{3}.\d{3})\n$/
    raise 'invalid server response'
  end
  @server_version = $1
  socket.write "RFB 003.003\n"
  data = socket.read(4)
  auth = data.to_s.unpack('N')[0]
  case auth
  when 0, nil
    raise 'connection failed'
  when 1
    # ok...
  when 2
    password = @options[:password] or raise 'Need to authenticate but no password given'
    challenge = socket.read CHALLENGE_SIZE
    response = Cipher::DES.encrypt password, challenge
    socket.write response
    ok = socket.read(4).to_s.unpack('N')[0]
    raise 'Unable to authenticate - %p' % ok unless ok == 0
  else
    raise 'Unknown authentication scheme - %d' % auth
  end

  # ClientInitialisation
  socket.write((options[:shared] ? 1 : 0).chr)

  # ServerInitialisation
  # TODO: parse this.
  socket.read(20)
  data = socket.read(4)
  # read this many bytes in chunks of 20
  size = data.to_s.unpack('N')[0]
  while size > 0
    len = [20, size].min
    # this is the hostname, and other stuff i think...
    socket.read(len)
    size -= len
  end
end

#key_down(which, options = {}) ⇒ Object


227
228
229
230
231
232
233
234
235
# File 'lib/net/vnc/vnc.rb', line 227

def key_down which, options={}
  packet = 0.chr * 8
  packet[0] = 4.chr
  key_code = get_key_code which
  packet[4, 4] = [key_code].pack('N')
  packet[1] = 1.chr
  socket.write packet
  wait options
end

#key_press(*args) ⇒ Object

this takes an array of keys, and successively holds each down then lifts them up in reverse order. FIXME: should wait. can't recurse in that case.

Raises:

  • (ArgumentError)

199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/net/vnc/vnc.rb', line 199

def key_press(*args)
  options = Hash === args.last ? args.pop : {}
  keys = args
  raise ArgumentError, 'Must have at least one key argument' if keys.empty?
  begin
    key_down keys.first
    if keys.length == 1
      yield if block_given?
    else
      key_press(*(keys[1..-1] + [options]))
    end
  ensure
    key_up keys.first
  end
end

#key_up(which, options = {}) ⇒ Object


237
238
239
240
241
242
243
244
245
# File 'lib/net/vnc/vnc.rb', line 237

def key_up which, options={}
  packet = 0.chr * 8
  packet[0] = 4.chr
  key_code = get_key_code which
  packet[4, 4] = [key_code].pack('N')
  packet[1] = 0.chr
  socket.write packet
  wait options
end

#pointer_move(x, y, options = {}) ⇒ Object


247
248
249
250
251
# File 'lib/net/vnc/vnc.rb', line 247

def pointer_move x, y, options={}
  # options[:relative]
  pointer.update x, y
  wait options
end

#portObject


103
104
105
# File 'lib/net/vnc/vnc.rb', line 103

def port
  BASE_PORT + @display
end

#type(text, options = {}) ⇒ Object

this types text on the server


150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/net/vnc/vnc.rb', line 150

def type text, options={}
  packet = 0.chr * 8
  packet[0] = 4.chr
  text.split(//).each do |char|
    packet[7] = char[0]
    packet[1] = 1.chr
    socket.write packet
    packet[1] = 0.chr
    socket.write packet
  end
  wait options
end

#type_string(text, options = {}) ⇒ Object

This types text on the server, but it holds the shift key down when necessary. It will also execute key_press for tabs and returns.


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/net/vnc/vnc.rb', line 171

def type_string text, options={}
  shift_key_down = nil

  text.each_char do |char|
    key_to_press = KEY_PRESS_CHARS[char]
    unless key_to_press.nil?
      key_press key_to_press
    else
      key_needs_shift = SHIFTED_CHARS.include? char

      if shift_key_down.nil? || shift_key_down != key_needs_shift
        if key_needs_shift
          key_down :shift
        else
          key_up :shift
        end
      end

      type char
      shift_key_down = key_needs_shift
    end
  end
  wait options
end

#wait(options = {}) ⇒ Object


280
281
282
# File 'lib/net/vnc/vnc.rb', line 280

def wait options={}
  sleep options[:wait] || @options[:wait]
end