浏览代码

no message

xingzai 3 年之前
父节点
当前提交
998526e584
共有 1 个文件被更改,包括 15 次插入11 次删除
  1. 15 11
      services/task.go

+ 15 - 11
services/task.go

@@ -7,8 +7,8 @@ import (
 	"hongze/hongze_cygx/models"
 	"hongze/hongze_cygx/utils"
 	"html"
-	"io/ioutil"
-	"net/http"
+	"net"
+	"os"
 	"strconv"
 	"strings"
 	"time"
@@ -17,17 +17,21 @@ import (
 func Task() {
 	fmt.Println("start")
 	//同步数据
-	responseClient, errClient := http.Get("http://myexternalip.com/raw") // 获取外网 IP
-	if errClient != nil {
-		fmt.Printf("获取外网 IP 失败,请检查网络\n")
-		panic(errClient)
+	addrs, err := net.InterfaceAddrs()
+
+	if err != nil {
+		fmt.Println(err)
+		os.Exit(1)
 	}
-	// 程序在使用完 response 后必须关闭 response 的主体。
-	defer responseClient.Body.Close()
 
-	body, _ := ioutil.ReadAll(responseClient.Body)
-	clientIP := fmt.Sprintf("%s", string(body))
-	print(clientIP)
+	for _, address := range addrs {
+		// 检查ip地址判断是否回环地址
+		if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
+			if ipnet.IP.To4() != nil {
+				fmt.Println("ip:", ipnet.IP.String())
+			}
+		}
+	}
 	if utils.RunMode == "release" {
 		//syncTacticsListAddreport := task.NewTask("syncTacticsListAddreport", "0 */5 * * * *", SyncTacticsListAddreport) //同步文章
 		//task.AddTask("syncTacticsListAddreport", syncTacticsListAddreport)