Add Allowed Sender IPs/Ranges Functionality to MailRelay #7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi Doug,
I know we had chatted a little bit about this a number of months back last year, but I finally took some time myself to try and look into the issue further.
The AllowedHosts variable didn't seem like the best place for this since it seems to serve a different purpose (if I understand it correctly, it seems to be for the hostnames that the server would respond to mainly, but wasn't the same as the "List of Allowed Sender IPs / Ranges" functionality I had been looking for).
After spending a bit of time reviewing the code I came up with the solution you can see down below.
Mainly, I'm bringing in an existing ipfilter library, and using that if the new
allowed_senders
parameter in the JSON file is populated with something other then"*"
(if it's not that, then it should be pointing to a .txt file and then each line in that text file will populate theAllowedSendersFilter
using that ipfilter Library).Since
AllowedSendersFilter
is a global variable, it can be accessed over in theclient.go
file each time the sendMail() function runs to check the client's remote IP and see if it's in the allowed list of IPs/Ranges.I've run some tests that confirmed the functionality was working as expected and then brought it over to the server I had been working on a few months back when initially trying out MailRelay and it seems to be behaving the way I need it to.
To summarize, (I didn't change the sample file in the Pull Request but I think it will still work due to the defaults) the JSON file would change to this:
And allowed_senders can be the default
"*"
option or point to a text file like"allowed_ips.txt"
(or whatever you want to name it really, so long as it has a new IP entry on each line).The
allowed_ips.txt
file on my end looks something like this (as a few examples, the localhost ones would mainly need to be included for the local testing since it might fail otherwise):I did add a CLI test option as well to be able to quickly verify if an IP would be allowed or not and that can be called by using something like this:
The way I've used these new capabilities so far has been to create about 5 different instances of MailRelay running on the same server (each on a different port of course), but they are able to share the
allowed_ips.txt
settings (so that doesn't get repeated in each JSON file) and I'm able to use different Sendgrid API Keys in each case, which helps me to keep our Sendgrid statistics segmented (so I can more easily check Sendgrid and review recent activity details in the future if needed to troubleshoot something for example).I think that should hopefully help others out that might have a similar need (and this helps us to move away from the Microsoft IIS 6 SMTP Server option and is much easier to configure / get up and running).