From 2b7462c2fe10e7ef471a175abc38b491920a6501 Mon Sep 17 00:00:00 2001 From: giaogiao Date: Mon, 31 Jul 2023 16:54:22 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=85=A8=E9=83=A8json?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 54 +++++++++++++++++++++++++++++++++++++++++++++++ main.go | 63 ++++++++++++++++++++++++++++++++++--------------------- user.json | 30 ++++++++++++++++---------- 3 files changed, 112 insertions(+), 35 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..b8462c1 --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ + + +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": 端口 + } + ] +} + + +``` diff --git a/main.go b/main.go index 6524633..2b6f453 100644 --- a/main.go +++ b/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 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"` +} +type TooLTT struct { + Settings Settings `json:"settings"` + Node []Node `json:"node"` +} - type Node []struct { - Remarks string `json:"remarks"` - Country string `json:"country"` - Port int `json:"port"` - } - // 打开json文件 +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) } } diff --git a/user.json b/user.json index 09caa4d..2521ebc 100644 --- a/user.json +++ b/user.json @@ -1,11 +1,19 @@ -[ - { - "remarks": "giao", - "country": "UK", - "port": 30000 - },{ - "remarks": "giao2", - "country": "UK", - "port": 30001 - } -] \ No newline at end of file +{ + "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 + } + ] +}