本文整理汇总了Golang中net/http.ProxyURL函数的典型用法代码示例。如果您正苦于以下问题:Golang ProxyURL函数的具体用法?Golang ProxyURL怎么用?Golang ProxyURL使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ProxyURL函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Send
// It will construct the client and the request, then send it
//
// This function has to be called as the last thing,
// after setting the other properties
func (f *Request) Send() (*http.Response, error) {
if f.client == nil {
f.client = http.DefaultClient
}
if f.timeout != 0 {
f.client.Timeout = f.timeout
}
if f.proxy != "" {
proxyUrl, err := url.Parse(f.proxy)
if err != nil {
return nil, err
}
if f.client.Transport != nil {
f.client.Transport.(*http.Transport).Proxy = http.ProxyURL(proxyUrl)
} else {
f.client.Transport = &http.Transport{
Proxy: http.ProxyURL(proxyUrl),
}
}
}
res, err := f.do(f.client)
return res, err
}
示例2: setupDiscoveryProxy
func (r *runner) setupDiscoveryProxy() error {
uri, err := url.Parse(r.config.NetworkConfig.ProxyURL)
if err != nil {
r.log.Warnf("Failed to parse proxy url: %v", err)
return nil
}
// discovery requests
transport, ok := discovery.Client.Transport.(*http.Transport)
if !ok {
r.log.Warnf("Failed to configure discovery proxy, transport was not the expected type: %T",
discovery.Client.Transport)
return nil
}
transport.Proxy = http.ProxyURL(uri)
// actual download requests
transport, ok = aciremote.Client.Transport.(*http.Transport)
if !ok {
r.log.Warnf("Failed to configure remote download proxy, transport was not the expected type: %T",
aciremote.Client.Transport)
return nil
}
transport.Proxy = http.ProxyURL(uri)
return nil
}
示例3: execute
// executes the given `Request` object and returns response
func (c *Client) execute(req *Request) (*Response, error) {
// Apply Request middleware
var err error
for _, f := range c.beforeRequest {
err = f(c, req)
if err != nil {
return nil, err
}
}
c.mutex.Lock()
if req.proxyURL != nil {
c.transport.Proxy = http.ProxyURL(req.proxyURL)
} else if c.proxyURL != nil {
c.transport.Proxy = http.ProxyURL(c.proxyURL)
} else {
c.transport.Proxy = nil
}
req.Time = time.Now()
c.httpClient.Transport = c.transport
resp, err := c.httpClient.Do(req.RawRequest)
c.mutex.Unlock()
response := &Response{
Request: req,
RawResponse: resp,
receivedAt: time.Now(),
}
if err != nil {
return response, err
}
if !req.isSaveResponse {
defer resp.Body.Close()
response.body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return response, err
}
response.size = int64(len(response.body))
}
// Apply Response middleware
for _, f := range c.afterResponse {
err = f(c, response)
if err != nil {
break
}
}
return response, err
}
示例4: FetchUrl
func FetchUrl(theurl string) string {
var client *http.Client
if proxy := os.Getenv("http_proxy"); proxy != `` {
proxyUrl, err := url.Parse(proxy)
CheckError(err)
transport := http.Transport{
Dial: TimeoutDialer(5*time.Second, 5*time.Second), // connect, read/write
Proxy: http.ProxyURL(proxyUrl),
}
client = &http.Client{Transport: &transport}
} else {
client = &http.Client{}
}
req, err := http.NewRequest(`GET`, theurl, nil)
CheckError(err)
resp, err := client.Do(req)
CheckError(err)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
CheckError(err)
return string(body)
}
示例5: doRequest
// doRequest makes a request to Chronos REST API
func (cl Client) doRequest(req *http.Request) ([]byte, error) {
// Init a client
client := cl.Client
if cl.ProxyURL != nil {
client = &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(cl.ProxyURL)}}
} else {
if cl.Client == nil {
client = &http.Client{}
}
}
// Do request
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Read data
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return data, errors.New("bad response: " + fmt.Sprintf("%d", resp.StatusCode))
}
return data, nil
}
示例6: main
func main() {
// CIRCUIT Client
proxyurl, _ := url.Parse(PROXY_URL)
circuit_tr := &http.Transport{
// Ignore SSL errors
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
circuit_client := &http.Client{Transport: circuit_tr}
// SALESFORCE Client
salesforce_tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
// Configure proxy
Proxy: http.ProxyURL(proxyurl),
}
salesforce_client := &http.Client{Transport: salesforce_tr}
// SERVER (Extension App)
mux := http.NewServeMux()
// Server Routes
mux.HandleFunc("/configure", configureHandler)
// periodically check for leads (polling due to infr. restrictions)
go poll(salesforce_client, circuit_client)
// Start server!
fmt.Println("... listening on " + PORT)
if err := http.ListenAndServe(PORT, mux); nil != err {
log.Fatal(err.Error())
}
}
示例7: proxyFuncFromEnv
// proxyFuncFromEnv builds a proxy function if the appropriate environment
// variable is set. It performs basic sanitization of the environment variable
// and returns any error encountered.
func proxyFuncFromEnv() (func(*http.Request) (*url.URL, error), error) {
proxy := os.Getenv(DiscoveryProxyEnv)
if proxy == "" {
return nil, nil
}
// Do a small amount of URL sanitization to help the user
// Derived from net/http.ProxyFromEnvironment
proxyURL, err := url.Parse(proxy)
if err != nil || !strings.HasPrefix(proxyURL.Scheme, "http") {
// proxy was bogus. Try prepending "http://" to it and
// see if that parses correctly. If not, we ignore the
// error and complain about the original one
var err2 error
proxyURL, err2 = url.Parse("http://" + proxy)
if err2 == nil {
err = nil
}
}
if err != nil {
return nil, fmt.Errorf("invalid proxy address %q: %v", proxy, err)
}
log.Printf("discovery: using proxy %q", proxyURL.String())
return http.ProxyURL(proxyURL), nil
}
示例8: NewClient
func NewClient(
esURL, index string, proxyURL *url.URL, tls *tls.Config,
username, password string,
params map[string]string,
onConnectCallback connectCallback,
) *Client {
proxy := http.ProxyFromEnvironment
if proxyURL != nil {
proxy = http.ProxyURL(proxyURL)
}
client := &Client{
Connection: Connection{
URL: esURL,
Username: username,
Password: password,
http: &http.Client{
Transport: &http.Transport{
TLSClientConfig: tls,
Proxy: proxy,
},
},
},
index: index,
params: params,
}
client.Connection.onConnectCallback = func() error {
if onConnectCallback != nil {
return onConnectCallback(client)
}
return nil
}
return client
}
示例9: runWorker
func (b *Boomer) runWorker(n int) {
var throttle <-chan time.Time
if b.Qps > 0 {
throttle = time.Tick(time.Duration(1e6/(b.Qps)) * time.Microsecond)
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
DisableCompression: b.DisableCompression,
DisableKeepAlives: b.DisableKeepAlives,
// TODO(jbd): Add dial timeout.
TLSHandshakeTimeout: time.Duration(b.Timeout) * time.Millisecond,
Proxy: http.ProxyURL(b.ProxyAddr),
}
if b.H2 {
http2.ConfigureTransport(tr)
} else {
tr.TLSNextProto = make(map[string]func(string, *tls.Conn) http.RoundTripper)
}
client := &http.Client{Transport: tr}
for i := 0; i < n; i++ {
if b.Qps > 0 {
<-throttle
}
b.makeRequest(client)
}
}
示例10: NewProxyAuthTransport
func NewProxyAuthTransport(rawTransport *http.Transport, customHeaders http.Header) (*ProxyAuthTransport, error) {
dialFn := rawTransport.Dial
if dialFn == nil {
dialFn = net.Dial
}
tr := &ProxyAuthTransport{Dial: dialFn, CustomHeaders: customHeaders}
proxyUrlFn := rawTransport.Proxy
if proxyUrlFn != nil {
wrappedDialFn := tr.wrapTransportDial()
rawTransport.Dial = wrappedDialFn
proxyUrl, err := proxyUrlFn(nil)
if err != nil {
return nil, err
}
if proxyUrl.Scheme != "http" {
return nil, fmt.Errorf("Only HTTP proxy supported, for SOCKS use http.Transport with custom dialers & upstreamproxy.NewProxyDialFunc")
}
if proxyUrl.User != nil {
tr.Username = proxyUrl.User.Username()
tr.Password, _ = proxyUrl.User.Password()
}
// strip username and password from the proxyURL because
// we do not want the wrapped transport to handle authentication
proxyUrl.User = nil
rawTransport.Proxy = http.ProxyURL(proxyUrl)
}
tr.Transport = rawTransport
return tr, nil
}
示例11: CheckAnonyPost
func CheckAnonyPost(proxyAddr string) (bool, float64) { //验证Post方法
start := time.Now()
proxy, err1 := url.Parse(proxyAddr)
if err1 != nil {
// log.Println(err1)
return false, 0
}
client := &http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
deadline := time.Now().Add(10 * time.Second)
c, err2 := net.DialTimeout(netw, addr, 10*time.Second)
if err2 != nil {
return nil, err2
}
c.SetDeadline(deadline)
return c, nil
},
DisableKeepAlives: true,
ResponseHeaderTimeout: 10 * time.Second,
DisableCompression: false,
Proxy: http.ProxyURL(proxy),
},
}
respPost, err4 := client.Post(PROXY_CHECKER_SERVICE, "application/x-www-form-urlencoded", strings.NewReader("username=credit")) //验证Post方法
if err4 != nil {
// log.Println(err4)
return false, 0
}
bodyPost, _ := ioutil.ReadAll(respPost.Body)
defer respPost.Body.Close()
return respPost.StatusCode == http.StatusOK && string(bodyPost) == "ok", time.Now().Sub(start).Seconds()
}
示例12: checkProxy
func checkProxy(proxy string, downloadedUrl string) (success bool, errorMessage string) {
getsInProgress <- 1
defer func() { <-getsInProgress }()
if !strings.HasPrefix(proxy, "http") {
proxy = "http://" + proxy
}
proxyUrl, err := url.Parse(proxy)
httpClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
response, err := httpClient.Get(downloadedUrl)
if err != nil {
return false, err.Error()
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return false, err.Error()
}
bodyString := strings.ToLower(strings.Trim(string(body), " \n\t\r"))
if strings.Index(bodyString, "<body") < 0 && strings.Index(bodyString, "<head") < 0 {
if strings.Index(bodyString, "<title>invalid request</title>") >= 0 {
return false, "Tracker responsed 'Invalid request' - might be dead"
} else {
return false, "Reveived page is not HTML: " + bodyString
}
}
return true, ""
}
示例13: httpProxyClient
func httpProxyClient(ip string, block *Block) *http.Client {
proxy, _ := url.Parse("http://" + ip)
transport := &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
timeout := time.Duration(10) * time.Second
deadline := time.Now().Add(timeout)
c, err := net.DialTimeout(netw, addr, timeout)
if err != nil {
return nil, err
}
c.SetDeadline(deadline)
return c, nil
},
Proxy: http.ProxyURL(proxy),
ResponseHeaderTimeout: time.Second * 10,
}
client := &http.Client{
Transport: transport,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
log.Println("redirect to:", req.URL.String(), ip)
block.block(ip)
return errors.New("does not allow redirect")
},
}
return client
}
示例14: BuildHTTPClient
func BuildHTTPClient(proxy, token string) *http.Client {
var base http.RoundTripper
base = http.DefaultTransport
// Proxy layer
if len(proxy) > 0 {
u, _ := url.Parse(proxy)
base = &http.Transport{
Proxy: http.ProxyURL(u),
}
}
// Authentication layer
if len(token) > 0 {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
base = &oauth2.Transport{
Source: ts,
Base: base,
}
}
// Rate limiting
transport := &RateLimitedTransport{
Base: base,
}
return &http.Client{
Transport: transport,
}
}
示例15: TestClient
func TestClient(t *testing.T) {
ts := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}),
)
defer ts.Close()
proxy, _ := url.Parse(ts.URL)
tr := &http.Transport{Proxy: http.ProxyURL(proxy)}
c := &http.Client{Transport: tr}
r := New()
r.Client(c)
res, err := r.Get("http://github.com").Send()
if err != nil {
t.Error(err)
}
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Error(err)
}
if string(body) != "ok" {
t.Error("Incorrect response.")
}
}