golang-exercises/dragon-scan/main.go

106 lines
1.7 KiB
Go

package main
import (
"fmt"
"log"
"net"
"os"
)
var (
Args []string
ExampleFlag1 bool
ipToScan string
portToScan string
)
func main() {
if len(os.Args) == 1 {
showHelp()
os.Exit(0)
}
storeArgs()
Loop:
for i, v := range Args {
switch v {
case "--help", "-h":
showHelp()
os.Exit(0)
case "--port", "-p":
portToScan = Args[i+1]
default:
if ip := findIP(v); ip != "" {
ipToScan = ip
continue Loop
}
if Args[i-1] == "--port" || Args[i-1] == "-p" {
continue Loop
}
fmt.Printf("Invalid parameter: \"%v\"\n", v)
os.Exit(0)
}
}
if ipToScan != "" {
if portToScan != "" {
if e := scanPort(); e != nil {
log.Fatal(e)
}
} else {
if e := scanPorts(); e != nil {
log.Fatal(e)
}
}
} else {
fmt.Println("no ip provided")
}
}
func findIP(s string) string {
addr := net.ParseIP(s)
if addr != nil {
return s
}
return ""
}
func scanPort() error {
fmt.Printf("Starting dragon-scan 0.0.1\n")
_, err := net.Dial("tcp", ipToScan+":"+portToScan)
if err == nil {
}
fmt.Printf("dragon-scan report for %v\n", ipToScan)
return nil
}
func scanPorts() error {
fmt.Printf("Starting dragon-scan 0.0.1\n")
var successful []int
for port := 0; port <= 3306; port++ {
_, err := net.Dial("tcp", ipToScan+":"+string(port))
if err == nil {
successful = append(successful, port)
}
}
fmt.Printf("dragon-scan report for %v\n", ipToScan)
showPorts(successful)
return nil
}
func showPorts(list []int) {
for _, port := range list {
fmt.Println(port)
}
}
func showHelp() {
fmt.Printf("This is where I would show the help, IF I HAD ANY!\n")
}
func storeArgs() {
for i := 1; i < len(os.Args); i++ {
Args = append(Args, os.Args[i])
}
}