package main
import (
“encoding/json”
“flag”
“fmt”
“io/ioutil”
“net”
“os”
“runtime”
“strconv”
“sync”
“time”
)
type Message struct {
Port int
Status string
}
var list Message
var list2 Message
func main() {
runtime.GOMAXPROCS(1)
//fmt.Println(time.Now().Weekday())
fmt.Println("Developed By Omid For test")
ipadd := flag.String("ip", "127.0.0.1", "IP Address To scan")
protocols := flag.String("proto", "tcp", "Protocol To scan")
flag.Parse()
if *protocols == "tcp" {
fmt.Println("Scanning " + *ipadd + " TCP Ports")
tcpscanner(*ipadd)
} else if *protocols == "udp" {
fmt.Println("Scanning " + *ipadd + " UDP Ports")
udpportscanner(*ipadd)
} else {
fmt.Println("Enter Protocol by proto")
fmt.Println("example to scan file.go -ip=localhost -proto=tcp")
}
}
func tcpscanner(ip string) {
count := 0
faildcount := 0
var wg sync.WaitGroup
for i := 1; i <= 65535; i++ {
wg.Add(1)
go func(j int) {
address := net.JoinHostPort(ip, strconv.Itoa(j))
// 3 second timeout
//fmt.Println(j)
conn, err := net.DialTimeout("tcp", address, 2*time.Second)
//fmt.Println(err)
if err != nil {
faildcount++
//m2 := Message{j, "Closed"}
//list2 = append(list2, m2)
} else {
count++
conn.Close()
m := Message{j, "open"}
list = append(list, m)
}
defer wg.Done()
time.Sleep(5 * time.Second)
}(i)
}
wg.Wait()
jso, _ := json.Marshal(list)
//jso2, _ := json.Marshal(list2)
//fmt.Println(string(jso))
week := time.Now().Weekday()
timm := time.Now().Minute()
week2 := fmt.Sprint(week)
timm2 := fmt.Sprint(timm)
if _, errf := os.Stat(ip + week2 + "tcpopen.json"); errf == nil {
_ = ioutil.WriteFile(ip+week2+"tcpopen"+timm2+".json", jso, 0644)
} else {
_ = ioutil.WriteFile(ip+week2+"tcpopen.json", jso, 0644)
}
fmt.Println("success count :" + strconv.Itoa(count))
fmt.Println("faild count :" + strconv.Itoa(faildcount))
}
func udpportscanner(ip string) {
count := 0
faildcount := 0
var wg sync.WaitGroup
for i := 1; i <= 65535; i++ {
wg.Add(1)
go func(j int) {
address := net.JoinHostPort(ip, strconv.Itoa(j))
// 3 second timeout
//fmt.Println(j)
conn, err := net.DialTimeout("udp", address, 60*time.Second)
//fmt.Println(err)
if err != nil {
faildcount++
//m2 := Message{j, "Closed"}
//list2 = append(list2, m2)
} else {
count++
conn.Close()
m := Message{j, "open"}
list = append(list, m)
}
defer wg.Done()
}(i)
}
wg.Wait()
jso, _ := json.Marshal(list)
//jso2, _ := json.Marshal(list2)
//fmt.Println(string(jso))
/* _ = ioutil.WriteFile("ok2.json", jso, 0644)
_ = ioutil.WriteFile("closed2.json", jso2, 0644)*/
fmt.Println("success count :" + strconv.Itoa(count))
fmt.Println("faild count :" + strconv.Itoa(faildcount))
week := time.Now().Weekday()
timm := time.Now().Minute()
week2 := fmt.Sprint(week)
timm2 := fmt.Sprint(timm)
if _, errf := os.Stat(ip + week2 + "udpopen.json"); errf == nil {
_ = ioutil.WriteFile(ip+week2+"udpopen"+timm2+".json", jso, 0644)
} else {
_ = ioutil.WriteFile(ip+week2+"udpopen.json", jso, 0644)
}
/* if _, errf := os.Stat(ip + week2 + "udpclose.json"); errf == nil {
_ = ioutil.WriteFile(ip+week2+"udpclose"+timm2+".json", jso2, 0644)
} else {
_ = ioutil.WriteFile(ip+week2+"udpclose.json", jso2, 0644)
} */
}
use this code and show for example use go run ./filename.go -ip “127.0.0.1” -proto “udp”