mod_onions/mod_onions.lua
changeset 1785 12ac88940fe3
parent 1784 b3e3ad35391a
child 1914 720b291cb340
equal deleted inserted replaced
1784:b3e3ad35391a 1785:12ac88940fe3
    26 
    26 
    27 local proxy_ip = module:get_option("onions_socks5_host") or "127.0.0.1";
    27 local proxy_ip = module:get_option("onions_socks5_host") or "127.0.0.1";
    28 local proxy_port = module:get_option("onions_socks5_port") or 9050;
    28 local proxy_port = module:get_option("onions_socks5_port") or 9050;
    29 local forbid_else = module:get_option("onions_only") or false;
    29 local forbid_else = module:get_option("onions_only") or false;
    30 local torify_all = module:get_option("onions_tor_all") or false;
    30 local torify_all = module:get_option("onions_tor_all") or false;
       
    31 local onions_map = module:get_option("onions_map") or {};
    31 
    32 
    32 local sessions = module:shared("sessions");
    33 local sessions = module:shared("sessions");
    33 
    34 
    34 -- The socks5listener handles connection while still connecting to the proxy,
    35 -- The socks5listener handles connection while still connecting to the proxy,
    35 -- then it hands them over to the normal listener (in mod_s2s)
    36 -- then it hands them over to the normal listener (in mod_s2s)
   183 
   184 
   184 	local conn, handler = socket.tcp();
   185 	local conn, handler = socket.tcp();
   185 
   186 
   186 	module:log("debug", "Connecting to " .. connect_host .. ":" .. connect_port);
   187 	module:log("debug", "Connecting to " .. connect_host .. ":" .. connect_port);
   187 
   188 
   188 	-- this is not necessarily the same as .to_host (it can be that this is a SRV record)
   189 	-- this is not necessarily the same as .to_host (it can be that this is from the onions_map)
   189 	host_session.socks5_to = connect_host;
   190 	host_session.socks5_to = connect_host;
   190 	host_session.socks5_port = connect_port;
   191 	host_session.socks5_port = connect_port;
   191 
   192 
   192 	conn:settimeout(0);
   193 	conn:settimeout(0);
   193 
   194 
   229 	session.sendq = nil;
   230 	session.sendq = nil;
   230 end
   231 end
   231 -- Try to intercept anything to *.onion
   232 -- Try to intercept anything to *.onion
   232 local function route_to_onion(event)
   233 local function route_to_onion(event)
   233 	local stanza = event.stanza;
   234 	local stanza = event.stanza;
   234 
   235 	local to_host = event.to_host;
   235 	if not event.to_host:find(".onion(.?)$") then
   236 	local onion_host = nil;
   236 		if forbid_else then
   237 	local onion_port = nil;
       
   238 
       
   239 	if not to_host:find(".onion(.?)$") then
       
   240 		if onions_map[to_host] then
       
   241 			if type(onions_map[to_host]) == "string" then
       
   242 				onions_host = onions_map[to_host];
       
   243 			else
       
   244 				onion_host = onions_map[to_host].host;
       
   245 				onion_port = onions_map[to_host].port;
       
   246 			end
       
   247 		elseif forbid_else then
   237 			module:log("debug", event.to_host .. " is not an onion. Blocking it.");
   248 			module:log("debug", event.to_host .. " is not an onion. Blocking it.");
   238 			return false;
   249 			return false;
   239 		elseif not torify_all then
   250 		elseif not torify_all then
   240 			return;
   251 			return;
   241 		end
   252 		end
   242 	end
   253 	end
   243 
   254 
   244 	module:log("debug", "Onion routing something to ".. event.to_host);
   255 	module:log("debug", "Onion routing something to ".. to_host);
   245 
   256 
   246 	if hosts[event.from_host].s2sout[event.to_host] then
   257 	if hosts[event.from_host].s2sout[to_host] then
   247 		return;
   258 		return;
   248 	end
   259 	end
   249 
   260 
   250 	local host_session = s2s_new_outgoing(event.from_host, event.to_host);
   261 	local host_session = s2s_new_outgoing(event.from_host, to_host);
   251 
   262 
   252 	host_session.bounce_sendq = bounce_sendq;
   263 	host_session.bounce_sendq = bounce_sendq;
   253 	host_session.sendq = { {tostring(stanza), stanza.attr and stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)} };
   264 	host_session.sendq = { {tostring(stanza), stanza.attr and stanza.attr.type ~= "error" and stanza.attr.type ~= "result" and st.reply(stanza)} };
   254 
   265 
   255 	hosts[event.from_host].s2sout[event.to_host] = host_session;
   266 	hosts[event.from_host].s2sout[to_host] = host_session;
   256 
   267 
   257 	connect_socks5(host_session, event.to_host, 5269);
   268 	connect_socks5(host_session, onion_host or to_host, onion_port or 5269);
   258 
   269 
   259 	return true;
   270 	return true;
   260 end
   271 end
   261 
   272 
   262 module:log("debug", "Onions ready and loaded");
   273 module:log("debug", "Onions ready and loaded");