-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaisubs.go
130 lines (105 loc) · 3.38 KB
/
aisubs.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
package main
import (
"bufio"
"context"
"fmt"
"os"
"strings"
"time"
"github.com/sashabaranov/go-openai"
"golang.org/x/net/publicsuffix"
)
func generateSubdomains(subdomain, domain, apiKey string, amount string) ([]string, error) {
client := openai.NewClient(apiKey)
for {
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "You are a helpful assistant that generates new subdomains based on a given subdomain.",
}, {
Role: openai.ChatMessageRoleUser,
Content: "Generate " + amount + " subdomains similar to " + subdomain + ". Only reply with the bare subdomains, each on a new line without additional text.",
},
},
},
)
if err != nil {
if strings.Contains(err.Error(), "Rate limit exceeded") || strings.Contains(err.Error(), "That model is currently overloaded with other requests") {
fmt.Println("Rate limit exceeded. Sleeping for 20 seconds...")
time.Sleep(20 * time.Second)
continue
}
if strings.Contains(err.Error(), "You exceeded your current quota") {
fmt.Println("You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors")
}
return nil, fmt.Errorf("error generating subdomains: %w", err)
}
choices := resp.Choices
if len(choices) == 0 {
return nil, fmt.Errorf("no subdomains generated")
}
aiGeneratedSubdomains := make([]string, 0, len(choices[0].Message.Content))
lines := strings.Split(choices[0].Message.Content, "\n")
for _, line := range lines {
if !strings.Contains(line, " ") {
continue
}
if !strings.Contains(line, ".") {
continue
}
parts := strings.Split(line, " ")
result := parts[1]
domainparts := strings.Split(result, ".")
subdomain := domainparts[0]
mainDomain, err := extractMainDomain(domain)
if err != nil {
fmt.Println("Error: ", err)
continue
}
aiGeneratedSubdomains = append(aiGeneratedSubdomains, fmt.Sprintf("%s.%s", subdomain, mainDomain))
}
return aiGeneratedSubdomains, nil
}
}
func extractMainDomain(domain string) (string, error) {
mainDomain, err := publicsuffix.EffectiveTLDPlusOne(domain)
if err != nil {
return "", err
}
return mainDomain, nil
}
func main() {
if len(os.Args) < 5 {
fmt.Println("Usage: cat subdomains.txt | go run main.go --apikey <OpenAI API Key> --amount 5 | httpx -ip -sc -cl -title -silent")
fmt.Println("Usage: echo www.domain.com | go run main.go --apikey <OpenAI API Key> --amount 5")
return
}
apiKey := os.Args[2]
amount := os.Args[4]
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "*") {
continue
}
parts := strings.SplitN(line, ".", 2)
if len(parts) != 2 {
fmt.Println("Invalid input format. Expected <subdomain>.<domain>.")
continue
}
subdomain, domain := parts[0], parts[1]
newSubdomains, err := generateSubdomains(subdomain, domain, apiKey, amount)
if err != nil {
fmt.Printf("Error generating subdomains: %v\n", err)
continue
}
fmt.Println(strings.Join(newSubdomains, "\n"))
}
if err := scanner.Err(); err != nil {
fmt.Printf("Error reading input: %v\n", err)
}
}