class Jabber::Bytestreams::SOCKS5BytestreamsPeer

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

Attributes

address[R]
socket[R]

Public Class Methods

new(socket) click to toggle source

Initialize a new peer

socket
TCPSocket
# File 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

Public Instance Methods

start() click to toggle source

Start handshake process

# File 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