Browse Source

no message

xingzai 3 years ago
parent
commit
2756a2544f
1 changed files with 11 additions and 19 deletions
  1. 11 19
      services/task.go

+ 11 - 19
services/task.go

@@ -7,7 +7,8 @@ import (
 	"hongze/hongze_cygx/models"
 	"hongze/hongze_cygx/utils"
 	"html"
-	"net"
+	"io/ioutil"
+	"net/http"
 	"strconv"
 	"strings"
 	"time"
@@ -16,26 +17,17 @@ import (
 func Task() {
 	fmt.Println("start")
 	//同步数据
-	var (
-		addrs   []net.Addr
-		addr    net.Addr
-		ipNet   *net.IPNet // IP地址
-		isIpNet bool
-	)
-
-	addrs, _ = net.InterfaceAddrs()
-	// 取第一个非lo的网卡IP
-	for _, addr = range addrs {
-		// 这个网络地址是IP地址: ipv4, ipv6
-		if ipNet, isIpNet = addr.(*net.IPNet); isIpNet && !ipNet.IP.IsLoopback() {
-			// 跳过IPV6
-			if ipNet.IP.To4() != nil {
-				ipv4 := ipNet.IP.String() // 192.168.1.1
-				fmt.Println(ipv4)
-			}
-		}
+	responseClient, errClient := http.Get("http://myexternalip.com/raw") // 获取外网 IP
+	if errClient != nil {
+		fmt.Printf("获取外网 IP 失败,请检查网络\n")
+		panic(errClient)
 	}
+	// 程序在使用完 response 后必须关闭 response 的主体。
+	defer responseClient.Body.Close()
 
+	body, _ := ioutil.ReadAll(responseClient.Body)
+	clientIP := fmt.Sprintf("%s", string(body))
+	print(clientIP)
 	if utils.RunMode == "release" {
 		//syncTacticsListAddreport := task.NewTask("syncTacticsListAddreport", "0 */5 * * * *", SyncTacticsListAddreport) //同步文章
 		//task.AddTask("syncTacticsListAddreport", syncTacticsListAddreport)