配置全部json化
This commit is contained in:
parent
d1ddf7599f
commit
2b7462c2fe
|
@ -0,0 +1,54 @@
|
|||
<!--
|
||||
* @Author: giaogiao giaogiao
|
||||
* @Date: 2023-07-31 16:50:54
|
||||
* @LastEditors: giaogiao giaogiao
|
||||
* @LastEditTime: 2023-07-31 16:53:37
|
||||
* @FilePath: \go\README.MD
|
||||
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
||||
-->
|
||||
|
||||
json conf example
|
||||
```
|
||||
{
|
||||
"settings": {
|
||||
"api_url": "http://127.0.0.1:50101/api/get_ip_list?num=1&country=#country&state=all&city=all&zip=all&isp=all&ip_time=1&t=1&port=#port",
|
||||
"detection_time": 20,
|
||||
"detection_node_url": "https://ip.shagain.club/"
|
||||
},
|
||||
"node": [
|
||||
{
|
||||
"remarks": "giao",
|
||||
"country": "UK",
|
||||
"port": 30000
|
||||
},
|
||||
{
|
||||
"remarks": "giao2",
|
||||
"country": "UK",
|
||||
"port": 30001
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
json conf explain
|
||||
```
|
||||
{
|
||||
"settings": {
|
||||
"api_url": "请求切换IP api #country #port 字符串模版",
|
||||
"detection_time": 检测间隔 单位秒,
|
||||
"detection_node_url": "检测的url"
|
||||
},
|
||||
"node": [
|
||||
{
|
||||
"remarks": "备注",
|
||||
"country": "国家代码",
|
||||
"port": 端口
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
```
|
57
main.go
57
main.go
|
@ -2,7 +2,7 @@
|
|||
* @Author: giaogiao giaogiao
|
||||
* @Date: 2023-07-28 08:53:13
|
||||
* @LastEditors: giaogiao giaogiao
|
||||
* @LastEditTime: 2023-07-28 16:22:40
|
||||
* @LastEditTime: 2023-07-31 16:50:10
|
||||
* @FilePath: \go\main.go
|
||||
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
||||
*/
|
||||
|
@ -16,56 +16,69 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/parnurzeal/gorequest"
|
||||
)
|
||||
|
||||
func read_a_file() {
|
||||
|
||||
type Node []struct {
|
||||
type Settings struct {
|
||||
APIURL string `json:"api_url"`
|
||||
DetectionTime int `json:"detection_time"`
|
||||
DetectionNodeURL string `json:"detection_node_url"`
|
||||
}
|
||||
type Node struct {
|
||||
Remarks string `json:"remarks"`
|
||||
Country string `json:"country"`
|
||||
Port int `json:"port"`
|
||||
}
|
||||
// 打开json文件
|
||||
type TooLTT struct {
|
||||
Settings Settings `json:"settings"`
|
||||
Node []Node `json:"node"`
|
||||
}
|
||||
|
||||
var tooltt TooLTT
|
||||
|
||||
func read_configuration_file() {
|
||||
jsonFile, err := os.Open("user.json")
|
||||
|
||||
// 最好要处理以下错误
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// 要记得关闭
|
||||
defer jsonFile.Close()
|
||||
|
||||
byteValue, _ := ioutil.ReadAll(jsonFile)
|
||||
var node Node
|
||||
err = json.Unmarshal(byteValue, &node)
|
||||
|
||||
err = json.Unmarshal(byteValue, &tooltt)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
for _, item := range node {
|
||||
}
|
||||
|
||||
func read_a_file(node *[]Node) {
|
||||
for _, item := range *node {
|
||||
// fmt.Println(item.Remarks, item.Country, item.Port)
|
||||
text_url(item.Remarks, item.Country, item.Port)
|
||||
}
|
||||
}
|
||||
func text_url(remarks string, country string, port int) {
|
||||
var port_string string = strconv.Itoa(port)
|
||||
setup.CwLog().Infof("start_testing country:%s,port:%d", country, port)
|
||||
request := gorequest.New().Proxy("socks5://127.0.0.1:" + strconv.Itoa(port))
|
||||
resp, body, errs := request.Get("https://ip.shagain.club/").End()
|
||||
request := gorequest.New().Proxy("socks5://127.0.0.1:" + port_string)
|
||||
resp, body, errs := request.Get(tooltt.Settings.DetectionNodeURL).End()
|
||||
if errs != nil {
|
||||
fmt.Println("发现 端口:" + strconv.Itoa(port) + "无法连接 开始切换IP")
|
||||
fmt.Println("发现 端口:" + port_string + "无法连接 开始切换IP")
|
||||
setup.CwLog().Errorf("start_testing country:%s,port:%d,error:%s", country, port, errs)
|
||||
setup.CwLog().Infof("start_switching_ip country:%s,port:%d", country, port)
|
||||
resp, err := http.Get("http://127.0.0.1:50101/api/get_ip_list?num=1&country=" + country + "&state=all&city=all&zip=all&isp=all&ip_time=1&t=1&port=" + strconv.Itoa(port))
|
||||
|
||||
var url string = strings.Replace(tooltt.Settings.APIURL, "#country", country, -1)
|
||||
resp, err := http.Get(strings.Replace(url, "#port", port_string, -1))
|
||||
if errs != nil {
|
||||
fmt.Println("切换IP 失败 端口:" + strconv.Itoa(port))
|
||||
setup.CwLog().Errorf("switch_ip_to_return error resp:%s,err:%s", resp, err)
|
||||
fmt.Println("切换IP 失败 端口:" + port_string)
|
||||
return
|
||||
}
|
||||
fmt.Println("切换IP成功 端口:" + strconv.Itoa(port))
|
||||
fmt.Println("切换IP成功 端口:" + port_string)
|
||||
setup.CwLog().Infof("switch_ip_to_return resp:%s,err:%s", resp, err)
|
||||
return
|
||||
}
|
||||
|
@ -77,13 +90,15 @@ func init() {
|
|||
setup.InitLogger() //初始化log
|
||||
}
|
||||
func main() {
|
||||
|
||||
fmt.Println("程序已经启动")
|
||||
read_configuration_file()
|
||||
setup.CwLog().Warn("start") //测试log
|
||||
for {
|
||||
timeStr := time.Now().Format("2006-01-02 15:04:05") //当前时间的字符串,2006-01-02 15:04:05据说是golang的诞生时间,固定写法
|
||||
fmt.Println("开始检测 " + timeStr) //打印结果:2017-04-11 13:24:04
|
||||
time.Sleep(20 * time.Second)
|
||||
read_a_file()
|
||||
time.Sleep(time.Duration(tooltt.Settings.DetectionTime) * time.Second)
|
||||
read_a_file(&tooltt.Node)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
12
user.json
12
user.json
|
@ -1,11 +1,19 @@
|
|||
[
|
||||
{
|
||||
"settings": {
|
||||
"api_url": "http://127.0.0.1:50101/api/get_ip_list?num=1&country=#country&state=all&city=all&zip=all&isp=all&ip_time=1&t=1&port=#port",
|
||||
"detection_time": 20,
|
||||
"detection_node_url": "https://ip.shagain.club/"
|
||||
},
|
||||
"node": [
|
||||
{
|
||||
"remarks": "giao",
|
||||
"country": "UK",
|
||||
"port": 30000
|
||||
},{
|
||||
},
|
||||
{
|
||||
"remarks": "giao2",
|
||||
"country": "UK",
|
||||
"port": 30001
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue