Skip to content

Commit

Permalink
updated script to use with backend-service
Browse files Browse the repository at this point in the history
  • Loading branch information
superstes committed Jul 15, 2024
1 parent af8dac3 commit ae86567
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 65 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Download the binary for you system from [the releases](https://github.com/supers

[Read the documentation](https://github.com/superstes/geoip-lookup-service) on how to use it.

You need to use the lua/geoip_lookup_w_go_backend.lua script.
You need to use the `lua/geoip_lookup_w_backend.lua` script.

It is recommended to start Go-Backend with `-plain` command line argument to get the variable in plain text format.

Expand Down
58 changes: 46 additions & 12 deletions lua/geoip_lookup_w_backend.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
-- NOTE: the ltrim parameter can be used to remove a prefix - like: 'AS1337' => '1337'

local function http_request(lookup, src)
local function http_request(lookup, filter, src, ltrim)
local s = core.tcp()
s:connect('127.0.0.1:6970')
s:send('GET /?lookup=' .. lookup .. '&ip=' .. src .. ' HTTP/1.1\r\n\r\n')

local addr = '127.0.0.1'
local port = 6970

local hdrs = {
[1] = string.format('host: %s:%s', addr, port),
[2] = 'accept: */*',
[3] = 'connection: close'
}

local req = {
[1] = 'GET /?lookup=' .. lookup .. '&ip=' .. src .. '&filter=' .. filter .. ' HTTP/1.1',
[2] = table.concat(hdrs, '\r\n'),
[3] = '\r\n'
}

req = table.concat(req, '\r\n')

s:connect(addr, port)
s:send(req)
while true do
local line = s:receive('*l')
if not line then break end
Expand All @@ -12,30 +31,45 @@ local function http_request(lookup, src)
if res_body == nil then
return '00'
end
return string.sub(res_body, 1, -2)
return string.sub(res_body, 1 + ltrim, -2)
end

-- examples for maxmind:

local function lookup_geoip_country(txn)
country_code = http_request('country', txn.f:src())
txn:set_var('txn.geoip_country', country_code)
country_code = http_request('country', 'country.iso_code', txn.f:src(), 0)
txn:set_var('txn.geoip_country', country_code)
end

local function lookup_geoip_continent(txn)
continent_code = http_request('continent', txn.f:src())
txn:set_var('txn.geoip_continent', continent_code)
local function lookup_geoip_asn(txn)
asn = http_request('asn', 'autonomous_system_number', txn.f:src(), 0)
txn:set_var('txn.geoip_asn', asn)
end

local function lookup_geoip_asname(txn)
asname = http_request('asn', 'autonomous_system_organization', txn.f:src(), 0)
txn:set_var('txn.geoip_asname', asname)
end

-- examples for IPInfo:

local function lookup_geoip_country(txn)
country_code = http_request('country', 'country', txn.f:src(), 0)
txn:set_var('txn.geoip_country', country_code)
end

local function lookup_geoip_asn(txn)
asn = http_request('asn', txn.f:src())
asn = http_request('asn', 'asn', txn.f:src(), 2)
txn:set_var('txn.geoip_asn', asn)
end

local function lookup_geoip_asname(txn)
asname = http_request('asname', txn.f:src())
asname = http_request('asn', 'name', txn.f:src(), 0)
txn:set_var('txn.geoip_asname', asname)
end

-- examples end

core.register_action('lookup_geoip_country', {'tcp-req', 'http-req'}, lookup_geoip_country, 0)
core.register_action('lookup_geoip_continent', {'tcp-req', 'http-req'}, lookup_geoip_continent, 0)
core.register_action('lookup_geoip_asn', {'tcp-req', 'http-req'}, lookup_geoip_asn, 0)
core.register_action('lookup_geoip_asname', {'tcp-req', 'http-req'}, lookup_geoip_asname, 0)
52 changes: 0 additions & 52 deletions lua/geoip_lookup_w_go_backend.lua

This file was deleted.

0 comments on commit ae86567

Please sign in to comment.