|
@@ -0,0 +1,153 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "io"
|
|
|
+ "net"
|
|
|
+ "net/http"
|
|
|
+ "net/url"
|
|
|
+ "os/exec"
|
|
|
+ qu "qfw/util"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+func main() {
|
|
|
+ go timeTaskSendMsg_SendIp() //IP代理中心推送消息
|
|
|
+ go func() {
|
|
|
+ //time.Sleep(2 * time.Minute) //2分钟后开始切换IP
|
|
|
+ for {
|
|
|
+ runChangeIP_SendIp()
|
|
|
+ time.Sleep(time.Duration(config.Interval) * time.Minute)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ lock := make(chan bool)
|
|
|
+ <-lock
|
|
|
+}
|
|
|
+
|
|
|
+func runChangeIP_SendIp() {
|
|
|
+ if changing { //防止同时切换IP
|
|
|
+ return
|
|
|
+ }
|
|
|
+ available = false //正在切换ip,设置ip已不可用
|
|
|
+ changing = true //正在切换ip
|
|
|
+ fmt.Println("准备切换IP...")
|
|
|
+ //发布要换IP了
|
|
|
+ changeIP_SendIp() //切换ip
|
|
|
+ available = true
|
|
|
+ changing = false //ip切换完成
|
|
|
+ lifeTime = time.Now().Unix() + int64(config.Interval)*60 - 10 //ip有效时间(默认减少10秒)
|
|
|
+ ischangedfirst = true //开启定时检测的标记
|
|
|
+ fmt.Println("完成切换IP...")
|
|
|
+ //ip切换成功,推送消息
|
|
|
+}
|
|
|
+func changeIP_SendIp() {
|
|
|
+ for {
|
|
|
+ fmt.Println("start change ip...")
|
|
|
+ exec.Command("pppoe-stop").Run()
|
|
|
+ exec.Command("pppoe-start").Run()
|
|
|
+ fmt.Println("wait...")
|
|
|
+ time.Sleep(10 * time.Second) //避免换IP后无法获取IP
|
|
|
+ newIp := getMyIp() //获取新IP
|
|
|
+ if newIp != "" && newIp != myoldip {
|
|
|
+ fmt.Println("公网IP由", myoldip, "换为", newIp)
|
|
|
+ ds, us := checkSpeed_SendIp(config.CheckSpeedAddr) //测速
|
|
|
+ if ds >= config.DownSpeedLimit && us >= config.UploadSpeedLimit { //网速达标
|
|
|
+ fmt.Println("IP测速通过", newIp)
|
|
|
+ myoldip = newIp
|
|
|
+ break
|
|
|
+ } else {
|
|
|
+ fmt.Println("IP测速不通过", newIp, ds, us)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ fmt.Println("获取新IP失败...")
|
|
|
+ }
|
|
|
+ time.Sleep(5 * time.Second)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//测速
|
|
|
+func checkSpeed_SendIp(addr string) (float64, float64) {
|
|
|
+ conn, err := net.Dial("tcp", addr)
|
|
|
+ if err != nil {
|
|
|
+ return 0, 0
|
|
|
+ }
|
|
|
+ defer conn.Close()
|
|
|
+ t1 := time.Now()
|
|
|
+ buf := make([]byte, 1024)
|
|
|
+ for i := 0; i < 50; i++ {
|
|
|
+ io.ReadFull(conn, buf)
|
|
|
+ }
|
|
|
+ dd := time.Since(t1)
|
|
|
+ t1 = time.Now()
|
|
|
+ for i := 0; i < 50; i++ {
|
|
|
+ conn.Write(buf)
|
|
|
+ }
|
|
|
+ for {
|
|
|
+ _, err := conn.Write(buf)
|
|
|
+ if err != nil {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ time.Sleep(10 * time.Millisecond)
|
|
|
+ }
|
|
|
+ ud := time.Since(t1)
|
|
|
+ dp, up := float64(0.05)/dd.Seconds(), float64(0.05)/ud.Seconds()
|
|
|
+ msg := fmt.Sprintf(MSG_TPL, config.VpsName, float64(0.05)/dd.Seconds(), float64(0.05)/ud.Seconds())
|
|
|
+ fmt.Println("网速测试,", msg)
|
|
|
+ return dp, up
|
|
|
+}
|
|
|
+func timeTaskSendMsg_SendIp() {
|
|
|
+ if config.SendIp {
|
|
|
+ for {
|
|
|
+ if !ischangedfirst { //服务启动,切换一次IP,再推送消息
|
|
|
+ time.Sleep(10 * time.Second)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ sPorts, fPorts := checkIpAndPort_SendIp() //检测IP和端口可用性
|
|
|
+ //推送消息
|
|
|
+ value := url.Values{
|
|
|
+ "name": []string{config.VpsName}, //vps服务器名称(唯一)
|
|
|
+ "ip": []string{qu.SE.EncodeString(myoldip)}, //加密ip
|
|
|
+ "lifetime": []string{fmt.Sprint(lifeTime)}, //ip有效期
|
|
|
+ "okport": sPorts, //可用端口
|
|
|
+ "noport": fPorts, //不可用端口
|
|
|
+ "available": []string{fmt.Sprint(available)}, //ip是否可用
|
|
|
+ "changing": []string{fmt.Sprint(changing)}, //是否正在切换ip
|
|
|
+ "iptype": []string{config.IpType}, //ip类型 0:普通vps; 1:阿里云; 2:linux_vps
|
|
|
+ }
|
|
|
+ for i := 1; i <= 3; i++ {
|
|
|
+ client := &http.Client{
|
|
|
+ Timeout: 3 * time.Second, // 设置超时时间为5秒
|
|
|
+ }
|
|
|
+ // 发送POST请求
|
|
|
+ resp, err := client.PostForm(config.SendIpAddr, value)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("POST请求失败:", err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ resp.Body.Close()
|
|
|
+ break
|
|
|
+ }
|
|
|
+ fmt.Println("---send success---", time.Now().Unix())
|
|
|
+ time.Sleep(time.Duration(config.SendInterval) * time.Second)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func checkIpAndPort_SendIp() (sPorts, fPorts []string) {
|
|
|
+ //检测IP
|
|
|
+ if !changing { //正在切换IP,不做测速检测
|
|
|
+ fmt.Println("测速...")
|
|
|
+ ds, us := checkSpeed_SendIp(config.CheckSpeedAddr) //测速
|
|
|
+ if ds < config.DownSpeedLimit || us < config.UploadSpeedLimit { //网速不达标,切换IP
|
|
|
+ go runChangeIP_SendIp() //重新切换IP
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fmt.Println("检测端口...")
|
|
|
+ //检测端口
|
|
|
+ sPorts, fPorts = checkIpAndPort_SendIp()
|
|
|
+ if len(fPorts) == len(config.Ports) { //端口都不可用,重启端口
|
|
|
+ go reStartPorts() //本轮检测重启,不可用
|
|
|
+ }
|
|
|
+ time.Sleep(1 * time.Second)
|
|
|
+ return
|
|
|
+}
|