Repository navigation
Expand file tree
/
Copy pathserver.go
More file actions
270 lines (206 loc) · 7.6 KB
/
Copy pathserver.go
File metadata and controls
270 lines (206 loc) · 7.6 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
"github.com/martini-contrib/binding"
"strings"
"bufio"
"bytes"
"strconv"
"fmt"
)
type CIDR struct {
ip uint32
mask uint8
}
type rangeError struct {
msg string
}
func (err rangeError) Error() string {
return err.msg
}
type ipConvError struct {
msg string
}
func (err ipConvError) Error() string {
return err.msg
}
type formatError struct {
msg string
}
func (err formatError) Error() string {
return err.msg
}
func ipToInt(ip string) (int_ip uint32, err error) {
octets := strings.Split(ip, ".")
if (len(octets) != 4) {
return 0, ipConvError{"Wrong IP format"}
} else {
for x, octet := range octets {
value, err := strconv.Atoi(octet)
if (err != nil || value > 255 || value < 0) {
return 0, ipConvError{"Wrong IP value"}
}
int_ip |= (uint32)(value << (uint)(24 - 8 * x))
}
return int_ip, nil
}
}
func intToIP(int_ip uint32) (ip string) {
var buffer bytes.Buffer
for x := 0; x < 4; x++ {
shift := (uint)(24 - 8 * x)
octet := int_ip & (0xFF << shift) >> shift
buffer.WriteString(strconv.Itoa((int)(octet)))
if (x != 3) {
buffer.WriteString(".")
}
}
return buffer.String()
}
func rangeSize(start uint32, end uint32) (size int) {
return (int)(end - start + 1)
}
func smallestCIDR(size int) (smallest uint8) {
smallest = 1
for i := (uint8)(32); i > 1; i-- {
if (size <= (2 << (i - 1))) {
smallest = i
}
}
return 32 - smallest
}
func subnetMask(bits uint8) (mask uint32) {
mask = 0
for i := (uint8)(0); i < bits; i++ {
mask >>= 1
mask |= 0x80000000
}
return mask
}
func genSubnet(ip uint32, bits uint8) (subnet uint32) {
return ip & subnetMask(bits)
}
func subnetMax(ip uint32, bits uint8) (max uint32) {
return ip | ^subnetMask(bits)
}
func rangeSubnets(block_start uint32, block_end uint32) (subnets []CIDR) {
cidr := smallestCIDR(rangeSize(block_start, block_end))
for ; cidr <= 32; cidr++ {
sub_min := genSubnet(block_start, cidr)
sub_max := subnetMax(block_start, cidr)
if ((sub_min == block_start) && (sub_max == block_end)) {
return []CIDR{CIDR{block_start, cidr}}
}
if ((sub_min >= block_start) && (sub_max <= block_end)) {
break
}
}
sub_min := genSubnet(block_start, cidr)
sub_max := subnetMax(block_start, cidr)
// Some logic that will recursivly search from block_start to the first IP before
// the start of the subnet we just found.
// NOTE: This may never be hit, the way the above algo turned out, but is left
// for completeness.
if (block_start != sub_min) {
subnets = append(subnets, rangeSubnets(block_start, block_end - 1)...)
}
// Add in the subnet we found before, to preserve ordering
subnets = append(subnets, CIDR{sub_min, cidr})
// And some more logic that will search after the subnet we found to fill in
// to the end of the range.
if (block_end != sub_max) {
subnets = append(subnets, rangeSubnets(sub_max + 1, block_end)...)
}
return subnets
}
func Subnets(line string) (subnets []CIDR, err error) {
line = strings.Replace(line, " ", "", -1)
lr := strings.Split(line, "-")
if (len(lr) > 2) {
//
// Possibly stupid NMAP range format. Just ignore it.
//
return nil, formatError{"There's no NMAP ranges support"}
} else if (len(lr) == 1) {
//
// One signle IP (a.a.a.a) or CIDR (a.a.a.a/z)
//
parts := strings.Split(lr[0], "/")
if (len(parts) == 2) {
//
// Trying to read as a CIDR
//
ip, err := ipToInt(parts[0])
if (err != nil) {
return nil, formatError{"Wrong IP format"}
}
subnet, err := strconv.Atoi(parts[1])
if (err != nil || subnet < 0 || subnet > 32) {
return nil, formatError{"Wrong subnet mask"}
}
return []CIDR{CIDR{ip, (uint8)(subnet)}}, nil
} else if (len(parts) == 1) {
//
// Trying to read as an IP address
//
ip, err := ipToInt(lr[0])
if (err != nil) {
return nil, formatError{"Wrong IP format"}
}
return []CIDR{CIDR{ip, 32}}, nil
} else {
//
// Whoooa, too much slashes
//
return nil, formatError{"Wrong IP format"}
}
} else {
//
// IP block range (a.a.a.a-b.b.b.b)
//
block_start, err := ipToInt(lr[0])
if (err != nil) {
return nil, formatError{"Wrong IP format"}
}
block_end, err := ipToInt(lr[1])
if (err != nil) {
return nil, formatError{"Wrong IP format"}
}
if (block_start > block_end) {
return nil, formatError{"IP block start can't be greater than IP block end"}
}
return rangeSubnets(block_start, block_end), nil
}
}
type Conv struct {
List string `form:"list"`
}
func main() {
m := martini.Classic()
m.Use(martini.Static("assets"))
m.Use(render.Renderer())
m.Get("/", func(r render.Render) {
r.HTML(200, "index", nil)
})
m.Post("/convert", binding.Bind(Conv{}), func(r Conv) string {
var buffer bytes.Buffer
reader := strings.NewReader(r.List)
scanner := bufio.NewScanner(reader)
for (scanner.Scan()) {
subnets, err := Subnets(scanner.Text())
if (err != nil) {
continue
}
for _, subnet := range subnets {
if (subnet.mask == 32) {
buffer.WriteString(fmt.Sprintf("%s\n", intToIP(subnet.ip)))
} else {
buffer.WriteString(fmt.Sprintf("%s/%d\n", intToIP(subnet.ip), (int)(subnet.mask)))
}
}
}
return buffer.String()
})
m.Run()
}