Class: EventMachine::Protocols::Socks4

Inherits:
Connection
  • Object
show all
Defined in:
lib/em/protocols/socks4.rb

Overview

Basic SOCKS v4 client implementation

Use as you would any regular connection:

class MyConn < EM::P::Socks4

def post_init
  send_data("sup")
end

def receive_data(data)
  send_data("you said: #{data}")
end

end

EM.connect socks_host, socks_port, MyConn, host, port

Instance Attribute Summary

Attributes inherited from Connection

#signature

Instance Method Summary collapse

Methods inherited from Connection

#associate_callback_target, #close_connection, #close_connection_after_writing, #comm_inactivity_timeout, #comm_inactivity_timeout=, #connection_completed, #detach, #error?, #get_cipher_bits, #get_cipher_name, #get_cipher_protocol, #get_idle_time, #get_outbound_data_size, #get_peer_cert, #get_peername, #get_pid, #get_proxied_bytes, #get_sni_hostname, #get_sock_opt, #get_sockname, #get_status, new, #notify_readable=, #notify_readable?, #notify_writable=, #notify_writable?, #pause, #paused?, #pending_connect_timeout, #pending_connect_timeout=, #post_init, #proxy_completed, #proxy_incoming_to, #proxy_target_unbound, #receive_data, #reconnect, #resume, #send_data, #send_datagram, #send_file_data, #set_sock_opt, #ssl_handshake_completed, #ssl_verify_peer, #start_tls, #stop_proxying, #stream_file_data, #unbind

Constructor Details

#initialize(host, port) ⇒ Socks4

Returns a new instance of Socks4.



20
21
22
23
24
25
26
# File 'lib/em/protocols/socks4.rb', line 20

def initialize(host, port)
  @host = Socket.gethostbyname(host).last
  @port = port
  @socks_error_code = nil
  @buffer = ''
  setup_methods
end

Instance Method Details

#restore_methodsObject



35
36
37
38
39
40
# File 'lib/em/protocols/socks4.rb', line 35

def restore_methods
  class << self
    remove_method :post_init
    remove_method :receive_data
  end
end

#setup_methodsObject



28
29
30
31
32
33
# File 'lib/em/protocols/socks4.rb', line 28

def setup_methods
  class << self
    def post_init; socks_post_init; end
    def receive_data(*a); socks_receive_data(*a); end
  end
end

#socks_post_initObject



42
43
44
45
# File 'lib/em/protocols/socks4.rb', line 42

def socks_post_init
  header = [4, 1, @port, @host, 0].flatten.pack("CCnA4C")
  send_data(header)
end

#socks_receive_data(data) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/em/protocols/socks4.rb', line 47

def socks_receive_data(data)
  @buffer << data
  return  if @buffer.size < 8

  header_resp = @buffer.slice! 0, 8
  _, r = header_resp.unpack("cc")
  if r != 90
    @socks_error_code = r
    close_connection
    return
  end

  restore_methods

  post_init
  receive_data(@buffer)  unless @buffer.empty?
end