diff --git a/.circleci/config.yml b/.circleci/config.yml
index aea7adc..298c286 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -17,11 +17,11 @@ jobs:
- run:
name: 'Checkout dependencies'
command: |
- git clone --branch v0.10 https://github.com/pintsized/lua-resty-http.git repos/lua-resty-http
- git clone --branch v0.18 https://github.com/openresty/lua-resty-dns.git repos/lua-resty-dns
- git clone --branch v0.10 https://github.com/openresty/lua-resty-upload.git repos/lua-resty-upload
- git clone --branch v1.4 https://github.com/bungle/lua-resty-reqargs.git repos/lua-resty-reqargs
- git clone https://github.com/xiaooloong/lua-resty-iconv.git repos/lua-resty-iconv
+ git clone -q --branch v0.10 https://github.com/pintsized/lua-resty-http.git repos/lua-resty-http
+ git clone -q --branch v0.18 https://github.com/openresty/lua-resty-dns.git repos/lua-resty-dns
+ git clone -q --branch v0.10 https://github.com/openresty/lua-resty-upload.git repos/lua-resty-upload
+ git clone -q --branch v1.4 https://github.com/bungle/lua-resty-reqargs.git repos/lua-resty-reqargs
+ git clone -q https://github.com/xiaooloong/lua-resty-iconv.git repos/lua-resty-iconv
- run:
name: 'Nginx version'
command: nginx -V
diff --git a/conf/v1/hooks.conf b/conf/v1/hooks.conf
index 15fafa7..2a7d4e5 100644
--- a/conf/v1/hooks.conf
+++ b/conf/v1/hooks.conf
@@ -1,404 +1,404 @@
# HipChat!
location = /v1/hooks/hipchat/install {
- default_type application/json;
- content_by_lua_block {
-
- local cjson = require("cjson")
- local json_encode = cjson.encode
-
- local payload = {
- name = "GeoJS IP Lookup",
- description = "Lookup IP Geographic information",
- key = "com.jloh.geojs",
- links = {
- homepage = "https://geojs.io",
- self = "https://geojs.io/docs/chatops/"
- },
- vendor = {
- name = "James Loh",
- url = "https://jloh.co/"
- },
- capabilities = {
- hipchatApiConsumer = {
- scopes = {"send_notification"},
- fromName = "IP Information",
- avatar = {
- url = "https://static.jloh.co/geojs/avatar/hipchat/v1/geojs-icon_1x.png",
- ["url@2x"] = "https://static.jloh.co/geojs/avatar/hipchat/v1/geojs-icon_2x.png"
- }
- },
- webhook = {{
- url = "https://get.geojs.io/v1/hooks/hipchat",
- pattern = "^/[gG][eE][oO][jJ][sS]",
- event = "room_message",
- name = "GeoJS"
- }}
- }
- }
-
- ngx.say(json_encode(payload))
- }
+ default_type application/json;
+ content_by_lua_block {
+
+ local cjson = require("cjson")
+ local json_encode = cjson.encode
+
+ local payload = {
+ name = "GeoJS IP Lookup",
+ description = "Lookup IP Geographic information",
+ key = "com.jloh.geojs",
+ links = {
+ homepage = "https://geojs.io",
+ self = "https://geojs.io/docs/chatops/"
+ },
+ vendor = {
+ name = "James Loh",
+ url = "https://jloh.co/"
+ },
+ capabilities = {
+ hipchatApiConsumer = {
+ scopes = {"send_notification"},
+ fromName = "IP Information",
+ avatar = {
+ url = "https://static.jloh.co/geojs/avatar/hipchat/v1/geojs-icon_1x.png",
+ ["url@2x"] = "https://static.jloh.co/geojs/avatar/hipchat/v1/geojs-icon_2x.png"
+ }
+ },
+ webhook = {{
+ url = "https://get.geojs.io/v1/hooks/hipchat",
+ pattern = "^/[gG][eE][oO][jJ][sS]",
+ event = "room_message",
+ name = "GeoJS"
+ }}
+ }
+ }
+
+ ngx.say(json_encode(payload))
+ }
}
location = /v1/hooks/hipchat {
- default_type application/json;
- content_by_lua_block {
- -- Get our request body
- local get, post, files = require "resty.reqargs"()
-
- local split = require("geojs.utils").split
- local upstreamreq = require("geojs.utils").upstream_req
- local getptr = require("geojs.utils").get_ptr
- local trim = require("geojs.utils").trim
- local cjson = require("cjson")
- local json_encode = cjson.encode
- local json_decode = cjson.decode
- local reqpath = '/v1/ip/geo.json'
-
- -- Get the message from HipChat
- local message = post["item"]["message"]["message"]
- -- Get our IP we're wanting to look at
- -- HipChat gives us the full message with the slash command
- -- Just remove everything before the space
- local req_ip = string.gsub(message, "/.*%s", "")
- -- Trim whitespace we might have
- req_ip = trim(req_ip)
-
- -- Get our data with the requested IP
- local req_resp = upstreamreq(reqpath, req_ip)
- local req_ptr = getptr(req_ip)
- local req_data = json_decode(req_resp)
-
- -- Just in case we can't find out country
- if req_data['country'] == nil then
- req_data['country'] = "unknown country"
- end
-
- -- Define some card vars
- local card_title = string.format("%s is a %s IP belonging to %s", req_ip, req_data['country'], req_data['organization'])
- local txt_message = string.format("Results for %s
", req_ip)
- local card_simp_title = string.format("GeoIP results for %s", req_ip)
- local card_body = ""
- local card_icons = {}
-
- -- Only some items end up in vars since it has a dumb limit of 3 lines?
- if req_ptr then
- card_body = card_body .. 'PTR: ' .. req_ptr
- txt_message = txt_message .. 'PTR: ' .. req_ptr
- end
- if req_data['country'] then
- txt_message = txt_message .. '
Country: ' .. req_data['country']
- card_icons = {
- url = string.lower(string.format("https://static.jloh.co/geojs/flags/v1/%s.png", req_data['country_code'])),
- ["url@2x"] = string.lower(string.format("https://static.jloh.co/geojs/flags/v1/2x/%s.png", req_data['country_code']))
- }
- end
- if req_data['region'] then
- card_body = card_body .. '\nRegion: ' .. req_data['region']
- txt_message = txt_message .. '
Region: ' .. req_data['region']
- end
- if req_data['city'] then
- card_body = card_body .. '\nCity: ' .. req_data['city']
- txt_message = txt_message .. '
City: ' .. req_data['city']
- end
- if req_data['organization'] then
- txt_message = txt_message .. '
Organization: ' .. req_data['organization']
- end
- txt_message = txt_message .. '
Powered by GeoJS'
-
- local card_message = {
- style = "application",
- format = "medium",
- title = card_simp_title,
- id = post["item"]["message"]["id"],
- activity = {
- html = card_title
- },
- description = {
- format = "html",
- value = card_body
- },
- attributes = {{
- label = "Powered by",
- value = {
- label = "GeoJS",
- url = "https://geojs.io"
- }
- }},
- icon = card_icons
- }
-
- ngx.say(json_encode({
- message = txt_message,
- card = card_message,
- notify = "False",
- message_format = "html",
- }))
- }
+ default_type application/json;
+ content_by_lua_block {
+ -- Get our request body
+ local get, post, files = require "resty.reqargs"()
+
+ local split = require("geojs.utils").split
+ local upstreamreq = require("geojs.utils").upstream_req
+ local getptr = require("geojs.utils").get_ptr
+ local trim = require("geojs.utils").trim
+ local cjson = require("cjson")
+ local json_encode = cjson.encode
+ local json_decode = cjson.decode
+ local reqpath = '/v1/ip/geo.json'
+
+ -- Get the message from HipChat
+ local message = post["item"]["message"]["message"]
+ -- Get our IP we're wanting to look at
+ -- HipChat gives us the full message with the slash command
+ -- Just remove everything before the space
+ local req_ip = string.gsub(message, "/.*%s", "")
+ -- Trim whitespace we might have
+ req_ip = trim(req_ip)
+
+ -- Get our data with the requested IP
+ local req_resp = upstreamreq(reqpath, req_ip)
+ local req_ptr = getptr(req_ip)
+ local req_data = json_decode(req_resp)
+
+ -- Just in case we can't find out country
+ if req_data['country'] == nil then
+ req_data['country'] = "unknown country"
+ end
+
+ -- Define some card vars
+ local card_title = string.format("%s is a %s IP belonging to %s", req_ip, req_data['country'], req_data['organization'])
+ local txt_message = string.format("Results for %s
", req_ip)
+ local card_simp_title = string.format("GeoIP results for %s", req_ip)
+ local card_body = ""
+ local card_icons = {}
+
+ -- Only some items end up in vars since it has a dumb limit of 3 lines?
+ if req_ptr then
+ card_body = card_body .. 'PTR: ' .. req_ptr
+ txt_message = txt_message .. 'PTR: ' .. req_ptr
+ end
+ if req_data['country'] then
+ txt_message = txt_message .. '
Country: ' .. req_data['country']
+ card_icons = {
+ url = string.lower(string.format("https://static.jloh.co/geojs/flags/v1/%s.png", req_data['country_code'])),
+ ["url@2x"] = string.lower(string.format("https://static.jloh.co/geojs/flags/v1/2x/%s.png", req_data['country_code']))
+ }
+ end
+ if req_data['region'] then
+ card_body = card_body .. '\nRegion: ' .. req_data['region']
+ txt_message = txt_message .. '
Region: ' .. req_data['region']
+ end
+ if req_data['city'] then
+ card_body = card_body .. '\nCity: ' .. req_data['city']
+ txt_message = txt_message .. '
City: ' .. req_data['city']
+ end
+ if req_data['organization'] then
+ txt_message = txt_message .. '
Organization: ' .. req_data['organization']
+ end
+ txt_message = txt_message .. '
Powered by GeoJS'
+
+ local card_message = {
+ style = "application",
+ format = "medium",
+ title = card_simp_title,
+ id = post["item"]["message"]["id"],
+ activity = {
+ html = card_title
+ },
+ description = {
+ format = "html",
+ value = card_body
+ },
+ attributes = {{
+ label = "Powered by",
+ value = {
+ label = "GeoJS",
+ url = "https://geojs.io"
+ }
+ }},
+ icon = card_icons
+ }
+
+ ngx.say(json_encode({
+ message = txt_message,
+ card = card_message,
+ notify = "False",
+ message_format = "html",
+ }))
+ }
}
# Slack!
location = /v1/hooks/slack {
- default_type application/json;
- content_by_lua_block {
- -- Setup local vars
- local cjson = require "cjson"
- local split = require("geojs.utils").split
- local upstream_req = require("geojs.utils").upstream_req
- local getptr = require("geojs.utils").get_ptr
- local trim = require("geojs.utils").trim
- local cjson = require("cjson")
- local json_encode = cjson.encode
- local json_decode = cjson.decode
- local reqpath = '/v1/ip/geo.json'
- local ngx_var = ngx.var
-
- local slack_token = ngx_var.geojs_slack_token
- local message
- local slack_response_type
-
- -- Get our request body
- local get, post, files = require "resty.reqargs"()
-
- -- Verify our payload
- if post["token"] ~= slack_token then
- ngx.status = 403
- ngx.say(json_encode({
- error = "Verify token does not match expected value"
- }))
- ngx.exit(ngx.OK)
- end
-
- -- Get our slack request
- local slack_req = post['text']
-
- -- If people are just asking for help just reply with that
- if slack_req == "help" then
- resp_text = "Having some trouble? The GeoJS slack app can be used like so `/geojs 8.8.8.8`. Give it a try!\nIf you continue to have trouble reach out to us at contact@geojs.io"
- message = {
- text = resp_text
- }
- ngx.say(json_encode(message))
- else
-
- -- Get our IP address
- -- Slack gives it to us in a nice arg without the slash command
- local req_text = post['text']
-
- -- Check if we've got display set
- if string.match(req_text, "display") ~= nil then
- slack_response_type = "in_channel"
- req_ip = string.gsub(req_text, " display", "")
- -- Trim any extra whitespace we might have
- req_ip = trim(req_ip)
- else
- slack_response_type = "ephemeral"
- req_ip = req_text
- end
-
- -- Get our response URL that we use if we're taking to long
- local response_url = post['response_url']
-
- -- Get Geo data about this IP
- local req_resp = upstream_req(reqpath, req_ip)
- local req_ptr = getptr(req_ip)
- -- Turn our response into data
- local req_data = json_decode(req_resp)
-
- local resp = ''
- -- Build out response
- if req_ptr then
- resp = resp .. 'PTR: `' .. req_ptr .. '`'
- end
- if req_data['country'] then
- resp = resp .. '\nCountry: ' .. req_data['country']
- end
- if req_data['region'] then
- resp = resp .. '\nRegion: ' .. req_data['region']
- end
- if req_data['city'] then
- resp = resp .. '\nCity: ' .. req_data['city']
- end
- if req_data['organization'] then
- resp = resp .. '\nOrganization: ' .. req_data['organization']
- end
- local title = "IP Information for " .. req_ip
- message = {
- response_type = slack_response_type,
- attachments = {{
- mrkdwn_in = {
- "text",
- "pretext"
- },
- fallback = "", -- TODO: Actually put something here good lord
- pretext = title,
- text = resp,
- footer = "Powered by ",
- footer_icon = "https://static.jloh.co/geojs/avatar/hipchat/v1/geojs-icon_2x.png"
- }}
- }
- -- If we've taken longer than 3 seconds we have to send a POST request
- -- with the URL Slack gives us
- local request_time = ngx.now() - ngx.req.start_time()
- -- If we're right on 3 seconds or over send a POST
- if request_time > 2.9 then
- -- Reply to slack via POST
- local http = require "resty.http"
- local httpc = http.new()
- local res, err = httpc:request_uri(response_url, {
- method = "POST",
- body = json_encode(message),
- headers = {
- ["Content-Type"] = "application/json",
- }
- })
- if not res then
- ngx.log(ngx.ERR, "failed responding to slack webhook: ", err)
- return
- end
- else
- -- Just respond with all our datas
- ngx.say(json_encode(message))
- end
- end
- }
+ default_type application/json;
+ content_by_lua_block {
+ -- Setup local vars
+ local cjson = require "cjson"
+ local split = require("geojs.utils").split
+ local upstream_req = require("geojs.utils").upstream_req
+ local getptr = require("geojs.utils").get_ptr
+ local trim = require("geojs.utils").trim
+ local cjson = require("cjson")
+ local json_encode = cjson.encode
+ local json_decode = cjson.decode
+ local reqpath = '/v1/ip/geo.json'
+ local ngx_var = ngx.var
+
+ local slack_token = ngx_var.geojs_slack_token
+ local message
+ local slack_response_type
+
+ -- Get our request body
+ local get, post, files = require "resty.reqargs"()
+
+ -- Verify our payload
+ if post["token"] ~= slack_token then
+ ngx.status = 403
+ ngx.say(json_encode({
+ error = "Verify token does not match expected value"
+ }))
+ ngx.exit(ngx.OK)
+ end
+
+ -- Get our slack request
+ local slack_req = post['text']
+
+ -- If people are just asking for help just reply with that
+ if slack_req == "help" then
+ resp_text = "Having some trouble? The GeoJS slack app can be used like so `/geojs 8.8.8.8`. Give it a try!\nIf you continue to have trouble reach out to us at contact@geojs.io"
+ message = {
+ text = resp_text
+ }
+ ngx.say(json_encode(message))
+ else
+
+ -- Get our IP address
+ -- Slack gives it to us in a nice arg without the slash command
+ local req_text = post['text']
+
+ -- Check if we've got display set
+ if string.match(req_text, "display") ~= nil then
+ slack_response_type = "in_channel"
+ req_ip = string.gsub(req_text, " display", "")
+ -- Trim any extra whitespace we might have
+ req_ip = trim(req_ip)
+ else
+ slack_response_type = "ephemeral"
+ req_ip = req_text
+ end
+
+ -- Get our response URL that we use if we're taking to long
+ local response_url = post['response_url']
+
+ -- Get Geo data about this IP
+ local req_resp = upstream_req(reqpath, req_ip)
+ local req_ptr = getptr(req_ip)
+ -- Turn our response into data
+ local req_data = json_decode(req_resp)
+
+ local resp = ''
+ -- Build out response
+ if req_ptr then
+ resp = resp .. 'PTR: `' .. req_ptr .. '`'
+ end
+ if req_data['country'] then
+ resp = resp .. '\nCountry: ' .. req_data['country']
+ end
+ if req_data['region'] then
+ resp = resp .. '\nRegion: ' .. req_data['region']
+ end
+ if req_data['city'] then
+ resp = resp .. '\nCity: ' .. req_data['city']
+ end
+ if req_data['organization'] then
+ resp = resp .. '\nOrganization: ' .. req_data['organization']
+ end
+ local title = "IP Information for " .. req_ip
+ message = {
+ response_type = slack_response_type,
+ attachments = {{
+ mrkdwn_in = {
+ "text",
+ "pretext"
+ },
+ fallback = "", -- TODO: Actually put something here good lord
+ pretext = title,
+ text = resp,
+ footer = "Powered by ",
+ footer_icon = "https://static.jloh.co/geojs/avatar/hipchat/v1/geojs-icon_2x.png"
+ }}
+ }
+ -- If we've taken longer than 3 seconds we have to send a POST request
+ -- with the URL Slack gives us
+ local request_time = ngx.now() - ngx.req.start_time()
+ -- If we're right on 3 seconds or over send a POST
+ if request_time > 2.9 then
+ -- Reply to slack via POST
+ local http = require "resty.http"
+ local httpc = http.new()
+ local res, err = httpc:request_uri(response_url, {
+ method = "POST",
+ body = json_encode(message),
+ headers = {
+ ["Content-Type"] = "application/json",
+ }
+ })
+ if not res then
+ ngx.log(ngx.ERR, "failed responding to slack webhook: ", err)
+ return
+ end
+ else
+ -- Just respond with all our datas
+ ngx.say(json_encode(message))
+ end
+ end
+ }
}
location = /v1/hooks/slack/install {
- content_by_lua_block {
- -- Set our vars
- local ngx_redirect = ngx.redirect
- local ngx_var = ngx.var
- local http = require "resty.http"
- local httpc = http.new()
-
- -- Slack secrets
- local slack_client_id = ngx_var.geojs_slack_client_id
- local slack_client_secret = ngx_var.geojs_slack_client_secret
-
- -- Get our request body
- local get, post, files = require "resty.reqargs"()
-
- -- Construct our URI
- local response_url = "https://slack.com/api/oauth.access"
-
- -- Add slack code we got
- response = "code=" .. get["code"]
-
- -- Add in client ID/Secret
- response = response .. "&client_id=" .. slack_client_id
- response = response .. "&client_secret=" .. slack_client_secret
-
- -- Try and auth our request
- local res, err = httpc:request_uri(response_url, {
- method = "GET",
- query = response,
- headers = {
- ["Content-Type"] = "application/x-www-form-urlencoded",
- }
- })
- if not res then
- ngx.say("Hmmm. Something went wrong, but we're looking at it promise!")
- return
- else
- ngx_redirect("https://geojs.io/docs/chatops/slack/")
- end
- }
+ content_by_lua_block {
+ -- Set our vars
+ local ngx_redirect = ngx.redirect
+ local ngx_var = ngx.var
+ local http = require "resty.http"
+ local httpc = http.new()
+
+ -- Slack secrets
+ local slack_client_id = ngx_var.geojs_slack_client_id
+ local slack_client_secret = ngx_var.geojs_slack_client_secret
+
+ -- Get our request body
+ local get, post, files = require "resty.reqargs"()
+
+ -- Construct our URI
+ local response_url = "https://slack.com/api/oauth.access"
+
+ -- Add slack code we got
+ response = "code=" .. get["code"]
+
+ -- Add in client ID/Secret
+ response = response .. "&client_id=" .. slack_client_id
+ response = response .. "&client_secret=" .. slack_client_secret
+
+ -- Try and auth our request
+ local res, err = httpc:request_uri(response_url, {
+ method = "GET",
+ query = response,
+ headers = {
+ ["Content-Type"] = "application/x-www-form-urlencoded",
+ }
+ })
+ if not res then
+ ngx.say("Hmmm. Something went wrong, but we're looking at it promise!")
+ return
+ else
+ ngx_redirect("https://geojs.io/docs/chatops/slack/")
+ end
+ }
}
location = /v1/hooks/slack/install_direct {
- content_by_lua_block {
- -- Commands and junk
- local ngx_redirect = ngx.redirect
- local ngx_var = ngx.var
+ content_by_lua_block {
+ -- Commands and junk
+ local ngx_redirect = ngx.redirect
+ local ngx_var = ngx.var
- -- Set our vars!
- local slack_client_id = ngx_var.geojs_slack_client_id
- local slack_scope = "commands"
- local slack_domain = "https://slack.com/oauth/authorize"
+ -- Set our vars!
+ local slack_client_id = ngx_var.geojs_slack_client_id
+ local slack_scope = "commands"
+ local slack_domain = "https://slack.com/oauth/authorize"
- -- Slack redirect
- local redirect = slack_domain .. "?client_id=" .. slack_client_id .. "&scope=" .. slack_scope
+ -- Slack redirect
+ local redirect = slack_domain .. "?client_id=" .. slack_client_id .. "&scope=" .. slack_scope
- ngx_redirect(redirect)
- }
+ ngx_redirect(redirect)
+ }
}
# Twist app!
location = /v1/hooks/twistapp {
- default_type application/json;
- content_by_lua_block {
- -- Setup local vars
- local ngx_var = ngx.var
- local cjson = require "cjson"
- local split = require("geojs.utils").split
- local upstream_req = require("geojs.utils").upstream_req
- local getptr = require("geojs.utils").get_ptr
- local trim = require("geojs.utils").trim
- local cjson = require("cjson")
- local json_encode = cjson.encode
- local reqpath = '/v1/ip/geo.json'
- local message
-
- -- Define our verification token
- local verification_token = ngx_var.geojs_twistapp_token
-
- -- Get our request body
- local get, post, files = require "resty.reqargs"()
-
- -- Verify our payload
- if post["verify_token"] ~= verification_token then
- ngx.status = 403
- ngx.say("Verify token does not match expected value")
- ngx.exit(ngx.OK)
- end
-
- -- If we just recieved a ping event, respond with pong
- -- https://developer.twistapp.com/v2/#payload-when-a-ping-happens
- if post["event_type"] == "ping" then
- message = {
- content = "pong"
- }
- else
- -- Get our IP address
- -- Twist gives it to us in a nice arg without the slash command
- local req_ip = post['command_argument']
-
- -- Get Geo data about this IP
- local req_resp = upstream_req(reqpath, req_ip)
- local req_ptr = getptr(req_ip)
- -- Turn our response into data
- local req_data = cjson.decode(req_resp)
-
- -- Build out response
- local resp = '### IP information for **' .. req_ip .. '**'
- if req_ptr then
- resp = resp .. '\nPTR: `' .. req_ptr .. '`'
- end
- if req_data['country'] then
- resp = resp .. '\nCountry: ' .. req_data['country']
- end
- if req_data['region'] then
- resp = resp .. '\nRegion: ' .. req_data['region']
- end
- if req_data['city'] then
- resp = resp .. '\nCity: ' .. req_data['city']
- end
- if req_data['organization'] then
- resp = resp .. '\nOrganization: ' .. req_data['organization']
- end
- resp = resp .. '\nPowered by [GeoJS](https://geojs.io)'
- message = {
- content = resp
- }
- end
-
- -- Respond with all our datas
- ngx.say(json_encode(message))
- }
+ default_type application/json;
+ content_by_lua_block {
+ -- Setup local vars
+ local ngx_var = ngx.var
+ local cjson = require "cjson"
+ local split = require("geojs.utils").split
+ local upstream_req = require("geojs.utils").upstream_req
+ local getptr = require("geojs.utils").get_ptr
+ local trim = require("geojs.utils").trim
+ local cjson = require("cjson")
+ local json_encode = cjson.encode
+ local reqpath = '/v1/ip/geo.json'
+ local message
+
+ -- Define our verification token
+ local verification_token = ngx_var.geojs_twistapp_token
+
+ -- Get our request body
+ local get, post, files = require "resty.reqargs"()
+
+ -- Verify our payload
+ if post["verify_token"] ~= verification_token then
+ ngx.status = 403
+ ngx.say("Verify token does not match expected value")
+ ngx.exit(ngx.OK)
+ end
+
+ -- If we just recieved a ping event, respond with pong
+ -- https://developer.twistapp.com/v2/#payload-when-a-ping-happens
+ if post["event_type"] == "ping" then
+ message = {
+ content = "pong"
+ }
+ else
+ -- Get our IP address
+ -- Twist gives it to us in a nice arg without the slash command
+ local req_ip = post['command_argument']
+
+ -- Get Geo data about this IP
+ local req_resp = upstream_req(reqpath, req_ip)
+ local req_ptr = getptr(req_ip)
+ -- Turn our response into data
+ local req_data = cjson.decode(req_resp)
+
+ -- Build out response
+ local resp = '### IP information for **' .. req_ip .. '**'
+ if req_ptr then
+ resp = resp .. '\nPTR: `' .. req_ptr .. '`'
+ end
+ if req_data['country'] then
+ resp = resp .. '\nCountry: ' .. req_data['country']
+ end
+ if req_data['region'] then
+ resp = resp .. '\nRegion: ' .. req_data['region']
+ end
+ if req_data['city'] then
+ resp = resp .. '\nCity: ' .. req_data['city']
+ end
+ if req_data['organization'] then
+ resp = resp .. '\nOrganization: ' .. req_data['organization']
+ end
+ resp = resp .. '\nPowered by [GeoJS](https://geojs.io)'
+ message = {
+ content = resp
+ }
+ end
+
+ -- Respond with all our datas
+ ngx.say(json_encode(message))
+ }
}
# TODO: actually something with this
location = /v1/hooks/twistapp/installed {
- return 301 https://geojs.io/docs/chatops/;
+ return 301 https://geojs.io/docs/chatops/;
}
diff --git a/t/v1/03-hooks.t b/t/v1/hooks/01-sanity.t
similarity index 100%
rename from t/v1/03-hooks.t
rename to t/v1/hooks/01-sanity.t
diff --git a/t/v1/hooks/02-hipchat.t b/t/v1/hooks/02-hipchat.t
new file mode 100644
index 0000000..d91e4af
--- /dev/null
+++ b/t/v1/hooks/02-hipchat.t
@@ -0,0 +1,90 @@
+use Test::Nginx::Socket 'no_plan';
+use Cwd qw(cwd);
+
+my $pwd = cwd();
+
+$ENV{TEST_COVERAGE} ||= 0;
+
+our $HttpConfig = qq{
+ init_by_lua_block {
+ if $ENV{TEST_COVERAGE} == 1 then
+ require("luacov.runner").init()
+ end
+ }
+ geoip_country "$pwd/download-cache/maxmind/GeoIPv6.dat";
+ geoip_city "$pwd/download-cache/maxmind/GeoLiteCityv6.dat";
+ geoip_org "$pwd/download-cache/maxmind/GeoIPASNumv6.dat";
+ lua_package_path "$pwd/lib/?.lua;$pwd/repos/lua-resty-dns/lib/?.lua;$pwd/repos/lua-resty-http/lib/?.lua;$pwd/repos/lua-resty-iconv/lualib/?.lua;$pwd/repos/lua-resty-reqargs/lib/?.lua;$pwd/repos/lua-resty-upload/lib/?.lua;;";
+ real_ip_header X-Real-IP;
+ set_real_ip_from 127.0.0.1/32;
+};
+
+our $UpstreamConfig = qq{
+ server {
+ listen 8080;
+ include $pwd/conf/v1/ip.conf;
+ set \$geojs_dns_server '8.8.8.8';
+ }
+};
+
+our $JSONPayload = qq{
+ {
+ "event": "room_message",
+ "item": {
+ "message": {
+ "date": "2018-03-01T00:49:10.541852+00:00",
+ "from": {
+ "id": 159,
+ "links": {
+ "self": "https:\/\/hipchat.example.com\/v2\/user\/123"
+ },
+ "mention_name": "test",
+ "name": "John Doe",
+ "version": "123456"
+ },
+ "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
+ "mentions": [
+ ],
+ "message": "/geojs 8.8.8.8",
+ "type": "message"
+ },
+ "room": {
+ "id": 10,
+ "is_archived": false,
+ "links": {
+ "members": "https:\/\/hipchat.example.com\/v2\/room\/10\/member",
+ "participants": "https:\/\/hipchat.example.com\/v2\/room\/10\/participant",
+ "self": "https:\/\/hipchat.example.com\/v2\/room\/10",
+ "webhooks": "https:\/\/hipchat.example.com\/v2\/room\/10\/webhook"
+ },
+ "name": "Webhook Lounge",
+ "privacy": "private",
+ "version": "CB296BBB"
+ }
+ },
+ "oauth_client_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
+ "webhook_id": 2
+ }
+};
+
+run_tests();
+
+__DATA__
+=== TEST 1: Webhook
+--- http_config eval
+"$::HttpConfig
+$::UpstreamConfig"
+--- config
+ include "../../../conf/v1/hooks.conf";
+ set $geojs_dns_server '8.8.8.8';
+--- more_headers
+Content-type: application/json
+--- request eval
+"POST /v1/hooks/hipchat
+$::JSONPayload"
+--- no_error_log
+[error]
+--- no_error_log
+[error]
+--- response_body
+{"notify":"False","message_format":"html","message":"Results for 8.8.8.8<\/b>
PTR: google-public-dns-a.google.com
Country: United States
Organization: AS15169 Google LLC
Powered by GeoJS<\/a>","card":{"icon":{"url":"https:\/\/static.jloh.co\/geojs\/flags\/v1\/us.png","url@2x":"https:\/\/static.jloh.co\/geojs\/flags\/v1\/2x\/us.png"},"format":"medium","title":"GeoIP results for 8.8.8.8","id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","attributes":[{"label":"Powered by","value":{"label":"GeoJS","url":"https:\/\/geojs.io"}}],"activity":{"html":"8.8.8.8<\/strong> is a United States IP belonging to AS15169 Google LLC"},"style":"application","description":{"value":"PTR:<\/strong> google-public-dns-a.google.com","format":"html"}}}
diff --git a/t/v1/hooks/03-slack.t b/t/v1/hooks/03-slack.t
new file mode 100644
index 0000000..e8c9bc6
--- /dev/null
+++ b/t/v1/hooks/03-slack.t
@@ -0,0 +1,66 @@
+use Test::Nginx::Socket 'no_plan';
+use Cwd qw(cwd);
+
+my $pwd = cwd();
+
+$ENV{TEST_COVERAGE} ||= 0;
+
+our $HttpConfig = qq{
+ init_by_lua_block {
+ if $ENV{TEST_COVERAGE} == 1 then
+ require("luacov.runner").init()
+ end
+ }
+ geoip_country "$pwd/download-cache/maxmind/GeoIPv6.dat";
+ geoip_city "$pwd/download-cache/maxmind/GeoLiteCityv6.dat";
+ geoip_org "$pwd/download-cache/maxmind/GeoIPASNumv6.dat";
+ lua_package_path "$pwd/lib/?.lua;$pwd/repos/lua-resty-dns/lib/?.lua;$pwd/repos/lua-resty-http/lib/?.lua;$pwd/repos/lua-resty-iconv/lualib/?.lua;$pwd/repos/lua-resty-reqargs/lib/?.lua;$pwd/repos/lua-resty-upload/lib/?.lua;;";
+ real_ip_header X-Real-IP;
+ set_real_ip_from 127.0.0.1/32;
+};
+
+our $UpstreamConfig = qq{
+ server {
+ listen 8080;
+ include $pwd/conf/v1/ip.conf;
+ }
+};
+
+run_tests();
+
+__DATA__
+=== TEST 1: Webhook
+--- http_config eval
+"$::HttpConfig
+$::UpstreamConfig"
+--- config
+ include "../../../conf/v1/hooks.conf";
+ set $geojs_slack_token '1234';
+ set $geojs_dns_server '8.8.8.8';
+--- more_headers
+Content-type: application/x-www-form-urlencoded
+--- request eval
+"POST /v1/hooks/slack
+".CORE::join('&',
+'token=1234',
+'text=8.8.8.8 display',
+'command=geojs')
+--- no_error_log
+[error]
+
+=== TEST 2: Bad token
+--- http_config eval
+"$::HttpConfig"
+--- config
+ include "../../../conf/v1/hooks.conf";
+ set $geojs_slack_token '1234';
+ set $geojs_dns_server '8.8.8.8';
+--- more_headers
+Content-type: application/x-www-form-urlencoded
+--- request eval
+"POST /v1/hooks/slack
+".CORE::join("&",
+"token=12",
+"text=8.8.8.8 display",
+"command=geojs")
+--- error_code: 403
diff --git a/t/v1/hooks/04-twist.t b/t/v1/hooks/04-twist.t
new file mode 100644
index 0000000..a29fc36
--- /dev/null
+++ b/t/v1/hooks/04-twist.t
@@ -0,0 +1,94 @@
+use Test::Nginx::Socket 'no_plan';
+use Cwd qw(cwd);
+
+my $pwd = cwd();
+
+$ENV{TEST_COVERAGE} ||= 0;
+
+our $HttpConfig = qq{
+ init_by_lua_block {
+ if $ENV{TEST_COVERAGE} == 1 then
+ require("luacov.runner").init()
+ end
+ }
+ geoip_country "$pwd/download-cache/maxmind/GeoIPv6.dat";
+ geoip_city "$pwd/download-cache/maxmind/GeoLiteCityv6.dat";
+ geoip_org "$pwd/download-cache/maxmind/GeoIPASNumv6.dat";
+ lua_package_path "$pwd/lib/?.lua;$pwd/repos/lua-resty-dns/lib/?.lua;$pwd/repos/lua-resty-http/lib/?.lua;$pwd/repos/lua-resty-iconv/lualib/?.lua;$pwd/repos/lua-resty-reqargs/lib/?.lua;$pwd/repos/lua-resty-upload/lib/?.lua;;";
+ real_ip_header X-Real-IP;
+ set_real_ip_from 127.0.0.1/32;
+};
+
+our $UpstreamConfig = qq{
+ server {
+ listen 8080;
+ include $pwd/conf/v1/ip.conf;
+ }
+};
+
+run_tests();
+
+__DATA__
+=== TEST 1: Webhook
+--- http_config eval
+"$::HttpConfig
+$::UpstreamConfig"
+--- config
+ include "../../../conf/v1/hooks.conf";
+ set $geojs_twistapp_token '1234';
+ set $geojs_dns_server '8.8.8.8';
+--- more_headers
+Content-type: application/x-www-form-urlencoded
+--- request eval
+"POST /v1/hooks/twistapp
+".CORE::join('&',
+'verify_token=1234',
+'command_argument=8.8.8.8',
+'command=geojs')
+--- no_error_log
+[error]
+--- response_headers
+Content-Type: application/json
+--- response_body
+{"content":"### IP information for **8.8.8.8**\nPTR: `google-public-dns-a.google.com`\nCountry: United States\nOrganization: AS15169 Google LLC\nPowered by [GeoJS](https:\/\/geojs.io)"}
+
+=== TEST 2: Bad token
+--- http_config eval
+"$::HttpConfig"
+--- config
+ include "../../../conf/v1/hooks.conf";
+ set $geojs_twistapp_token '1234';
+ set $geojs_dns_server '8.8.8.8';
+--- more_headers
+Content-type: application/x-www-form-urlencoded
+--- request eval
+"POST /v1/hooks/twistapp
+".CORE::join("&",
+"verify_token=12",
+"command_argument=8.8.8.8",
+"command=geojs")
+--- error_code: 403
+--- response_headers
+Content-Type: application/json
+
+
+=== TEST 3: Correct response to Ping request
+--- http_config eval
+"$::HttpConfig"
+--- config
+ include "../../../conf/v1/hooks.conf";
+ set $geojs_twistapp_token '1234';
+ set $geojs_dns_server '8.8.8.8';
+--- more_headers
+Content-type: application/x-www-form-urlencoded
+--- request eval
+"POST /v1/hooks/twistapp
+".CORE::join("&",
+"verify_token=1234",
+"event_type=ping")
+--- no_error_log
+[error]
+--- response_headers
+Content-Type: application/json
+--- response_body
+{"content":"pong"}