🔨 ProxySlice -> Proxy struct

This commit is contained in:
Aykhan Shahsuvarov 2024-06-01 00:20:19 +04:00
parent af028a8f82
commit f8e4180a15
3 changed files with 28 additions and 22 deletions

View File

@ -11,7 +11,7 @@ import (
const ( const (
VERSION = "0.0.1" VERSION = "0.0.1"
DefaultUserAgent = "Dodo/" + VERSION DefaultUserAgent = "Dodo/" + VERSION
ProxyCheckURL = "https://google.com" ProxyCheckURL = "https://www.google.com"
DefaultMethod = "GET" DefaultMethod = "GET"
DefaultTimeout = 10000 // Milliseconds (10 seconds) DefaultTimeout = 10000 // Milliseconds (10 seconds)
DefaultDodosCount = 1 DefaultDodosCount = 1
@ -23,8 +23,6 @@ type IConfig interface {
MergeConfigs(newConfig IConfig) IConfig MergeConfigs(newConfig IConfig) IConfig
} }
type ProxySlice []map[string]string
type DodoConfig struct { type DodoConfig struct {
Method string Method string
URL string URL string
@ -34,7 +32,7 @@ type DodoConfig struct {
Params map[string]string Params map[string]string
Headers map[string]string Headers map[string]string
Cookies map[string]string Cookies map[string]string
Proxies ProxySlice Proxies []Proxy
Body string Body string
} }
@ -98,12 +96,18 @@ func (config *Config) SetDefaults() {
} }
} }
type Proxy struct {
URL string `json:"url" validate:"required,proxy_url"`
Username string `json:"username"`
Password string `json:"password"`
}
type JSONConfig struct { type JSONConfig struct {
Config Config
Params map[string]string `json:"params"` Params map[string]string `json:"params"`
Headers map[string]string `json:"headers"` Headers map[string]string `json:"headers"`
Cookies map[string]string `json:"cookies"` Cookies map[string]string `json:"cookies"`
Proxies ProxySlice `json:"proxies" validate:"url_map_slice"` Proxies []Proxy `json:"proxies" validate:"dive"`
Body string `json:"body"` Body string `json:"body"`
} }

View File

@ -268,13 +268,13 @@ func printProgress(wg *sync.WaitGroup, total int, message string, countSlice *[]
pw.Stop() pw.Stop()
} }
func getClientFunc(proxies config.ProxySlice, timeout time.Duration, dodosCount int) func() http.Client { func getClientFunc(proxies []config.Proxy, timeout time.Duration, dodosCount int) func() http.Client {
if len(proxies) > 0 { if len(proxies) > 0 {
activeProxyClientsArray := make([][]http.Client, dodosCount) activeProxyClientsArray := make([][]http.Client, dodosCount)
proxiesCount := len(proxies) proxiesCount := len(proxies)
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(dodosCount + 1) wg.Add(dodosCount + 1)
var proxiesSlice config.ProxySlice var proxiesSlice []config.Proxy
countSlice := make([]int, dodosCount) countSlice := make([]int, dodosCount)
go printProgress(&wg, proxiesCount, "Searching for active proxies🌐", &countSlice) go printProgress(&wg, proxiesCount, "Searching for active proxies🌐", &countSlice)
@ -330,7 +330,7 @@ func getClientFunc(proxies config.ProxySlice, timeout time.Duration, dodosCount
} }
func findActiveProxyClients( func findActiveProxyClients(
proxies config.ProxySlice, proxies []config.Proxy,
timeout time.Duration, timeout time.Duration,
activeProxyClients *[]http.Client, activeProxyClients *[]http.Client,
counter *int, counter *int,
@ -365,12 +365,12 @@ func findActiveProxyClients(
} }
} }
func getTransport(proxy map[string]string) (*http.Transport, error) { func getTransport(proxy config.Proxy) (*http.Transport, error) {
proxyURL, err := url.Parse(proxy["url"]) proxyURL, err := url.Parse(proxy.URL)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if _, ok := proxy["username"]; !ok { if proxy.Username != "" {
transport := &http.Transport{ transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL), Proxy: http.ProxyURL(proxyURL),
} }
@ -382,7 +382,7 @@ func getTransport(proxy map[string]string) (*http.Transport, error) {
&url.URL{ &url.URL{
Scheme: proxyURL.Scheme, Scheme: proxyURL.Scheme,
Host: proxyURL.Host, Host: proxyURL.Host,
User: url.UserPassword(proxy["username"], proxy["password"]), User: url.UserPassword(proxy.Username, proxy.Password),
}, },
), ),
} }

View File

@ -4,7 +4,6 @@ import (
"reflect" "reflect"
"strings" "strings"
"github.com/aykhans/dodo/config"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
"golang.org/x/net/http/httpguts" "golang.org/x/net/http/httpguts"
) )
@ -39,16 +38,19 @@ func NewValidator() *validator.Validate {
}, },
) )
validation.RegisterValidation( validation.RegisterValidation(
"url_map_slice", "proxy_url",
func(fl validator.FieldLevel) bool { func(fl validator.FieldLevel) bool {
proxies := fl.Field().Interface().(config.ProxySlice) url := fl.Field().String()
for _, proxy := range proxies { if url == "" {
if _, ok := proxy["url"]; !ok { return false
return false }
} if err := validation.Var(url, "url"); err != nil {
if err := validation.Var(proxy["url"], "url"); err != nil { return false
return false }
} if !(url[:7] == "http://" ||
url[:9] == "socks5://" ||
url[:10] == "socks5h://") {
return false
} }
return true return true
}, },