forked from yagop/telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisup.lua
131 lines (118 loc) · 3.02 KB
/
isup.lua
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
do
local socket = require("socket")
local cronned = load_from_file('data/isup.lua')
local function save_cron(msg, url, delete)
local origin = get_receiver(msg)
if not cronned[origin] then
cronned[origin] = {}
end
if not delete then
table.insert(cronned[origin], url)
else
for k,v in pairs(cronned[origin]) do
if v == url then
table.remove(cronned[origin], k)
end
end
end
serialize_to_file(cronned, 'data/isup.lua')
return 'Saved!'
end
local function is_up_socket(ip, port)
print('Connect to', ip, port)
local c = socket.try(socket.tcp())
c:settimeout(3)
local conn = c:connect(ip, port)
if not conn then
return false
else
c:close()
return true
end
end
local function is_up_http(url)
-- Parse URL from input, default to http
local parsed_url = URL.parse(url, { scheme = 'http', authority = '' })
-- Fix URLs without subdomain not parsed properly
if not parsed_url.host and parsed_url.path then
parsed_url.host = parsed_url.path
parsed_url.path = ""
end
-- Re-build URL
local url = URL.build(parsed_url)
local protocols = {
["https"] = https,
["http"] = http
}
local options = {
url = url,
redirect = false,
method = "GET"
}
local response = { protocols[parsed_url.scheme].request(options) }
local code = tonumber(response[2])
if code == nil or code >= 400 then
return false
end
return true
end
local function isup(url)
local pattern = '^(%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?):?(%d?%d?%d?%d?%d?)$'
local ip,port = string.match(url, pattern)
local result = nil
-- !isup 8.8.8.8:53
if ip then
port = port or '80'
result = is_up_socket(ip, port)
else
result = is_up_http(url)
end
return result
end
local function cron()
for chan, urls in pairs(cronned) do
for k,url in pairs(urls) do
print('Checking', url)
if not isup(url) then
local text = url..' looks DOWN from here. 😱'
send_msg(chan, text, ok_cb, false)
end
end
end
end
local function run(msg, matches)
if matches[1] == 'cron delete' then
if not is_sudo(msg) then
return 'This command requires privileged user'
end
return save_cron(msg, matches[2], true)
elseif matches[1] == 'cron' then
if not is_sudo(msg) then
return 'This command requires privileged user'
end
return save_cron(msg, matches[2])
elseif isup(matches[1]) then
return matches[1]..' looks UP from here. 😃'
else
return matches[1]..' looks DOWN from here. 😱'
end
end
return {
description = "Check if a website or server is up.",
usage = {
"!isup [host]: Performs a HTTP request or Socket (ip:port) connection",
"!isup cron [host]: Every 5mins check if host is up. (Requires privileged user)",
"!isup cron delete [host]: Disable checking that host."
},
patterns = {
"^!isup (cron delete) (.*)$",
"^!isup (cron) (.*)$",
"^!isup (.*)$",
"^!ping (.*)$",
"^!ping (cron delete) (.*)$",
"^!ping (cron) (.*)$"
},
run = run,
cron = cron
}
end