Comparison

plugins/mod_admin_telnet.lua @ 5708:667cf4e45356

mod_admin_telnet: Refactor s2s:close and s2s:closeall
author Kim Alvefur <zash@zash.se>
date Thu, 20 Jun 2013 20:53:29 +0200
parent 5706:896094da72e4
child 5709:5557b4a4cb49
comparison
equal deleted inserted replaced
5707:36a289e9244c 5708:667cf4e45356
774 .." presented by "..domain.."."); 774 .." presented by "..domain..".");
775 end 775 end
776 776
777 function def_env.s2s:close(from, to) 777 function def_env.s2s:close(from, to)
778 local print, count = self.session.print, 0; 778 local print, count = self.session.print, 0;
779 779 local s2s_sessions = module:shared"/*/s2s/sessions";
780 if not (from and to) then 780
781 local match_id;
782 if from and not to then
783 match_id, from = from;
784 elseif not to then
781 return false, "Syntax: s2s:close('from', 'to') - Closes all s2s sessions from 'from' to 'to'"; 785 return false, "Syntax: s2s:close('from', 'to') - Closes all s2s sessions from 'from' to 'to'";
782 elseif from == to then 786 elseif from == to then
783 return false, "Both from and to are the same... you can't do that :)"; 787 return false, "Both from and to are the same... you can't do that :)";
784 end 788 end
785 789
786 if hosts[from] and not hosts[to] then 790 for _, session in pairs(s2s_sessions) do
787 -- Is an outgoing connection 791 local id = session.type..tostring(session):sub(10);
788 local session = hosts[from].s2sout[to]; 792 if (match_id and match_id == id)
789 if not session then 793 or (session.from_host == from and session.to_host == to) then
790 print("No outgoing connection from "..from.." to "..to) 794 print(("Closing connection from %s to %s [%s]"):format(session.from_host, session.to_host, id));
791 else
792 (session.close or s2smanager.destroy_session)(session); 795 (session.close or s2smanager.destroy_session)(session);
793 count = count + 1; 796 count = count + 1 ;
794 print("Closed outgoing session from "..from.." to "..to); 797 end
795 end 798 end
796 elseif hosts[to] and not hosts[from] then
797 -- Is an incoming connection
798 for session in pairs(incoming_s2s) do
799 if session.to_host == to and session.from_host == from then
800 (session.close or s2smanager.destroy_session)(session);
801 count = count + 1;
802 end
803 end
804
805 if count == 0 then
806 print("No incoming connections from "..from.." to "..to);
807 else
808 print("Closed "..count.." incoming session"..((count == 1 and "") or "s").." from "..from.." to "..to);
809 end
810 elseif hosts[to] and hosts[from] then
811 return false, "Both of the hostnames you specified are local, there are no s2s sessions to close";
812 else
813 return false, "Neither of the hostnames you specified are being used on this server";
814 end
815
816 return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s"); 799 return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s");
817 end 800 end
818 801
819 function def_env.s2s:closeall(host) 802 function def_env.s2s:closeall(host)
820 local count = 0; 803 local count = 0;
821 804 local s2s_sessions = module:shared"/*/s2s/sessions";
822 if not host or type(host) ~= "string" then return false, "wrong syntax: please use s2s:closeall('hostname.tld')"; end 805 for _,session in pairs(s2s_sessions) do
823 if hosts[host] then 806 if not host or session.from_host == host or session.to_host == host then
824 for session in pairs(incoming_s2s) do 807 session:close();
825 if session.to_host == host then
826 (session.close or s2smanager.destroy_session)(session);
827 count = count + 1; 808 count = count + 1;
828 end 809 end
829 end 810 end
830 for _, session in pairs(hosts[host].s2sout) do
831 (session.close or s2smanager.destroy_session)(session);
832 count = count + 1;
833 end
834 else
835 for session in pairs(incoming_s2s) do
836 if session.from_host == host then
837 (session.close or s2smanager.destroy_session)(session);
838 count = count + 1;
839 end
840 end
841 for _, h in pairs(hosts) do
842 if h.s2sout[host] then
843 (h.s2sout[host].close or s2smanager.destroy_session)(h.s2sout[host]);
844 count = count + 1;
845 end
846 end
847 end
848
849 if count == 0 then return false, "No sessions to close."; 811 if count == 0 then return false, "No sessions to close.";
850 else return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s"); end 812 else return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s"); end
851 end 813 end
852 814
853 def_env.host = {}; def_env.hosts = def_env.host; 815 def_env.host = {}; def_env.hosts = def_env.host;