require 'socket' def error(msg) puts "error: #{msg}" exit 1 end class BackShotClient def initialize(addr, port) @addr = addr @port = port @hello = false end def get_resp(sock) msg = sock.gets error("not ok: #{msg}") if msg.chomp != "ok" end def hello TCPSocket.open(@addr, @port) { |sock| sock.puts "hello" @hello = true if sock.gets.chomp == "hello" } end def send_file_info(path) hello unless @hello TCPSocket.open(@addr, @port) { |sock| sock.puts "file #{path} #{File.size path}" get_resp sock } end def send_file(path) self.send_file_info path TCPSocket.open(@addr, @port) { |sock| sock.write File.read path get_resp sock } end def send_exec(path) TCPSocket.open(@addr, @port) { |sock| sock.puts "run #{path}" get_resp sock } end def send_return(path) TCPSocket.open(@addr, @port) { |sock| sock.puts "return #{path}" File.open(path, "w+") { |file| file.write sock.read } sock.puts "thanks" } end end def send_file_info(path) TCPSocket.open("127.0.0.1") end bsc = BackShotClient.new "127.0.0.1", 8081 bsc.send_file "test.rb" bsc.send_exec "test.rb" bsc.send_return "happy.txt"