Jump to content

Module:Sensitive IP addresses/API

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 03:46, 26 July 2016 (first go at defining the API). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This module provides functions for handling sensitive IP addresses.

-- Load modules
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType

local sensitivityReasons = {
	political = true,
	technical = true,
}

-- Plan of attack:
-- * Load the data from Module:Sensitive IP addresses/list via a formatting module
--   at Module:Sensitive IP addresses/data
--   * This module will do preprocessing that must be done for every query
-- * Make an API so that other modules can query this module for data about
--   sensitive IPs and ranges.
--   * Export query results as both a Lua table and as JSON
-- * Use this API to create a table to be used in
--   [[Template:Sensitive IP addresses]].

-------------------------------------------------------------------------------
-- Sensitive IP API
-------------------------------------------------------------------------------

-- This API is used by external tools and gadgets, so it should be kept
-- backwards-compatible. Clients query the API with a query table, and the
-- API returns a response table. The response table is available as a Lua table
-- for other Lua modules, and as JSON for external clients.

-- Example query tables:
--
-- Query IP addresses and ranges:
-- {
-- 	ips = {'1.2.3.4', '2001:db8::ff00:12:3456'},
-- 	ranges = {'4.5.6.0/24', '2001:db8::ff00:12:0/112'}
-- }
--
-- Query specific entities:
-- {
-- 	entities = {'ussenate', 'ushr'}
-- }
--
-- Query all entities:
-- {
-- 	entities = {'all'}
-- }
--
-- Combined query:
-- {
-- 	ips = {'1.2.3.4', '2001:db8::ff00:12:3456'},
-- 	ranges = {'4.5.6.0/24', '2001:db8::ff00:12:0/112'},
-- 	entities = {'ussenate', 'ushr'}
-- }

-- Example response:
--
-- {
--     ips = {
--         {
--             ip = '1.2.3.4',
--             ['ip-version'] = 'IPv4',
--             ['matches-range'] = '1.2.3.0/24',
--             ['entity-id'] = 'entityid'
--         }
--     },
--     ranges = {
--         {
--             range = '4.5.6.0/24',
--             ['ip-version'] = 'IPv4',
--             ['matches-range'] = '4.5.0.0/16',
--             ['entity-id'] = 'entityid'
--         }
--     },
--     ['matched-ranges'] = {
--         {
--             range = '1.2.3.0/24',
--             ['ip-version'] = 'IPv4',
--             ['entity-id'] = 'entityid'
--         },
--         {
--             range = '4.5.0.0/16',
--             ['ip-version'] = 'IPv4',
--             ['entity-id'] = 'entityid'
--         }
--     },
--     entities = {
--         {
--             id = 'entityid',
--             name = 'The entity name',
--             description = 'A description of the entity',
--             ['ipv4-ranges'] = {
--                 '1.2.3.0/24',
--                 '4.5.0.0/16'
--                 '6.7.0.0/16'
--             },
--             ['ipv6-ranges'] = {
--                 '2001:db8::ff00:12:0/112'
--             },
--             notes = 'Notes about the entity or its ranges'
--         }
--     }
-- }
		

--------------------------------------------------------------------------------
-- Exports
--------------------------------------------------------------------------------

local p = {}

function p.isValidSensitivityReason(s)
	checkType('isValidSensitivityReason', 1, s, 'string')
	return sensitivityReasons[s] ~= nil
end

function p.getSensitivityReasons()
	local ret = {}
	for reason in pairs(sensitivityReasons) do
		ret[#ret + 1] = reason
	end
	table.sort(ret)
	return ret
end

function p.query()
end

return p