原生HTTP库代理:

package main

import (
	"context"
	"fmt"
	"golang.org/x/net/proxy"
	"io/ioutil"
	"net"
	"net/http"
	"net/url"
	"strings"
	"time"
)

var testApi = "https://api.myip.la/en?json"

func main() {
	getMyIp()

	//白名单代理
	var proxyIP = "ip:port"
	go httpProxy(proxyIP, "", "")
	go Socks5Proxy(proxyIP, "", "")
	time.Sleep(time.Minute)
}

func getMyIp() {
	rsp, err := http.Get("https://api.myip.la/en?json")
	if err != nil {
		fmt.Println("获取本机ip失败", err.Error())
		return
	}
	defer rsp.Body.Close()
	body, err := ioutil.ReadAll(rsp.Body)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Println(time.Now().Format("2006-01-02 15:04:05 07"), "本机ip:", string(body))

}

//http代理
func httpProxy(proxyUrl, user, pass string) {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println(time.Now().Format("2006-01-02 15:04:05 07"), "http", "返回信息:", err)
		}
	}()
	urli := url.URL{}

	if !strings.Contains(proxyUrl, "http") {
		proxyUrl = fmt.Sprintf("http://%s", proxyUrl)
	}

	urlProxy, _ := urli.Parse(proxyUrl)
	if user != "" && pass != "" {
		urlProxy.User = url.UserPassword(user, pass)
	}

	client := &http.Client{
		Transport: &http.Transport{
			Proxy: http.ProxyURL(urlProxy),
		},
	}
	rqt, err := http.NewRequest("GET", testApi, nil)
	if err != nil {
		panic(err)
		return
	}
	response, err := client.Do(rqt)
	if err != nil {
		panic(err)
		return
	}

	defer response.Body.Close()
	body, _ := ioutil.ReadAll(response.Body)
	fmt.Println(time.Now().Format("2006-01-02 15:04:05 07"), proxyUrl, "【http success】", "返回信息:", response.Status, string(body))

	return
}

//socks5代理
func Socks5Proxy(proxyUrl, user, pass string) {

	defer func() {
		if err := recover(); err != nil {
			fmt.Println(time.Now().Format("2006-01-02 15:04:05 07"), proxyUrl, "返回信息:", err)
		}
	}()

	var userAuth proxy.Auth
	if user != "" && pass != "" {
		userAuth.User = user
		userAuth.Password = pass
	}
	dialer, err := proxy.SOCKS5("tcp", proxyUrl, &userAuth, proxy.Direct)
	if err != nil {
		panic(err)
	}
	httpClient := &http.Client{
		Transport: &http.Transport{
			DialContext: func(ctx context.Context, network, addr string) (conn net.Conn, err error) {
				return dialer.Dial(network, addr)
			},
		},
		Timeout: time.Second * 10,
	}

	//请求网络
	if resp, err := httpClient.Get(testApi); err != nil {
		panic(err)
	} else {
		defer resp.Body.Close()
		body, _ := ioutil.ReadAll(resp.Body)
		fmt.Println(time.Now().Format("2006-01-02 15:04:05 07"), proxyUrl, "【socks5 success】", "返回信息:", string(body))
	}
}

github.com/levigross/grequests库代理:

package main

import (
	"github.com/levigross/grequests"
	"log"
	"net/http"
	"net/url"
	"time"
)

func main(){
	urli := url.URL{}
        //这里填写获取到的IP+端口
	urlproxy, err := urli.Parse("http://" + "代理IP" + ":" + "端口")
	if err != nil{
		panic("ip出错!")
	}

	//如果无需账号密码可以忽略这句代码
	urlproxy.User = url.UserPassword("鉴权账号","鉴权密码")

	netTransport := &http.Transport{
		Proxy:                 http.ProxyURL(urlproxy),
		MaxIdleConnsPerHost:   10,
		ResponseHeaderTimeout: time.Second * time.Duration(5),
	}
	httpClient := &http.Client{
		Timeout:   time.Second * 60,
		Transport: netTransport,
	}

	session := grequests.NewSession(&grequests.RequestOptions{
		RequestTimeout:      60 * time.Second,
		TLSHandshakeTimeout: 60 * time.Second,
		DialKeepAlive:       60 * time.Second,
		HTTPClient: httpClient,
	})


	resp, err := session.Get("https://myip.ipip.net/",
		&grequests.RequestOptions{
			DialTimeout:    10 * time.Second,
			RequestTimeout: 10 * time.Second,
			UserAgent:      "Lingjiang",
		})
	if err != nil {
		log.Println("err", err)
	} else {
		log.Println(resp.String())
	}
}