Skip to content

Commit

Permalink
minor changes in api.go, checkip.go, dns.go, shodan.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Jozef Reisinger committed Oct 28, 2021
1 parent e0bbe5d commit fb8cfe9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
10 changes: 6 additions & 4 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ func makeAPIcall(apiurl string, headers map[string]string, queryParams map[strin
}

// Set query parameters.
vals := url.Values{}
for k, v := range queryParams {
vals.Add(k, v)
if len(queryParams) != 0 {
vals := url.Values{}
for k, v := range queryParams {
vals.Add(k, v)
}
apiURL.RawQuery = vals.Encode()
}
apiURL.RawQuery = vals.Encode()

req, err := http.NewRequest("GET", apiURL.String(), nil)
if err != nil {
Expand Down
15 changes: 13 additions & 2 deletions checkip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ package checkip

import (
"fmt"
"log"
"net"
"os"
"sync"
)

func init() {
log.SetFlags(0)
log.SetPrefix(os.Args[0] + ": ")
}

// Checker checks an IP address. ok is false if it considers the IP address to
// be suspicious. If the check fails (err != nil), ok must be true - presumption
// of innocence. Checker can be printed to see what it has found about the IP
Expand All @@ -25,13 +32,17 @@ func Run(checkers []Checker, ipaddr net.IP) int {
for _, checker := range checkers {
wg.Add(1)
go func(checker Checker) {
defer wg.Done()
ok, err := checker.Check(ipaddr)
if err == nil && !ok {
if err != nil {
log.Print(err)
return
}
if !ok {
mu.Lock()
suspicious++
mu.Unlock()
}
wg.Done()
}(checker)
}
wg.Wait()
Expand Down
7 changes: 3 additions & 4 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ type DNS struct {

// Check does a reverse lookup for a given IP address.
func (d *DNS) Check(ipaddr net.IP) (bool, error) {
names, err := net.LookupAddr(ipaddr.String())
if err != nil {
return true, err
}
// We are ignoring error. It says: nodename nor servname provided, or
// not known
names, _ := net.LookupAddr(ipaddr.String())
d.Names = names
return true, nil
}
Expand Down
5 changes: 3 additions & 2 deletions shodan.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ func (s *Shodan) Check(ipaddr net.IP) (bool, error) {
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return true, fmt.Errorf("calling API: %s", resp.Status)
// StatusNotFound is returned when shodan doesn't know the IP address.
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotFound {
return true, fmt.Errorf("calling %s: %s", apiURL, resp.Status)
}

if err := json.NewDecoder(resp.Body).Decode(&s); err != nil {
Expand Down

0 comments on commit fb8cfe9

Please sign in to comment.