Class: Jabber::Bytestreams::SOCKS5BytestreamsPeer

Inherits:
Object
  • Object
show all
Defined in:
lib/gems/xmpp4r-0.4/lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb

Overview

This class will be instantiated by SOCKS5BytestreamsServer upon accepting a new connection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ SOCKS5BytestreamsPeer

Initialize a new peer

socket
TCPSocket


146
147
148
149
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb', line 146

def initialize(socket)
  @socket = socket
  Jabber::debuglog("SOCKS5 BytestreamsServer: accepted peer #{@socket.peeraddr[2]}:#{@socket.peeraddr[1]}")
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



141
142
143
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb', line 141

def address
  @address
end

#socketObject (readonly)

Returns the value of attribute socket.



141
142
143
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb', line 141

def socket
  @socket
end

Instance Method Details

#startObject

Start handshake process



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb', line 153

def start
  if !@socket.respond_to? :getbyte
    class << @socket; alias getbyte getc; end
  end

  auth_ver = @socket.getbyte
  if auth_ver != 5
    # Unsupported version
    @socket.close
    return
  end

  auth_nmethods = @socket.getbyte
  auth_methods = @socket.read(auth_nmethods)
  unless auth_methods.index("\x00")
    # Client won't accept no authentication
    @socket.write("\x05\xff")
    @socket.close
    return
  end
  @socket.write("\x05\x00")
  Jabber::debuglog("SOCKS5 BytestreamsServer: peer #{@socket.peeraddr[2]}:#{@socket.peeraddr[1]} authenticated")

  req = @socket.read(4)
  if req != "\x05\x01\x00\x03"
    # Unknown version, command, reserved, address-type
    @socket.close
    return
  end
  req_addrlen = @socket.getbyte
  req_addr = @socket.read(req_addrlen)
  req_port = @socket.read(2)
  if req_port != "\x00\x00"
    # Port is not 0
    @socket.write("\x05\x01")
    @socket.close
    return
  end
  @socket.write("\x05\x00\x00\x03#{req_addrlen.chr}#{req_addr}\x00\x00")
  Jabber::debuglog("SOCKS5 BytestreamsServer: peer #{@socket.peeraddr[2]}:#{@socket.peeraddr[1]} connected for #{req_addr}")

  @address = req_addr
end