-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.go
executable file
·150 lines (122 loc) · 2.51 KB
/
common.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
package main
import (
"bufio"
"io"
"log"
"net"
"os"
"strconv"
"strings"
)
func IPParse(ipstr string) []string{
var iplist []string
//支持逗号分隔
ips:=strings.Split(ipstr,",")
for _,ip_temp :=range ips{
ipaddr:=net.ParseIP(ip_temp)
//标准IP校验
if ipaddr!=nil{
iplist=append(iplist, ipaddr.String())
}
//CIDR格式解析
ipaddr,ipnet,err:=net.ParseCIDR(ip_temp)
if err==nil{
for ip := ipaddr.Mask(ipnet.Mask); ipnet.Contains(ip); incip(ip) {
iplist=append(iplist,ip.String())
}
}
}
return iplist
}
func incip(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
func PortParse(ports string)([]int,error){
var err error
var portlist []int
//有,号优先,分割处理
portsItem:=strings.Split(ports,",")
for _,port :=range portsItem{
//1-65535解析
if strings.Contains(port,"-"){
port:=strings.Split(port,"-")
portA,err:=strconv.Atoi(port[0])
if err!=nil{
return nil,err
}
portB,err:=strconv.Atoi(port[1])
if err!=nil{
return nil,err
}
for i:=portA;i<=portB;i++{
//默认会访问的端口443 80,不需要写进列表
if i!=443 && i!=80{
portlist=append(portlist,i)
}
}
}else{
portNumber,err:=strconv.Atoi(port)
if err!=nil{
return portlist,err
}
//默认会访问的端口443 80,不需要写进列表
if portNumber!=443 && portNumber!=80{
portlist=append(portlist,portNumber)
}
}
}
return portlist,err
}
//读取IP或host列表
func ListReader(path string,opr string) []string{
var list []string
fi,err:=os.Open(path)
if err!=nil{
log.Printf("Error: %s\n", err)
return nil
}
defer fi.Close()
br:=bufio.NewReader(fi)
for{
line,_,err:=br.ReadLine()
if err==io.EOF{
break
}
line_str:=strings.TrimSpace(string(line))
if opr=="ip"{ //如果是ip行,就解析ip行地址,支持每行写法是:ip,ip,ip,CIDR或者单个ip
list=append(list,IPParse(line_str)...)
}else if opr=="host"{
list=append(list,line_str)
}
}
return list
}
// Slice string去重
func SliceStringUnique(s []string) []string {
result := []string{}
m := make(map[string]bool) //map的值不重要
for _, v := range s {
if _, ok := m[v]; !ok {
result = append(result, v)
m[v] = true
}
}
return result
}
// Slice int去重
func SliceIntUnique(s []int) []int {
result := []int{}
m := make(map[int]bool) //map的值不重要
for _, v := range s {
if _, ok := m[v]; !ok {
result = append(result, v)
m[v] = true
}
}
return result
}