2020-07-20 15:12:09 -05:00
require 'socket'
require './http.rb'
require 'time'
class FakeTunnelConnector
2020-07-22 01:00:20 -05:00
attr_accessor :port , :server , :ssl_context
def initialize ( port , ssl_context = nil )
2020-07-20 15:12:09 -05:00
@port = port
@server = TCPServer . open ( port )
2020-07-22 01:00:20 -05:00
@ssl_context = ssl_context
2020-07-20 15:12:09 -05:00
end
def listen_and_respond
2020-07-22 01:00:20 -05:00
if ssl_context
tls_server = ssl_server
tcp_socket = tls_server . accept
else
tcp_socket = server . accept
end
2020-07-20 16:31:10 -05:00
response = read_socket ( tcp_socket )
location = response [ :start_line ] [ :location ]
write_response ( tcp_socket , location )
2020-07-22 03:17:22 -05:00
close_connection ( tcp_socket )
2020-07-20 15:12:09 -05:00
end
2020-07-22 01:00:20 -05:00
def ssl_server
OpenSSL :: SSL :: SSLServer . new server , ssl_context if ssl_context
end
2020-07-20 15:12:09 -05:00
def read_socket ( tcp_socket )
2020-07-20 16:31:10 -05:00
start_line = [ ]
headers = [ ]
2020-07-22 01:00:20 -05:00
puts 'Request Incoming:'
puts '-------------------'
2020-07-20 15:12:09 -05:00
# read lines from socket
while ( line = tcp_socket . gets ) && ( line . chomp . length > 0 )
# check for a valid http verb sent
2020-07-20 16:31:10 -05:00
start_line = parse_http_start_request_line ( line ) if Http :: METHODS . include? ( line . split . first )
2020-07-20 15:12:09 -05:00
header_line = parse_http_header_request_line ( line )
headers << header_line unless header_line . nil?
end
2020-07-20 16:31:10 -05:00
puts start_line
2020-07-22 01:00:20 -05:00
puts 'Request Headers:'
puts '-------------------'
2020-07-20 15:12:09 -05:00
puts headers
puts " \r \n "
2020-07-22 01:00:20 -05:00
{ start_line : start_line , headers : headers }
2020-07-20 15:12:09 -05:00
end
def parse_http_start_request_line ( line )
split_request = line . split
verb = split_request . first
location = split_request . select { | l | l if l . start_with? ( '/' ) } . first
protocol = split_request . last
{
verb : verb ,
location : location ,
protocol : protocol
}
end
def parse_http_header_request_line ( line )
if line . split . first . end_with? ( ':' )
split_request = line . split ( ':' )
key = split_request [ 0 ]
value = if split_request . count > = 3
split_request [ 1 ] + ':' + split_request [ 2 ]
else
split_request [ 1 ]
end
Hash [ key , value ]
end
end
2020-07-20 16:31:10 -05:00
def write_response ( tcp_socket , location )
2020-07-22 02:33:08 -05:00
routed_response_string = route_request ( location )
2020-07-22 03:17:22 -05:00
body = ok ( routed_response_string )
2020-07-21 02:54:39 -05:00
tcp_socket . print ( ok_headers )
2020-07-22 03:17:22 -05:00
tcp_socket . print ( body )
end
def close_connection ( tcp_socket )
tcp_socket . close
2020-07-20 15:12:09 -05:00
end
2020-07-22 01:00:20 -05:00
def ok ( body = 'Success' )
2020-07-22 02:53:00 -05:00
body
2020-07-21 02:54:39 -05:00
end
def ok_headers
" HTTP/1.1 200 OK \r \n " +
2020-07-22 01:00:20 -05:00
" Date: #{ Time . now . utc } \r \n " +
" \r \n "
2020-07-20 15:12:09 -05:00
end
2020-07-20 16:31:10 -05:00
def route_request ( location )
if location == '/scans'
scans
2020-07-22 02:12:48 -05:00
elsif location == '/session'
2020-07-20 16:31:10 -05:00
login
else
2020-07-22 03:17:22 -05:00
login
2020-07-20 16:31:10 -05:00
end
end
def scans
2020-07-22 02:12:48 -05:00
body = ' {
" folders " : [
{ " unread_count " : 0 , " custom " : 0 , " default_tag " : 0 , " type " :" trash " , " name " :" Trash " , " id " : 821 } ,
{ " unread_count " : 4 , " custom " : 0 , " default_tag " : 1 , " type " :" main " , " name " :" My Scans " , " id " : 822 }
] ,
" scans " : [
{ " id " : 851 , " name " :" RiskIO: Nessus API Test " , " folder_id " : 822 , " type " :" local " , " read " :false , " last_modification_date " : 1487137935 , " creation_date " : 1487137921 , " status " :" completed " , " uuid " :" 5afd0023-977a-9124-2638-f14d0958677672812a5da3529d5f " , " shared " :false , " user_permissions " : 128 , " owner " :" ebellis " , " timezone " :" Zulu " , " rrules " :" FREQ=ONETIME " , " starttime " :" 20170202T160000 " , " enabled " :true , " control " :true } ,
{ " id " : 889 , " name " :" Kenna Security: Nessus API " , " folder_id " : 822 , " type " :" local " , " read " :false , " last_modification_date " : 1487017337 , " creation_date " : 1487017329 , " status " :" completed " , " uuid " :" 0efb416d-bf92-92a6-ec3b-fb309c0a100d9cc9c3d05cb8d15c " , " shared " :false , " user_permissions " : 128 , " owner " :" ebellis " , " timezone " :null , " rrules " :null , " starttime " :null , " enabled " :true , " control " :true } ,
{ " id " : 832 , " name " :" RiskIO: Nessus API Bellis " , " folder_id " : 822 , " type " :" local " , " read " :false , " last_modification_date " : 1486158311 , " creation_date " : 1486156633 , " status " :" completed " , " uuid " :" e96a9447-c500-a02c-b321-aaa54243ea83668c7d3bec760ead " , " shared " :false , " user_permissions " : 128 , " owner " :" ebellis " , " timezone " :null , " rrules " :null , " starttime " :null , " enabled " :true , " control " :true } ,
{ " id " : 834 , " name " :" RiskIO: EMPTY SCAN " , " folder_id " : 822 , " type " :" local " , " read " :false , " last_modification_date " : 1486156549 , " creation_date " : 1486156540 , " status " :" completed " , " uuid " :" fe3c45b9-e17c-748f-85e2-f6529e7c997fcc048b683781b8fc " , " shared " :false , " user_permissions " : 128 , " owner " :" ebellis " , " timezone " :null , " rrules " :null , " starttime " :null , " enabled " :true , " control " :true } ,
{ " id " : 835 , " name " :" RiskIO: Nessus API " , " folder_id " : 822 , " type " :" local " , " read " :false , " last_modification_date " : 1486156549 , " creation_date " : 1486156540 , " status " :" completed " , " uuid " :" f4170000-e17c-748f-85e2-f6529e7c997fcc048b683781b8fc " , " shared " :false , " user_permissions " : 128 , " owner " :" ebellis " , " timezone " :null , " rrules " :null , " starttime " :null , " enabled " :true , " control " :true } ,
{ " id " : 823 , " name " :" Smoke Test Scan " , " folder_id " : 821 , " type " :" local " , " read " :true , " last_modification_date " : 1480977444 , " creation_date " : 1480977430 , " status " :" completed " , " uuid " :" bf1d0958-5a67-f413-dffd-35131927f5084ad38817344c69ef " , " shared " :false , " user_permissions " : 128 , " owner " :" ebellis " , " timezone " :null , " rrules " :null , " starttime " :null , " enabled " :false , " control " :true } ,
{ " id " : 826 , " name " :" RiskIO " , " folder_id " : 821 , " type " :" local " , " read " :true , " last_modification_date " : 1480976737 , " creation_date " : 1480974579 , " status " :" completed " , " uuid " :" 60f7f497-4f3b-e3a8-6364-72a6f763d20b2da84f89010c7883 " , " shared " :false , " user_permissions " : 128 , " owner " :" ebellis " , " timezone " :null , " rrules " :null , " starttime " :null , " enabled " :false , " control " :true } ,
{ " id " : 873 , " name " :" Public IP Scan " , " folder_id " : 821 , " type " :null , " read " :true , " last_modification_date " : 0 , " creation_date " : 0 , " status " :" empty " , " uuid " :null , " shared " :false , " user_permissions " : 128 , " owner " :" ebellis " , " timezone " :null , " rrules " :null , " starttime " :null , " enabled " :false , " control " :true }
] ,
" timestamp " : 1487197426 } '
2020-07-20 16:31:10 -05:00
ok ( body )
end
def login
2020-07-22 02:53:00 -05:00
body = '{"token":"3b51619a125d90cd0778bd4c02cd7be34de6dc9268eecd25"}'
ok ( body ) + " \r \n "
2020-07-20 16:31:10 -05:00
end
2020-07-22 01:00:20 -05:00
end