-
Notifications
You must be signed in to change notification settings - Fork 130
/
main.go
223 lines (210 loc) · 5.33 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"scanPort/app/scan"
"scanPort/app/wsConn"
"strconv"
"time"
"github.com/gorilla/websocket"
)
var (
wConn *wsConn.WsConnection
osBase string
version string
)
func main() {
var (
port = flag.Int("port", 25252, "端口号")
h = flag.Bool("h", false, "帮助信息")
)
version = "v2.1"
flag.Parse()
//帮助信息
if *h == true {
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"
//serverUri:=fmt.Sprintf("http://127.0.0.1:%d",*port)
//openErr := open(serverUriOsInfo(serverUri))
openErr := open(serverUri)
if openErr != nil {
fmt.Println(openErr, serverUri)
}
//绑定路由地址
http.HandleFunc("/", indexHandle)
http.HandleFunc("/run", runHandle)
http.HandleFunc("/ws", wsHandle)
//启动服务端口
addr := ":" + strconv.Itoa(*port)
log.Println(" ^_^ 服务已启动...")
http.ListenAndServe(addr, nil)
}
//首页
func indexHandle(w http.ResponseWriter, r *http.Request) {
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或端口号不能为空"
b, _ := json.Marshal(resp)
w.Write(b)
return
}
type Params struct {
Ip string `json:"ip"`
Port string `json:"port"`
Process int `json:"process"`
Timeout int `json:"timeout"`
Debug int `json:"debug"`
}
params := Params{
Ip: scanIp,
Port: scanPort,
Process: 10,
Timeout: 100,
Debug: 1,
}
debug := true
//初始化
scanIP := scan.NewScanIp(params.Timeout, params.Process, debug)
ips, err := scanIP.GetAllIp(params.Ip)
if err != nil {
wConn.WriteMessage(1, []byte(fmt.Sprintf(" ip解析出错.... %v", err.Error())))
return
}
//扫所有的ip
filePath, _ := mkdir("log")
fileName := filePath + params.Ip + "_port.txt"
for i := 0; i < len(ips); i++ {
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 {
if err := f.Close(); err != nil {
fmt.Println(err)
}
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.Close(); err != nil {
fmt.Println(err)
}
continue
}
}
}
open(fileName)
b, _ := json.Marshal(resp)
w.Write(b)
return
}
//ws服务
func wsHandle(w http.ResponseWriter, r *http.Request) {
wsUp := websocket.Upgrader{
HandshakeTimeout: time.Second * 5,
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
EnableCompression: false,
}
wsSocket, err := wsUp.Upgrade(w, r, nil)
if err != nil {
fmt.Println(err)
}
wConn = wsConn.New(wsSocket)
for {
data, err := wConn.ReadMessage()
if err != nil {
wConn.Close()
return
}
if err := wConn.WriteMessage(data.MessageType, data.Data); err != nil {
wConn.Close()
return
}
}
}
//系统信息
func serverUriOsInfo(serverUri string) string {
osInfo := map[string]interface{}{}
osInfo["version"] = version
osInfo["os"] = runtime.GOOS
osInfo["cpu"] = runtime.NumCPU()
addrArr, _ := net.InterfaceAddrs()
osInfo["addr"] = fmt.Sprint(addrArr)
osInfo["time"] = time.Now().Unix()
osInfos, _ := json.Marshal(osInfo)
osBase = base64.StdEncoding.EncodeToString(osInfos)
token := hmacSha256(osBase, "dzx")
serverUri += "Z00X" + base64.StdEncoding.EncodeToString([]byte("&os_base||"+osBase+"&token||"+token))
return serverUri
}
func usage(str string) {
fmt.Fprintf(os.Stderr, str)
flag.PrintDefaults()
}
func mkdir(path string) (string, error) {
delimiter := "/"
dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
filePtah := dir + delimiter + path + delimiter
err := os.MkdirAll(filePtah, 0777)
if err != nil {
return "", err
}
return filePtah, nil
}
func open(uri string) error {
var commands = map[string]string{
"windows": "start",
"darwin": "open",
"linux": "xdg-open",
}
run, ok := commands[runtime.GOOS]
if !ok {
return fmt.Errorf("%s platform ???", runtime.GOOS)
}
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/c", "start ", uri)
//cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
} else {
cmd = exec.Command(run, uri)
}
return cmd.Start()
}
func hmacSha256(src string, secret string) string {
h := hmac.New(sha256.New, []byte(secret))
h.Write([]byte(src))
shaStr := fmt.Sprintf("%x", h.Sum(nil))
//shaStr:=hex.EncodeToString(h.Sum(nil))
return base64.StdEncoding.EncodeToString([]byte(shaStr))
}