proxy-loadbalancer/src/proxy/checkProxy.go

42 lines
888 B
Go
Raw Normal View History

2024-04-12 01:26:45 -06:00
package proxy
import (
"fmt"
"io"
2024-04-12 18:23:18 -06:00
"main/config"
2024-04-12 01:26:45 -06:00
"net/http"
"net/url"
)
2024-04-12 18:23:18 -06:00
func sendRequestThroughProxy(pxy string, targetURL string) (string, error) {
proxyUser, proxyPass, _, parsedProxyUrl, err := splitProxyURL(pxy)
2024-04-12 01:26:45 -06:00
if err != nil {
return "", err
}
2024-04-12 18:23:18 -06:00
if proxyUser != "" && proxyPass != "" {
parsedProxyUrl.User = url.UserPassword(proxyUser, proxyPass)
2024-04-12 01:26:45 -06:00
}
transport := &http.Transport{
Proxy: http.ProxyURL(parsedProxyUrl),
}
client := &http.Client{
Transport: transport,
2024-04-12 18:23:18 -06:00
Timeout: config.GetConfig().ProxyConnectTimeout,
2024-04-12 01:26:45 -06:00
}
response, err := client.Get(targetURL)
if err != nil {
return "", err
}
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
return "", err
}
return string(bodyBytes), nil
}
return "", fmt.Errorf("bad response code %d", response.StatusCode)
}