-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmian.go
150 lines (124 loc) · 2.89 KB
/
mian.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"
"flag"
"fmt"
"net/url"
"os"
"strings"
"github.com/edoardottt/golazy"
)
func main() {
helpPtr := flag.Bool("h", false, "Show usage.")
levelsPtr := flag.Int("l", -1, "Number of levels to print. -1 to print all levels.")
flag.Parse()
if *helpPtr {
help()
}
input := ScanTargets()
output := GetPaths(golazy.RemoveDuplicateValues(input), *levelsPtr)
for _, elem := range output {
fmt.Println(elem)
}
}
// help shows the usage.
func help() {
var usage = `Take as input on stdin a list of urls and print on stdout all the unique paths (at any level).
$> cat input | URLPath -l 2
-l is number of levels to print. -1 to print all levels.`
fmt.Println()
fmt.Println(usage)
fmt.Println()
os.Exit(0)
}
// ScanTargets return the array of elements
// taken as input on stdin.
func ScanTargets() []string {
var result []string
// accept domains on stdin
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
domain := sc.Text()
result = append(result, domain)
}
return result
}
// GetPaths.
func GetPaths(s []string, levels int) []string {
var result []string
for _, elem := range s {
if len(elem) != 0 {
var paths []string
if golazy.HasProtocol(elem) {
if GetPath(elem) != "" {
paths = GetAllLevelsPaths(GetPath(elem), elem, levels)
}
}
if len(paths) != 0 {
result = append(result, paths...)
}
}
}
return golazy.RemoveDuplicateValues(result)
}
// GetPath.
func GetPath(input string) string {
u, err := url.Parse(input)
if err != nil {
return ""
}
if len(u.Path) > 1 {
return u.Path[1:]
} else {
return ""
}
}
// GetAllLevelsPaths.
func GetAllLevelsPaths(input string, url string, levels int) []string {
if input == "" {
return []string{}
}
var result []string
if input[len(input)-1] != '/' {
input += "/"
}
var elems = strings.Split(input, "/")
var tmpurl = strings.Split(url, "/")
if len(elems) == 2 {
if strings.Contains(elems[0], ".") || strings.Contains(elems[0], "?") {
return []string{}
}
return []string{tmpurl[0] + "//" + tmpurl[2] + "/" + elems[0]}
}
for i := range elems {
if elems[i] == "*" {
break
}
if levels >= 0 && i >= levels {
for j := 0; j < levels; j++ {
if strings.Contains(elems[j], "*") || elems[j] == "*" {
break
}
if strings.Contains(elems[j], ".") || strings.Contains(elems[j], "?") {
break
}
resTemp := strings.Join(elems[:j+1], "/")
resTemp = tmpurl[0] + "//" + tmpurl[2] + "/" + resTemp
result = append(result, resTemp)
}
} else {
for j := 0; j < i; j++ {
if strings.Contains(elems[j], "*") || elems[j] == "*" {
break
}
if strings.Contains(elems[j], ".") || strings.Contains(elems[j], "?") {
break
}
resTemp := strings.Join(elems[:j+1], "/")
resTemp = tmpurl[0] + "//" + tmpurl[2] + "/" + resTemp
result = append(result, resTemp)
}
}
}
return golazy.RemoveDuplicateValues(result)
}