Skip to content

Commit

Permalink
新增支持 RDAP 查询使用 HTTP 代理
Browse files Browse the repository at this point in the history
  • Loading branch information
KincaidYang committed Apr 14, 2024
1 parent d414c8c commit c8d4dd5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
},
"cacheExpiration": 3600,
"port": 8043,
"rateLimit": 50
"rateLimit": 50,
"ProxyServer": "http://127.0.0.1:8080",
"ProxySuffixes": []
}
8 changes: 8 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ var (
// RateLimit is used to set the number of concurrent requests
RateLimit int
ConcurrencyLimiter chan struct{}
// ProxyServer is the proxy server
ProxyServer string
// ProxySuffixes is the list of TLDs that use a proxy server
ProxySuffixes []string
)

func init() {
Expand Down Expand Up @@ -62,4 +66,8 @@ func init() {
// Set the number of concurrent requests
RateLimit = config.RateLimit
ConcurrencyLimiter = make(chan struct{}, RateLimit)

// Set the proxy server
ProxyServer = config.ProxyServer
ProxySuffixes = config.ProxySuffixes
}
4 changes: 4 additions & 0 deletions config/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ type Config struct {
Port int `json:"port"`
// RateLimit is the maximum number of requests that a client can make in a specified period of time.
RateLimit int `json:"rateLimit"`
// ProxyServer is the proxy server to use for certain TLDs.
ProxyServer string `json:"proxyServer"`
// ProxySuffixes is the list of TLDs that use a proxy server.
ProxySuffixes []string `json:"proxySuffixes"`
}
38 changes: 38 additions & 0 deletions rdap_tools/rdap_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,22 @@ import (
"io"
"log"
"net/http"
"net/url"

"github.com/KincaidYang/whois/config"
"github.com/KincaidYang/whois/server_lists"
)

// contains function is used to check if a string is in a slice of strings.
func contains(slice []string, str string) bool {
for _, item := range slice {
if item == str {
return true
}
}
return false
}

// RDAPQuery function is used to query the RDAP (Registration Data Access Protocol) information for a given domain.
func RDAPQuery(domain, tld string) (string, error) {
rdapServer, ok := server_lists.TLDToRdapServer[tld]
Expand All @@ -28,6 +39,15 @@ func RDAPQuery(domain, tld string) (string, error) {
}
req.Header.Set("Accept", "application/rdap+json")

if contains(config.ProxySuffixes, tld) || contains(config.ProxySuffixes, "all") {
proxyURL, _ := url.Parse(config.ProxyServer)
config.HttpClient.Transport = &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
} else {
config.HttpClient.Transport = &http.Transport{}
}

resp, err := config.HttpClient.Do(req)
if err != nil {
return "", err
Expand Down Expand Up @@ -67,6 +87,15 @@ func RDAPQueryIP(ip, tld string) (string, error) {
}
req.Header.Set("Accept", "application/rdap+json")

if contains(config.ProxySuffixes, tld) || contains(config.ProxySuffixes, "all") {
proxyURL, _ := url.Parse(config.ProxyServer)
config.HttpClient.Transport = &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
} else {
config.HttpClient.Transport = &http.Transport{}
}

resp, err := config.HttpClient.Do(req)
if err != nil {
return "", err
Expand Down Expand Up @@ -106,6 +135,15 @@ func RDAPQueryASN(as, tld string) (string, error) {
}
req.Header.Set("Accept", "application/rdap+json")

if contains(config.ProxySuffixes, tld) || contains(config.ProxySuffixes, "all") {
proxyURL, _ := url.Parse(config.ProxyServer)
config.HttpClient.Transport = &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
} else {
config.HttpClient.Transport = &http.Transport{}
}

resp, err := config.HttpClient.Do(req)
if err != nil {
return "", err
Expand Down

0 comments on commit c8d4dd5

Please sign in to comment.