Skip to content

Commit

Permalink
Better handling of server error response
Browse files Browse the repository at this point in the history
Show FAIL status if the address is listed at the provider
Show ERR status if an error occured during the lookup
  • Loading branch information
Ajnasz committed May 4, 2021
1 parent 01af805 commit 8f5c8e8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,24 @@ go build

## Execute

```sh
./dnsbl-check -i 1.2.3.4 -p providers
```

## Output

The program returns every result in a new line, fields are separated by TAB character `\t`.

The line starts with the status: `OK` or `ERR`
The line starts with the status: `OK` or `FAIL` or `ERR`
- `OK` returned if no listing found for the address
- `FAIL` returned if listing found for the address
- `ERR` returned if the address lookup failed
Second field is the address
Third field is the provider
Fourth field is filled only if the address listed at the provider. If no reason returned from te provider, the `unknown reason` text will be shown.

Fourth field is filled only if the statis is either `FAIL` or `ERR`. If the status is `FAIL` and no reason returned from te provider, the `unknown reason` text will be shown. If the status is `ERR` the error message will be shown here.

```
OK 45.95.168.196 zen.spamhaus.org
ERR 45.95.168.196 dnsbl-1.uceprotect.net IP 45.95.168.196 is UCEPROTECT-Level 1 listed. See http://www.uceprotect.net/rblcheck.php?ipr=45.95.168.196
OK 127.0.0.2 dyn.rbl.polspam.pl
FAIL 127.0.0.2 bl.spamcop.net Blocked - see https://www.spamcop.net/bl.shtml?127.0.0.2
ERR 127.0.0.2 spam.dnsbl.anonmails.de lookup 2.0.0.127.spam.dnsbl.anonmails.de on 127.0.0.53:53: server misbehaving
```
21 changes: 13 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,24 @@ type LookupResult struct {
Address string
Reason string
Provider DNSBLProvider
Error error
}

func lookup(address string, provider DNSBLProvider) LookupResult {
isListed, err := provider.IsBlacklisted(address)
if err != nil {
fmt.Println(err)
os.Exit(1)
return LookupResult{
Provider: provider,
Address: address,
Error: err,
}
}

if isListed {
desc, err := provider.GetReason(address)

if err != nil {
fmt.Println("ERROR", err.Error())
}

return LookupResult{
Error: err,
Address: address,
IsBlacklisted: true,
Provider: provider,
Expand Down Expand Up @@ -164,6 +165,10 @@ func getProviders(fn string) ([]string, error) {
}

func processLookupResult(result LookupResult) {
if result.Error != nil {
fmt.Println(fmt.Sprintf("ERR\t%s\t%s\t%s", result.Address, result.Provider.GetName(), result.Error))
return
}
if result.IsBlacklisted {
var reason string

Expand All @@ -173,7 +178,7 @@ func processLookupResult(result LookupResult) {
reason = result.Reason
}

fmt.Println(fmt.Sprintf("ERR\t%s\t%s\t%s", result.Address, result.Provider.GetName(), reason))
fmt.Println(fmt.Sprintf("FAIL\t%s\t%s\t%s", result.Address, result.Provider.GetName(), reason))
} else {
fmt.Println(fmt.Sprintf("OK\t%s\t%s", result.Address, result.Provider.GetName()))
}
Expand All @@ -187,7 +192,7 @@ func main() {
list, err := getProviders(*domainsFile)

if err != nil {
fmt.Println("Error reading domains")
fmt.Fprintln(os.Stderr, "Error reading domains")
os.Exit(1)
}

Expand Down

0 comments on commit 8f5c8e8

Please sign in to comment.