-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.rb
More file actions
executable file
·232 lines (203 loc) · 7.57 KB
/
server.rb
File metadata and controls
executable file
·232 lines (203 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/ruby
require 'socket'
require 'openssl'
require 'colorize'
require 'timeout'
require 'optparse'
require 'logger'
require 'net/http'
LOGGER = Logger.new(STDOUT)
PUBLIC_IP = Net::HTTP.get URI "https://api.ipify.org"
MAX_BUFFER = 1024 * 640 # 640KB
PORT = 443 # don't change this, because this server imitates the real https server
SERVER_NAME = "Rubinius_1.0.16/(#{RUBY_VERSION})/(#{OpenSSL::OPENSSL_VERSION})"
CONN_OK = "HTTP/1.1 200 Established\r\nDate: #{Time.now}\r\nServer: #{SERVER_NAME}\r\n\r\n"
CONN_FAIL = "HTTP/1.1 502 Bad Gateway\r\nDate: #{Time.now}\r\nServer: #{SERVER_NAME}\r\n\r\n<h1>502 Bad Gateway</h1>"
TTL = 10 # 10 seconds io select timeout
TCP_CONN_TIME_ABORT = 10 # 10 seconds timeout to abort the connection if tls connection is still in the PINIT state. This feature was made to drop all tcp connections that didn't start the tls negotiation process
OPTIONS = {}
# SSL Configuration
SSL = {
SSLClientCA: nil,
SSLExtraChainCert: nil,
SSLCACertificateFile: nil,
SSLCACertificatePath: nil,
SSLCertificateStore: nil,
SSLTmpDhCallback: nil,
SSLVerifyClient: OpenSSL::SSL::VERIFY_PEER,
SSLVerifyDepth: 5,
SSLVerifyCallback: nil,
SSLTimeout: 2,
SSLOPTIONS: nil,
SSLCiphers: nil,
SSLStartImmediately: true,
SSLCertName: nil,
SSLVer: OpenSSL::SSL::TLS1_3_VERSION
}
OptionParser.new do |opts|
opts.banner = "htun server\n\n".bold + 'Usage: ./server.rb [OPTIONS]'
opts.on('-v', '--verbose', 'run verbosely') do |v|
OPTIONS[:verbose] = v
end
opts.on('-h', '--help', 'prints help') do
puts opts
exit
end
opts.on('-fKEY', '--auth-keyfile=KEY',
'htun authorization key file, example: ./server.rb --auth-keyfile auth.key') do |auth_key|
OPTIONS[:auth_key] = auth_key
end
opts.on('-cCERT', '--certificate=CERT',
'ssl certificate, example: ./server.rb --certificate certificate.crt --key private.key') do |cert|
OPTIONS[:cert] = cert
end
opts.on('-kKEY', '--key=KEY',
'private key, example: ./server.rb --certificate certificate.crt --key private.key') do |key|
OPTIONS[:key] = key
end
end.parse!
if Process.uid != 0
LOGGER.error 'you must run it as root!'.red
exit
end
if !OPTIONS[:cert] || !OPTIONS[:key] || !OPTIONS[:auth_key]
LOGGER.error "please provide your ssl certificate, authentication key file and private key!".red
LOGGER.info "example: ./server.rb --certificate/-c certificate.crt --key/-k private.key --auth-keyfile/-f auth_key.txt".gray
exit
end
if !File.exist?(OPTIONS[:cert])
LOGGER.error 'certificate file not found! Please double check your local directory'.red
exit 1
end
if !File.exist?(OPTIONS[:key])
LOGGER.error 'key file not found! Please double check your local directory'.red
exit 1
end
if !File.exist?(OPTIONS[:auth_key])
LOGGER.error 'auth key file not found! Please double check your local directory'.red
exit 1
end
AUTH_KEY = File.read(OPTIONS[:auth_key]).chomp
socket = TCPServer.new(PORT)
LOGGER.info "listening on #{PORT}".bold
ssl_context = OpenSSL::SSL::SSLContext.new
ssl_context.cert = OpenSSL::X509::Certificate.new(File.open(OPTIONS[:cert]))
ssl_context.key = OpenSSL::PKey::RSA.new(File.open(OPTIONS[:key]))
ssl_context.verify_mode = SSL[:SSLVerifyClient]
ssl_context.verify_depth = SSL[:SSLVerifyDepth]
ssl_context.timeout = SSL[:SSLTimeout]
ssl_context.min_version = SSL[:SSLVer] # *IMPORTANT* TLS_1.3
def handle_client(connection)
LOGGER.info "new connection #{connection.peeraddr[-1]}:#{connection.peeraddr[1]}" if OPTIONS[:verbose]
request = connection.readpartial(MAX_BUFFER)
if request.nil? || request.empty?
LOGGER.warn 'empty request!' if OPTIONS[:verbose]
connection.close if connection
Thread.exit
end
request_split = request.split("\r\n")
if request.match?(/CONNECT/)
auth_header = request_split.find { |h| h.match(/Authorization/) }
unless auth_header
LOGGER.warn 'unauthorized attempt detected (no header provided)!'.red
connection.close
Thread.exit
end
if auth_header
auth_key = auth_header.downcase.gsub('authorization:', '').strip
if auth_key != AUTH_KEY
LOGGER.warn 'unauthorized attempt detected (wrong auth key)!'.red
connection.close
Thread.exit
end
end
endpoint_host, endpoint_port = request_split.first.split(' ')[1].split(':')
LOGGER.info "#{endpoint_host}:#{endpoint_port}".green if OPTIONS[:verbose]
endpoint_connection = TCPSocket.new(endpoint_host, endpoint_port)
endpoint_connection.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
if endpoint_connection
connection.puts(CONN_OK)
else
connection.puts(CONN_FAIL)
connection.close
Thread.exit
end
begin
loop do
fds = IO.select([connection, endpoint_connection], nil, nil, TTL)
if fds[0].member?(connection)
buf = connection.readpartial(MAX_BUFFER)
endpoint_connection.print(buf)
elsif fds[0].member?(endpoint_connection)
buf = endpoint_connection.readpartial(MAX_BUFFER)
connection.print(buf)
end
end
rescue StandardError
LOGGER.error "closing connection with #{endpoint_host}:#{endpoint_port}".red if OPTIONS[:verbose]
endpoint_connection.close if endpoint_connection
connection.close if connection
Thread.exit
end
else
host = request_split[1].downcase.gsub('host:', '').strip
endpoint_host, endpoint_port = host.split(':')
endpoint_port = 80 if endpoint_port.nil?
endpoint_port = endpoint_port.to_i
if endpoint_host == PUBLIC_IP
response = "HTTP/1.1 200 OK\r\nServer: #{SERVER_NAME}\r\nContent-Type: text/html\r\n\r\n#{File.read('index.html')}"
connection.puts(response)
connection.close
LOGGER.info "webpage is shown, closing the connection...".green if OPTIONS[:verbose]
Thread.exit
end
auth_header = request_split.find { |h| h.match(/Authorization/) }
unless auth_header
LOGGER.warn "unauthorized attempt detected (no header provided)!".red
connection.close
Thread.exit
end
if auth_header
auth_key = auth_header.downcase.gsub('authorization:', '').strip
if auth_key != AUTH_KEY
LOGGER.warn "unauthorized attempt detected (wrong auth key)!".red
connection.close
Thread.exit
end
end
begin
endpoint_connection = TCPSocket.new(endpoint_host, endpoint_port)
LOGGER.info "#{endpoint_host}:#{endpoint_port}".green if OPTIONS[:verbose]
endpoint_connection.puts(request)
response = endpoint_connection.readpartial(MAX_BUFFER)
connection.puts(response)
connection.close
Thread.exit
rescue StandardError
LOGGER.error "#{endpoint_host}:#{endpoint_port}".red if OPTIONS[:verbose]
connection.puts(CONN_FAIL)
connection.close if connection
Thread.exit
end
end
end
# The main loop
loop do
Thread.new(socket.accept) do |connection|
tls = OpenSSL::SSL::SSLSocket.new(connection, ssl_context)
tls_connection = nil
Timeout.timeout(10) do
tls_connection = tls.accept
end
if tls_connection
handle_client(tls_connection) if tls_connection
else
connection.close
end
rescue Timeout::Error
connection.close if connection && tls.state == 'PINIT'
rescue StandardError => e
LOGGER.error "#{e}".red if OPTIONS[:verbose]
connection.close if connection
end
end