使用Go语言编写Socks5代理自动获取程序

使用Go语言编写Socks5代理自动获取程序

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "io/ioutil"
    "net/http"
    "net/url"
    "strings"
    "time"
)

func main() {
    aLine := getone()
    for _, ipaddrport := range aLine {
        fmt.Println(ipaddrport)
    }
}

//填写Token密钥
var token = ""

func getone() []string {
    sUrl := "http://list.rola-ip.site:8088/user_get_ip_list"
    query := url.Values{}
    query.Add("token", token)
    query.Add("qty", "100")    //每次获取IP数量
    query.Add("country", "us") //国家
    query.Add("time", "5")
    query.Add("format", "json")     //返回格式
    query.Add("protocol", "socks5") //筛选格式
    query.Add("filter", "1")        //是否去重
    statusCode, apiResJson := HttpRequestJson("GET", sUrl, query, nil)
    if apiResJson.Code == 101 {
        fmt.Println("GET_IP_LIST", statusCode, apiResJson.Code, apiResJson.Msg)
        aMsg := strings.SplitN(string(apiResJson.Msg), " ", 2)
        ip := aMsg[0]
        //msg := aMsg[1]
        del_whitelist_by_remark("golang")
        user_add_whitelist(ip, "golang")
        time.Sleep(5 * time.Second)
        return getone()
    }
    aRows := apiResJson.Data.([]interface{})
    aRet := []string{}
    for _, _mRow := range aRows {
        mRow := _mRow.(string)
        aRet = append(aRet, mRow)
    }
    return aRet
}

func del_whitelist_by_remark(remark string) {
    aRows := user_get_whitelist()
    for _, _mRow := range aRows {
        mRow := _mRow.(map[string]interface{})
        //fmt.Println(mRow)
        sRemark := mRow["remark"].(string)
        sIp := mRow["ip"].(string)
        if sRemark == remark {
            user_del_whitelist(sIp)
        }
    }
}

func user_get_whitelist() []interface{} {
    //查看白名单
    sUrl := "http://admin.rola-ip.co/user_get_whitelist"
    query := url.Values{}
    query.Add("token", token)
    _, jsonResponse := HttpRequestJson("GET", sUrl, query, nil)
    aRows := jsonResponse.Data.([]interface{})
    return aRows
}

func user_add_whitelist(ip string, remark string) {
    //添加白名单
    sUrl := "http://admin.rola-ip.co/user_add_whitelist"
    query := url.Values{}
    query.Add("token", token)
    query.Add("ip", ip)
    query.Add("remark", remark)
    statusCode, jsonResponse := HttpRequestJson("GET", sUrl, query, nil)
    fmt.Println("user_add_whitelist", statusCode, jsonResponse.Code, jsonResponse.Data, jsonResponse.Msg)
}

func user_del_whitelist(ip string) {
    //删除白名单
    sUrl := "http://admin.rola-ip.co/user_del_whitelist"
    query := url.Values{}
    query.Add("token", token)
    query.Add("ip", ip)
    statusCode, jsonResponse := HttpRequestJson("GET", sUrl, query, nil)
    fmt.Println("user_del_whitelist", statusCode, jsonResponse.Code, jsonResponse.Data, jsonResponse.Msg)
}

/*
HTTP处理用函数
*/
type JsonRequest struct {
}

type JsonResponse struct {
    Code int32       `json:"code"`
    Data interface{} `json:"data"`
    Msg  string      `json:"msg"`
}

func HttpRequestJson(method string, sUrl string, query url.Values, userReqJson interface{}) (int, JsonResponse) {
    //以JSON形式提交并返回JSON
    bReqData, err := json.Marshal(userReqJson)
    if err != nil {
        panic(err)
    }
    statusCode, clientResBody := HttpRequestByte(method, sUrl, query, bytes.NewBuffer(bReqData))
    apiResJson := JsonResponse{}
    err = json.Unmarshal(clientResBody, &apiResJson)
    if err != nil {
        panic(err)
    }
    return statusCode, apiResJson
}

func HttpRequestByte(method string, sUrl string, query url.Values, body io.Reader) (int, []byte) {
    //以byte数组返回结果
    if query != nil {
        sUrl += "?" + query.Encode()
    }
    clientReq, err := http.NewRequest(method, sUrl, body)
    if err != nil {
        panic(err)
    }
    httpClient := &http.Client{}
    clientRes, err := httpClient.Do(clientReq) //向后端服务器提交数据
    if err != nil {
        panic(err)
    }
    clientResBody, err := ioutil.ReadAll(clientRes.Body) //取得后端服务器返回的数据
    clientRes.Body.Close()
    if err != nil {
        panic(err)
    }
    return clientRes.StatusCode, clientResBody
}

 

上一篇:socks5代理是什么意思?与http代理的不同?


下一篇:让远端服务器使用上本地计算机的酸酸乳的socks5代理