Class: Jabber::Bytestreams::SOCKS5BytestreamsPeer

Inherits:
Object
  • Object
show all
Defined in:
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

144
145
146
147
# File 'lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb', line 144

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


139
140
141
# File 'lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb', line 139

def address
  @address
end

#socketObject (readonly)

Returns the value of attribute socket


139
140
141
# File 'lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb', line 139

def socket
  @socket
end

Instance Method Details

#startObject

Start handshake process


151
152
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
# File 'lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb', line 151

def start
  auth_ver = @socket.getc
  if auth_ver != 5
    # Unsupported version
    @socket.close
    return
  end

  auth_nmethods = @socket.getc
  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.getc
  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