Skip to content

Commit

Permalink
feat: enhance port scanning with HTTP check for common web ports
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Dec 1, 2024
1 parent 2acc5ac commit 356e5bd
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions pkg/stage/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,40 @@ package stage
import (
"fmt"
"net"
"net/http"
"time"
)

func ScanTCPPort(ip string, port int) bool {
target := fmt.Sprintf("%s:%d", ip, port)
conn, err := net.DialTimeout("tcp", target, 2*time.Second)
if err == nil {
defer conn.Close()
return true
}

// 检查是否是常见的 HTTP 端口
commonHTTPPorts := []int{80, 443, 8080, 8443, 4000, 8000, 8888}
for _, httpPort := range commonHTTPPorts {
if port == httpPort {
return ScanHTTPPort(ip, port)
}
}

return false
}

func ScanHTTPPort(ip string, port int) bool {
target := fmt.Sprintf("http://%s:%d", ip, port)
client := &http.Client{
Timeout: 2 * time.Second,
}

resp, err := client.Head(target)
if err != nil {
fmt.Println(err)
return false
}
defer conn.Close()
defer resp.Body.Close()
return true
}

Expand Down

0 comments on commit 356e5bd

Please sign in to comment.