Software / code / prosody
Comparison
plugins/mod_proxy65.lua @ 3007:9d122a6ae674
Merge 0.7->trunk
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Wed, 05 May 2010 15:33:58 +0100 |
| parent | 3006:a3580f556c27 |
| child | 3377:9328179c9c76 |
comparison
equal
deleted
inserted
replaced
| 2999:9a8f942433c4 | 3007:9d122a6ae674 |
|---|---|
| 18 local st = require "util.stanza"; | 18 local st = require "util.stanza"; |
| 19 local componentmanager = require "core.componentmanager"; | 19 local componentmanager = require "core.componentmanager"; |
| 20 local config_get = require "core.configmanager".get; | 20 local config_get = require "core.configmanager".get; |
| 21 local connlisteners = require "net.connlisteners"; | 21 local connlisteners = require "net.connlisteners"; |
| 22 local sha1 = require "util.hashes".sha1; | 22 local sha1 = require "util.hashes".sha1; |
| 23 local server = require "net.server"; | |
| 23 | 24 |
| 24 local host, name = module:get_host(), "SOCKS5 Bytestreams Service"; | 25 local host, name = module:get_host(), "SOCKS5 Bytestreams Service"; |
| 25 local sessions, transfers, component, replies_cache = {}, {}, nil, {}; | 26 local sessions, transfers, component, replies_cache = {}, {}, nil, {}; |
| 26 | 27 |
| 27 local proxy_port = config_get(host, "core", "proxy65_port") or 5000; | 28 local proxy_port = config_get(host, "core", "proxy65_port") or 5000; |
| 28 local proxy_interface = config_get(host, "core", "proxy65_interface") or "*"; | 29 local proxy_interface = config_get(host, "core", "proxy65_interface") or "*"; |
| 29 local proxy_address = config_get(host, "core", "proxy65_address") or (proxy_interface ~= "*" and proxy_interface) or host; | 30 local proxy_address = config_get(host, "core", "proxy65_address") or (proxy_interface ~= "*" and proxy_interface) or host; |
| 30 local proxy_acl = config_get(host, "core", "proxy65_acl"); | 31 local proxy_acl = config_get(host, "core", "proxy65_acl"); |
| 32 local max_buffer_size = 4096; | |
| 31 | 33 |
| 32 local connlistener = { default_port = proxy_port, default_interface = proxy_interface, default_mode = "*a" }; | 34 local connlistener = { default_port = proxy_port, default_interface = proxy_interface, default_mode = "*a" }; |
| 33 | 35 |
| 34 function connlistener.onincoming(conn, data) | 36 function connlistener.onincoming(conn, data) |
| 35 local session = sessions[conn] or {}; | 37 local session = sessions[conn] or {}; |
| 82 module:log("debug", "target connected ... "); | 84 module:log("debug", "target connected ... "); |
| 83 elseif transfers[sha].target ~= nil then | 85 elseif transfers[sha].target ~= nil then |
| 84 transfers[sha].initiator = conn; | 86 transfers[sha].initiator = conn; |
| 85 session.sha = sha; | 87 session.sha = sha; |
| 86 module:log("debug", "initiator connected ... "); | 88 module:log("debug", "initiator connected ... "); |
| 87 throttle_sending(conn, transfers[sha].target); | 89 server.link(conn, transfers[sha].target, max_buffer_size); |
| 88 throttle_sending(transfers[sha].target, conn); | 90 server.link(transfers[sha].target, conn, max_buffer_size); |
| 89 end | 91 end |
| 90 conn:write(string.char(5, 0, 0, 3, sha:len()) .. sha .. string.char(0, 0)); -- VER, REP, RSV, ATYP, BND.ADDR (sha), BND.PORT (2 Byte) | 92 conn:write(string.char(5, 0, 0, 3, sha:len()) .. sha .. string.char(0, 0)); -- VER, REP, RSV, ATYP, BND.ADDR (sha), BND.PORT (2 Byte) |
| 91 conn:lock_read(true) | 93 conn:lock_read(true) |
| 92 else | 94 else |
| 93 module:log("warn", "Neither data transfer nor initial connect of a participator of a transfer.") | 95 module:log("warn", "Neither data transfer nor initial connect of a participator of a transfer.") |
| 232 origin.send(get_disco_items(stanza)); | 234 origin.send(get_disco_items(stanza)); |
| 233 return true; | 235 return true; |
| 234 elseif xmlns == "http://jabber.org/protocol/bytestreams" then | 236 elseif xmlns == "http://jabber.org/protocol/bytestreams" then |
| 235 origin.send(get_stream_host(origin, stanza)); | 237 origin.send(get_stream_host(origin, stanza)); |
| 236 return true; | 238 return true; |
| 239 else | |
| 240 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
| 241 return true; | |
| 237 end | 242 end |
| 238 elseif stanza.name == "iq" and type == "set" then | 243 elseif stanza.name == "iq" and type == "set" then |
| 244 module:log("debug", "Received activation request from %s", stanza.attr.from); | |
| 239 local reply, from, to, sid = set_activation(stanza); | 245 local reply, from, to, sid = set_activation(stanza); |
| 240 if reply ~= nil and from ~= nil and to ~= nil and sid ~= nil then | 246 if reply ~= nil and from ~= nil and to ~= nil and sid ~= nil then |
| 241 local sha = sha1(sid .. from .. to, true); | 247 local sha = sha1(sid .. from .. to, true); |
| 242 if transfers[sha] == nil then | 248 if transfers[sha] == nil then |
| 243 module:log("error", "transfers[sha]: nil"); | 249 module:log("error", "transfers[sha]: nil"); |
| 244 elseif(transfers[sha] ~= nil and transfers[sha].initiator ~= nil and transfers[sha].target ~= nil) then | 250 elseif(transfers[sha] ~= nil and transfers[sha].initiator ~= nil and transfers[sha].target ~= nil) then |
| 245 origin.send(reply); | 251 origin.send(reply); |
| 246 transfers[sha].activated = true; | 252 transfers[sha].activated = true; |
| 247 transfers[sha].target:lock_read(false); | 253 transfers[sha].target:lock_read(false); |
| 248 transfers[sha].initiator:lock_read(false); | 254 transfers[sha].initiator:lock_read(false); |
| 255 else | |
| 256 module:log("debug", "Both parties were not yet connected"); | |
| 257 local message = "Neither party is connected to the proxy"; | |
| 258 if transfers[sha].initiator then | |
| 259 message = "The recipient is not connected to the proxy"; | |
| 260 elseif transfers[sha].target then | |
| 261 message = "The sender (you) is not connected to the proxy"; | |
| 262 end | |
| 263 origin.send(st.error_reply(stanza, "cancel", "not-allowed", message)); | |
| 249 end | 264 end |
| 250 else | 265 else |
| 251 module:log("error", "activation failed: sid: %s, initiator: %s, target: %s", tostring(sid), tostring(from), tostring(to)); | 266 module:log("error", "activation failed: sid: %s, initiator: %s, target: %s", tostring(sid), tostring(from), tostring(to)); |
| 252 end | 267 end |
| 253 end | 268 end |
| 260 module:log("error", "Possibly two proxy65 components are configured to share the same port."); | 275 module:log("error", "Possibly two proxy65 components are configured to share the same port."); |
| 261 end | 276 end |
| 262 | 277 |
| 263 connlisteners.start(module.host .. ':proxy65'); | 278 connlisteners.start(module.host .. ':proxy65'); |
| 264 component = componentmanager.register_component(host, handle_to_domain); | 279 component = componentmanager.register_component(host, handle_to_domain); |
| 265 local sender_lock_threshold = 4096; | |
| 266 function throttle_sending(sender, receiver) | |
| 267 sender:pattern(sender_lock_threshold); | |
| 268 local sender_locked; | |
| 269 local _sendbuffer = receiver.sendbuffer; | |
| 270 function receiver.sendbuffer() | |
| 271 _sendbuffer(); | |
| 272 if sender_locked and receiver.bufferlen() < sender_lock_threshold then | |
| 273 sender:lock_read(false); -- Unlock now | |
| 274 sender_locked = nil; | |
| 275 end | |
| 276 end | |
| 277 | |
| 278 local _readbuffer = sender.readbuffer; | |
| 279 function sender.readbuffer() | |
| 280 _readbuffer(); | |
| 281 if not sender_locked and receiver.bufferlen() >= sender_lock_threshold then | |
| 282 sender_locked = true; | |
| 283 sender:lock_read(true); | |
| 284 end | |
| 285 end | |
| 286 end |