Last active
August 29, 2015 13:56
-
-
Save tomjakubowski/9267238 to your computer and use it in GitHub Desktop.
simple debugging proxy for nrepl
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
#!/usr/bin/env ruby | |
require 'bencode' | |
require 'edn' | |
require 'socket' | |
if ARGV.length < 3 | |
puts "usage: #{$0} <source-port> <dest-host> <dest-port>" | |
exit | |
end | |
sport, dhost, dport = ARGV | |
puts "Listening on #{sport}, proxying to #{dhost}:#{dport}" | |
server = TCPServer.open('127.0.0.1', sport.to_i) | |
def decode_message(message) | |
decoded_stream = BEncode::Parser.new(StringIO.new(message.chomp)) | |
out = [] | |
until decoded_stream.eos? | |
out << decoded_stream.parse!.to_edn | |
end | |
out.join "\n " | |
end | |
loop do | |
source = server.accept | |
dest = TCPSocket.new(dhost, dport) | |
block_size = 32768 | |
loop do | |
ready = select([source, dest], nil, nil) | |
if ready[0].include?(source) | |
data = source.recv(block_size) | |
if data.empty? | |
puts "closed at local end" | |
break | |
end | |
puts " => #{decode_message(data)}" | |
dest.write(data) | |
end | |
if ready[0].include?(dest) | |
data = dest.recv(block_size) | |
if data.empty? | |
puts "closed at remote end" | |
break | |
end | |
puts "<= #{decode_message(data)}" | |
source.write(data) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment