Created
November 18, 2020 20:41
-
-
Save elorest/d8235bed6973b19d4e6e11d5f8dcede3 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 "http" | |
require "random" | |
require "openssl/cipher" | |
require "crypto/subtle" | |
random = Random.new | |
key = random.random_bytes(32) | |
def encrypt(value : Bytes, key) | |
cipher = OpenSSL::Cipher.new("aes-256-cbc") | |
cipher.encrypt | |
cipher.key = key | |
iv = cipher.random_iv | |
encrypted_data = IO::Memory.new | |
encrypted_data.write(cipher.update(value)) | |
encrypted_data.write(cipher.final) | |
encrypted_data.write(iv) | |
encrypted_data.to_slice | |
end | |
target_socket = "httpsocket.sock" | |
File.delete("httpsocket.sock") rescue "oh no!" | |
class ProcessHandler | |
include HTTP::Handler | |
property key : Bytes | |
def initialize(@key) | |
end | |
def call(context) | |
pp context.request | |
pp context.request.path | |
pp context.request.query_params | |
if body = context.request.body | |
data = Bytes.new(context.request.headers["Content-Length"].to_i) | |
body.read_fully(data) | |
pp data | |
# context.response.write data | |
context.response.write encrypt(data, key) | |
else | |
context.response.write "something went wrong!".to_slice | |
end | |
end | |
end | |
server = HTTP::Server.new( [HTTP::ErrorHandler.new, ProcessHandler.new(key)]) | |
spawn do | |
socket = UNIXSocket.new target_socket | |
#request = HTTP::Request.new(method, "/", HTTP::Headers{"X-Unix-Socket" => "/tmp/httpMS.sock"}) | |
request = HTTP::Request.new("GET", "/verify_signature?vin=2343") | |
request.body = random.random_bytes(16) | |
request.to_io(socket) | |
sleep 0.1 | |
pp HTTP::Client::Response.from_io(socket).body.to_slice | |
socket.close | |
end | |
server.bind_unix target_socket | |
server.listen |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment