xdaili

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 14, 2020 License: MIT Imports: 7 Imported by: 2

README

xdaili go.dev reference

Package xdaili provides a custom http.Transport and helpers for dynamic proxy service powered by xdaili.cn .

Documentation

Overview

Package xdaili provides a custom http.Transport and helpers for dynamic proxy service powered by xdaili.cn .

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAuthHeader

func GetAuthHeader(order, secret string) (string, string)

GetAuthHeader returns a HTTP header key-value for authenticated request to use proxy provided by xdaili.

func GetProxyURL

func GetProxyURL() func(*http.Request) (*url.URL, error)

GetProxyURL returns a proxy function (for use in a Transport) to use proxy provided by xdaili.

Types

type ProxyAuthTransport

type ProxyAuthTransport struct {
	http.RoundTripper
	// contains filtered or unexported fields
}

func NewProxyAuthTransport

func NewProxyAuthTransport(upstream *http.Transport, order, secret string) *ProxyAuthTransport

NewProxyAuthTransport returns a modified http.Transport with authentication required by http://www.xdaili.cn/ .

Example
package main

import (
	"crypto/tls"
	"io/ioutil"
	"log"
	"net/http"
	"time"

	"bitbucket.org/ai69/xdaili"
)

var (
	proxyOrder  = "YOUR_OWN_ORDER_NUM"
	proxySecret = "YOUR_OWN_PROXY_SECRET"
	apiUrl      = "https://bot.whatismyipaddress.com/"
)

func main() {
	upstream := http.DefaultTransport.(*http.Transport)
	upstream.Proxy = xdaili.GetProxyURL()
	upstream.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
	transport := xdaili.NewProxyAuthTransport(upstream, proxyOrder, proxySecret)

	client := &http.Client{
		Timeout:   20 * time.Second,
		Transport: transport,
	}

	log.Println("example starts")

	start := time.Now()
	resp, err := client.Get(apiUrl)
	elapsed := time.Since(start)

	if err != nil {
		log.Fatalf("client error: %v", err)
	} else if resp == nil {
		log.Fatalln("got empty response")
	} else if resp.StatusCode < 200 || resp.StatusCode > 299 {
		log.Fatalf("got unexpected response: %v", resp.Status)
	} else {
		defer resp.Body.Close()
	}

	raw, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatalf("got response error: %v", err)
	}

	log.Printf("example ends with valid result, time cost: %v", elapsed)
	log.Printf("content: %s", raw)

}
Output:

func (*ProxyAuthTransport) RoundTrip

func (ct *ProxyAuthTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip adds authentication signatures in header of requests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL