Class: SocksTunnel::Remote::Server

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/socks_tunnel/remote.rb

Instance Method Summary collapse

Instance Method Details

#post_initObject



24
25
26
27
# File 'lib/socks_tunnel/remote.rb', line 24

def post_init
  @coder = Coder.new
  @buffer = Buffer.new
end

#receive_data(data) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/socks_tunnel/remote.rb', line 35

def receive_data(data)
  if @connection
    @buffer << data
    @buffer.each do |segment|
      @connection.send_data(@coder.decode(segment))
    end
  else
    @buffer << data
    addr = @buffer.shift
    if addr
      addr = @coder.decode(addr)
      host, port = addr.split(':')
      port = (port.nil? || port.empty?) ? 80 : port.to_i
      @connection = EventMachine.connect(host, port, Connection)
      @connection.server = self
      @buffer.each do |segment|
        @connection.send_data(@coder.decode(segment))
      end
    end
  end
rescue
  @connection.close_connection if @connection
  close_connection
end

#send_encoded_data(data) ⇒ Object



29
30
31
32
33
# File 'lib/socks_tunnel/remote.rb', line 29

def send_encoded_data(data)
  return if data.nil? || data.empty?
  send_data(@coder.encode(data))
  send_data(Config.delimiter)
end

#unbindObject



60
61
62
# File 'lib/socks_tunnel/remote.rb', line 60

def unbind
  @connection.close_connection if @connection
end