import ip from "ip"; export type IPOrSubnet = ip.SubnetInfo | string; export type SPFCheckResult = "pass" | "fail" | "soft-fail" | "none"; export type SPFCheck = (ipAddress: string) => "pass" | "fail" | "soft-fail" | "none"; export class SPFRecords { public fail = false; public softFail = false; private ips: IPOrSubnet[] = []; public appendIPs(ips: string[]) { this.ips.push(...ips); return this; } public appendSubNet(ip4) { this.ips.push(ip.cidrSubnet(ip4)); return this; } public include(include: SPFRecords) { this.ips.push(...include.ips); return this; } public toString() { return `${this.ips.join("\n")}`; } public check(ipAddress: string): SPFCheckResult { for (const iterator of this.ips) { if (typeof iterator === "string") { if (ipAddress === iterator) { return "pass"; } continue; } if (iterator.contains(ipAddress)) { return "pass"; } } return this.fail ? "fail" : (this.softFail ? "soft-fail" : "none"); } }