diff --git a/config.json b/config.json index 2580aaa..6eae29c 100644 --- a/config.json +++ b/config.json @@ -6,5 +6,7 @@ }, "cacheExpiration": 3600, "port": 8043, - "rateLimit": 50 + "rateLimit": 50, + "ProxyServer": "http://127.0.0.1:8080", + "ProxySuffixes": [] } \ No newline at end of file diff --git a/config/config.go b/config/config.go index d16031b..8a458d2 100644 --- a/config/config.go +++ b/config/config.go @@ -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() { @@ -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 } diff --git a/config/struct.go b/config/struct.go index 38ee283..4ff2d14 100644 --- a/config/struct.go +++ b/config/struct.go @@ -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"` } diff --git a/rdap_tools/rdap_query.go b/rdap_tools/rdap_query.go index 8853e06..706c5b0 100644 --- a/rdap_tools/rdap_query.go +++ b/rdap_tools/rdap_query.go @@ -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] @@ -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 @@ -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 @@ -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