配置全部json化

This commit is contained in:
giaogiao 2023-07-31 16:54:22 +08:00
parent d1ddf7599f
commit 2b7462c2fe
3 changed files with 112 additions and 35 deletions

54
README.md Normal file
View File

@ -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": 端口
}
]
}
```

59
main.go
View File

@ -2,7 +2,7 @@
* @Author: giaogiao giaogiao * @Author: giaogiao giaogiao
* @Date: 2023-07-28 08:53:13 * @Date: 2023-07-28 08:53:13
* @LastEditors: giaogiao giaogiao * @LastEditors: giaogiao giaogiao
* @LastEditTime: 2023-07-28 16:22:40 * @LastEditTime: 2023-07-31 16:50:10
* @FilePath: \go\main.go * @FilePath: \go\main.go
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/ */
@ -16,56 +16,69 @@ import (
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/parnurzeal/gorequest" "github.com/parnurzeal/gorequest"
) )
func read_a_file() { type Settings struct {
APIURL string `json:"api_url"`
type Node []struct { DetectionTime int `json:"detection_time"`
DetectionNodeURL string `json:"detection_node_url"`
}
type Node struct {
Remarks string `json:"remarks"` Remarks string `json:"remarks"`
Country string `json:"country"` Country string `json:"country"`
Port int `json:"port"` 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") jsonFile, err := os.Open("user.json")
// 最好要处理以下错误
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
// 要记得关闭
defer jsonFile.Close() defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile) byteValue, _ := ioutil.ReadAll(jsonFile)
var node Node
err = json.Unmarshal(byteValue, &node) err = json.Unmarshal(byteValue, &tooltt)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
for _, item := range node { }
func read_a_file(node *[]Node) {
for _, item := range *node {
// fmt.Println(item.Remarks, item.Country, item.Port) // fmt.Println(item.Remarks, item.Country, item.Port)
text_url(item.Remarks, item.Country, item.Port) text_url(item.Remarks, item.Country, item.Port)
} }
} }
func text_url(remarks string, country string, port int) { 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) setup.CwLog().Infof("start_testing country:%s,port:%d", country, port)
request := gorequest.New().Proxy("socks5://127.0.0.1:" + strconv.Itoa(port)) request := gorequest.New().Proxy("socks5://127.0.0.1:" + port_string)
resp, body, errs := request.Get("https://ip.shagain.club/").End() resp, body, errs := request.Get(tooltt.Settings.DetectionNodeURL).End()
if errs != nil { 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().Errorf("start_testing country:%s,port:%d,error:%s", country, port, errs)
setup.CwLog().Infof("start_switching_ip country:%s,port:%d", country, port) 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 { 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 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) setup.CwLog().Infof("switch_ip_to_return resp:%s,err:%s", resp, err)
return return
} }
@ -77,13 +90,15 @@ func init() {
setup.InitLogger() //初始化log setup.InitLogger() //初始化log
} }
func main() { func main() {
fmt.Println("程序已经启动") fmt.Println("程序已经启动")
read_configuration_file()
setup.CwLog().Warn("start") //测试log setup.CwLog().Warn("start") //测试log
for { for {
timeStr := time.Now().Format("2006-01-02 15:04:05") //当前时间的字符串2006-01-02 15:04:05据说是golang的诞生时间固定写法 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 fmt.Println("开始检测 " + timeStr) //打印结果2017-04-11 13:24:04
time.Sleep(20 * time.Second) time.Sleep(time.Duration(tooltt.Settings.DetectionTime) * time.Second)
read_a_file() read_a_file(&tooltt.Node)
} }
} }

View File

@ -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", "remarks": "giao",
"country": "UK", "country": "UK",
"port": 30000 "port": 30000
},{ },
{
"remarks": "giao2", "remarks": "giao2",
"country": "UK", "country": "UK",
"port": 30001 "port": 30001
} }
] ]
}