Skip to content

Instantly share code, notes, and snippets.

@jamesmoriarty
Last active September 26, 2021 00:25
Show Gist options
  • Save jamesmoriarty/a6100395d2efb17dcd06173300f988bb to your computer and use it in GitHub Desktop.
Save jamesmoriarty/a6100395d2efb17dcd06173300f988bb to your computer and use it in GitHub Desktop.
HTTP CONNECT by example
require 'socket'
listen_socket = TCPServer.new('127.0.0.1', 9292)
client_conn = listen_socket.accept
request_line = client_conn.gets
while(line = client_conn.gets) do
break unless line.include?(':')
end
host, port = *request_line.split[1].split(':')
dest_conn = TCPSocket.new(host, port)
client_conn.write "HTTP/1.1 200 OK\r\n"
def transfer(src_conn, dest_conn)
IO.copy_stream(src_conn, dest_conn)
rescue => e
puts e.message
end
[
Thread.new { transfer(client_conn, dest_conn) },
Thread.new { transfer(dest_conn, client_conn) }
].each(&:join)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment