-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (87 loc) · 2.62 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
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/0x4f53/textsubs"
"github.com/spf13/cobra"
)
var (
domains bool
pair bool
keepDomains bool
breakFused bool
resolve bool
output []string
input string
)
var rootCmd = &cobra.Command{
Use: "subs [input_file]",
Short: "subs",
Long: "Grab valid subdomains from files!\n(Visit https://github.com/0x4f53/subs for more details)",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
input = args[0]
},
}
func main() {
rootCmd.Flags().BoolVarP(&domains, "domains", "d", false, "Get domains only")
rootCmd.Flags().BoolVarP(&keepDomains, "keepDomains", "k", false, "Keep both subdomains and domains in the output")
rootCmd.Flags().BoolVarP(&breakFused, "break", "b", false, "Attempt to break fused domains and subdomains (e.g.: 0x4f.inwwwapple.com becomes 0x4f.in www.apple.com)")
rootCmd.Flags().BoolVarP(&pair, "pair", "p", false, "Get pairs as json output in the form of {subdomain:\"subdomain.example.com\", domain:\"example.com\"}")
rootCmd.Flags().BoolVarP(&resolve, "resolve", "r", false, "Only get items that resolve (using local DNS settings)")
rootCmd.Flags().BoolP("help", "h", false, "Help")
if err := rootCmd.Execute(); err != nil {
os.Exit(-2)
}
file, err := os.ReadFile(input)
if err != nil {
fmt.Fprintln(os.Stderr, "Error: Could not read \""+input+"\"")
os.Exit(-1)
}
if domains {
output, _ = textsubs.DomainsOnly(string(file), breakFused)
if resolve {
output = textsubs.Resolve(output)
}
} else if pair {
pairs, _ := textsubs.SubdomainAndDomainPair(string(file), keepDomains, breakFused)
if resolve {
var subdomainsSlice []string
for _, item := range pairs {
subdomainsSlice = append(subdomainsSlice, item.Subdomain)
}
subdomainsSlice = textsubs.Resolve(subdomainsSlice)
for _, item := range pairs {
for _, resolvedSubdomain := range subdomainsSlice {
if item.Subdomain == resolvedSubdomain {
jsonBytes, _ := json.Marshal(item)
output = append(output, string(jsonBytes))
}
}
}
} else {
for _, item := range pairs {
jsonBytes, _ := json.Marshal(item)
output = append(output, string(jsonBytes))
}
}
} else {
output, _ = textsubs.SubdomainsOnly(string(file), breakFused)
if keepDomains {
keepDomainsSlice, _ := textsubs.DomainsOnly(string(file), breakFused)
for _, domain := range keepDomainsSlice {
output = append(output, domain)
}
}
if resolve {
output = textsubs.Resolve(output)
}
}
if len(output) > 0 {
for _, item := range output {
fmt.Fprintln(os.Stdout, item)
}
os.Exit(0)
}
}