go_chang922_ip/main.go

122 lines
3.6 KiB
Go
Raw Permalink Normal View History

2023-07-28 08:57:50 +08:00
/*
* @Author: giaogiao giaogiao
* @Date: 2023-07-28 08:53:13
* @LastEditors: giaogiao giaogiao
2023-07-31 17:46:59 +08:00
* @LastEditTime: 2023-07-31 17:32:36
2023-07-28 08:57:50 +08:00
* @FilePath: \go\main.go
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
package main
import (
2023-07-28 15:10:48 +08:00
"encoding/json"
2023-07-28 08:57:50 +08:00
"fmt"
2023-07-28 15:26:22 +08:00
"hello/setup"
2023-07-28 15:10:48 +08:00
"io/ioutil"
2023-07-28 15:31:48 +08:00
"net/http"
2023-07-28 15:10:48 +08:00
"os"
"strconv"
2023-07-31 16:54:22 +08:00
"strings"
2023-07-31 17:46:59 +08:00
"sync"
2023-07-28 15:10:48 +08:00
"time"
"github.com/parnurzeal/gorequest"
2023-07-28 08:57:50 +08:00
)
2023-07-31 16:54:22 +08:00
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"`
}
2023-07-28 15:10:48 +08:00
2023-07-31 16:54:22 +08:00
var tooltt TooLTT
2023-07-28 15:10:48 +08:00
2023-07-31 16:54:22 +08:00
func read_configuration_file() {
jsonFile, err := os.Open("user.json")
2023-07-28 15:10:48 +08:00
if err != nil {
fmt.Println(err)
2023-07-28 16:11:32 +08:00
return
2023-07-28 15:10:48 +08:00
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
2023-07-31 16:54:22 +08:00
err = json.Unmarshal(byteValue, &tooltt)
2023-07-28 15:10:48 +08:00
if err != nil {
fmt.Println(err)
2023-07-28 16:11:32 +08:00
return
2023-07-28 15:10:48 +08:00
}
2023-07-31 17:46:59 +08:00
//fmt.Println(len(tooltt.Node))求结构体数组大小
2023-07-31 16:54:22 +08:00
}
func read_a_file(node *[]Node) {
2023-07-31 17:46:59 +08:00
var wg sync.WaitGroup
wg.Add(len(*node))
2023-07-31 16:54:22 +08:00
for _, item := range *node {
2023-07-28 15:10:48 +08:00
// fmt.Println(item.Remarks, item.Country, item.Port)
2023-07-31 17:46:59 +08:00
go text_url(item.Remarks, item.Country, item.Port, wg.Done)
2023-07-28 15:10:48 +08:00
}
2023-07-31 17:46:59 +08:00
wg.Wait()
2023-07-28 08:57:50 +08:00
}
2023-07-31 17:46:59 +08:00
func text_url(remarks string, country string, port int, done func()) {
defer done()
2023-07-31 16:54:22 +08:00
var port_string string = strconv.Itoa(port)
2023-07-28 15:26:22 +08:00
setup.CwLog().Infof("start_testing country:%s,port:%d", country, port)
2023-07-31 16:54:22 +08:00
request := gorequest.New().Proxy("socks5://127.0.0.1:" + port_string)
2023-11-18 10:02:59 +08:00
sum := 0
for i := 0; i <= 3; i++ {
_, _, errs := request.Get(tooltt.Settings.DetectionNodeURL).End()
if errs != nil {
fmt.Println("发现 端口:" + port_string + "无法连接 第" + strconv.Itoa(sum) + "次数")
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)
sum++
}
}
2023-07-31 16:54:22 +08:00
resp, body, errs := request.Get(tooltt.Settings.DetectionNodeURL).End()
2023-07-28 15:10:48 +08:00
if errs != nil {
2023-11-18 10:02:59 +08:00
fmt.Println("发现 端口:" + port_string + "无法连接3次 开始切换IP")
2023-07-28 15:26:22 +08:00
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)
2023-07-31 16:54:22 +08:00
var url string = strings.Replace(tooltt.Settings.APIURL, "#country", country, -1)
resp, err := http.Get(strings.Replace(url, "#port", port_string, -1))
2023-07-28 16:11:32 +08:00
if errs != nil {
2023-07-31 16:54:22 +08:00
setup.CwLog().Errorf("switch_ip_to_return error resp:%s,err:%s", resp, err)
fmt.Println("切换IP 失败 端口:" + port_string)
2023-07-28 16:11:32 +08:00
return
}
2023-07-31 16:54:22 +08:00
fmt.Println("切换IP成功 端口:" + port_string)
2023-07-28 15:31:48 +08:00
setup.CwLog().Infof("switch_ip_to_return resp:%s,err:%s", resp, err)
2023-07-28 16:11:32 +08:00
return
2023-07-28 15:10:48 +08:00
}
2023-07-28 15:26:22 +08:00
setup.CwLog().Infof("requestToReturnTheResult resp:%s,body:%s", resp, body)
setup.CwLog().Infof("the_current_port_is_fine port:%d", port)
2023-07-28 08:57:50 +08:00
}
2023-07-28 15:10:48 +08:00
2023-07-28 15:26:22 +08:00
func init() {
setup.InitLogger() //初始化log
}
2023-07-28 08:57:50 +08:00
func main() {
2023-07-31 16:54:22 +08:00
2023-07-28 16:11:32 +08:00
fmt.Println("程序已经启动")
2023-07-31 16:54:22 +08:00
read_configuration_file()
2023-07-28 15:26:22 +08:00
setup.CwLog().Warn("start") //测试log
2023-07-28 15:10:48 +08:00
for {
2023-07-28 16:11:32 +08:00
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
2023-07-31 16:54:22 +08:00
time.Sleep(time.Duration(tooltt.Settings.DetectionTime) * time.Second)
read_a_file(&tooltt.Node)
2023-07-28 15:10:48 +08:00
}
2023-07-28 08:57:50 +08:00
}