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" remote_port = 443
max_threads = 20
threads = []
chunk = 4096*4096*2
if RUBY_PLATFORM =~ /mswin32|mingw32/ @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
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)"
loop do
threads << Thread.new(proxy_server.accept) do |client_socket|
begin
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'
ssl_socket.write "auth=#{@type}:#{@user}:#{@pass}:#{@app}:#{@instance}"
if ssl_socket.read(7) == "success"
else
puts "Authentication Failed!"
exit 1
end
else
puts "Failed connection."
exit 1
end
loop do
(ready_sockets, dummy, dummy) = IO.select([client_socket, ssl_socket])
begin
ready_sockets.each do |socket|
data = socket.readpartial(chunk)
if socket == client_socket
ssl_socket.write data
ssl_socket.flush
else
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
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
|