mod_proxy65/mod_proxy65.lua
author Matthew Wild <mwild1@gmail.com>
Sat, 31 Oct 2009 00:41:47 +0000
changeset 68 0df3e4d1f1a3
parent 66 b86ae5e21a56
child 69 87dfd34dceb2
permissions -rw-r--r--
mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     1
-- Copyright (C) 2009 Thilo Cestonaro
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     2
-- 
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     3
-- This project is MIT/X11 licensed. Please see the
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     4
-- COPYING file in the source package for more information.
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     5
--
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     6
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     7
if module:get_host_type() ~= "component" then
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     8
	error("proxy65 should be loaded as a component, please see http://prosody.im/doc/components", 0);
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     9
end
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    10
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    11
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    12
local jid_split = require "util.jid".split;
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    13
local st = require "util.stanza";
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    14
local component_register = require "core.componentmanager".register_component;
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    15
local component_deregister = require "core.componentmanager".deregister_component;
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    16
local configmanager = require "core.configmanager";
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    17
local config_get = require "core.configmanager".get;
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    18
local connlisteners_register = require "net.connlisteners".register;
65
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    19
local connlisteners_start = require "net.connlisteners".start;
64
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    20
local connlisteners_deregister = require "net.connlisteners".deregister;
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    21
local adns, dns = require "net.adns", require "net.dns";
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    22
local add_task = require "util.timer".add_task;
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    23
local max_dns_depth = config.get("*", "core", "dns_max_depth") or 3;
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    24
local dns_timeout = config.get("*", "core", "dns_timeout") or 60;
65
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    25
local sha1 = require "util.hashes".sha1;
64
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    26
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    27
local replies_cache = {};
68
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
    28
local host = module:get_host();
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
    29
local name = "SOCKS5 Bytestreams Service";
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
    30
local sessions, transfers, component = {}, {}, nil;
64
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    31
68
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
    32
local proxy_port = config_get(host, "core", "proxy65_port") or 5000;
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
    33
local proxy_interface = config_get(host, "core", "proxy65_interface") or "*";
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
    34
local proxy_address = config_get(host, "core", "proxy65_address") or (proxy_interface ~= "*" and proxy_interface) or module.host;
65
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    35
68
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
    36
local connlistener = {
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
    37
	registered = false, default_port = proxy_port, 
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
    38
	default_interface = proxy_interface, default_mode = "*a"
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
    39
	};
64
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    40
65
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    41
local function bin2hex(bin)
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    42
	return bin:gsub(".", function (c) return ("%02x"):format(c:byte()); end)
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    43
end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    44
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    45
function new_session(conn)
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    46
	local w = function(s) conn.write(s:gsub("\n", "\r\n")); end;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    47
	local session = { conn = conn;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    48
			send = function (t) w(tostring(t)); end;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    49
			disconnect = function () conn.close(); end;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    50
			};
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    51
	return session;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    52
end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    53
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    54
function connlistener.listener(conn, data)
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    55
	local session = sessions[conn];
66
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    56
	
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    57
	if session == nil and data ~= nil and data:sub(1):byte() == 0x05 and data:len() > 2 then
65
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    58
		local nmethods = data:sub(2):byte();
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    59
		local methods = data:sub(3);
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    60
		local supported = false;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    61
		for i=1, nmethods, 1 do
66
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    62
			if(methods:sub(i):byte() == 0x00) then -- 0x00 == method: NO AUTH
65
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    63
				supported = true;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    64
				break;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    65
			end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    66
		end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    67
		if(supported) then
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    68
			module:log("debug", "new session found ... ")
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    69
			session = new_session(conn);
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    70
			sessions[conn] = session;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    71
			session.send(string.char(5, 0));
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    72
		end
66
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    73
		return;
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    74
	end
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    75
	if session ~= nil then
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    76
		if session.sha ~= nil and transfers[session.sha] ~= nil then
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    77
			local sha = session.sha;
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    78
			if transfers[sha].activated == true and transfers[sha].initiator == conn and transfers[sha].target ~= nil then
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    79
				transfers[sha].target.write(data);
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    80
				return;
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    81
			end
65
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
    82
		end
66
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    83
		if data ~= nil and data:len() == 0x2F and  -- 40 == length of SHA1 HASH, and 7 other bytes => 47 => 0x2F
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    84
			data:sub(1):byte() == 0x05 and -- SOCKS5 has 5 in first byte
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    85
			data:sub(2):byte() == 0x01 and -- CMD must be 1
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    86
			data:sub(3):byte() == 0x00 and -- RSV must be 0
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    87
			data:sub(4):byte() == 0x03 and -- ATYP must be 3
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    88
			data:sub(5):byte() == 40 and -- SHA1 HASH length must be 64 (0x40)
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    89
			data:sub(-2):byte() == 0x00 and -- PORT must be 0, size 2 byte
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    90
			data:sub(-1):byte() == 0x00 		
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    91
		then
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    92
			local sha = data:sub(6, 45); -- second param is not count! it's the ending index (included!)
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    93
			if transfers[sha] == nil then
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    94
				transfers[sha] = {};
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    95
				transfers[sha].activated = false;
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    96
				transfers[sha].target = conn;
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    97
				session.sha = sha;
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    98
				module:log("debug", "target connected ... ");
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
    99
			elseif transfers[sha].target ~= nil then
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
   100
				transfers[sha].initiator = conn;
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
   101
				session.sha = sha;
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
   102
				module:log("debug", "initiator connected ... ");
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
   103
			end
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
   104
			session.send(string.char(5, 0, 0, 3, sha:len()) .. sha .. string.char(0, 0)); -- VER, REP, RSV, ATYP, BND.ADDR (sha), BND.PORT (2 Byte)
b86ae5e21a56 mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents: 65
diff changeset
   105
		end
65
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   106
	end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   107
end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   108
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   109
function connlistener.disconnect(conn, err)
68
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   110
	if sessions[conn] then
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   111
		-- Clean up any session-related stuff here
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   112
		sessions[conn] = nil;
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   113
	end
65
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   114
end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   115
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   116
local function get_disco_info(stanza)
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   117
	local reply = replies_cache.disco_info;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   118
	if reply == nil then
68
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   119
	 	reply = st.iq({type='result', from=host}):query("http://jabber.org/protocol/disco#info")
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   120
			:tag("identity", {category='proxy', type='bytestreams', name=name}):up()
65
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   121
			:tag("feature", {var="http://jabber.org/protocol/bytestreams"});
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   122
		replies_cache.disco_info = reply;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   123
	end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   124
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   125
	reply.attr.id = stanza.attr.id;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   126
	reply.attr.to = stanza.attr.from;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   127
	return reply;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   128
end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   129
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   130
local function get_disco_items(stanza)
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   131
	local reply = replies_cache.disco_items;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   132
	if reply == nil then
68
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   133
	 	reply = st.iq({type='result', from=host}):query("http://jabber.org/protocol/disco#items");
65
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   134
		replies_cache.disco_info = reply;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   135
	end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   136
	
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   137
	reply.attr.id = stanza.attr.id;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   138
	reply.attr.to = stanza.attr.from;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   139
	return reply;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   140
end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   141
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   142
local function get_stream_host(stanza)
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   143
	local reply = replies_cache.stream_host;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   144
	local sid = stanza.tags[1].attr.sid;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   145
	if reply == nil then
68
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   146
		reply = st.iq({type="result", from=host})
65
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   147
			:query("http://jabber.org/protocol/bytestreams")
68
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   148
			:tag("streamhost", {jid=host, host=proxy_address, port=proxy_port}); -- TODO get the correct data
65
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   149
		replies_cache.stream_host = reply;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   150
	end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   151
	
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   152
	reply.attr.id = stanza.attr.id;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   153
	reply.attr.to = stanza.attr.from;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   154
	reply.tags[1].attr.sid = sid;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   155
	return reply;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   156
end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   157
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   158
module.unload = function()
68
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   159
	component_deregister(host);
65
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   160
	connlisteners_deregister("proxy65");
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   161
end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   162
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   163
local function set_activation(stanza)
68
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   164
	local from, to, sid, reply = nil;
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   165
	from = stanza.attr.from;
65
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   166
	if stanza.tags[1] ~= nil and tostring(stanza.tags[1].name) == "query" then
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   167
		if stanza.tags[1].attr ~= nil then
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   168
			sid = stanza.tags[1].attr.sid;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   169
		end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   170
		if stanza.tags[1].tags[1] ~= nil and tostring(stanza.tags[1].tags[1].name) == "activate" then
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   171
			to = stanza.tags[1].tags[1][1];
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   172
		end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   173
	end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   174
	if from ~= nil and to ~= nil and sid ~= nil then
68
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   175
		reply = st.iq({type="result", from=host});
65
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   176
		reply.attr.id = stanza.attr.id;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   177
	end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   178
	return reply, from, to, sid;
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   179
end
a35eb0764ac6 mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents: 64
diff changeset
   180
68
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   181
function handle_to_domain(origin, stanza)
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   182
	local to_node, to_host, to_resource = jid_split(stanza.attr.to);
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   183
	if to_node == nil then
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   184
		local type = stanza.attr.type;
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   185
		if type == "error" or type == "result" then return; end
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   186
		if stanza.name == "iq" and type == "get" then
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   187
			local xmlns = stanza.tags[1].attr.xmlns
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   188
			if xmlns == "http://jabber.org/protocol/disco#info" then
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   189
				origin.send(get_disco_info(stanza));
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   190
				return true;
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   191
			elseif xmlns == "http://jabber.org/protocol/disco#items" then
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   192
				origin.send(get_disco_items(stanza));
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   193
				return true;
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   194
			elseif xmlns == "http://jabber.org/protocol/bytestreams" then
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   195
				origin.send(get_stream_host(stanza));
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   196
				return true;
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   197
			end
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   198
		elseif stanza.name == "iq" and type == "set" then
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   199
			local reply, from, to, sid = set_activation(stanza);
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   200
			if reply ~= nil and from ~= nil and to ~= nil and sid ~= nil then
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   201
				local sha = sha1(sid .. from .. to, true);
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   202
				if transfers[sha] == nil then
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   203
					module:log("error", "transfers[sha]: nil");
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   204
				elseif(transfers[sha] ~= nil and transfers[sha].initiator ~= nil and transfers[sha].target ~= nil) then
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   205
					origin.send(reply);
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   206
					transfers[sha].activated = true;
64
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   207
				end
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   208
			end
68
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   209
		end
64
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   210
	end
68
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   211
	return;
64
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   212
end
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   213
68
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   214
if not connlisteners_register('proxy65', connlistener) then
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   215
	error("mod_proxy65: Could not establish a connection listener. Check your configuration please.");
64
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   216
end
853c3c7e9936 mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   217
68
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   218
connlisteners_start('proxy65');
0df3e4d1f1a3 mod_proxy65: Reviewed and re-factored the code, added proxy_address to specify the address which the proxy advertises for clients to connect to
Matthew Wild <mwild1@gmail.com>
parents: 66
diff changeset
   219
component = component_register(host, handle_to_domain);