-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add REST API and refactor directory structure
- Loading branch information
Jozef Reisinger
committed
Nov 10, 2021
1 parent
9888383
commit 261a2c4
Showing
22 changed files
with
186 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"strings" | ||
|
||
checkip "github.com/jreisinger/checkip/pkg" | ||
) | ||
|
||
type Checkers []checkip.Checker | ||
|
||
func (c Checkers) Handler(w http.ResponseWriter, r *http.Request) { | ||
ip := strings.Split(r.URL.Path, "/")[3] | ||
netip := net.ParseIP(ip) | ||
if netip == nil { | ||
w.WriteHeader(http.StatusNotFound) | ||
fmt.Fprintf(w, "wrong IP address: %q", ip) | ||
return | ||
} | ||
results := checkip.Run(c, netip) | ||
b, err := checkip.JSON(results) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
fmt.Fprintf(w, "marshalling JSON: %v", err) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
fmt.Fprintf(w, "%s", b) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Checkip quickly finds information about an IP address from a CLI. | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"sort" | ||
|
||
checkip "github.com/jreisinger/checkip/pkg" | ||
"github.com/logrusorgru/aurora" | ||
) | ||
|
||
type byName []checkip.Result | ||
|
||
func (x byName) Len() int { return len(x) } | ||
func (x byName) Less(i, j int) bool { return x[i].Name < x[j].Name } | ||
func (x byName) Swap(i, j int) { x[i], x[j] = x[j], x[i] } | ||
|
||
// Print prints condensed results to stdout. | ||
func Print(results []checkip.Result) error { | ||
sort.Sort(byName(results)) | ||
|
||
var malicious, totalSec float64 | ||
for _, r := range results { | ||
if r.Err != nil { | ||
log.Print(r.ErrMsg) | ||
} | ||
if r.Type == "Info" || r.Type == "InfoSec" { | ||
fmt.Printf("%-15s %s\n", r.Name, r.Info) | ||
} | ||
if r.Type == "Sec" || r.Type == "InfoSec" { | ||
totalSec++ | ||
if r.IsMalicious { | ||
malicious++ | ||
} | ||
} | ||
} | ||
probabilityMalicious := malicious / totalSec | ||
|
||
var msg string | ||
switch { | ||
case probabilityMalicious <= 0.15: | ||
msg = fmt.Sprint(aurora.Green("Malicious")) | ||
case probabilityMalicious <= 0.50: | ||
msg = fmt.Sprint(aurora.Yellow("Malicious")) | ||
default: | ||
msg = fmt.Sprint(aurora.Red("Malicious")) | ||
} | ||
|
||
_, err := fmt.Printf("%s\t%.0f%% (%d/%d)\n", msg, probabilityMalicious*100, int(malicious), int(totalSec)) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"net" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/jreisinger/checkip/api" | ||
"github.com/jreisinger/checkip/cmd" | ||
checkip "github.com/jreisinger/checkip/pkg" | ||
) | ||
|
||
var s = flag.Bool("s", false, "serve JSON API") | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
checkers := []checkip.Checker{ | ||
&checkip.AS{}, | ||
&checkip.AbuseIPDB{}, | ||
&checkip.Blocklist{}, | ||
&checkip.CINSArmy{}, | ||
&checkip.DNS{}, | ||
&checkip.Geo{}, | ||
&checkip.IPsum{}, | ||
&checkip.OTX{}, | ||
&checkip.Shodan{}, | ||
&checkip.ThreatCrowd{}, | ||
&checkip.VirusTotal{}, | ||
} | ||
|
||
if *s { | ||
c := api.Checkers(checkers) | ||
http.HandleFunc("/api/v1/", c.Handler) | ||
log.Fatal(http.ListenAndServe(":8000", nil)) | ||
} else { | ||
|
||
log.SetFlags(0) | ||
log.SetPrefix(os.Args[0] + ": ") | ||
|
||
if len(flag.Args()) != 1 { | ||
log.Fatal("missing IP address") | ||
} | ||
|
||
ipaddr := net.ParseIP(flag.Arg(0)) | ||
if ipaddr == nil { | ||
log.Fatalf("wrong IP address: %s\n", flag.Arg(0)) | ||
} | ||
|
||
results := checkip.Run(checkers, ipaddr) | ||
|
||
cmd.Print(results) | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.