Module: Socksproxyable::InstanceMethodsAuthenticate

Included in:
TCPSocket
Defined in:
lib/socksify/socksproxyable.rb

Overview

instance method #socks_authenticate

Instance Method Summary collapse

Instance Method Details

#socks_authenticate(socks_username, socks_password) ⇒ Object

rubocop:disable Metrics

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/socksify/socksproxyable.rb', line 31

def socks_authenticate(socks_username, socks_password)
  if socks_username || socks_password
    Socksify.debug_debug 'Sending username/password authentication'
    write "\005\001\002"
  else
    Socksify.debug_debug 'Sending no authentication'
    write "\005\001\000"
  end
  Socksify.debug_debug 'Waiting for authentication reply'
  auth_reply = recv(2)
  raise SOCKSError, "Server doesn't reply authentication" if auth_reply.empty?

  if auth_reply[0..0] != "\004" && auth_reply[0..0] != "\005"
    raise SOCKSError, "SOCKS version #{auth_reply[0..0]} not supported"
  end

  if socks_username || socks_password
    if auth_reply[1..1] != "\002"
      raise SOCKSError, "SOCKS authentication method #{auth_reply[1..1]} neither requested nor supported"
    end

    auth = "\001"
    auth += socks_username.to_s.length.chr
    auth += socks_username.to_s
    auth += socks_password.to_s.length.chr
    auth += socks_password.to_s
    write auth
    auth_reply = recv(2)
    raise SOCKSError, 'SOCKS authentication failed' if auth_reply[1..1] != "\000"
  elsif auth_reply[1..1] != "\000"
    raise SOCKSError, "SOCKS authentication method #{auth_reply[1..1]} neither requested nor supported"
  end
end