Created
August 1, 2016 15:07
-
-
Save vitalyp/54cf45f3dc99463a26b676fec7d28f87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# with comments | |
def get_random_bytes count | |
random = `dd if=/dev/urandom | head -c #{count}` | |
puts "random data is:\n#{random}\n\r-----length is: #{random.length}" | |
random | |
end | |
def clip_encoded_with data_str, encoding | |
encoded = data_str.encode(encoding, invalid: :replace, undef: :replace, replace: '') | |
puts "encoded string is:\n#{encoded}\n\r-----length is: #{encoded.length}" | |
encoded | |
end | |
def replace_chars data_str, match_code, replacement | |
replaced = data_str.each_byte.map.with_index do |b, i| | |
b == match_code ? replacement : data_str[i] | |
end.join | |
puts "replaced string is:\n#{replaced}\n\r-----length is: #{replaced.length}" | |
replaced | |
end | |
def send_socket host, port, data | |
require 'socket' | |
sock = TCPSocket.new(host, port) | |
sock.write data | |
sock.close | |
end | |
# read 1024 byes from /dev/urandom: | |
random = get_random_bytes 1024 | |
# remove not UTF-8 chars | |
utf8 = clip_encoded_with random, 'UTF-8' | |
# replace spaces with asterisk | |
replaced = replace_chars utf8, 32, '*' | |
# send data to TCP server | |
send_socket 'localhost', 2345, replaced |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
server = TCPServer.new('localhost', 2345) | |
loop do | |
socket = server.accept | |
request = socket.gets | |
STDERR.puts request | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment