Class: Pagoda::Tunnel

Inherits:
Object
  • Object
show all
Defined in:
lib/pagoda-tunnel.rb

Constant Summary collapse

VERSION =
"0.1.3"

Instance Method Summary collapse

Constructor Details

#initialize(type, user, pass, app, instance, port = 3306) ⇒ Tunnel

Your code goes here…



10
11
12
13
14
15
16
17
# File 'lib/pagoda-tunnel.rb', line 10

def initialize(type, user, pass, app, instance, port = 3306)
  @type     = type
  @user     = user
  @pass     = pass
  @app      = app
  @instance = instance
  @port     = port
end

Instance Method Details

#next_available_port(start_port) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/pagoda-tunnel.rb', line 35

def next_available_port(start_port)
  until port_available?("0.0.0.0", start_port)
    # puts "port #{start_port} was not available"
    start_port += 1
  end
  start_port
end

#port_available?(ip, port) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pagoda-tunnel.rb', line 19

def port_available?(ip, port)
  begin
    Timeout::timeout(1) do
      begin
        s = TCPSocket.new(ip, port)
        s.close
        return false
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        return true
      end
    end
  rescue Timeout::Error
  end
  return true
end

#startObject



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/pagoda-tunnel.rb', line 43

def start
  
  [:INT, :TERM].each do |sig|
    Signal.trap(sig) do
      puts "Tunnel Closed."
      puts "-----------------------------------------------"
      puts
      exit
    end
  end
  
  remote_host = "tunnel.pagodabox.com" # switch to tunnel.pagodabox.com
  remote_port = 443

  max_threads     = 20
  threads         = []

  chunk           = 4096*4096*2

  # If we are running on windows checking available ports does not work
  # thus we must just attempt a few times and fail if we cant we start at a high number and pray it works :)
  
  if RUBY_PLATFORM =~ /mswin32|mingw32/ # running on windows
    @port = 45000
  else
    @port = next_available_port(@port)
  end

  retrys = 0
  begin
    proxy_server = TCPServer.new('0.0.0.0', @port)
  rescue Exception => e
    @port += 1
    retry if retrys += 1 < 4
    # puts "unable to connect to #{@port}. The algorithm is broken"
    exit
  end
  
  puts
  puts "Tunnel Established!  Accepting connections on :"
  puts "-----------------------------------------------"
  puts
  puts "HOST : 127.0.0.1 (or localhost)"
  puts "PORT : #{@port}"
  puts "USER : (found in pagodabox dashboard)"
  puts "PASS : (found in pagodabox dashboard)"
  puts
  puts "-----------------------------------------------"
  puts "(note : ctrl-c To close this tunnel)"
  
  # puts "what th heck"
  loop do

    # puts "start a new thread for every client connection"
    threads << Thread.new(proxy_server.accept) do |client_socket|
      begin
        # puts "client connection"
        begin
          server_socket         = TCPSocket.new(remote_host, remote_port)
          ssl_context           = OpenSSL::SSL::SSLContext.new()  
          ssl_socket            = OpenSSL::SSL::SSLSocket.new(server_socket, ssl_context)
          ssl_socket.sync_close = true
          ssl_socket.connect
        rescue Errno::ECONNREFUSED
          puts "connection refused"
          client_socket.close
          raise
        end

        
        if ssl_socket.read(4) == 'auth'
          # puts "authentication"
          # puts "auth=#{@type}:#{@user}:#{@pass}:#{@app}:#{@instance}" 
          ssl_socket.write "auth=#{@type}:#{@user}:#{@pass}:#{@app}:#{@instance}" 
          if ssl_socket.read(7) == "success"
            # puts "successful connection"
          else
            puts "Authentication Failed!"
            exit 1
          end
        else
          puts "Failed connection."
          exit 1
        end

        loop do
          # puts "wait for data on either socket"
          (ready_sockets, dummy, dummy) = IO.select([client_socket, ssl_socket])

          # puts "full duplex connection until data stream ends"
          begin
            ready_sockets.each do |socket|
              data = socket.readpartial(chunk)
              if socket == client_socket
                # puts "SERVER <== CLIENT"
                ssl_socket.write data
                ssl_socket.flush
              else
                # puts "SERVER ==> CLIENT"
                client_socket.write data
                client_socket.flush
              end
            end
          rescue EOFError
            break
          end
        end

      rescue StandardError => error
      end
      client_socket.close rescue StandardError
      ssl_socket.close rescue StandardError
    end

    # puts "clean up the dead threads, and wait until we have available threads"
    threads = threads.select { |thread| thread.alive? ? true : (thread.join; false) }
    while threads.size >= max_threads
      sleep 1
      threads = threads.select { |thread| thread.alive? ? true : (thread.join; false) }
    end
  end
end