-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (104 loc) · 2.5 KB
/
main.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
package main
import (
"io"
"net"
"net/http"
"net/url"
"regexp"
"sync"
"time"
cf "github.com/m4n5ter/dyndns/cloudflare"
"github.com/m4n5ter/dyndns/config"
dp "github.com/m4n5ter/dyndns/dnspod"
"github.com/m4n5ter/log"
)
var (
Config config.Config
PublicIp net.Addr
Logger = log.Default().WithGroup("MAIN")
wg sync.WaitGroup
)
func init() {
Config.CheckIpUrl = "http://checkip.dyndns.com/"
dp.Logger = Logger.WithGroup("DP")
config.Logger = Logger.WithGroup("CONFIG")
cf.Logger = Logger.WithGroup("CF")
}
func main() {
config.Load(&Config)
// 获取公网 IP
PublicIp = getPublicIp()
// 启动 DDNS
wg.Add(2)
go func() {
dp.DDNS(PublicIp, Config.DnspodConfig)
wg.Done()
}()
go func() {
cf.DDNS(PublicIp, Config.CloudflareConfig)
wg.Done()
}()
wg.Wait()
}
// 获取 public ip TODO: 可以提供更多的 public ip 获取方式,并且可以在其中一个获取失败时切换到另一个
func getPublicIp() net.Addr {
Logger.Info("正在获取公网IP,请稍后...", "check_ip_url", Config.CheckIpUrl)
conn := connectUrl(Config.CheckIpUrl)
defer conn.Close()
// 组装一个使用自定义连接的 http client
transport := &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
return conn, nil
},
}
client := &http.Client{
Timeout: time.Second * 15,
Transport: transport,
}
response, err := client.Get(Config.CheckIpUrl)
if err != nil {
Logger.Panicf("请求 %s 失败: %v\n", Config.CheckIpUrl, err)
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
Logger.Panicf("获取公网IP失败: %s\n", err)
}
re := regexp.MustCompile(`(?m)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})`)
ipStr := re.FindString(string(body))
pip, err := net.ResolveIPAddr("ip", ipStr)
if err != nil {
Logger.Panicf("解析公网IP失败: %s\n", err)
}
Logger.Info("公网IP获取成功:", "ip", pip)
return pip
}
// connectUrl 生成一个优先使用 IPv4 的连接
func connectUrl(uri string) net.Conn {
parsedUrl, err := url.Parse(uri)
if err != nil {
Logger.Panicf("解析 %s 失败: %v\n", uri, err)
}
host := parsedUrl.Hostname()
ips, err := net.LookupIP(host)
if err != nil {
Logger.Panicf("Lookup %s 失败: %v\n", host, err)
}
var ip net.IP
// v4
for _, i := range ips {
if i.To4() != nil {
ip = i
break
}
}
// v6
if ip == nil {
ip = ips[0].To16()
}
conn, err := net.Dial("tcp", ip.String()+":443")
if err != nil {
Logger.Panicf("连接 %s 失败: %v\n", Config.CheckIpUrl, err)
}
return conn
}