diff --git a/otouto/plugins/ip_info.lua b/otouto/plugins/ip_info.lua index df948e7..4d50e47 100644 --- a/otouto/plugins/ip_info.lua +++ b/otouto/plugins/ip_info.lua @@ -82,7 +82,7 @@ function ip_info:action(msg, config, matches) if image_url then utilities.send_typing(self, msg.chat.id, 'upload_photo') - local file = download_to_file(image_url) + local file = download_to_file(image_url, 'map.png') utilities.send_photo(self, msg.chat.id, file, text, msg.message_id) else utilities.send_reply(self, msg, text) diff --git a/otouto/plugins/isup.lua b/otouto/plugins/isup.lua new file mode 100644 index 0000000..ea37df0 --- /dev/null +++ b/otouto/plugins/isup.lua @@ -0,0 +1,85 @@ +local isup = {} + +local http = require('socket.http') +local https = require('ssl.https') +local socket = require('socket') +local URL = require('socket.url') +local utilities = require('otouto.utilities') + +function isup:init(config) + isup.triggers = { + "^/isup (.*)$", + "^/ping (.*)$" + } + + isup.doc = [[* +]]..config.cmd_pat..[[isup* __: Prüft, ob die URL up ist]] +end + +function isup: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 + +function isup: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 + +function isup: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 = isup:is_up_socket(ip, port) + else + result = isup:is_up_http(url) + end + return result +end + +function isup:action(msg, config) + if isup:isup(matches[1]) then + utilities.send_reply(self, msg, matches[1]..' ist UP! ✅') + return + else + utilities.send_reply(self, msg, matches[1]..' ist DOWN! ❌') + return + end +end + +return isup