home *** CD-ROM | disk | FTP | other *** search
/ Windows News 2005 February / WN_129_CD.iso / Windows / Extensions Firefox / SwitchProxy Tool / switchproxy_1.3.1.xpi / chrome / switchproxy.jar / content / anon.js next >
Encoding:
Text File  |  2004-08-05  |  7.4 KB  |  233 lines

  1. var gAnonRotate            = false;
  2. var gAnonTimout            = null;
  3. var gAnonUri            = null;
  4. var gAnonLabel            = "unknown";
  5. var gAnonCurrent        = "unknown";
  6. var gCurrentCount        = 0;
  7. var gAnonUpdateTimout    = null;
  8. var gAnonLastUrl        = "";
  9.  
  10. /*
  11. * Load an anonomous proxy from sUri.
  12. *    cleanLoad is set to true when this is called 
  13. *    for the first time for sUri
  14. */
  15. function switchproxy_anon_loadProxy(sUri, cleanLoad){
  16.     
  17.     clearTimeout(gAnonTimout);
  18.     gAnonTimout = null;
  19.     
  20.     if(cleanLoad){
  21.         gAnonRotate = true;
  22.     }
  23.     else if(!gAnonRotate){
  24.         return;
  25.     }
  26.     
  27.     if(sUri == null && gAnonUri != null){
  28.         sUri = gAnonUri;
  29.     }
  30.     
  31.     try{    
  32.         var iPause            = (60) * 1000;
  33.         var oProxy            = switchProxy_ds_getResource(sUri);
  34.         var oProxyConfig    = switchProxy_ds_getPropertyValuesFor(sUri);
  35.         var sProxyLabel        = oProxyConfig[gSProxyRdfNodeName];
  36.         var oPrefs            = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  37.         
  38.         //Set Interval
  39.         if(oProxyConfig[gSProxyRdfNodeUriRoot + "#interval"] && !isNaN(oProxyConfig[gSProxyRdfNodeUriRoot + "#interval"]) && parseInt(oProxyConfig[gSProxyRdfNodeUriRoot + "#interval"]) > 0)
  40.             iPause = parseInt(oProxyConfig[gSProxyRdfNodeUriRoot + "#interval"]) * 1000;
  41.         
  42.         //Get Proxy List
  43.         var aProxyList         = switchProxy_ds_getValuesFor(oProxy, switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#proxy"));
  44.         if(aProxyList.length == 0){
  45.             alert("SwitchProxy\n"+ switchproxy_getString("anon.config.empty"));
  46.             return;
  47.         }
  48.         
  49.         // Pick a Random proxy
  50.         var iPick = Math.round(Math.random() * (aProxyList.length - 1));
  51.         
  52.         if(gAnonLastUrl == aProxyList[iPick])
  53.             iPick = Math.round(Math.random() * (aProxyList.length - 1));
  54.         
  55.         var gAnonLastUrl    = aProxyList[iPick];
  56.         var aProxy            = switchproxy_splitDomain(aProxyList[iPick]);
  57.             gAnonLabel        = sProxyLabel;
  58.             gAnonCurrent    = aProxyList[iPick];
  59.             gCurrentCount    = ( iPause / 1000 );
  60.             gAnonUri        = sUri;
  61.         
  62.         //Exit if couldn't split domain
  63.         if(aProxy.length == 0){
  64.             alert("SwitchProxy\n"+ switchproxy_getString("error.unknown"));
  65.             return;
  66.         }
  67.         
  68.         //Reset all proxy prefs
  69.         for(key in gSwichProxy_options){
  70.             
  71.             if(key != "networkProxyAutoconfigURL" && oPrefs.prefHasUserValue(gSwichProxy_options[key]))
  72.                 oPrefs.clearUserPref(gSwichProxy_options[key]);
  73.         }
  74.         
  75.         //Set Proxy
  76.         oPrefs.setIntPref("network.proxy.type", 1);
  77.         oPrefs.setCharPref("switchproxy.proxy.current", sUri);
  78.         
  79.         // Set Types
  80.         if(!oProxyConfig[gSProxyRdfNodeUriRoot + "#http"] || oProxyConfig[gSProxyRdfNodeUriRoot + "#http"] == "true"){
  81.             oPrefs.setCharPref("network.proxy.http", aProxy[0]);
  82.             oPrefs.setIntPref("network.proxy.http_port", parseInt(aProxy[1]));
  83.         }
  84.         if(!oProxyConfig[gSProxyRdfNodeUriRoot + "#ssl"] || oProxyConfig[gSProxyRdfNodeUriRoot + "#ssl"] == "true"){
  85.             oPrefs.setCharPref("network.proxy.ssl", aProxy[0]);
  86.             oPrefs.setIntPref("network.proxy.ssl_port", parseInt(aProxy[1]));
  87.         }
  88.         if(!oProxyConfig[gSProxyRdfNodeUriRoot + "#ftp"] || oProxyConfig[gSProxyRdfNodeUriRoot + "#ftp"] == "true"){
  89.             oPrefs.setCharPref("network.proxy.ftp", aProxy[0]);
  90.             oPrefs.setIntPref("network.proxy.ftp_port", parseInt(aProxy[1]));
  91.         }
  92.         if(!oProxyConfig[gSProxyRdfNodeUriRoot + "#gopher"] || oProxyConfig[gSProxyRdfNodeUriRoot + "#gopher"] == "true"){
  93.             oPrefs.setCharPref("network.proxy.gopher", aProxy[0]);
  94.             oPrefs.setIntPref("network.proxy.gopher_port", parseInt(aProxy[1]));
  95.         }
  96.         if(!oProxyConfig[gSProxyRdfNodeUriRoot + "#socks"] || oProxyConfig[gSProxyRdfNodeUriRoot + "#socks"] == "true"){
  97.             oPrefs.setCharPref("network.proxy.socks", aProxy[0]);
  98.             oPrefs.setIntPref("network.proxy.socks_port", parseInt(aProxy[1]));
  99.         }
  100.         if(oProxyConfig[gSProxyRdfNodeUriRoot + "#socks_type"] && !isNaN(oProxyConfig[gSProxyRdfNodeUriRoot + "#socks_type"])){
  101.             oPrefs.setIntPref("network.proxy.socks_version", parseInt(oProxyConfig[gSProxyRdfNodeUriRoot + "#socks_type"]));
  102.         }
  103.         
  104.         //Start loop
  105.         gAnonTimout = setTimeout("switchproxy_anon_countdown('"+ sUri +"');", 1000);
  106.         
  107.         //Update Status
  108.         try{
  109.             switchproxy_setStatus(switchproxy_getString("toolbar.using") +" "+ sProxyLabel +" ["+ aProxyList[iPick] +"] ("+ gCurrentCount +")");
  110.             gSwitchP_StatusBar.setAttribute("label", "Proxy: "+ sProxyLabel +" ["+ aProxyList[iPick] +"] ("+ gCurrentCount +")");
  111.         }catch(e){ }
  112.         
  113.         
  114.     }catch(err){alert("SwitchProxy\n"+ switchproxy_getString("error.unknown") + "\n"+ err);}
  115. }
  116.  
  117. // Goto next anon proxy in list
  118. function switchproxy_anon_nextProxy(){
  119.     try{
  120.         var oContext    = document.getElementById('context-proxy-list');
  121.         var sUri        = oContext.selectedItem.value;
  122.         var sType        = oContext.selectedItem.getAttribute("proxyType");
  123.  
  124.         if(sType == "3")
  125.             switchproxy_anon_loadProxy(sUri);
  126.         
  127.     } catch(e){ }
  128. }
  129.  
  130. //Stop Anonomous Proxy Rotation
  131. function switchproxy_anon_stopRotation(){
  132.     gAnonRotate        = false;
  133.     gAnonUri        = null;
  134.     gAnonLabel        = "unknown";
  135.     gAnonCurrent    = "unknown";
  136.     gCurrentCount    = 0;
  137.     
  138.     if(gAnonTimout != null)
  139.         clearTimeout(gAnonTimout);
  140.         
  141.     gAnonTimout     = null;
  142. }
  143.  
  144. //Get the current proxy
  145. function switchproxy_anon_getCurrent(){
  146.     return gAnonCurrent;
  147. }
  148.  
  149. //Get the current countdown
  150. function switchproxy_anon_getCurrentCount(){
  151.     return gCurrentCount;
  152. }
  153.  
  154. //Pause Rotation by sTime in milliseconds
  155. function switchproxy_anon_pauseRotation(sTime){
  156.     if(gAnonTimout != null){
  157.         clearTimeout(gAnonTimout);
  158.         gAnonTimout = setTimeout("switchproxy_anon_countdown('"+ gAnonUri +"');", sTime);    
  159.     }
  160. }
  161.  
  162. //Countdown Rotation
  163. function switchproxy_anon_countdown(sUri){
  164.     
  165.     if(!gAnonRotate)
  166.         return;
  167.     
  168.     gCurrentCount--;
  169.     
  170.     //End
  171.     if(gCurrentCount < 0){
  172.         switchproxy_anon_loadProxy(sUri);
  173.         return;
  174.     }
  175.     
  176.     //Continue
  177.     gAnonTimout = setTimeout("switchproxy_anon_countdown('"+ sUri +"');", 1000);
  178.     try{
  179.         switchproxy_setStatus(switchproxy_getString("toolbar.using") +" "+ gAnonLabel +" ["+ gAnonCurrent +"] ("+ gCurrentCount +")");
  180.         gSwitchP_StatusBar.setAttribute("label", "Proxy: "+ gAnonLabel +" ["+ gAnonCurrent +"] ("+ gCurrentCount +")");
  181.     } catch(e){ }
  182. }
  183.  
  184. //Auto-Update Proxy Lists with URL
  185. function switchproxy_anon_autoUpdateLists(){
  186.  
  187.     var iNow    = (new Date()).getTime();
  188.     var iTime        = iNow - (90000 * 1000); //25 hours ago
  189.     
  190.     //Get all anon configurations
  191.     var aAnon = switchProxy_ds_getElementsForValue(gSProxyRdfNodeUriRoot + "#networkProxyType", "3");
  192.     
  193.     //Find one that needs updating
  194.     var sValue     = "";
  195.     var sUrl     = "";
  196.     var sAuto     = "";
  197.     var iLast    = 0;
  198.     
  199.     for(var i = 0; i < aAnon.length; i++){
  200.         try{
  201.             sAuto     = switchProxy_ds_getValueFor(aAnon[i], switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#autoUpdate"));
  202.             sUrl     = switchProxy_ds_getValueFor(aAnon[i], switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#autoUpdateUrl"));
  203.             sValue     = switchProxy_ds_getValueFor(aAnon[i], switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#autoUpdateTime"));
  204.             if(sAuto == "true" && !switchproxy_isEmpty(sUrl) && sValue != null && !isNaN(sValue)){
  205.                 iValue = parseInt(sValue); 
  206.                 
  207.                 //Needs to be updated
  208.                 if(iValue <= iTime){
  209.                 
  210.                     //Import
  211.                     switchproxy_anon_import("url", sUrl, null, aAnon[i].Value);
  212.                     
  213.                     //Update Timestamp
  214.                     switchProxy_ds_removeProperty(gSProxyRdfNodeUriRoot + "#autoUpdateTime", aAnon[i]);
  215.                     switchProxy_ds_addProperty(aAnon[i], switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#autoUpdateTime"), iNow, true);
  216.                             
  217.                     break;
  218.                 }
  219.             }
  220.         }catch(err){ }
  221.     }
  222.     
  223.     //Check Again
  224.     if(aAnon.length > 0){
  225.         var iRecheck = ((43200 * 1000) / aAnon.length) //Break up checks throughout the day
  226.         gAnonUpdateTimout = setTimeout("switchproxy_anon_autoUpdateLists()", iRecheck);
  227.     }
  228. }
  229.  
  230. //Get Auto Update Timout Object
  231. function switchproxy_anon_getAutoUpdateTimout(){
  232.     return gAnonUpdateTimout;
  233. }