Whitelist All IPs for Salesforce

Whitelist All IPs for a Salesforce organization

目前為 2020-02-24 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Whitelist All IPs for Salesforce
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Whitelist All IPs for a Salesforce organization
// @author       https://github.com/rdehler (Script by Steven Chong)
// @match        https://*.salesforce.com/05G*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var sid,
        pendingIps = [],
        IP_RANGE = 255,
        loadingImage = createLoadingImage(),
        counterElement = document.createElement('p'),
        myIp = '',
        pbButton, whitelistAllButton, whitelistMyIpButton;

    function init() {
        pbButton = document.querySelector('.pbButton');

        if (!pbButton) {
            requestAnimationFrame(init);
        } else {
            whitelistAllButton = addButton('Whitelist All IPs', pbButton);
            whitelistAllButton.onclick = function() { allowAll(pbButton); };

            whitelistMyIpButton = addButton('Whitelist my IP', pbButton);
            whitelistMyIpButton.disabled = true;
            request('https://cors-anywhere.herokuapp.com/http://api.ipify.org/?format=text','get').then(function(ip){
                myIp = ip;
                whitelistMyIpButton.value = 'Whitelist my IP (' + ip + ')';
                whitelistMyIpButton.onclick = function() { allowMyIp(pbButton); };
                whitelistMyIpButton.disabled = false;
                console.log(whitelistMyIpButton);
            });
        }
    }

    function initSid() {
        var theSid = document.cookie.match(/(^|;\s*)sid=(.+?);/);
        if (!theSid || !theSid[2]) {
            requestAnimationFrame(initSid);
        } else {
            sid = theSid[2];
            init();
        }
    }

    initSid();

    function addButton(text, pbButton){
        var button = document.createElement('input');
        button.type = 'button';
        button.className = 'btn';
        button.value = text;
        pbButton.appendChild(button);
        return button;
    }

    function createLoadingImage(){
        var loadingImage = document.createElement('img');
        loadingImage.src = '/img/loading.gif';
        loadingImage.className = 'LoadinImage';
        return loadingImage;
    }

    function allowAll(pbButton){
        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?')){
            if (pbButton) {
                pbButton.innerHTML = '';
                pbButton.appendChild(createLoadingImage());
                counterElement.id = 'ipCounter';
                counterElement.innerText = '0/' + IP_RANGE;
                pbButton.appendChild(counterElement);
            }

            for(var i = 0 ; i <= IP_RANGE ;i+=2){
                addIp(i);
            }
        }
    }

    function allowMyIp(pbButton){
        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?')){
            if (pbButton) {
                pbButton.innerHTML = '';
                pbButton.appendChild(createLoadingImage())
            }
            addMyIp();
        }
    }

    function addIp(ipPrefix){
        pendingIps[ipPrefix] = true;
        request('/05G/e','get').then(function(result){
            var confirmationToken = result.match(/input type="hidden" name="_CONFIRMATIONTOKEN" id="_CONFIRMATIONTOKEN" value="([^"]*)"/)[1];
            return request('/05G/e?IpStartAddress=' + ipPrefix + '.0.0.0&IpEndAddress=' + (ipPrefix + 1) + '.255.255.255&save=1&_CONFIRMATIONTOKEN=' + confirmationToken,'post');
        }).then(function(result){
            console.log(ipPrefix + ' is done');
            pendingIps[ipPrefix] = false;
            var ipsLeft = pendingIps.reduce(function(sum,curVal){
                return curVal ? ++sum : sum;
            },0);
            console.log(ipsLeft + ' ips left');
            counterElement.innerText = (IP_RANGE-ipsLeft) + '/' + IP_RANGE;
            if(ipsLeft === 0) location.reload();
        });
    }

    function addMyIp(){
        request('/05G/e','get').then(function(result){
            var confirmationToken = result.match(/input type="hidden" name="_CONFIRMATIONTOKEN" id="_CONFIRMATIONTOKEN" value="([^"]*)"/)[1];
            return request('/05G/e?IpStartAddress=' + myIp + '&IpEndAddress=' + myIp + '&save=1&_CONFIRMATIONTOKEN=' + confirmationToken,'post');
        }).then(function(result){
            console.log(myIp + ' is done');
            location.reload();
        });
    }

    function request(url,method){
        method = method || 'GET';
        if(typeof GM_xmlhttpRequest === "function"){
            return new Promise(function(fulfill,reject){
                GM_xmlhttpRequest({
                    method:method,
                    url:url,
                    headers:{
                        Authorization:'Bearer ' + sid,
                        Accept:'*/*'
                    },
                    onload:function(response){
                        if( response.status.toString().indexOf('2') === 0){
                            fulfill(response.response);
                        }else{
                            reject(Error(response.statusText));
                        }
                    },
                    onerror:function(response){
                        rejected(Error("Network Error"));
                    }
                });
            });
        }
        return new Promise(function(fulfill,reject){
            var xhr = new XMLHttpRequest();
            xhr.open(method,url);
            xhr.onload = function(){
                if( xhr.status.toString().indexOf('2') === 0){
                    fulfill(xhr.response);
                }else{
                    reject(Error(xhr.statusText));
                }
            };
            xhr.onerror = function(){
                rejected(Error("Network Error"));
            };
            xhr.setRequestHeader('Authorization','Bearer ' + sid);
            xhr.send();
        });
    }
})();