forked from pingc0y/go_proxy_pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebApiServer.go
129 lines (116 loc) · 2.65 KB
/
webApiServer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"encoding/json"
"github.com/gin-gonic/gin"
"log"
"strconv"
"strings"
)
type Home struct {
TunnelProxy map[string]string `yaml:"tunnelProxy" json:"tunnelProxy"`
Sum int `yaml:"sum" json:"sum"`
Type map[string]int `yaml:"type" json:"type"`
Anonymity map[string]int `yaml:"anonymity" json:"anonymity"`
Country map[string]int `yaml:"country" json:"country"`
Source map[string]int `yaml:"source" json:"source"`
}
func Run() {
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
//首页
r.GET("/", index)
//查询
r.GET("/get", get)
//删除
r.GET("/delete", deleteProxy)
//验证代理
r.GET("/verify", verify)
//抓取代理
r.GET("/spider", spiderUp)
log.Printf("webApi启动 - 监听IP端口 -> %s\n", conf.Config.Ip+":"+conf.Config.Port)
err := r.Run(conf.Config.Ip + ":" + conf.Config.Port)
if err != nil {
log.Printf("webApi启动启动失败%v", err)
return
}
}
func index(c *gin.Context) {
home := getProxyPoolStats(true)
jsonByte, _ := json.Marshal(&home)
jsonStr := string(jsonByte)
c.String(200, jsonStr)
}
func get(c *gin.Context) {
// 读取查询参数
ty := c.DefaultQuery("type", "all")
an := c.DefaultQuery("anonymity", "all")
re := c.DefaultQuery("country", "all")
co := c.DefaultQuery("count", "1")
// 解析 count 参数,默认为1
count, err := strconv.Atoi(co)
if err != nil {
c.JSON(400, gin.H{
"code": 400,
"msg": "count 参数错误",
})
return
}
// 调用 getProxyIp 函数,获取符合条件的代理
proxyList := getProxyIp(strings.Split(ty, ","), an, re, count)
jsonByte, err := json.Marshal(proxyList)
if err != nil {
c.JSON(500, gin.H{
"code": 500,
"msg": "JSON 序列化失败",
})
return
}
// 返回代理列表
c.String(200, string(jsonByte))
}
func deleteProxy(c *gin.Context) {
ip := c.Query("ip")
port := c.Query("port")
protocol := c.Query("protocol")
if ip == "" || port == "" || protocol == "" {
c.JSON(400, gin.H{
"code": 400,
"msg": "参数不能为空",
})
return
}
count := delProxy(ip, port, protocol)
c.JSON(200, gin.H{
"code": 200,
"count": count,
"msg": "删除成功",
})
}
func verify(c *gin.Context) {
if verifyIS {
c.JSON(200, gin.H{
"code": 200,
"msg": "代理池正在验证中,请稍候再试",
})
} else {
go VerifyProxy()
c.JSON(200, gin.H{
"code": 200,
"msg": "代理池开始验证",
})
}
}
func spiderUp(c *gin.Context) {
if run {
c.JSON(200, gin.H{
"code": 200,
"msg": "代理池正在抓取中,请稍候再试",
})
} else {
go spiderRun()
c.JSON(200, gin.H{
"code": 200,
"msg": "开始抓取代理IP",
})
}
}