Software / code / prosody
Comparison
plugins/mod_proxy65.lua @ 4374:c38f20f172b3
mod_proxy65: Cleanup.
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Fri, 02 Sep 2011 23:50:34 +0500 |
| parent | 3694:a7d88f58abbb |
| child | 4375:81f5e83211dd |
comparison
equal
deleted
inserted
replaced
| 4373:9a20acf315c9 | 4374:c38f20f172b3 |
|---|---|
| 18 local connlisteners = require "net.connlisteners"; | 18 local connlisteners = require "net.connlisteners"; |
| 19 local sha1 = require "util.hashes".sha1; | 19 local sha1 = require "util.hashes".sha1; |
| 20 local server = require "net.server"; | 20 local server = require "net.server"; |
| 21 | 21 |
| 22 local host, name = module:get_host(), "SOCKS5 Bytestreams Service"; | 22 local host, name = module:get_host(), "SOCKS5 Bytestreams Service"; |
| 23 local sessions, transfers, replies_cache = {}, {}, {}; | 23 local sessions, transfers = {}, {}; |
| 24 | 24 |
| 25 local proxy_port = module:get_option("proxy65_port") or 5000; | 25 local proxy_port = module:get_option("proxy65_port") or 5000; |
| 26 local proxy_interface = module:get_option("proxy65_interface") or "*"; | 26 local proxy_interface = module:get_option("proxy65_interface") or "*"; |
| 27 local proxy_address = module:get_option("proxy65_address") or (proxy_interface ~= "*" and proxy_interface) or host; | 27 local proxy_address = module:get_option("proxy65_address") or (proxy_interface ~= "*" and proxy_interface) or host; |
| 28 local proxy_acl = module:get_option("proxy65_acl"); | 28 local proxy_acl = module:get_option("proxy65_acl"); |
| 120 module:add_identity("proxy", "bytestreams", name); | 120 module:add_identity("proxy", "bytestreams", name); |
| 121 module:add_feature("http://jabber.org/protocol/bytestreams"); | 121 module:add_feature("http://jabber.org/protocol/bytestreams"); |
| 122 | 122 |
| 123 module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function(event) | 123 module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function(event) |
| 124 local origin, stanza = event.origin, event.stanza; | 124 local origin, stanza = event.origin, event.stanza; |
| 125 local reply = replies_cache.disco_info; | 125 origin.send(st.reply(stanza):query("http://jabber.org/protocol/disco#info") |
| 126 if reply == nil then | 126 :tag("identity", {category='proxy', type='bytestreams', name=name}):up() |
| 127 reply = st.iq({type='result', from=host}):query("http://jabber.org/protocol/disco#info") | 127 :tag("feature", {var="http://jabber.org/protocol/bytestreams"}) ); |
| 128 :tag("identity", {category='proxy', type='bytestreams', name=name}):up() | |
| 129 :tag("feature", {var="http://jabber.org/protocol/bytestreams"}); | |
| 130 replies_cache.disco_info = reply; | |
| 131 end | |
| 132 | |
| 133 reply.attr.id = stanza.attr.id; | |
| 134 reply.attr.to = stanza.attr.from; | |
| 135 origin.send(reply); | |
| 136 return true; | 128 return true; |
| 137 end, -1); | 129 end, -1); |
| 138 | 130 |
| 139 module:hook("iq-get/host/http://jabber.org/protocol/disco#items:query", function(event) | 131 module:hook("iq-get/host/http://jabber.org/protocol/disco#items:query", function(event) |
| 140 local origin, stanza = event.origin, event.stanza; | 132 local origin, stanza = event.origin, event.stanza; |
| 141 local reply = replies_cache.disco_items; | 133 origin.send(st.reply(stanza):query("http://jabber.org/protocol/disco#items")); |
| 142 if reply == nil then | |
| 143 reply = st.iq({type='result', from=host}):query("http://jabber.org/protocol/disco#items"); | |
| 144 replies_cache.disco_items = reply; | |
| 145 end | |
| 146 | |
| 147 reply.attr.id = stanza.attr.id; | |
| 148 reply.attr.to = stanza.attr.from; | |
| 149 origin.send(reply); | |
| 150 return true; | 134 return true; |
| 151 end, -1); | 135 end, -1); |
| 152 | 136 |
| 153 module:hook("iq-get/host/http://jabber.org/protocol/bytestreams:query", function(event) | 137 module:hook("iq-get/host/http://jabber.org/protocol/bytestreams:query", function(event) |
| 154 local origin, stanza = event.origin, event.stanza; | 138 local origin, stanza = event.origin, event.stanza; |
| 155 local reply = replies_cache.stream_host; | 139 |
| 156 local err_reply = replies_cache.stream_host_err; | 140 -- check ACL |
| 141 while proxy_acl and #proxy_acl > 0 do -- using 'while' instead of 'if' so we can break out of it | |
| 142 local jid = stanza.attr.from; | |
| 143 for _, acl in ipairs(proxy_acl) do | |
| 144 if jid_compare(jid, acl) then break; end | |
| 145 end | |
| 146 module:log("warn", "Denying use of proxy for %s", tostring(stanza.attr.from)); | |
| 147 origin.send(st.error_reply(stanza, "auth", "forbidden")); | |
| 148 return true; | |
| 149 end | |
| 150 | |
| 157 local sid = stanza.tags[1].attr.sid; | 151 local sid = stanza.tags[1].attr.sid; |
| 158 local allow = false; | 152 origin.send(st.reply(stanza):tag("query", {xmlns="http://jabber.org/protocol/bytestreams", sid=sid}) |
| 159 local jid = stanza.attr.from; | 153 :tag("streamhost", {jid=host, host=proxy_address, port=proxy_port})); |
| 160 | |
| 161 if proxy_acl and #proxy_acl > 0 then | |
| 162 for _, acl in ipairs(proxy_acl) do | |
| 163 if jid_compare(jid, acl) then allow = true; end | |
| 164 end | |
| 165 else | |
| 166 allow = true; | |
| 167 end | |
| 168 if allow == true then | |
| 169 if reply == nil then | |
| 170 reply = st.iq({type="result", from=host}) | |
| 171 :query("http://jabber.org/protocol/bytestreams") | |
| 172 :tag("streamhost", {jid=host, host=proxy_address, port=proxy_port}); | |
| 173 replies_cache.stream_host = reply; | |
| 174 end | |
| 175 else | |
| 176 module:log("warn", "Denying use of proxy for %s", tostring(jid)); | |
| 177 if err_reply == nil then | |
| 178 err_reply = st.iq({type="error", from=host}) | |
| 179 :query("http://jabber.org/protocol/bytestreams") | |
| 180 :tag("error", {code='403', type='auth'}) | |
| 181 :tag("forbidden", {xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'}); | |
| 182 replies_cache.stream_host_err = err_reply; | |
| 183 end | |
| 184 reply = err_reply; | |
| 185 end | |
| 186 reply.attr.id = stanza.attr.id; | |
| 187 reply.attr.to = stanza.attr.from; | |
| 188 reply.tags[1].attr.sid = sid; | |
| 189 origin.send(reply); | |
| 190 return true; | 154 return true; |
| 191 end); | 155 end); |
| 192 | 156 |
| 193 module.unload = function() | 157 module.unload = function() |
| 194 connlisteners.deregister(module.host .. ':proxy65'); | 158 connlisteners.deregister(module.host .. ':proxy65'); |