- Create a new Google Drive Script [Howto].
- Paste in contents of code.gs and execute function setup().
- Modify contents of gmailfilter.xml to match your domain.
- Go to email settings and import gmailfilter.xml.
- Setup time based trigger (I recommend at the very least every 10 minutes. I have mine set to 1 minute.)
- That's it! (Monitor inbox for success)
Created
May 29, 2013 18:49
-
-
Save Mteigers/5672731 to your computer and use it in GitHub Desktop.
Google App Script to allow other users in your organization (or elsewhere) to whitelist IP addresses that may have been blocked by CloudFlare's firewall.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* CloudFlare Whitelister by Peter Olds | |
* | |
* Peter Olds for Kyäni, Inc. (c) 2013 | |
* | |
**/ | |
var INCOMING_LABEL = 'CloudFlare Whitelist Request'; // This is the label that the script loops through. | |
var ARCHIVE_REQUESTS = true; // Would you like to save IP Allow requests? | |
var COMPLETE_LABEL = '[COMPLETE] ' + INCOMING_LABEL; // Once a request is complete if ARCHIVE_REQUESTS is true it is assigned this label. | |
var ALLOW_FROM_EXTERNAL = true; // For security reasons you may not want people outside your organization whitelisting IP's (if you use the provided Gmail Filter this is handled automatically) | |
// Needed CloudFlare API Information | |
var CLOUDFLARE_EMAIL = '[email protected]'; // The email you use to login to CloudFlare. | |
var CLOUDFLARE_TKN = 'yourapikey'; // Your CloudFlare API Key (found in your account page) | |
var CLOUDFLARE_URL = 'https://www.cloudflare.com/api_json.html'; | |
// Don't modify | |
var DOMAIN_EXT = Session.getActiveUser().getEmail().split('@'); | |
function setup() { | |
GmailApp.createLabel(INCOMING_LABEL); | |
if(ARCHIVE_REQUESTS) { | |
GmailApp.createLabel(COMPLETE_LABEL); | |
} | |
} | |
function sendCloudflareRequest( ip ) | |
{ | |
var response = undefined; | |
var payload = | |
{ | |
'a' : 'wl', | |
'tkn' : CLOUDFLARE_TKN, | |
'email': CLOUDFLARE_EMAIL, | |
'key' : ip | |
}; | |
var options = | |
{ | |
'method' : 'post', | |
'payload': payload | |
}; | |
var res = UrlFetchApp.fetch(CLOUDFLARE_URL, options); | |
if( res.getResponseCode() == 200 ) { | |
response = "- <b>" + ip + "</b> has been added to the CloudFlare whitelist.\n<br />"; | |
} else { | |
response = res.getContentText(); | |
} | |
return response; | |
} | |
function checkExternals(email) | |
{ | |
var ext = email.split('@')[1]; | |
if( ext !== DOMAIN_EXT ) { | |
return false; | |
} | |
return true; | |
} | |
function monitorEmails() { | |
var regex = /([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/g; | |
var xLabel = GmailApp.getUserLabelByName(INCOMING_LABEL); | |
var complete = GmailApp.getUserLabelByName(COMPLETE_LABEL); | |
var toDos = xLabel.getThreads(); | |
for(var i in toDos) { | |
var response = ''; | |
var toDo = toDos[i]; | |
var email = toDo.getMessages()[0]; | |
var subject = email.getSubject(); | |
if(!ALLOW_FROM_EXTERNAL) { | |
if(!checkExternals(email.getFrom())) { | |
continue; | |
} | |
} | |
var matches = subject.match( regex ); | |
for( var a in matches ) | |
{ | |
var thisIP = matches[a]; | |
if( thisIP === undefined ) continue; | |
var res = sendCloudflareRequest( thisIP ); | |
if( res !== undefined ) | |
response += res; | |
} | |
if( response !== '' ) { | |
response += "<br /><br />\n\n Thank you! <strong>PLEASE DO NOT RESPOND TO THIS EMAIL!</strong>"; | |
email.replyAll( '', { | |
htmlBody: response, | |
noReply: true | |
}); | |
} | |
xLabel.removeFromThread(toDos[i]); | |
if(ARCHIVE_REQUESTS) { | |
toDos[i].markRead(); | |
toDos[i].addLabel(complete); | |
toDos[i].moveToArchive(); | |
} else { | |
toDos[i].moveToTrash(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'> | |
<title>Mail Filters</title> | |
<id>tag:mail.google.com,2008:filters:1369850215536</id> | |
<updated>2013-05-29T17:58:31Z</updated> | |
<author> | |
<name>Peter Olds</name> | |
<email>[email protected]</email> | |
</author> | |
<entry> | |
<category term='filter'></category> | |
<title>Mail Filter</title> | |
<id>tag:mail.google.com,2008:filter:1369850215536</id> | |
<updated>2013-05-29T17:58:31Z</updated> | |
<content></content> | |
<apps:property name='from' value='*@yourdomain.com -noreply'/> | |
<apps:property name='to' value='[email protected],'/> | |
<apps:property name='subject' value='*allow*'/> | |
<apps:property name='label' value='CloudFlare Whitelist Request'/> | |
<apps:property name='shouldMarkAsRead' value='true'/> | |
<apps:property name='shouldArchive' value='true'/> | |
</entry> | |
</feed> |
- Send an email to [email protected]
- If using provided Gmail Filter follow format for proper subject: Allow 127.0.0.1 192.168.0.1 (Replace with IP's that need unblocking.)
- Email body is not needed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment