go_chang922_ip/main.go

122 lines
3.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @Author: giaogiao giaogiao
* @Date: 2023-07-28 08:53:13
* @LastEditors: giaogiao giaogiao
* @LastEditTime: 2023-07-31 17:32:36
* @FilePath: \go\main.go
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
package main
import (
"encoding/json"
"fmt"
"hello/setup"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/parnurzeal/gorequest"
)
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"`
}
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)
err = json.Unmarshal(byteValue, &tooltt)
if err != nil {
fmt.Println(err)
return
}
//fmt.Println(len(tooltt.Node))求结构体数组大小
}
func read_a_file(node *[]Node) {
var wg sync.WaitGroup
wg.Add(len(*node))
for _, item := range *node {
// fmt.Println(item.Remarks, item.Country, item.Port)
go text_url(item.Remarks, item.Country, item.Port, wg.Done)
}
wg.Wait()
}
func text_url(remarks string, country string, port int, done func()) {
defer done()
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:" + port_string)
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++
}
}
resp, body, errs := request.Get(tooltt.Settings.DetectionNodeURL).End()
if errs != nil {
fmt.Println("发现 端口:" + port_string + "无法连接3次 开始切换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)
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 {
setup.CwLog().Errorf("switch_ip_to_return error resp:%s,err:%s", resp, err)
fmt.Println("切换IP 失败 端口:" + port_string)
return
}
fmt.Println("切换IP成功 端口:" + port_string)
setup.CwLog().Infof("switch_ip_to_return resp:%s,err:%s", resp, err)
return
}
setup.CwLog().Infof("requestToReturnTheResult resp:%s,body:%s", resp, body)
setup.CwLog().Infof("the_current_port_is_fine port:%d", port)
}
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(time.Duration(tooltt.Settings.DetectionTime) * time.Second)
read_a_file(&tooltt.Node)
}
}