package main
import (
"flag"
"fmt"
"github.com/gookit/color"
"net"
"os"
"regexp"
"strconv"
"strings"
"sync"
)
var (
port int //端口
portRange string //端口范围
parallelCounts int //并行数
)
func init() {
flag.IntVar(&port, "p", 80, "port")
flag.StringVar(&portRange, "r", "", "range ports. eg. 1-65535")
flag.IntVar(¶llelCounts, "n", 50, "parallel counts")
//给默认用法输出,加点自定义的提示
flag.Usage = func() {
fmt.Println("Usege:portscan [options] <ip> \noptions:")
flag.PrintDefaults()
}
flag.Parse()
}
func scanPort(ip net.IP, port int, wg *sync.WaitGroup, parallelChan *chan int) {
defer wg.Done()
tcpAddr := net.TCPAddr{
IP: ip,
Port: port,
}
conn, err := net.DialTCP("tcp", nil, &tcpAddr)
if err == nil {
color.Green.Println("port " + strconv.Itoa(port) + " is opening")
_ = conn.Close()
}
<-*parallelChan //通道不需要接收
}
func main() {
args := flag.Args() //获取命令行参数
if len(args) != 1 { //如果一个参数未输入
flag.Usage() //打印用法
os.Exit(1) //退出
}
//解析IP格式是否为点分十进制,获取命令行第一个参数,注意这里是不包含flag绑定的那几个参数
ip := net.ParseIP(flag.Arg(0))
if ip == nil {
fmt.Println("ip address is not a valid format,please check for this.")
os.Exit(4)
} else {
wg := sync.WaitGroup{} // 用于协程任务控制
if portRange == "" { //没指定端口范围的话,则扫描指定段端口
wg.Add(1)
parallelChan := make(chan int)
go func() {
parallelChan <- 1
}()
go scanPort(ip, port, &wg, ¶llelChan)
wg.Wait()
} else { //如果-r参数传入了
//从头匹配数字-数字的格式
matched, _ := regexp.Match(`^\d+-\d+$`, []byte(portRange))
if !matched {
fmt.Println("the ports range is not a valid format,please check for this.")
os.Exit(2)
}
ports := strings.Split(portRange, "-") //-符号分割
startPort, err1 := strconv.Atoi(ports[0]) //字符串转int
endPort, err2 := strconv.Atoi(ports[1])
if err1 != nil || err2 != nil || startPort < 1 || endPort < 2 || endPort <= startPort || parallelCounts < 1 {
flag.Usage()
os.Exit(3)
}
wg.Add(endPort - startPort + 1)
parallelChan := make(chan int, parallelCounts) // 用于控制协程数
for i := startPort; i <= endPort; i++ {
parallelChan <- 1
go scanPort(ip, i, &wg, ¶llelChan)
}
wg.Wait()
}
}
}
优化
输出加上时间戳,加入端口超范围判断。
package main
import (
"flag"
"fmt"
"net"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/gookit/color"
)
var (
port int //端口
portRange string //端口范围
parallelCounts int //并行数
)
func init() {
flag.IntVar(&port, "p", 80, "port")
flag.StringVar(&portRange, "r", "", "range ports. eg. 1-65535")
flag.IntVar(¶llelCounts, "n", 50, "parallel counts")
//给默认用法输出,加点自定义的提示
flag.Usage = func() {
fmt.Println("Usege:portscan [options] <ip> \noptions:")
flag.PrintDefaults()
}
flag.Parse()
}
func scanPort(ip net.IP, port int, wg *sync.WaitGroup, parallelChan *chan int) {
defer wg.Done()
tcpAddr := net.TCPAddr{
IP: ip,
Port: port,
}
conn, err := net.DialTCP("tcp", nil, &tcpAddr)
if err == nil {
timeStr := time.Now().Format("2006-01-02 15:04:05") //获取时间戳,这是固定写法
color.Cyan.Printf("[%s]", timeStr)
color.Green.Println(" port " + strconv.Itoa(port) + " is opening")
_ = conn.Close()
}
<-*parallelChan //通道不需要接收
}
func main() {
args := flag.Args() //获取命令行参数
if len(args) != 1 { //如果一个参数未输入
flag.Usage() //打印用法
os.Exit(1) //退出
}
//解析IP格式是否为点分十进制,获取命令行第一个参数,注意这里是不包含flag绑定的那几个参数
ip := net.ParseIP(flag.Arg(0))
if ip == nil {
color.Red.Println("ip address is not a valid format, please check for this.")
os.Exit(4)
} else {
wg := sync.WaitGroup{} // 用于协程任务控制
if portRange == "" { //没指定端口范围的话,则扫描-p参数,启用一个协程
if port > 65535 {
color.Red.Println("The port is too big, please check for this.")
os.Exit(6)
}
wg.Add(1)
parallelChan := make(chan int)
go func() {
parallelChan <- 1
}()
go scanPort(ip, port, &wg, ¶llelChan)
wg.Wait()
} else { //如果-r参数传入了
//从头匹配数字-数字的格式
matched, _ := regexp.Match(`^\d+-\d+$`, []byte(portRange))
if !matched {
color.Red.Println("the ports range is not a valid format,please check for this.")
os.Exit(2)
}
ports := strings.Split(portRange, "-") //-符号分割
startPort, err1 := strconv.Atoi(ports[0]) //字符串转int
endPort, err2 := strconv.Atoi(ports[1])
if startPort > 65535 || endPort > 65535 {
color.Red.Println("The port is too big, please check for this.")
os.Exit(3)
}
if err1 != nil || err2 != nil || startPort < 1 || endPort < 2 || endPort <= startPort || parallelCounts < 1 {
flag.Usage()
os.Exit(5)
}
wg.Add(endPort - startPort + 1)
parallelChan := make(chan int, parallelCounts) // 用于控制协程数
for i := startPort; i <= endPort; i++ {
parallelChan <- 1
go scanPort(ip, i, &wg, ¶llelChan)
}
wg.Wait()
}
}
}