daShangDao_kfzgw-info/proxy/proxy.go
97694732@qq.com ac2a39742d 各种修改
2026-06-11 13:21:55 +08:00

349 lines
8.3 KiB
Go

package main
/*
#include <stdlib.h>
*/
import "C"
import (
"crypto/md5"
"encoding/json"
"fmt"
"math/rand"
"strings"
"sync"
"time"
"unsafe"
"github.com/parnurzeal/gorequest"
)
const (
TailProxyType = "TAIL_PROXY" // 尾巴代理类型
)
// 互斥锁,用于保护全局随机数生成器的并发访问
var (
randMutex sync.Mutex
globalRand *rand.Rand
)
// 初始化函数
func init() {
globalRand = rand.New(rand.NewSource(time.Now().UnixNano()))
}
// 根据代理类型获取代理URL
func getProxyByType(proxyType, machineCode string) (string, error) {
switch proxyType {
case TailProxyType:
return getTailProxyURL(machineCode)
default:
return "", fmt.Errorf("不支持的代理类型: %s", proxyType)
}
}
// 获取尾巴代理URL
func getTailProxyURL(machineCode string) (string, error) {
// 获取代理列表
proxies, err := getProxies(machineCode)
if err != nil {
return "", err
}
if len(proxies) == 0 {
return "", fmt.Errorf("未获取到有效代理")
}
// 随机选择一个代理
proxy := randomElement(proxies)
proxyURL := fmt.Sprintf("http://%s", proxy)
return proxyURL, nil
}
// 线程安全的随机元素选择
func randomElement(slice []string) string {
randMutex.Lock()
defer randMutex.Unlock()
return slice[globalRand.Intn(len(slice))]
}
// MachineCodeResponse 查询机器码响应结构体
type MachineCodeResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data MachineCodeData `json:"data"`
}
// MachineCodeData 机器码数据
type MachineCodeData struct {
MachineCode string `json:"machine_code"` // 机器码
IpExpTime string `json:"ip_exp_time"` // 使用时间
IpThread int `json:"ip_thread"` // 线程数
IpCardCode string `json:"ip_card_code"` // 卡密
}
// 查询机器码
func getMachineCode(tailCardSecret string) (*MachineCodeData, error) {
url := "http://36.134.51.135:7842/api/proxies/ip_show"
data := map[string]interface{}{
"ip_card_code": tailCardSecret,
"agent_id": 9999,
}
_, body, errs := gorequest.New().Post(url).Send(data).End()
if len(errs) > 0 {
return nil, fmt.Errorf("查询机器码失败: %v", errs)
}
var resp MachineCodeResponse
if err := json.Unmarshal([]byte(body), &resp); err != nil {
return nil, fmt.Errorf("解析响应失败: %v", err)
}
switch resp.Code {
case 201:
machineCode, err := rechargeCard(tailCardSecret, resp.Data.MachineCode)
if err != nil {
return nil, err
}
resp.Data.MachineCode = machineCode
return &resp.Data, nil
case 200:
return &resp.Data, nil
default:
return nil, fmt.Errorf("查询机器码失败: %s", resp.Message)
}
}
// 充值卡密
func rechargeCard(tailCardSecret, machineCode string) (string, error) {
url := "http://36.134.51.135:7842/api/proxies/ip_recharge"
data := map[string]interface{}{
"machine_code": machineCode,
"ip_card_code": tailCardSecret,
"agent_id": 9999,
}
_, body, errs := gorequest.New().Post(url).Send(data).End()
if len(errs) > 0 {
return "", fmt.Errorf("充值卡密失败: %v", errs)
}
var resp struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
IpExpTime string `json:"ip_exp_time"`
IpThread int `json:"ip_thread"`
MachineCode string `json:"machine_code"`
} `json:"data"`
}
if err := json.Unmarshal([]byte(body), &resp); err != nil {
return "", fmt.Errorf("解析响应失败: %v", err)
}
if resp.Code != 200 {
return "", fmt.Errorf("充值卡密失败: %s", resp.Message)
}
return resp.Data.MachineCode, nil
}
// 获取代理服务器列表
func getProxies(machineCode string) ([]string, error) {
// 生成签名
sign := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("9999%s9999", machineCode))))
// 构建请求URL
getProxiesUrl := fmt.Sprintf("http://36.134.51.135:7842/api/proxies/get_proxy?machine_code=%s&sign=%s&agent_id=9999",
machineCode, sign)
// 发送GET请求
req := gorequest.New().Get(getProxiesUrl).Timeout(20 * time.Second)
_, body, errs := req.End()
if len(errs) > 0 {
return nil, fmt.Errorf("获取代理失败: %v", errs)
}
// 检查是否为JSON错误响应
trimmedBody := strings.TrimSpace(body)
if strings.HasPrefix(trimmedBody, "{") && strings.HasSuffix(trimmedBody, "}") {
var errorResp struct {
Code int `json:"code"`
Message string `json:"message"`
}
if err := json.Unmarshal([]byte(body), &errorResp); err == nil {
return nil, fmt.Errorf("获取代理失败: %s (错误码: %d)", errorResp.Message, errorResp.Code)
}
}
// 解析响应
lines := strings.Split(trimmedBody, "\n")
var proxies []string
for _, line := range lines {
line = strings.TrimSpace(line)
if line != "" && !strings.HasPrefix(line, "{") {
proxies = append(proxies, line)
}
}
if len(proxies) == 0 {
return nil, fmt.Errorf("未获取到有效代理")
}
return proxies, nil
}
// 检查卡密是否过期
func checkTailCardSecretExpired(tailCardSecret string) (bool, error) {
resp, err := getMachineCode(tailCardSecret)
if err != nil {
return false, fmt.Errorf("请求错误: %v", err)
}
targetTime, err := time.Parse("2006-01-02 15:04:05", resp.IpExpTime)
if err != nil {
return false, fmt.Errorf("时间格式错误: %v", err)
}
if targetTime.After(time.Now()) {
return true, nil
}
return false, fmt.Errorf("卡密已经过期!过期时间: %v", targetTime)
}
// =================== C导出函数 =======================
// ResponseAPI 统一响应数据结构
type ResponseAPI struct {
Success bool `json:"success"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
// 生成JSON响应字符串
func jsonResponse(data interface{}, err error) string {
resp := ResponseAPI{
Success: err == nil,
Message: getErrorMsg(err),
Data: data,
}
bytes, _ := json.Marshal(resp)
return string(bytes)
}
// 获取错误信息
func getErrorMsg(err error) string {
if err != nil {
return err.Error()
}
return ""
}
// GetProxyByType 根据代理类型获取代理URL
//
//export GetProxyByType
func GetProxyByType(proxyType, machineCode *C.char) *C.char {
proxyTypeStr := C.GoString(proxyType)
machineCodeStr := C.GoString(machineCode)
proxyURL, err := getProxyByType(proxyTypeStr, machineCodeStr)
return C.CString(jsonResponse(proxyURL, err))
}
// GetTailProxyURL 获取尾巴代理URL
//
//export GetTailProxyURL
func GetTailProxyURL(machineCode *C.char) *C.char {
machineCodeStr := C.GoString(machineCode)
proxyURL, err := getTailProxyURL(machineCodeStr)
return C.CString(jsonResponse(proxyURL, err))
}
// GetMachineCode 查询机器码
//
//export GetMachineCode
func GetMachineCode(tailCardSecret *C.char) *C.char {
tailCardSecretStr := C.GoString(tailCardSecret)
resp, err := getMachineCode(tailCardSecretStr)
return C.CString(jsonResponse(resp, err))
}
// RechargeCard 充值卡密
//
//export RechargeCard
func RechargeCard(tailCardSecret, machineCode *C.char) *C.char {
goTailCardSecret := C.GoString(tailCardSecret)
goMachineCode := C.GoString(machineCode)
newMachineCode, err := rechargeCard(goTailCardSecret, goMachineCode)
if err != nil {
return C.CString(jsonResponse(nil, err))
}
response := map[string]interface{}{
"machineCode": newMachineCode,
"rechargeInfo": "充值成功",
}
return C.CString(jsonResponse(response, nil))
}
// GetProxies 获取代理服务器列表
//
//export GetProxies
func GetProxies(machineCode *C.char) *C.char {
goMachineCode := C.GoString(machineCode)
proxies, err := getProxies(goMachineCode)
if err != nil {
return C.CString(jsonResponse(nil, err))
}
response := map[string]interface{}{
"count": len(proxies),
"proxies": proxies,
}
return C.CString(jsonResponse(response, nil))
}
// CheckTailCardSecretExpired 检查卡密是否过期
//
//export CheckTailCardSecretExpired
func CheckTailCardSecretExpired(tailCardSecret *C.char) *C.char {
goTailCardSecret := C.GoString(tailCardSecret)
isValid, err := checkTailCardSecretExpired(goTailCardSecret)
if err != nil {
return C.CString(jsonResponse(nil, err))
}
response := map[string]interface{}{
"isValid": isValid,
"checkInfo": "卡密有效",
}
return C.CString(jsonResponse(response, nil))
}
// FreeCString 释放C字符串内存
//
//export FreeCString
func FreeCString(str *C.char) {
C.free(unsafe.Pointer(str))
}
// GetVersion 获取版本
//
//export GetVersion
func GetVersion() *C.char {
return C.CString("v3")
}
// 空main函数
func main() {
}