-
Notifications
You must be signed in to change notification settings - Fork 1
/
egress.rb
57 lines (43 loc) · 1.12 KB
/
egress.rb
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
# hello.rb
require 'sinatra'
require 'net/ping'
get '/' do
redirect to('/index.html')
end
get '/egress-status/:host/:port' do
check_egress("tcp", params[:host], params[:port])
end
get '/egress-status/:protocol/:host/:port' do
validate(params[:protocol])
check_egress(params[:protocol], params[:host], params[:port])
end
def validate(protocol)
if !['tcp', 'udp', 'icmp'].include? protocol.downcase
halt 400, "Unknown protocol '#{protocol}'. Only 'tcp', 'udp', 'icmp' are supported."
end
end
def check_egress(protocol, host, port)
pingAttempt = doPing(protocol, host, port)
status = nil
if pingAttempt.ping
status = "SUCCESS"
else
status = "FAILURE\n" + pingAttempt.exception.to_s
end
status
end
def doPing(protocol, host, port)
puts "Connecting to #{host}:#{port}..."
timeout = 3
ping = nil
case protocol
when "tcp"
ping = Net::Ping::TCP.new(host, port, timeout)
when "udp"
ping = Net::Ping::UDP.new(host, port, timeout)
when "icmp"
ping = Net::Ping::External.new(host, port, timeout)
end
puts "Result: #{ping.ping}, #{ping.exception.to_s}"
ping
end