Skip to content

Instantly share code, notes, and snippets.

@bararchy
Last active February 15, 2016 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bararchy/ffb234ce6688d839e7eb to your computer and use it in GitHub Desktop.
Save bararchy/ffb234ce6688d839e7eb to your computer and use it in GitHub Desktop.
Sphere Protocol
require 'json'
require 'colorize'
require 'socket'
puts 'Usage: ruby test_client server port dir_path'.bold
server = ARGV[0]
port = ARGV[1]
path_to_dir = ARGV[2]
serial = ARGV[3]
profile = ARGV[4]
puts 'Connecting to sphere server'.green
socket = TCPSocket.new(server, port.to_i)
puts "Loading files in: #{path_to_dir}"
headers = {is_serial: serial, files: []}
Dir.glob("#{path_to_dir}/*").each do |file|
f = File.open(file, 'rb')
file_size = f.size
file_name = File.basename(file)
puts "File name: #{file_name}, Size: #{file_size}"
headers[:files] << {name: file_name, size: file_size, profile: profile}
f.close
end
puts "Generated headers: #{headers.to_json}"
separator = "\r\n\r\n"
socket.write headers.to_json
socket.write separator
puts 'Streaming files to sphere'
Dir.glob("#{path_to_dir}/*").each do |file|
f = File.open(file, 'rb')
sent = IO.copy_stream(f, socket)
puts "Sent: #{sent}"
f.close
end
st = Time.now
puts "Finished writing to server: #{st}".green
puts 'waiting for response'
begin
header = socket.gets("\x0d\x0a\x0d\x0a", 4096)
content_length = header.match(/Content-Length:(.*?)\r?\n/i)[1].to_i
response = socket.read(content_length)
rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EINTR, Errno::EWOULDBLOCK, EOFError
puts 'Error reading response'
end
puts 'Server Response:'
puts header.bold
puts response
et = Time.now
puts "\r\nGot response: #{et}".green
puts "Process took #{et-st} sec"
puts 'cleaning up'
socket.close
Protocol:
Content-Length: byte size of response
\r\n\r\n
{"files":[{"File_Name":"Profile_name: Answer"}
Example:
Content-Length: 4028
{"files":[{"file-1kb_(6th_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(6th_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(6th_copy).dat\n, FPScan: Clean, "},{"file-1kb_(19th_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(19th_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(19th_copy).dat\n, FPScan: Clean, "},{"file-1kb_(8th_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(8th_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(8th_copy).dat\n, FPScan: Clean, "},{"file-1kb_(13th_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(13th_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(13th_copy).dat\n, FPScan: Clean, "},{"file-1kb_(5th_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(5th_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(5th_copy).dat\n, FPScan: Clean, "},{"file-1kb_(4th_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(4th_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(4th_copy).dat\n, FPScan: Clean, "},{"file-1kb_(12th_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(12th_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(12th_copy).dat\n, FPScan: Clean, "},{"file-1kb_(14th_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(14th_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(14th_copy).dat\n, FPScan: Clean, "},{"file-1kb_(15th_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(15th_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(15th_copy).dat\n, FPScan: Clean, "},{"file-1kb.dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb.dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb.dat\n, FPScan: Clean, "},{"file-1kb_(copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(copy).dat\n, FPScan: Clean, "},{"file-1kb_(another_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(another_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(another_copy).dat\n, FPScan: Clean, "},{"file-1kb_(10th_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(10th_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(10th_copy).dat\n, FPScan: Clean, "},{"file-1kb_(17th_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(17th_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(17th_copy).dat\n, FPScan: Clean, "},{"file-1kb_(16th_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(16th_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(16th_copy).dat\n, FPScan: Clean, "},{"file-1kb_(18th_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(18th_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(18th_copy).dat\n, FPScan: Clean, "},{"file-1kb_(7th_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(7th_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(7th_copy).dat\n, FPScan: Clean, "},{"file-1kb_(3rd_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(3rd_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(3rd_copy).dat\n, FPScan: Clean, "},{"file-1kb_(11th_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(11th_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(11th_copy).dat\n, FPScan: Clean, "},{"file-1kb_(9th_copy).dat":"ClamAV: Clean, File Type: /home/ubuntu/file-1kb_(9th_copy).dat: data\n, MD5 Sum: ede3d3b685b4e137ba4cb2521329a75e /home/ubuntu/file-1kb_(9th_copy).dat\n, FPScan: Clean, "}]}
Protocol:
{is_serial: serial, files: [{"name":"File_name", "size":file_size_in_bytes, "profile":"Profile_number"}]}
/r/n/r/n //two lines of space
<<STREAM_FILES_HERE>> // should be streamed in the same order they apper in the header
Example:
{"is_serial":"false","files":[{"name":"file-1kb (6th copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (19th copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (8th copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (13th copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (5th copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (4th copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (12th copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (14th copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (15th copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb.dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (another copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (10th copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (17th copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (16th copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (18th copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (7th copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (3rd copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (11th copy).dat","size":1000,"profile":"1,2,3,4"},{"name":"file-1kb (9th copy).dat","size":1000,"profile":"1,2,3,4"}]}
\r\n\r\n
<<FILE_STREAM_HERE>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment