Whitelist All IPs for Salesforce

Whitelist All IPs for a Salesforce organization

  1. // ==UserScript==
  2. // @name Whitelist All IPs for Salesforce
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.7
  5. // @description Whitelist All IPs for a Salesforce organization
  6. // @author https://github.com/rdehler (Script by Steven Chong)
  7. // @match https://*.salesforce.com/05G*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. function initialize(sid) {
  15. var pbButton = document.querySelector('.pbButton');
  16.  
  17. if (!pbButton) {
  18. requestAnimationFrame(function() { initialize(sid); });
  19. } else {
  20. var loadingImage = createLoadingImage();
  21.  
  22. var deleteAllButton = addButton('Delete All', pbButton);
  23. deleteAllButton.onclick = function() { deleteAll(loadingImage, pbButton, sid, function() { return confirm('This remove ALL trusted IP ranges. Would you like to proceed?'); }); };
  24.  
  25. var whitelistAllButton = addButton('Whitelist All IPs', pbButton);
  26. whitelistAllButton.onclick = function() { allowAll(loadingImage, pbButton, sid); };
  27.  
  28. var whitelistMyIpButton = addButton('Whitelist my IP', pbButton);
  29. whitelistMyIpButton.disabled = true;
  30. request('https://cors-anywhere.herokuapp.com/http://api.ipify.org/?format=text','get').then(function(ip){
  31. whitelistMyIpButton.value = 'Whitelist my IP (' + ip + ')';
  32. whitelistMyIpButton.onclick = function() { allowMyIp(loadingImage, pbButton, ip, sid); };
  33. whitelistMyIpButton.disabled = false;
  34. });
  35.  
  36. if (/^\?deleteall=\d+/.test(location.search)) deleteAll(loadingImage, pbButton, sid, function() { return true; });
  37. }
  38. }
  39.  
  40. function initSid() {
  41. var theSid = document.cookie.match(/(^|;\s*)sid=(.+?);/);
  42. if (!theSid || !theSid[2]) {
  43. requestAnimationFrame(initSid);
  44. } else {
  45. initialize(theSid[2]);
  46. }
  47. }
  48.  
  49. initSid();
  50.  
  51. function addButton(text, pbButton){
  52. var button = document.createElement('input');
  53. button.type = 'button';
  54. button.className = 'btn';
  55. button.value = text;
  56. pbButton.appendChild(button);
  57. return button;
  58. }
  59.  
  60. function createLoadingImage(){
  61. var loadingImage = document.createElement('img');
  62. loadingImage.src = '/img/loading.gif';
  63. loadingImage.className = 'LoadinImage';
  64. loadingImage.style.verticalAlign = 'middle';
  65. loadingImage.style.margin = '0.2rem';
  66. return loadingImage;
  67. }
  68.  
  69. function deleteAll(loadingImage, pbButton, sid, confirmFunc) {
  70. if(confirmFunc()){
  71. var actionLinks = [];
  72. var actionColumns = document.querySelectorAll('.actionColumn');
  73. for (var i = 0, iL = actionColumns.length; i < iL; i++) {
  74. var links = actionColumns[i].querySelectorAll('.actionLink');
  75.  
  76. for (var j = 0, jL = links.length; j < jL; j++) {
  77. var href = links[j].getAttribute('href');
  78. if (/^javascript:srcUp\(%27(.+)%27\);$/.test(href)) href = decodeURIComponent(href.replace(/^javascript:srcUp\(%27(.+)%27\);$/, '$1'));
  79. console.log(href);
  80. if (/\/setup\/own\/deleteredirect\.jsp/.test(href)) {
  81. actionLinks.push(href);
  82. break;
  83. }
  84. }
  85. }
  86.  
  87. if (actionLinks.length > 0) {
  88. var ipsLeft = actionLinks.length;
  89.  
  90. function doRequest(link) {
  91. request(link, 'get', sid).then(function() {
  92. console.log(--ipsLeft + ' ips left');
  93. counterElement.innerText = 'deleting ' + ipsLeft + '/' + actionLinks.length;
  94. if(ipsLeft === 0) location.replace(location.pathname + '?deleteall=' + new Date().getTime() + location.search.replace(/^\?deleteall=\d+&/, '?').replace(/^\?/, '&'));
  95. });
  96. }
  97.  
  98. pbButton.innerHTML = '';
  99.  
  100. var counterElement = document.createElement('span');
  101. counterElement.id = 'ipCounter';
  102. counterElement.innerText = 'deleting 0/' + actionLinks.length;
  103. pbButton.appendChild(counterElement);
  104.  
  105. pbButton.appendChild(loadingImage);
  106.  
  107. for (var k = 0, kL = actionLinks.length; k < kL; k++) {
  108. doRequest(actionLinks[k]);
  109. }
  110. } else if (/^\?deleteall=\d+/.test(location.search)) {
  111. location.replace(location.pathname + location.search.replace(/^\?deleteall=\d+&/, '?').replace(/^\?deleteall=\d+$/, ''));
  112. }
  113. }
  114. }
  115.  
  116. function allowAll(loadingImage, pbButton, sid){
  117. if(confirm('This will allow users to connect from every computer without verification code or security token. This might present a security threat. Would you like to proceed?')){
  118. var IP_RANGE = 255;
  119. pbButton.innerHTML = '';
  120.  
  121. var counterElement = document.createElement('span');
  122. counterElement.id = 'ipCounter';
  123. counterElement.innerText = 'adding 0/' + IP_RANGE;
  124. pbButton.appendChild(counterElement);
  125.  
  126. pbButton.appendChild(loadingImage);
  127.  
  128. var pendingIps = [];
  129.  
  130. for(var i = 0 ; i <= IP_RANGE ;i+=2){
  131. addIp(i, counterElement, pendingIps, sid, IP_RANGE);
  132. }
  133. }
  134. }
  135.  
  136. function allowMyIp(loadingImage, pbButton, myIp, sid){
  137. if(confirm('This will allow users to connect from ' + myIp + ' without verification code or security token. This might present a security threat. Would you like to proceed?')){
  138. if (pbButton) {
  139. pbButton.innerHTML = '';
  140. pbButton.appendChild(loadingImage);
  141. }
  142. addMyIp(myIp, sid);
  143. }
  144. }
  145.  
  146. function addIp(ipPrefix, counterElement, pendingIps, sid, IP_RANGE){
  147. pendingIps[ipPrefix] = true;
  148. request('/05G/e', 'get', sid).then(function(result){
  149. var confirmationToken = result.match(/input type="hidden" name="_CONFIRMATIONTOKEN" id="_CONFIRMATIONTOKEN" value="([^"]*)"/)[1];
  150. return request('/05G/e?IpStartAddress=' + ipPrefix + '.0.0.0&IpEndAddress=' + (ipPrefix + 1) + '.255.255.255&save=1&_CONFIRMATIONTOKEN=' + confirmationToken, 'post', sid);
  151. }).then(function(result){
  152. console.log(ipPrefix + ' is done');
  153. pendingIps[ipPrefix] = false;
  154. var ipsLeft = pendingIps.reduce(function(sum,curVal){
  155. return curVal ? ++sum : sum;
  156. },0);
  157. console.log(ipsLeft + ' ips left');
  158. counterElement.innerText = 'adding ' + (IP_RANGE-ipsLeft) + '/' + IP_RANGE;
  159. if(ipsLeft === 0) location.reload();
  160. });
  161. }
  162.  
  163. function addMyIp(myIp, sid){
  164. request('/05G/e','get', sid).then(function(result){
  165. var confirmationToken = result.match(/input type="hidden" name="_CONFIRMATIONTOKEN" id="_CONFIRMATIONTOKEN" value="([^"]*)"/)[1];
  166. return request('/05G/e?IpStartAddress=' + myIp + '&IpEndAddress=' + myIp + '&save=1&_CONFIRMATIONTOKEN=' + confirmationToken, 'post', sid);
  167. }).then(function(result){
  168. console.log(myIp + ' is done');
  169. location.reload();
  170. });
  171. }
  172.  
  173. function request(url, method, sid){
  174. method = method || 'GET';
  175. if(typeof GM_xmlhttpRequest === "function"){
  176. return new Promise(function(fulfill,reject){
  177. GM_xmlhttpRequest({
  178. method:method,
  179. url:url,
  180. headers:{
  181. Authorization:'Bearer ' + sid,
  182. Accept:'*/*'
  183. },
  184. onload:function(response){
  185. if( response.status.toString().indexOf('2') === 0){
  186. fulfill(response.response);
  187. }else{
  188. reject(Error(response.statusText));
  189. }
  190. },
  191. onerror:function(response){
  192. rejected(Error("Network Error"));
  193. }
  194. });
  195. });
  196. }
  197. return new Promise(function(fulfill,reject){
  198. var xhr = new XMLHttpRequest();
  199. xhr.open(method,url);
  200. xhr.onload = function(){
  201. if( xhr.status.toString().indexOf('2') === 0){
  202. fulfill(xhr.response);
  203. }else{
  204. reject(Error(xhr.statusText));
  205. }
  206. };
  207. xhr.onerror = function(){
  208. rejected(Error("Network Error"));
  209. };
  210. if (sid) xhr.setRequestHeader('Authorization','Bearer ' + sid);
  211. xhr.send();
  212. });
  213. }
  214. })();