class RSocks::HttpProxyParser

Attributes

password[R]
username[R]

Public Class Methods

new(state_machine, config) click to toggle source
# File lib/r_socks/http_proxy_parser.rb, line 8
def initialize(state_machine, config)
  @state_machine = state_machine
  @auth_method = config.auth_method
  @default_user = ENV['RSOCKS_USER'] || 'default'
  @default_password = ENV['RSOCKS_PASSWORD'] || 'default'
  @adaptor = config.auth_adaptor
  @health_check_route = config.health_check_route
  @need_auth = config.auth_method != :no_auth
end

Public Instance Methods

call(data) click to toggle source
# File lib/r_socks/http_proxy_parser.rb, line 18
def call(data)
  parser_connect_http(data)
  @state_machine.start!
  [@schema_parse.host, @schema_parse.port]
end

Private Instance Methods

auth_user() click to toggle source
# File lib/r_socks/http_proxy_parser.rb, line 40
def auth_user
  temp = @header['proxy-authorization']
  pattern = /^Basic /
  token = temp.gsub(pattern, '')
  begin
    str = Base64.decode64(token)
    @username, @password = str.split(':')
  rescue
    raise RSocks::HttpNotSupport, "token parse failed #{token}"
  end

  if @adaptor
    return @adaptor.call(@username, @password)
  else
    return @password == @default_password && @username == @default_user
  end
end
generate_header(arr) click to toggle source
# File lib/r_socks/http_proxy_parser.rb, line 65
def generate_header(arr)
  header = {}
  arr.each do |val|
    name, value = val.split(':')
    next if name.nil?
    header[name.strip.downcase] = value&.strip
  end
  @header = header
end
health_check_request(arr_data) click to toggle source
# File lib/r_socks/http_proxy_parser.rb, line 75
def health_check_request(arr_data)
  raise RSocks::HealthChecking if arr_data[0] == 'GET' && @health_check_route == arr_data[1]
end
host_format_checking(data) click to toggle source
# File lib/r_socks/http_proxy_parser.rb, line 58
def host_format_checking(data)
  temp = data.split("\s")
  health_check_request(temp)
  raise RSocks::HttpNotSupport if temp[0] != 'CONNECT'
  @schema_parse = URI("tcp://#{temp[1]}/")
end
parser_connect_http(data) click to toggle source
# File lib/r_socks/http_proxy_parser.rb, line 26
def parser_connect_http(data)
  temp = data.split("\r\n")
  host_format_checking(temp.shift)
  generate_header(temp)

  if @need_auth
    state = auth_user
    raise RSocks::HttpAuthFailed unless state
    return state
  end

  true
end