Browse Source

更新细节

Signed-off-by: allan716 <[email protected]>
allan716 3 years ago
parent
commit
ae842fda30
2 changed files with 62 additions and 7 deletions
  1. 61 6
      pkg/rod_helper/rod_base.go
  2. 1 1
      pkg/rod_helper/rod_base_test.go

+ 61 - 6
pkg/rod_helper/rod_base.go

@@ -13,6 +13,8 @@ import (
 	"sync"
 	"time"
 
+	"github.com/allanpk716/ChineseSubFinder/pkg/regex_things"
+
 	"github.com/allanpk716/ChineseSubFinder/pkg/global_value"
 	"github.com/allanpk716/ChineseSubFinder/pkg/my_folder"
 	"github.com/allanpk716/ChineseSubFinder/pkg/my_util"
@@ -268,13 +270,21 @@ func PageNavigate(page *rod.Page, desURL string, timeOut time.Duration) (*rod.Pa
 	Status := e.Response.Status
 	ResponseURL := e.Response.URL
 
-	if Status != 200 {
+	if Status >= 400 {
+		publicIP := "xx.xx.xx.xx"
+		publicIP, err = GetPublicIP(page, timeOut, nil)
+		if err != nil {
+			return nil, 0, "", errors.New(fmt.Sprintf("status code >= 400, PublicIP: %v, Status is %d, ResponseURL is %v", publicIP, Status, ResponseURL))
+		}
 		if page != nil {
-			page.Close()
+			_ = page.Close()
 		}
-		return nil, Status, ResponseURL, errors.New(fmt.Sprintf("status code is not 200, is %d, ResponseURL is %v", Status, ResponseURL))
+		return nil, Status, ResponseURL, errors.New(fmt.Sprintf("status code >= 400, PublicIP: %v, Status is %d, ResponseURL is %v", publicIP, Status, ResponseURL))
 	}
 
+	// 出去前把 TimeOUt 取消了
+	page = page.CancelTimeout()
+
 	return page, Status, ResponseURL, nil
 }
 
@@ -324,16 +334,61 @@ func PageNavigateWithProxy(page *rod.Page, proxyUrl string, desURL string, timeO
 	Status := e.Response.Status
 	ResponseURL := e.Response.URL
 
-	if Status != 200 {
+	if Status >= 400 {
+
+		publicIP := "xx.xx.xx.xx"
+		publicIP, err = GetPublicIP(page, timeOut, nil)
+		if err != nil {
+			return nil, 0, "", errors.New(fmt.Sprintf("status code >= 400, PublicIP: %v, Status is %d, ResponseURL is %v", publicIP, Status, ResponseURL))
+		}
 		if page != nil {
-			page.Close()
+			_ = page.Close()
 		}
-		return nil, Status, ResponseURL, errors.New(fmt.Sprintf("status code is not 200, is %d, ResponseURL is %v", Status, ResponseURL))
+		return nil, Status, ResponseURL, errors.New(fmt.Sprintf("status code >= 400, PublicIP: %v, Status is %d, ResponseURL is %v", publicIP, Status, ResponseURL))
 	}
 
+	// 出去前把 TimeOUt 取消了
+	page = page.CancelTimeout()
+
 	return page, Status, ResponseURL, nil
 }
 
+func GetPublicIP(page *rod.Page, timeOut time.Duration, customDectIPSites []string) (string, error) {
+	defPublicIPSites := []string{
+		"https://myip.biturl.top/",
+		"https://ip4.seeip.org/",
+		"https://ipecho.net/plain",
+		"https://api-ipv4.ip.sb/ip",
+		"https://api.ipify.org/",
+		"http://myexternalip.com/raw",
+	}
+
+	customPublicIPSites := make([]string, 0)
+	if customDectIPSites != nil {
+		customPublicIPSites = append(customPublicIPSites, customDectIPSites...)
+	} else {
+		customPublicIPSites = append(customPublicIPSites, defPublicIPSites...)
+	}
+
+	for _, publicIPSite := range customPublicIPSites {
+
+		publicIPPage, _, _, err := PageNavigate(page, publicIPSite, timeOut)
+		if err != nil {
+			return "", err
+		}
+		html, err := publicIPPage.HTML()
+		if err != nil {
+			return "", err
+		}
+		matcheds := regex_things.ReMatchIP.FindAllString(html, -1)
+		if html != "" && matcheds != nil && len(matcheds) >= 1 {
+			return matcheds[0], nil
+		}
+	}
+
+	return "", errors.New("get public ip failed")
+}
+
 func HttpGetFromBrowser(browser *rod.Browser, inputUrl string, tt time.Duration, debugMode ...bool) (string, *rod.Page, error) {
 
 	page, _, _, err := NewPageNavigate(browser, inputUrl, tt, debugMode...)

+ 1 - 1
pkg/rod_helper/rod_base_test.go

@@ -31,7 +31,7 @@ func TestNewBrowser(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	println(status, responseUrl)
+	println("Status:", status, "ResponseUrl:", responseUrl)
 
 	page, err = browser.Page(proto.TargetCreateTarget{URL: desURL})
 	if err != nil {