-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.go
127 lines (105 loc) · 2.52 KB
/
misc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* File: misc.go
* Author: Ming Cheng<[email protected]>
*
* Created Date: Saturday, December 26th 2020, 7:21:58 pm
* Last Modified: Sunday, December 27th 2020, 8:40:05 pm
*
* http://www.opensource.org/licenses/MIT
*/
package simplyddns
import (
"fmt"
"net"
"net/http"
"sync"
tld "github.com/jpillora/go-tld"
"github.com/sirupsen/logrus"
"golang.org/x/net/proxy"
)
// ProxyHttpClient to create http client with socks5 proxy
func ProxyHttpClient(addr string) (*http.Client, error) {
// setup a http client
httpTransport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
}
// create a socks5 dialer
dialer, err := proxy.SOCKS5("tcp", addr, nil, proxy.Direct)
if err != nil {
return nil, err
}
// set our socks5 as the dialer
if contextDialer, ok := dialer.(proxy.ContextDialer); ok {
httpTransport.DialContext = contextDialer.DialContext
}
return &http.Client{
Transport: httpTransport,
}, nil
}
// https://github.com/jpillora/go-tld
func ParseDomain(domain string) (*tld.URL, error) {
if u, err := tld.Parse(fmt.Sprintf("http://%s/foo", domain)); err != nil {
return nil, err
} else {
if !u.ICANN && (u.Domain == "" && u.TLD == "") {
return nil, fmt.Errorf("%v is not a vaildate domain", domain)
}
return u, nil
}
}
// NewLogger to return logger instance
var (
log *logrus.Logger
once sync.Once
)
func NewLogger() *logrus.Logger {
once.Do(func() {
log = logrus.New()
})
return log
}
// func init() {
// log = NewLogger()
// }
// ValidateRecords 批量验证 DNS 域名是否已经是对应的 IP 地址
func ValidateRecords(domains []string, addr *net.IP) error {
for _, domain := range domains {
if _, err := ParseDomain(domain); err != nil {
return err
}
if err := ValidateRecord(domain, addr); err != nil {
return err
}
}
return nil
}
// ValidateRecord 批量验证 DNS 域名是否已经是对应的 IP 地址
func ValidateRecord(domain string, addr *net.IP) error {
found := false
if records, err := net.LookupIP(domain); err != nil {
return err
} else {
for _, record := range records {
if record.Equal(*addr) {
found = true
}
}
if found {
return nil
}
}
return fmt.Errorf("domain %s is not found address %s", domain, addr.String())
}
// ValidateConfig 验证配置对象是否合适
func ValidateConfig(config *JobConfig) error {
if config == nil {
return fmt.Errorf("configure is nil")
}
// check domain
for _, domain := range config.Target.Domains {
if _, err := ParseDomain(domain); err != nil {
return err
}
}
return nil
}