initial commit
commit
5a6860194a
Binary file not shown.
@ -0,0 +1,68 @@
|
|||||||
|
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"
|
||||||
|
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
require './pipesocket'
|
||||||
|
|
||||||
|
cli = PipeSocket.new("pipe0", "127.0.0.1", 8080)
|
||||||
|
cli.connect
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
require './pipesocket.rb'
|
||||||
|
|
||||||
|
sp = SocketPipe.new "sockpipe", "127.0.0.1", "8080"
|
||||||
|
sp.listen
|
||||||
@ -0,0 +1,162 @@
|
|||||||
|
require 'socket'
|
||||||
|
include Socket::Constants
|
||||||
|
|
||||||
|
# A PipeSocket is an object that
|
||||||
|
|
||||||
|
def error(msg)
|
||||||
|
puts "PipeSocket: #{msg}"
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
|
class PipeSocket
|
||||||
|
def initialize(name, addr, port)
|
||||||
|
@path = name
|
||||||
|
@port = port
|
||||||
|
@addr = addr
|
||||||
|
if File.exist?(name)
|
||||||
|
error("File #{name} already exists.")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def connect
|
||||||
|
puts "Opening #{@path} for read/write"
|
||||||
|
File.mkfifo(@path)
|
||||||
|
File.open(@path, "r+") { |pipe|
|
||||||
|
loop do
|
||||||
|
socket = Socket.tcp(@addr, @port)
|
||||||
|
msg = pipe.gets
|
||||||
|
socket.puts msg
|
||||||
|
resp = socket.gets
|
||||||
|
unless resp.nil?
|
||||||
|
puts resp.chomp
|
||||||
|
break if resp.chomp == "shutdown"
|
||||||
|
end
|
||||||
|
break if msg.chomp == "shutdown"
|
||||||
|
socket.close
|
||||||
|
end
|
||||||
|
}
|
||||||
|
puts "Closing #{@path}"
|
||||||
|
File.delete(@path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
#not useful?
|
||||||
|
class SocketPipe
|
||||||
|
def initialize(name, addr, port)
|
||||||
|
@path, @addr, @port = name, addr, port
|
||||||
|
if File.exist?(name)
|
||||||
|
error("File #{name} already exists.")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def listen
|
||||||
|
puts "Opening #{@path} for reading"
|
||||||
|
File.mkfifo(@path)
|
||||||
|
Socket.tcp_server_loop(@port) { |sock|
|
||||||
|
msg = sock.gets
|
||||||
|
File.open(@path, "r+") { |pipe| pipe.puts msg }
|
||||||
|
sock.close
|
||||||
|
break if msg.chomp == "shutdown"
|
||||||
|
}
|
||||||
|
puts "Closing #{@path}"
|
||||||
|
File.delete(@path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# BackShot should be a server listening for
|
||||||
|
# a PipeSocket connection, which receives a
|
||||||
|
# ruby file containing an instance of the
|
||||||
|
# BackShot class
|
||||||
|
|
||||||
|
class BackShot
|
||||||
|
attr_accessor :send_back
|
||||||
|
attr_accessor :command
|
||||||
|
|
||||||
|
@@inst = 0
|
||||||
|
|
||||||
|
def initialize(addr, port)
|
||||||
|
@name = "backshot#{@@inst}"
|
||||||
|
@@inst += 1
|
||||||
|
@addr = addr
|
||||||
|
@port = port
|
||||||
|
|
||||||
|
@return = nil
|
||||||
|
@execute = nil
|
||||||
|
|
||||||
|
#a channel to send back the result
|
||||||
|
end
|
||||||
|
|
||||||
|
def BackShot.message(msg)
|
||||||
|
puts "BackShot: #{msg}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def listen
|
||||||
|
BackShot.message "listening on port #{@port}"
|
||||||
|
loop do
|
||||||
|
#hello
|
||||||
|
Socket.tcp_server_loop(@port) { |sock|
|
||||||
|
msg = sock.gets
|
||||||
|
if msg.chomp == "hello"
|
||||||
|
sock.puts "hello"
|
||||||
|
break
|
||||||
|
else
|
||||||
|
sock.puts "goodbye"
|
||||||
|
end
|
||||||
|
sock.close
|
||||||
|
}
|
||||||
|
|
||||||
|
def recv_file(filename, expected_size)
|
||||||
|
Socket.tcp_server_loop(@port) { |sock|
|
||||||
|
data = sock.recv(expected_size)
|
||||||
|
File.open(filename, "w+") { |file| file.write data }
|
||||||
|
sock.puts "ok"
|
||||||
|
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
#recv files
|
||||||
|
Socket.tcp_server_loop(@port) { |sock|
|
||||||
|
msg = sock.gets
|
||||||
|
if msg.start_with? "file"
|
||||||
|
puts "file info recv"
|
||||||
|
# file name size
|
||||||
|
file, name, size = msg.split(" ")
|
||||||
|
size = size.chomp
|
||||||
|
sock.puts "ok"
|
||||||
|
|
||||||
|
recv_file(name, size)
|
||||||
|
elsif msg.start_with? "run"
|
||||||
|
puts "run recv"
|
||||||
|
# execute file
|
||||||
|
exec, name = msg.split(" ")
|
||||||
|
@execute = name
|
||||||
|
sock.puts "ok"
|
||||||
|
#sock.close
|
||||||
|
elsif msg.start_with? "return"
|
||||||
|
puts "return recv"
|
||||||
|
ret, name = msg.split(" ")
|
||||||
|
@return = name
|
||||||
|
if @execute == nil
|
||||||
|
sock.puts "error"
|
||||||
|
# sock.close
|
||||||
|
break
|
||||||
|
end
|
||||||
|
`./#{@execute}`
|
||||||
|
sock.write File.read(@return)
|
||||||
|
msg = sock.gets
|
||||||
|
if msg.chomp == "thanks"
|
||||||
|
@execute = nil
|
||||||
|
#sock.close
|
||||||
|
break
|
||||||
|
end
|
||||||
|
else
|
||||||
|
sock.puts "what"
|
||||||
|
#sock.close
|
||||||
|
end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
bs = BackShot.new "127.0.0.1", 8081
|
||||||
|
bs.listen
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
require 'socket'
|
||||||
|
|
||||||
|
puts "Opening a TCP Server on port 8080..."
|
||||||
|
|
||||||
|
TCPServer.open("127.0.0.1", 8080) { |server|
|
||||||
|
loop do
|
||||||
|
conn = server.accept
|
||||||
|
msg = conn.gets.chomp
|
||||||
|
puts msg
|
||||||
|
conn.puts "response"
|
||||||
|
conn.close
|
||||||
|
break if msg == "shutdown"
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
puts "Shutting down..."
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
require './pipesocket'
|
||||||
|
|
||||||
|
srv = PipeSocket.new("pipe1", "127.0.0.1", 8080)
|
||||||
|
srv.listen
|
||||||
|
srv.close
|
||||||
Loading…
Reference in New Issue