-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_example.go
More file actions
211 lines (171 loc) · 5.08 KB
/
proxy_example.go
File metadata and controls
211 lines (171 loc) · 5.08 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
package main
import (
"context"
"fmt"
"log"
"net"
"time"
"github.com/hellenic-development/dns-lookup/pkg/dns"
"github.com/hellenic-development/dns-lookup/pkg/whois"
)
func main() {
fmt.Println("=== DNS Lookup and WHOIS with Proxy Support ===\n")
// Example 1: Using SOCKS5 proxy with ProxyURL
exampleSOCKS5Proxy()
// Example 2: Using HTTP proxy with ProxyURL
exampleHTTPProxy()
// Example 3: Using custom dialer function
exampleCustomDialer()
// Example 4: No proxy (direct connection)
exampleDirectConnection()
}
func exampleSOCKS5Proxy() {
fmt.Println("--- Example 1: SOCKS5 Proxy via ProxyURL ---")
// Configure DNS client with SOCKS5 proxy
dnsConfig := &dns.Config{
Timeout: 10 * time.Second,
ProxyURL: "socks5://localhost:1080", // Replace with your SOCKS5 proxy
}
dnsClient, err := dns.NewClient(dnsConfig)
if err != nil {
log.Printf("Failed to create DNS client: %v\n", err)
return
}
// Configure WHOIS client with SOCKS5 proxy
whoisConfig := &whois.Config{
Timeout: 15 * time.Second,
ProxyURL: "socks5://localhost:1080", // Replace with your SOCKS5 proxy
}
whoisClient, err := whois.NewClient(whoisConfig)
if err != nil {
log.Printf("Failed to create WHOIS client: %v\n", err)
return
}
// Perform DNS lookup
ctx := context.Background()
result, err := dnsClient.Lookup(ctx, "example.com", dns.RecordTypeA)
if err != nil {
log.Printf("DNS lookup failed: %v\n", err)
} else {
fmt.Printf("DNS Result: %v\n", result.Records)
}
// Perform WHOIS lookup
whoisResult, err := whoisClient.Lookup(ctx, "example.com")
if err != nil {
log.Printf("WHOIS lookup failed: %v\n", err)
} else {
fmt.Printf("WHOIS Registrar: %s\n", whoisResult.Registrar)
}
fmt.Println()
}
func exampleHTTPProxy() {
fmt.Println("--- Example 2: HTTP Proxy via ProxyURL ---")
// Configure with HTTP proxy
dnsConfig := &dns.Config{
Timeout: 10 * time.Second,
ProxyURL: "http://proxy.example.com:8080", // Replace with your HTTP proxy
}
dnsClient, err := dns.NewClient(dnsConfig)
if err != nil {
log.Printf("Failed to create DNS client: %v\n", err)
return
}
ctx := context.Background()
result, err := dnsClient.Lookup(ctx, "google.com", dns.RecordTypeA)
if err != nil {
log.Printf("DNS lookup failed: %v\n", err)
} else {
fmt.Printf("DNS Result: %v\n", result.Records)
}
fmt.Println()
}
func exampleCustomDialer() {
fmt.Println("--- Example 3: Custom Dialer Function ---")
// Create a custom dialer with specific configuration
customDialer := func(ctx context.Context, network, address string) (net.Conn, error) {
// You can add custom logic here, such as:
// - Custom DNS resolution
// - Connection pooling
// - Logging
// - Custom timeout handling
// - Routing through specific interfaces
// - etc.
fmt.Printf("Custom dialer connecting to %s via %s\n", address, network)
dialer := &net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 30 * time.Second,
// You can also bind to a specific local address:
// LocalAddr: &net.TCPAddr{IP: net.ParseIP("192.168.1.100")},
}
return dialer.DialContext(ctx, network, address)
}
// Configure DNS client with custom dialer
dnsConfig := &dns.Config{
Timeout: 10 * time.Second,
Dialer: customDialer,
}
dnsClient, err := dns.NewClient(dnsConfig)
if err != nil {
log.Printf("Failed to create DNS client: %v\n", err)
return
}
// Configure WHOIS client with custom dialer
whoisConfig := &whois.Config{
Timeout: 15 * time.Second,
Dialer: customDialer,
}
whoisClient, err := whois.NewClient(whoisConfig)
if err != nil {
log.Printf("Failed to create WHOIS client: %v\n", err)
return
}
ctx := context.Background()
// Perform DNS lookup
result, err := dnsClient.Lookup(ctx, "github.com", dns.RecordTypeA)
if err != nil {
log.Printf("DNS lookup failed: %v\n", err)
} else {
fmt.Printf("DNS Result: %v\n", result.Records)
}
// Perform WHOIS lookup
whoisResult, err := whoisClient.Lookup(ctx, "github.com")
if err != nil {
log.Printf("WHOIS lookup failed: %v\n", err)
} else {
fmt.Printf("WHOIS Registrar: %s\n", whoisResult.Registrar)
}
fmt.Println()
}
func exampleDirectConnection() {
fmt.Println("--- Example 4: Direct Connection (No Proxy) ---")
// Default configuration uses direct connection
dnsConfig := dns.DefaultConfig()
dnsClient, err := dns.NewClient(dnsConfig)
if err != nil {
log.Printf("Failed to create DNS client: %v\n", err)
return
}
whoisConfig := whois.DefaultConfig()
whoisClient, err := whois.NewClient(whoisConfig)
if err != nil {
log.Printf("Failed to create WHOIS client: %v\n", err)
return
}
ctx := context.Background()
// Perform DNS lookup
result, err := dnsClient.Lookup(ctx, "cloudflare.com", dns.RecordTypeA)
if err != nil {
log.Printf("DNS lookup failed: %v\n", err)
} else {
fmt.Printf("DNS Result: %v\n", result.Records)
}
// Perform WHOIS lookup
whoisResult, err := whoisClient.Lookup(ctx, "cloudflare.com")
if err != nil {
log.Printf("WHOIS lookup failed: %v\n", err)
} else {
fmt.Printf("WHOIS Registrar: %s\n", whoisResult.Registrar)
fmt.Printf("Name Servers: %v\n", whoisResult.NameServers)
}
fmt.Println()
}