-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (122 loc) · 3.11 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"github.com/urfave/cli/v2"
"github.com/zbysir/lookip/internal/lib/public_ip"
dns2 "github.com/zbysir/lookip/internal/pkg/dns"
"github.com/zbysir/lookip/internal/pkg/dns/alidns"
"github.com/zbysir/lookip/internal/pkg/dns/cloudflare"
"github.com/zbysir/lookip/internal/pkg/signal"
"github.com/zbysir/lookip/internal/worker"
"log"
"os"
"strings"
"sync"
)
func main() {
c := cli.NewApp()
c.Name = "lookip"
c.Usage = ""
c.Version = ""
c.Flags = []cli.Flag{
&cli.StringFlag{
Name: "name",
Usage: "e.g. '*.domain.com' or 'domain.com'",
EnvVars: []string{"NAME"},
Required: true,
},
&cli.StringFlag{
Name: "domain",
Usage: "e.g. domain.com",
EnvVars: []string{"DOMAIN"},
Required: false,
},
&cli.StringFlag{
Name: "dns",
Usage: "'ali' or 'cloudflare'",
EnvVars: []string{"DNS"},
Required: false,
},
&cli.StringFlag{
Name: "access-key-id",
Usage: "ali AccessKeyID",
EnvVars: []string{"ACCESS_KEY_ID"},
Required: false,
},
&cli.StringFlag{
Name: "access-key-secret",
Usage: "ali AccessKeySecret",
EnvVars: []string{"ACCESS_KEY_SECRET"},
Required: false,
},
&cli.StringFlag{
Name: "cf_token",
Usage: "cloudflare token",
EnvVars: []string{"CF_TOKEN"},
Required: false,
},
&cli.StringFlag{
Name: "cf_zone_id",
Usage: "cloudflare zone id",
EnvVars: []string{"CF_ZONE_ID"},
Required: false,
},
&cli.StringFlag{
Name: "region-id",
Usage: "regionId, e.g. zh-hangzhou",
EnvVars: []string{"REGION_ID"},
Required: false,
Value: "zh-hangzhou",
},
&cli.StringFlag{
Name: "ip-getter",
Usage: "ip-getter, Can use values from the following array: [httpbin(httpbin.org}, 3322(3322.net)]",
EnvVars: []string{"IP_GETTER"},
Required: false,
Value: "httpbin",
},
}
c.Action = func(c *cli.Context) error {
ctx, _ := signal.NewTermContext()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
regionId := c.String("region-id")
domain := c.String("domain")
cfZoneId := c.String("cf_zone_id")
cfToken := c.String("cf_token")
key := c.String("access-key-id")
secret := c.String("access-key-secret")
name := c.String("name")
ipGetter := c.String("ip-getter")
dnsType := c.String("dns")
var dnss []dns2.DNS
if dnsType != "" {
for _, d := range strings.Split(dnsType, ",") {
if d != "ali" && d != "cloudflare" {
log.Fatalf("dns type `%s` not support", d)
}
switch d {
case "cloudflare":
dnss = append(dnss, cloudflare.NewCF(cfToken, cfZoneId, name))
default:
dnss = append(dnss, alidns.NewAliDns(regionId, key, secret, domain, name))
}
}
} else {
dnss = append(dnss, alidns.NewAliDns(regionId, key, secret, domain, name))
}
g := public_ip.Factory(ipGetter)
log.Printf("use `%s` to get ip", g.Name())
log.Printf("use `%s` to update dns", dnsType)
w := worker.NewIpWorker(dns2.NewCombinedDNS(dnss), g)
w.LoopUpdateIp(ctx)
}()
wg.Wait()
return nil
}
err := c.Run(os.Args)
if err != nil {
panic(err)
}
}