GO语言如何接入代码Demo

以下分别展示API模式、账密认证模式下的Go语言demo示例:

一、API教程demo示例

package main

import (
    "context"
    "fmt"
    "io/ioutil"
    "net"
    "net/http"
    "net/url"
    "strings"
    "time"

    "golang.org/x/net/proxy"
)

// 代理IP设置
var ip = "代理服务器"   //示例:192.168.0.1
var port = "端口" //示例:2333
// 代理服务器
var proxyServer = "http://" + ip + ":" + port

// 测试地址
var testApi = "https://ipinfo.ipidea.io"

func main() {
    var proxyIP = proxyServer
    go httpProxy(proxyIP, "", "")
    go Socks5Proxy(proxyIP, "", "")
    time.Sleep(time.Minute)
}

// 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", "response:", 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:", 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, "response:", 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】", "response:", string(body))
    }
}


二、账密认证教程demo示例


package main

import (
    "context"
    "fmt"
    "io/ioutil"
    "net"
    "net/http"
    "net/url"
    "strings"
    "time"

    "golang.org/x/net/proxy"
)

// 账密设置
var account = "认证帐户名"
var password = "认证账户密码"

// 代理服务器
var proxyServer = "代理地址"  //示例:xxx.na.ipidea.online:2336;

// 测试地址
var testApi = "https://ipinfo.ipidea.io"

func main() {
    go httpProxy(proxyServer, account, password)
    go Socks5Proxy(proxyServer, account, password)

    time.Sleep(time.Minute)
}

// 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", "response:", 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:", 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, "response:", 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】", "response:", string(body))
    }
}