Skip to content

Commit

Permalink
Better reports
Browse files Browse the repository at this point in the history
  • Loading branch information
alex27riva committed Oct 20, 2024
1 parent 4dcb2a8 commit 36dab84
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
27 changes: 21 additions & 6 deletions cmd/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package cmd
import (
"fmt"
"github.com/fatih/color"
"github.com/rodaine/table"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
Expand All @@ -17,6 +18,9 @@ import (
"soc-cli/internal/util"
)

var reportLimit = 3
var reportMaxLen = 40

func analyzeIP(ip string) {

// Validate provided IP address
Expand Down Expand Up @@ -84,15 +88,26 @@ func analyzeIP(ip string) {

// Print the individual reports if available
if len(abuseIPDBData.Data.Reports) > 0 {
fmt.Println("\nReports:")
for _, report := range abuseIPDBData.Data.Reports {
fmt.Printf("Reported By: %s\nReported At: %s\nComment: %s\n",
report.ReporterCountry, report.ReportedAt, report.Comment)
fmt.Println("Reports:")
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()

tbl := table.New("Date", "Country", "Comment")
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)

for index, report := range abuseIPDBData.Data.Reports {
if index > reportLimit {
break
}
humanTime, _ := util.HumanReadableDate(report.ReportedAt)
tbl.AddRow(humanTime, report.ReporterCountry, util.ShortStr(report.Comment, reportMaxLen))
}
} else {
fmt.Println("No reports found for this IP.")
tbl.Print()

}

} else {
fmt.Println("No reports found for this IP.")
}

}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/rodaine/table v1.3.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand All @@ -26,13 +27,17 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rodaine/table v1.3.0 h1:4/3S3SVkHnVZX91EHFvAMV7K42AnJ0XuymRR2C5HlGE=
github.com/rodaine/table v1.3.0/go.mod h1:47zRsHar4zw0jgxGxL9YtFfs7EGN6B/TaS+/Dmk4WxU=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
2 changes: 1 addition & 1 deletion internal/apis/abuseipdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"soc-cli/internal/util"
)

const abuseAPIURL = "https://api.abuseipdb.com/api/v2/check?ipAddress=%s&maxAgeInDays=90"
const abuseAPIURL = "https://api.abuseipdb.com/api/v2/check?ipAddress=%s&maxAgeInDays=90&verbose"

type abuseIPDBResponse struct {
Data struct {
Expand Down
46 changes: 46 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ See the LICENSE file for details.
*/
package util

import (
"fmt"
"time"
)

// Remove duplicates from a slice
func RemoveDuplicates(items []string) []string {
uniqueItems := make(map[string]bool)
Expand All @@ -18,3 +23,44 @@ func RemoveDuplicates(items []string) []string {
}
return result
}

// Shorten string to given length
func ShortStr(s string, maxLength int) string {
if len(s) > maxLength {
return s[:maxLength]
}
return s
}

func HumanReadableDate(dateString string) (string, error) {
layout := time.RFC3339
parsedTime, err := time.Parse(layout, dateString)
if err != nil {
return "", err
}

now := time.Now()
diff := now.Sub(parsedTime)
return formatDuration(diff), nil
}

func formatDuration(d time.Duration) string {
days := int(d.Hours() / 24)
weeks := days / 7

if weeks > 0 {
return fmt.Sprintf("%d weeks ago", weeks)
}
if days > 0 {
return fmt.Sprintf("%d days ago", days)
}
hours := int(d.Hours())
if hours > 0 {
return fmt.Sprintf("%d hours ago", hours)
}
minutes := int(d.Minutes())
if minutes > 0 {
return fmt.Sprintf("%d minutes ago", minutes)
}
return "just now"
}

0 comments on commit 36dab84

Please sign in to comment.