comparison mod_net_proxy/mod_net_proxy.lua @ 2931:e79b9a55aa2e

mod_net_proxy: Fixed backwards compatibility to Prosody 0.10
author Pascal Mathis <mail@pascalmathis.com>
date Thu, 15 Mar 2018 21:39:46 +0100
parents 9a62780e7ee2
children 7319fd5dbc89
comparison
equal deleted inserted replaced
2930:9a62780e7ee2 2931:e79b9a55aa2e
11 local bit = assert(softreq "bit" or softreq "bit32", "No bit module found. See https://prosody.im/doc/depends#bitop"); 11 local bit = assert(softreq "bit" or softreq "bit32", "No bit module found. See https://prosody.im/doc/depends#bitop");
12 local hex = require "util.hex"; 12 local hex = require "util.hex";
13 local ip = require "util.ip"; 13 local ip = require "util.ip";
14 local net = require "util.net"; 14 local net = require "util.net";
15 local portmanager = require "core.portmanager"; 15 local portmanager = require "core.portmanager";
16
17 -- Backwards Compatibility
18 local function net_ntop_bc(input)
19 if input:len() == 4 then
20 return string.format("%d.%d.%d.%d", input:byte(1, 4));
21 elseif input:len() == 16 then
22 local octets = { nil, nil, nil, nil, nil, nil, nil, nil };
23
24 -- Convert received bytes into IPv6 address and skip leading zeroes for each group
25 for index = 1, 8 do
26 high, low = input:byte(index * 2 - 1, index * 2);
27 octets[index] = string.format("%x", high * 256 + low);
28 end
29 local address = table.concat(octets, ":", 1, 8);
30
31 -- Search for the longest sequence of zeroes
32 local token;
33 local length = (address:match("^0:[0:]+()") or 1) - 1;
34 for s in address:gmatch(":0:[0:]+") do
35 if length < #s then
36 length, token = #s, s;
37 end
38 end
39
40 -- Return the shortened IPv6 address
41 return address:gsub(token or "^0:[0:]+", "::", 1);
42 end
43 end
44
45 local net_ntop = net.ntop or net_ntop_bc
16 46
17 -- Utility Functions 47 -- Utility Functions
18 local function _table_invert(input) 48 local function _table_invert(input)
19 local output = {}; 49 local output = {};
20 for key, value in pairs(input) do 50 for key, value in pairs(input) do
195 return PROTO_HANDLER_STATUS.FAILURE, nil; 225 return PROTO_HANDLER_STATUS.FAILURE, nil;
196 end 226 end
197 227
198 -- Parse source and destination addresses 228 -- Parse source and destination addresses
199 if addr_family == ADDR_FAMILY.INET then 229 if addr_family == ADDR_FAMILY.INET then
200 src_addr = net.ntop(payload:sub(offset, offset + 3)); offset = offset + 4; 230 src_addr = net_ntop(payload:sub(offset, offset + 3)); offset = offset + 4;
201 dst_addr = net.ntop(payload:sub(offset, offset + 3)); offset = offset + 4; 231 dst_addr = net_ntop(payload:sub(offset, offset + 3)); offset = offset + 4;
202 elseif addr_family == ADDR_FAMILY.INET6 then 232 elseif addr_family == ADDR_FAMILY.INET6 then
203 src_addr = net.ntop(payload:sub(offset, offset + 15)); offset = offset + 16; 233 src_addr = net_ntop(payload:sub(offset, offset + 15)); offset = offset + 16;
204 dst_addr = net.ntop(payload:sub(offset, offset + 15)); offset = offset + 16; 234 dst_addr = net_ntop(payload:sub(offset, offset + 15)); offset = offset + 16;
205 elseif addr_family == ADDR_FAMILY.UNIX then 235 elseif addr_family == ADDR_FAMILY.UNIX then
206 src_addr = payload:sub(offset, offset + 107); offset = offset + 108; 236 src_addr = payload:sub(offset, offset + 107); offset = offset + 108;
207 dst_addr = payload:sub(offset, offset + 107); offset = offset + 108; 237 dst_addr = payload:sub(offset, offset + 107); offset = offset + 108;
208 end 238 end
209 239