Class: Pair

Inherits:
Thor
  • Object
show all
Defined in:
bin/pairkit

Instance Method Summary collapse

Instance Method Details

#claim_invite_codeObject



21
22
23
24
25
# File 'bin/pairkit', line 21

def claim_invite_code
  public_key = get_or_generate_ssh_key()
  claim_response = RestClient.get(SERVER_WEB + "/claim_invite_code", :params => {:invite_code => options[:invite_code], :public_key => public_key})
  puts claim_response
end

#share(*ports) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'bin/pairkit', line 28

def share(*ports)
  public_key = get_or_generate_ssh_key()
  
  # Register with the server
  response = RestClient.post(SERVER_WEB + "/share",
    :public_key => public_key,
    :ports => ports)

  share_result = J.load(response, :symbolize_keys => true)

  # Collect all the remote:local port forward pairs
  forwarded_ports = share_result[:remote_port_forwards].collect{ |remote_port, local_port| [remote_port, local_port] }
  reverse_tunnel_args = forwarded_ports.collect{ |ports|  "-R #{ports[0]}:localhost:#{ports[1]}" }

  # Setup our reverse tunnel so the server forwards its remote ports to our local ports
  setup_reverse_tunnel_cmd = "ssh -N #{reverse_tunnel_args.join(' ')} #{share_result[:ssh_user_name]}@#{share_result[:ssh_host]}"
  puts "Running: #{setup_reverse_tunnel_cmd}"

  pids = []
  pids << Kernel.fork { `#{setup_reverse_tunnel_cmd}` }

  # Let them know we're good to go
  puts "OK! We're forwarding ports as long as you keep this process running."
  puts ""
  forwarded_ports.each do |ports|
    puts "pairkit.com:#{ports[0]} -> localhost:#{ports[1]}"
  end
  puts ""
  puts "When you're ready to kill the port forwards, type Control+C."

  # Wait for a Control+C to quit
  trap("SIGINT") do 
    # Cleanup our ssh forwards before we quit
    pids.each {|pid| Process.kill("HUP", pid) }
    puts "Thank you, come again! =-)"
    exit!
  end
  cmd = nil
  while cmd != "quit" do
    cmd = STDIN.gets
  end
end