Skip to content

Commit

Permalink
Merge pull request #11 from xs25cn/feature/v2.0
Browse files Browse the repository at this point in the history
Feature/v2.0
  • Loading branch information
duzhenxun authored Feb 2, 2023
2 parents 11f0d87 + 8379174 commit 091094f
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 67 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
先来体验一下运行后的效果:

### v1.0版
纯命令行执行
![image.png](https://upload-images.jianshu.io/upload_images/19018717-e8826aa18d92aa57.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![image.png](https://upload-images.jianshu.io/upload_images/19018717-f62194125b0a4a5b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
v1.0版下载地址:https://github.com/xs25cn/scanPort/releases/tag/v1.0


### v2.0版
改版后直接浏览器内操作即可
![](https://tva1.sinaimg.cn/large/008i3skNly1gt4rg43sqgj30iq0h7aay.jpg)

### v2.1版
修复bug

### 安装使用
你可以直接下载已编译好的文件直接运行
https://github.com/xs25cn/scanPort/tree/master/bin


43 changes: 19 additions & 24 deletions app/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ type ScanIp struct {
process int
}

func NewScanIp(timeout int,process int,debug bool) *ScanIp {
func NewScanIp(timeout int, process int, debug bool) *ScanIp {
return &ScanIp{
debug:debug,
timeout:timeout,
process:process,
debug: debug,
timeout: timeout,
process: process,
}
}

//获取开放端口号
func (s *ScanIp) GetIpOpenPort(ip string, port string,wsConn *wsConn.WsConnection) []int {
func (s *ScanIp) GetIpOpenPort(ip string, port string, wsConn *wsConn.WsConnection) []int {
var (
total int
pageCount int
Expand All @@ -46,8 +46,7 @@ func (s *ScanIp) GetIpOpenPort(ip string, port string,wsConn *wsConn.WsConnectio
}
num = int(math.Ceil(float64(total) / float64(pageCount)))

s.sendLog(fmt.Sprintf("【%v】需要扫描端口总数:%v 个,总协程:%v 个,并发:%v 个,超时:%d 毫秒",ip, total, pageCount, num, s.timeout),wsConn)

s.sendLog(fmt.Sprintf("【%v】需要扫描端口总数:%v 个,总协程:%v 个,并发:%v 个,超时:%d 毫秒", ip, total, pageCount, num, s.timeout), wsConn)

start := time.Now()
all := map[int][]int{}
Expand All @@ -70,21 +69,22 @@ func (s *ScanIp) GetIpOpenPort(ip string, port string,wsConn *wsConn.WsConnectio
opened := s.isOpen(ip, value[i])
if opened {
tmpPorts = append(tmpPorts, value[i])
s.sendLog(fmt.Sprintf("【%v】端口:%v ............... 开放 ............... ", ip, value[i]),wsConn)
}else{
//s.sendLog(fmt.Sprintf("【%v】端口:%v 关闭", ip, value[i]),wsConn)
s.sendLog(fmt.Sprintf("【%v】端口:%v ............... 开放 ............... ", ip, value[i]), wsConn)
} else {
//s.sendLog(fmt.Sprintf("【%v】端口:%v 关闭", ip, value[i]), wsConn)
fmt.Println(fmt.Sprintf("【%v】端口:%v 关闭", ip, value[i]))
}
}
mutex.Lock()
openPorts = append(openPorts, tmpPorts...)
mutex.Unlock()
if len(tmpPorts) > 0 {
s.sendLog(fmt.Sprintf("【%v】协程%v 执行完成,时长: %.3fs,开放端口: %v", ip, key, time.Since(start).Seconds(), tmpPorts),wsConn)
s.sendLog(fmt.Sprintf("【%v】协程%v 执行完成,时长: %.3fs,开放端口: %v", ip, key, time.Since(start).Seconds(), tmpPorts), wsConn)
}
}(v, k)
}
wg.Wait()
s.sendLog(fmt.Sprintf("【%v】^_^扫描结束,执行时长%.3fs , 所有开放的端口:%v",ip, time.Since(start).Seconds(), openPorts),wsConn)
s.sendLog(fmt.Sprintf("【%v】^_^扫描结束,执行时长%.3fs , 所有开放的端口:%v", ip, time.Since(start).Seconds(), openPorts), wsConn)
return openPorts
}

Expand Down Expand Up @@ -125,17 +125,14 @@ func (s *ScanIp) GetAllIp(ip string) ([]string, error) {
return ips, nil
}



//记录日志
func (s *ScanIp) sendLog(str string,wsConn *wsConn.WsConnection) {
//if s.debug == true {
//fmt.Println(str)
wsConn.WriteMessage(1,[]byte(str))
//}
func (s *ScanIp) sendLog(str string, wsConn *wsConn.WsConnection) {
if s.debug == true {
fmt.Println(str)
wsConn.WriteMessage(1, []byte(str))
}
}


//获取所有端口
func (s *ScanIp) getAllPort(port string) ([]int, error) {
var ports []int
Expand Down Expand Up @@ -177,14 +174,12 @@ func (s *ScanIp) filterPort(str string) (int, error) {
return port, nil
}



//查看端口号是否打开
func (s *ScanIp) isOpen(ip string, port int) bool {
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), time.Millisecond*time.Duration(s.timeout))
if err != nil {
if strings.Contains(err.Error(),"too many open files"){
fmt.Println("连接数超出系统限制!"+err.Error())
if strings.Contains(err.Error(), "too many open files") {
fmt.Println("连接数超出系统限制!" + err.Error())
os.Exit(1)
}
return false
Expand Down
10 changes: 6 additions & 4 deletions app/ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<div class="row">
<div class="col-xs-12 col-md-12" id="app">
<h2>
小手端口扫描工具 v2.0
小手端口扫描工具 v2.1
<mark>{{httpStatus}}</mark>
</h2>

Expand Down Expand Up @@ -98,12 +98,12 @@ <h5>
},
//下载数据
down: function (str, e) {

console.log(str)
//先建立一个webscoket
this.webSocketTest();

const _this = this
const server_url = "http://" + this.httpServer + ":" + this.getPort() + "/run"
const server_url = "http://" + this.httpServer + ":" + this.getPort() + "/run?ip="+this.ip+"&port="+this.port
console.log(server_url)
axios({
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
Expand All @@ -115,6 +115,7 @@ <h5>
port: this.port
}
}).then(function (response) {
console.log(response)
_this.httpStatus = response.data.msg;
});
},
Expand All @@ -126,6 +127,7 @@ <h5>
_this.message = "建立连接"
};
ws.onmessage = function (evt) {
console.log("=============")
console.log(evt.data)
_this.runText += evt.data + "\n"
let r = _this.$refs.run
Expand Down
Binary file removed bin/linux/scanPort_linux
Binary file not shown.
Binary file added bin/v1/scanPort_linux
Binary file not shown.
Binary file added bin/v1/scanPort_mac
Binary file not shown.
Binary file added bin/v1/scanPort_win.exe
Binary file not shown.
Binary file added bin/v2/scanPort_linux
Binary file not shown.
Binary file renamed bin/mac/scanPort_mac → bin/v2/scanPort_mac
Binary file not shown.
Binary file renamed bin/win/scanPort_win.exe → bin/v2/scanPort_win.exe
Binary file not shown.
75 changes: 36 additions & 39 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"flag"
"fmt"
"github.com/gorilla/websocket"
"log"
"net"
"net/http"
Expand All @@ -19,11 +18,13 @@ import (
"scanPort/app/wsConn"
"strconv"
"time"

"github.com/gorilla/websocket"
)

var (
wConn *wsConn.WsConnection
osBase string
wConn *wsConn.WsConnection
osBase string
version string
)

Expand All @@ -32,15 +33,17 @@ func main() {
port = flag.Int("port", 25252, "端口号")
h = flag.Bool("h", false, "帮助信息")
)
version = "v2.0"
version = "v2.1"
flag.Parse()
//帮助信息
if *h == true {
usage("scanPort version: scanPort/v2.0\n Usage: scanPort [-h] [-ip ip地址] [-n 进程数] [-p 端口号范围] [-t 超时时长] [-path 日志保存路径]\n\nOptions:\n")
usage("scanPort version: scanPort/v2.1\n Usage: scanPort [-h] [-ip ip地址] [-n 进程数] [-p 端口号范围] [-t 超时时长] [-path 日志保存路径]\n\nOptions:\n")
return
}
serverUri := "https://ip.xs25.cn/?p=" + strconv.Itoa(*port)
openErr := open(serverUriOsInfo(serverUri))
serverUri := "https://ip.xs25.cn"
//serverUri:=fmt.Sprintf("http://127.0.0.1:%d",*port)
//openErr := open(serverUriOsInfo(serverUri))
openErr := open(serverUri)
if openErr != nil {
fmt.Println(openErr, serverUri)
}
Expand All @@ -57,53 +60,47 @@ func main() {

//首页
func indexHandle(w http.ResponseWriter, r *http.Request) {
s := "小手端口扫描 "+version+" (by:Duzhenxun)"
s := "小手端口扫描 " + version + " (by:Duzhenxun)"
w.Write([]byte(s))
//http.FileServer(http.Dir("app/ui"))
}

//运行
func runHandle(w http.ResponseWriter, r *http.Request) {
resp := map[string]interface{}{
"code": 200,
"msg": "ok",
}

w.Header().Set("Access-Control-Allow-Origin", "*") //允许访问所有域
w.Header().Add("Access-Control-Allow-Headers", "Content-Type") //header的类型
w.Header().Set("content-type", "application/json") //返回数据格式是json
query := r.URL.Query()
scanIp := query.Get("ip")
scanPort := query.Get("port")
if scanIp == "" || scanPort == "" {
resp["code"] = 201
resp["msg"] = "ip或端口号不能为空"

resp := map[string]interface{}{
"code": 200,
"msg": "ok",
b, _ := json.Marshal(resp)
w.Write(b)
return
}
decoder := json.NewDecoder(r.Body)
type Params struct {
Ip string `json:"ip"`
Port string `json:"port"`
Process int `json:"process"`
Timeout int `json:"timeout"`
Debug int `json:"debug"`
}
var params Params
decoder.Decode(&params)
if params.Ip == "" {
resp["code"] = 201
resp["msg"] = "缺少字段 ip"
b, _ := json.Marshal(resp)
w.Write(b)
return
}

if params.Port == "" {
params.Port = "80"
}
if params.Process == 0 {
params.Process = 10
params := Params{
Ip: scanIp,
Port: scanPort,
Process: 10,
Timeout: 100,
Debug: 1,
}
if params.Timeout == 0 {
params.Timeout = 100
}
debug := false
if params.Debug == 0 {
params.Debug = 1
debug = true
}

debug := true
//初始化
scanIP := scan.NewScanIp(params.Timeout, params.Process, debug)
ips, err := scanIP.GetAllIp(params.Ip)
Expand All @@ -115,7 +112,7 @@ func runHandle(w http.ResponseWriter, r *http.Request) {
filePath, _ := mkdir("log")
fileName := filePath + params.Ip + "_port.txt"
for i := 0; i < len(ips); i++ {
ports := scanIP.GetIpOpenPort(ips[i], params.Port,wConn)
ports := scanIP.GetIpOpenPort(ips[i], params.Port, wConn)
if len(ports) > 0 {
f, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
Expand All @@ -124,7 +121,7 @@ func runHandle(w http.ResponseWriter, r *http.Request) {
}
continue
}
if _, err := f.WriteString(fmt.Sprintf("%v【%v】开放:%v \n", time.Now().Format("2006-01-02 15:04:05"),ips[i], ports)); err != nil {
if _, err := f.WriteString(fmt.Sprintf("%v【%v】开放:%v \n", time.Now().Format("2006-01-02 15:04:05"), ips[i], ports)); err != nil {
if err := f.Close(); err != nil {
fmt.Println(err)
}
Expand Down Expand Up @@ -168,7 +165,7 @@ func wsHandle(w http.ResponseWriter, r *http.Request) {
}

//系统信息
func serverUriOsInfo(serverUri string) string{
func serverUriOsInfo(serverUri string) string {
osInfo := map[string]interface{}{}
osInfo["version"] = version
osInfo["os"] = runtime.GOOS
Expand Down

0 comments on commit 091094f

Please sign in to comment.