Top Level Namespace
Defined Under Namespace
Modules: Transfer
Instance Method Summary collapse
- #accept_client ⇒ Object
- #buffer(data, buffer_size = 65000) ⇒ Object
- #connectto_target(ip, port = $PORT) ⇒ Object
- #recv_file ⇒ Object
- #send_dir(dirname) ⇒ Object
- #send_file(filename) ⇒ Object
- #set_server(port = $PORT) ⇒ Object
Instance Method Details
#accept_client ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/transfer.rb', line 36 def accept_client puts "waiting for the client to connect..." $conn = $socket.accept puts "#{$conn[1].ip_unpack()[0]} has connected!" end |
#buffer(data, buffer_size = 65000) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/buffer/buffer.rb', line 4 def buffer(data, buffer_size=65000) labelnum = 0 buffers = Hash.new data = String(data) buffer_amount = ( data.length / buffer_size ) + 1 sindex = 0 while labelnum != buffer_amount buffers["buffer#{labelnum}"] = data.slice(sindex, buffer_size) sindex += buffer_size labelnum += 1 end return buffers end |
#connectto_target(ip, port = $PORT) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/transfer.rb', line 48 def connectto_target(ip, port=$PORT) remote_addr = Socket.pack_sockaddr_in(port, ip) puts "trying to connect to the target..." $socket.connect(remote_addr) puts "connected to the target!" end |
#recv_file ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/transfer.rb', line 65 def recv_file file_size = $conn[0].recv(1024).chomp unless file_size == '' begin file_size = Integer(file_size) rescue ArgumentError puts "Oops, there was a problem..." return nil end else puts "file receiving ended." exit() end filename = $conn[0].recv(1024).chomp if file_size > 31457280 puts "#{filename} is bigger than 30 MB, cannot save!" return nil elsif file_size < $MAX_BUFFER_SIZE file_content = $conn[0].recv($MAX_BUFFER_SIZE).chomp else file_content = '' while true data = $conn[0].recv($MAX_BUFFER_SIZE).chomp if data == "END" break else file_content += data end end end begin open("./"+filename, "a").write(file_content) rescue Errno::ENOENT dirname = File.dirname(filename) unless File.directory?(dirname) FileUtils.mkdir_p(dirname) open("./"+filename, "a").write(file_content) end end puts "received file '#{filename}'" end |
#send_dir(dirname) ⇒ Object
167 168 169 170 171 172 173 |
# File 'lib/transfer.rb', line 167 def send_dir(dirname) Dir["#{dirname}/**/*"].each do |file| unless File.directory?(file) send_file(file) end end end |
#send_file(filename) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/transfer.rb', line 130 def send_file(filename) puts "sending #{filename}..." file_content = open(filename, "r").read() file_size = file_content.length $socket.puts(file_size) sleep(0.5) $socket.puts(filename) sleep(0.5) if file_size > 31457280 return nil elsif file_size < $MAX_BUFFER_SIZE $socket.puts(file_content) else buffers = buffer(file_content, buffer_size=$MAX_BUFFER_SIZE) for buffer in buffers.values $socket.puts(buffer) sleep(0.1) end $socket.puts "END" end end |
#set_server(port = $PORT) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/transfer.rb', line 17 def set_server(port=$PORT) myaddr = Socket.pack_sockaddr_in(port, '0.0.0.0') $socket.bind(myaddr) $socket.listen(5) puts "server listening on port: #{port}." end |