Skip to content
This repository was archived by the owner on Sep 25, 2019. It is now read-only.

Commit 45448f6

Browse files
committed
fix ip monitor
1 parent ba8368b commit 45448f6

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

cron/fetcher.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func getDetectedItem() {
2626
}
2727

2828
for _, s := range stras {
29-
_, domain, _ := utils.ParseUrl(s.Url)
29+
_, domain, _, _ := utils.ParseUrl(s.Url)
3030
ipIdcArr := getIpAndIdc(domain)
3131

3232
for _, tmp := range ipIdcArr {
@@ -53,13 +53,22 @@ func getIpAndIdc(domain string) []g.IpIdc {
5353
}
5454

5555
ipIdcArr := make([]g.IpIdc, 0)
56-
ips, _ := utils.LookupIP(domain, 5000)
57-
for _, ip := range ips {
56+
57+
if utils.IsIP(domain) {
5858
var tmp g.IpIdc
59-
tmp.Ip = ip
59+
tmp.Ip = domain
6060
tmp.Idc = "default"
6161
ipIdcArr = append(ipIdcArr, tmp)
62+
} else {
63+
ips, _ := utils.LookupIP(domain, 5000)
64+
for _, ip := range ips {
65+
var tmp g.IpIdc
66+
tmp.Ip = ip
67+
tmp.Idc = "default"
68+
ipIdcArr = append(ipIdcArr, tmp)
69+
}
6270
}
71+
6372
return ipIdcArr
6473
}
6574

@@ -76,8 +85,13 @@ func newDetectedItem(s *model.Strategy, ip string, idc string) g.DetectedItem {
7685
Timeout: s.Timeout,
7786
}
7887

79-
schema, domain, path := utils.ParseUrl(s.Url)
80-
detectedItem.Target = schema + "//" + ip + path
88+
schema, domain, port, path := utils.ParseUrl(s.Url)
89+
if port == "" {
90+
detectedItem.Target = schema + "//" + ip + path
91+
} else {
92+
detectedItem.Target = schema + "//" + ip + ":" + port + path
93+
}
94+
8195
detectedItem.Domain = domain
8296

8397
return detectedItem

utils/utils.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"fmt"
5+
"log"
56
"regexp"
67
"strings"
78
"time"
@@ -28,17 +29,22 @@ func IsIP(ip string) bool {
2829
return false
2930
}
3031

31-
func ParseUrl(target string) (string, string, string) {
32-
var path string
32+
func ParseUrl(target string) (schema, host, port, path string) {
3333
targetArr := strings.Split(target, "//")
34-
schema := targetArr[0]
34+
schema = targetArr[0]
3535
url := strings.Split(targetArr[1], "/")
36-
host := url[0]
36+
addrArr := strings.Split(url[0], ":")
37+
if len(addrArr) == 2 {
38+
host = addrArr[0]
39+
port = addrArr[1]
40+
} else {
41+
host = url[0]
42+
}
3743

3844
for _, seg := range url[1:] {
3945
path += ("/" + seg)
4046
}
41-
return schema, host, path
47+
return
4248
}
4349

4450
func KeysOfMap(m map[string]string) []string {

0 commit comments

Comments
 (0)