-
-
Save chintanparikh/3ea1671ab2b73e39c4af 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
require 'debugger' | |
require 'socket' | |
require 'digest' | |
require 'json' | |
HOST = "0.0.0.0" | |
PORT = ARGV[0] | |
USERNAME = 'username' | |
PASSWORD = 'password' | |
include Socket::Constants | |
socket = Socket.new(AF_INET, SOCK_STREAM, 0) | |
sockaddr = Socket.pack_sockaddr_in(PORT, '0.0.0.0' ) | |
socket.connect(sockaddr) | |
socket.puts "#{{request: "BEGIN"}.to_json}" | |
key = socket.readline.chomp | |
print key | |
hashed = Digest::MD5.hexdigest("#{USERNAME}#{PASSWORD}#{key}") | |
socket.puts "#{{request: "AUTHENTICATE", username: USERNAME, hash: hashed}.to_json}" | |
socket.close |
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 'securerandom' | |
require 'socket' | |
require 'json' | |
USERS = { | |
"username" => "password", | |
"chintanparikh" => "anotherpassword", | |
"letmein" => "lastpassword" | |
} | |
CLIENTS = {} | |
HOSTNAME = '0.0.0.0' | |
PORT = ARGV[0] | |
include Socket::Constants | |
socket = Socket.new(AF_INET, SOCK_STREAM, 0) | |
sockaddr = Socket.pack_sockaddr_in(PORT, '0.0.0.0') | |
socket.bind(sockaddr) | |
socket.listen(5) | |
loop do | |
Thread.start(socket.accept) do |client, addr| | |
# client, addr = socket.accept | |
request_hash = JSON.parse(client.readline.chomp) | |
print request_hash | |
case request_hash["request"] | |
when "BEGIN" | |
key = SecureRandom.hex(64) | |
client.write key | |
when "AUTHENTICATE" | |
print request_hash["username"] | |
end | |
end | |
end | |
socket.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment