From 7ea0198cc897d60aa0dbb41c99eeeb587129cde9 Mon Sep 17 00:00:00 2001 From: Jakub Jarosz Date: Fri, 22 Mar 2024 19:20:12 +0000 Subject: [PATCH] Add cli --- README.md | 2 +- cmd/hpot/main.go | 13 ++++++------- hpot.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2aa25d8..c58064d 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,4 @@ # hpot -`hpot` is a Go library and a CLI and TUI honeypot application. +`hpot` is a Go CLI HoneyPot application. diff --git a/cmd/hpot/main.go b/cmd/hpot/main.go index f29fba8..c488fdd 100644 --- a/cmd/hpot/main.go +++ b/cmd/hpot/main.go @@ -1,12 +1,11 @@ package main -import "github.com/qba73/hpot" +import ( + "os" -func main() { - pot := hpot.NewHoneyPotServer() - pot.Ports = []int{8081, 8082} + "github.com/qba73/hpot" +) - if err := pot.ListenAndServe(); err != nil { - panic(err) - } +func main() { + os.Exit(hpot.Main()) } diff --git a/hpot.go b/hpot.go index c02467f..39c3952 100644 --- a/hpot.go +++ b/hpot.go @@ -1,10 +1,13 @@ package hpot import ( + "flag" "fmt" "net" "net/http" "os" + "strconv" + "strings" "sync" ) @@ -90,3 +93,47 @@ func (p *Pot) serve(l net.Listener) { conn.Close() } } + +func parsePorts(ports string) ([]int, error) { + var prts []int + px := strings.Split(ports, ",") + for _, p := range px { + p = strings.TrimSpace(p) + pr, err := strconv.Atoi(p) + if err != nil { + return nil, err + } + prts = append(prts, pr) + } + return prts, nil +} + +var usage = `Usage: hpot [-v] port,port,port + +Start the HopneyPot and listen on incoming connections on provided, coma separated ports. + +In verbose mode (-v), reports all incoming connections.` + +func Main() int { + verbose := flag.Bool("v", false, "verbose output") + flag.Parse() + if len(flag.Args()) == 0 { + fmt.Println(usage) + return 0 + } + + ports, err := parsePorts(flag.Args()[0]) + if err != nil { + fmt.Println(usage) + return 1 + } + + pot := NewHoneyPotServer() + pot.Verbose = *verbose + pot.Ports = ports + + if err := pot.ListenAndServe(); err != nil { + return 1 + } + return 0 +}