Mika56 / PHP-SPF-Check

Simple library to check an IP address against a domain's SPF record
MIT License
44 stars 25 forks source link

manuel check #40

Open malohtie opened 3 years ago

malohtie commented 3 years ago

Hi, is it possible to check manuelly the spf by comparing string and ip and get result if pass or fail or none ...

exemple :

check
v=spf1 ip4:35.190.247.0/24 ip4:64.233.160.0/19 ip4:66.102.0.0/20 ip4:66.249.80.0/20 ip4:72.14.192.0/18 ip4:74.125.0.0/16 ip4:108.177.8.0/21 ip4:173.194.0.0/16 ip4:209.85.128.0/17 ip4:216.58.192.0/19 ip4:216.239.32.0/19 ~all

against 

108.177.8.0

should return SPF PASS
Mika56 commented 3 years ago

Hi, Quick and dirty solution: create a class implementing DNSRecordGetterInterface, or extend DNSRecordGetter to override a domain:

<?php
use Mika56\SPFCheck\DNSRecordGetter;
use Mika56\SPFCheck\SPFCheck;
class DNSRecordGetterOverride extends DNSRecordGetter
{
    private $spfRecord;
    public function __construct(string $spfRecord)
    {
        $this->spfRecord = $spfRecord;
    }

    public function getSPFRecordForDomain($domain)
    {
        if($domain === 'example.com') {
            return $this->spfRecord;
        }
        return parent::getSPFRecordForDomain($domain);
    }
}

$checker = new SPFCheck(new DNSRecordGetterOverride('v=spf1 ip4:35.190....'));
var_dump($checker->isIPAllowed('108.177.8.0', 'example.com'));

I'm keeping your issue opened because that's a feature that's relevant to add