-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (93 loc) · 2.48 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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"sync"
"github.com/kenjoe41/scoped/pkg/options"
"github.com/kenjoe41/scoped/pkg/scoped"
)
func main() {
flags := options.Scanflags()
outOfScopeSlice := []string{}
inScopeSlice := []string{}
// If we have an out of scope file, let's read it to a slice.
if flags.OutofScopeFile != "" {
err := scoped.ReadFileToSlice(flags.OutofScopeFile, &outOfScopeSlice)
if err != nil {
log.Fatalf("error reading Out of Scope domains file: %s", err)
}
}
// if we have an in scope domains file, let's read them to a slice.
if flags.InScopeFile != "" {
err := scoped.ReadFileToSlice(flags.InScopeFile, &inScopeSlice)
if err != nil {
log.Fatalf("error reading in scope domains file: %s", err)
}
}
domainsChan := make(chan string)
outputChan := make(chan string)
var domainsWG sync.WaitGroup
domainsWG.Add(1)
go func() {
defer domainsWG.Done()
for domain := range domainsChan {
// Check if domain is in Out of Scope Slice
if !scoped.Contains(&outOfScopeSlice, domain, flags.ExcludeSubs) {
// If inScopeSlice is not empty, then we have to print only inscope domains.
if len(inScopeSlice) > 0 {
if scoped.Contains(&inScopeSlice, domain, flags.ExcludeSubs) {
outputChan <- domain
}
} else {
// inScopeSlice is empty so we print the domain
outputChan <- domain
}
} else if scoped.Contains(&inScopeSlice, domain, true) {
outputChan <- domain
}
}
}()
var outputWG sync.WaitGroup
outputWG.Add(1)
go func() {
defer outputWG.Done()
for domain := range outputChan {
fmt.Println(domain)
}
}()
// Close the Output Chan after domain worker is done.
go func() {
domainsWG.Wait()
close(outputChan)
}()
// Check if we have a domains input file.
// And read it to domainsChan
if flags.DomainsFile != "" {
err := scoped.ReadFileToChan(flags.DomainsFile, domainsChan)
if err != nil {
log.Fatal(err)
}
} else {
// Check for stdin input
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
fmt.Fprintln(os.Stderr, "No domains or urls detected. Hint: cat domains.txt | scoped -of outofscope.txt")
flag.Usage()
os.Exit(1)
}
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
domainsChan <- sc.Text()
}
// check there were no errors reading stdin (unlikely)
if err := sc.Err(); err != nil {
log.Fatal(err)
}
}
// Close DomainsChan and wait until the output waitgroup is done
close(domainsChan)
outputWG.Wait()
}