Skip to content

Instantly share code, notes, and snippets.

@drbig
Last active April 24, 2017 18:24
Show Gist options
  • Save drbig/4446871 to your computer and use it in GitHub Desktop.
Save drbig/4446871 to your computer and use it in GitHub Desktop.
Pry session over telnet. Minimal and forking (plus a thread for the TCPServer).
drbig@swordfish:pts/16 ~/P/pry-tcprepl> cat pry-tcprepl.rb
require 'pry'
require 'pty'
class PryTcpRepl
attr_reader :thread
def initialize(where, host = '127.0.0.1', port = '9123')
@where = where
@host = host
@port = port
@thread = false
end
def run!
@thread ||= Thread.new do
TCPServer.open(@host, @port) do |s|
loop do
c = s.accept
pid = fork do
puts 'Client fork in.'
m, s = PTY.open
m.raw!
s.raw!
c.sync = m.sync = s.sync = true
old_ios = Array.new
[STDIN, STDOUT, STDERR].each do |i|
old_ios.push(i.dup)
i.reopen(s)
end
Thread.new { IO.copy_stream(c, m) }
Thread.new { IO.copy_stream(m, c) }
Pry.start(@where)
[STDIN, STDOUT, STDERR].each_with_index do |i, x|
i.reopen(old_ios[x])
end
s.close
m.close
c.close_write
c.close_read
puts 'Client fork out.'
exit(0)
end
Process.detach(pid)
puts "New client from #{c.peeraddr(:numeric).last} - pid #{pid}."
end
end
end
end
end
drbig@swordfish:pts/16 ~/P/pry-tcprepl> cat test.rb
require 'sinatra/base'
load 'pry-tcprepl.rb'
class MyApp < Sinatra::Base
get '/' do
'Hello world!'
end
end
repl = PryTcpRepl.new(MyApp)
repl.run!
MyApp.run!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment