@@ -3,39 +3,30 @@ package checks
3
3
import (
4
4
"context"
5
5
"net"
6
+
7
+ "github.com/xray-web/web-check-api/checks/clients/ip"
6
8
)
7
9
8
10
type IpAddress struct {
9
11
Address net.IP `json:"ip"`
10
12
Family int `json:"family"`
11
13
}
12
14
13
- type IpGetter interface {
14
- GetIp ( ctx context. Context , host string ) ([] IpAddress , error )
15
+ type NetIp struct {
16
+ lookup ip. Lookup
15
17
}
16
18
17
- type IpGetterFunc func (ctx context.Context , host string ) ([]IpAddress , error )
18
-
19
- func (f IpGetterFunc ) GetIp (ctx context.Context , host string ) ([]IpAddress , error ) {
20
- return f (ctx , host )
21
- }
22
-
23
- type NetIp struct {}
24
-
25
- func NewNetIp () * NetIp {
26
- return & NetIp {}
19
+ func NewNetIp (lookup ip.Lookup ) * NetIp {
20
+ return & NetIp {lookup : lookup }
27
21
}
28
22
29
23
func (l * NetIp ) GetIp (ctx context.Context , host string ) ([]IpAddress , error ) {
30
- resolver := & net.Resolver {
31
- PreferGo : true ,
32
- }
33
- ip4 , err := resolver .LookupIP (ctx , "ip4" , host )
24
+ ip4 , err := l .lookup .LookupIP (ctx , "ip4" , host )
34
25
if err != nil {
35
- return nil , err
26
+ // do nothing
36
27
}
37
- ip6 , err := resolver .LookupIP (ctx , "ip6" , host )
38
- if err != nil {
28
+ ip6 , err := l . lookup .LookupIP (ctx , "ip6" , host )
29
+ if err != nil && len ( ip4 ) == 0 && len ( ip6 ) == 0 {
39
30
return nil , err
40
31
}
41
32
@@ -49,15 +40,3 @@ func (l *NetIp) GetIp(ctx context.Context, host string) ([]IpAddress, error) {
49
40
50
41
return ipAddresses , nil
51
42
}
52
-
53
- type Ip struct {
54
- getter IpGetter
55
- }
56
-
57
- func NewIp (l IpGetter ) * Ip {
58
- return & Ip {getter : l }
59
- }
60
-
61
- func (i * Ip ) Lookup (ctx context.Context , host string ) ([]IpAddress , error ) {
62
- return i .getter .GetIp (ctx , host )
63
- }
0 commit comments