Class: BinProxy::Connection::Filters::Socks

Inherits:
Base
  • Object
show all
Includes:
Logger
Defined in:
lib/binproxy/connection/filters.rb

Defined Under Namespace

Classes: ClientHeader

Constant Summary collapse

SOCKS_OK =
"\x00\x5a" + "\x00" * 6
SOCKS_ERR =
"\x00\x5b" + "\x00" * 6

Instance Attribute Summary collapse

Attributes inherited from Base

#conn

Instance Method Summary collapse

Methods included from Logger

log

Methods inherited from Base

#initialize, #write

Constructor Details

This class inherits a constructor from BinProxy::Connection::Filters::Base

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



74
75
76
# File 'lib/binproxy/connection/filters.rb', line 74

def header
  @header
end

#socks_stateObject (readonly)

Returns the value of attribute socks_state.



74
75
76
# File 'lib/binproxy/connection/filters.rb', line 74

def socks_state
  @socks_state
end

Instance Method Details

#initObject



92
93
94
95
# File 'lib/binproxy/connection/filters.rb', line 92

def init
  @buf = StringIO.new
  @state = :new
end

#read(data) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/binproxy/connection/filters.rb', line 96

def read(data)
  return data unless @state == :new

  @buf.string << data
  @header = ClientHeader.read(@buf)

  # no exception means we've read a full header...
  log.debug "Read SOCKS header #{@header}"
  @state = :connecting

  conn.connect @header.host_or_ip, @header.port

  # return any extra data
  @buf.read
rescue EOFError, IOError
  #partial read of header, reset to try again on next packet
  @buf.pos = 0
  nil
rescue EM::ConnectionError => e
  #synchronous error when connecting upstream, e.g. bogus hostname
  log.warn "Can't connect to '#{@header.host_or_ip}': #{e.message}"
  #TODO -close the connection
  nil
end

#session_closing(reason) ⇒ Object



125
126
127
# File 'lib/binproxy/connection/filters.rb', line 125

def session_closing(reason)
  conn.send_data SOCKS_ERR if @state == :connecting
end

#upstream_connected(upstream_conn) ⇒ Object



120
121
122
123
124
# File 'lib/binproxy/connection/filters.rb', line 120

def upstream_connected(upstream_conn)
  log.error "unexpected upstream_connected in state #{@state}" unless @state == :connecting
  @state = :connected
  conn.send_data SOCKS_OK
end