-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (120 loc) · 3.15 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
package main
import (
"context"
"errors"
"fmt"
"os"
"slices"
"github.com/cloudflare/cloudflare-go"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
// Before will setup the logger.
func Before(c *cli.Context) error {
if c.Bool("json") {
// Configure the JSON logger if enabled.
logrus.SetFormatter(&logrus.JSONFormatter{})
}
if c.Bool("debug") {
// Set the debug log level if enabled.
logrus.SetLevel(logrus.DebugLevel)
}
return nil
}
// Action will perform the update operation.
func Action(c *cli.Context) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var api *cloudflare.API
var err error
if c.String("token") != "" {
api, err = cloudflare.NewWithAPIToken(c.String("token"))
if err != nil {
return cli.Exit(err.Error(), 1)
}
} else if c.String("key") != "" && c.String("email") != "" {
api, err = cloudflare.New(c.String("key"), c.String("email"))
if err != nil {
return cli.Exit(err.Error(), 1)
}
} else {
return cli.Exit("either --key and --email or --token must be defined", 1)
}
var errs []error
update := c.StringSlice("update")
if slices.Contains(update, "ip4") {
if err := UpdateDomain4(ctx, api, c.String("zone"), c.String("domain"), c.String("ipurl")); err != nil {
errs = append(errs, err)
}
}
if slices.Contains(update, "ip6") {
if err := UpdateDomain6(ctx, api, c.String("zone"), c.String("domain"), c.String("ipurl")); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
func main() {
app := cli.NewApp()
app.Name = "cloudflare-ddns"
app.Version = fmt.Sprintf("%v, commit %v, built at %v", version, commit, date)
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "token",
EnvVars: []string{"CF_API_TOKEN"},
Usage: "The API Token that has the Zone.DNS permission for the specific zone.",
},
&cli.StringFlag{
Name: "key",
EnvVars: []string{"CF_API_KEY"},
Usage: "The Global (not CA) Cloudflare API Key generated on the \"My Account\" page.",
},
&cli.StringFlag{
Name: "email",
EnvVars: []string{"CF_API_EMAIL"},
Usage: "Email address associated with your Cloudflare account.",
},
&cli.StringFlag{
Name: "zone",
Required: true,
EnvVars: []string{"CF_ZONE"},
Usage: "Zone",
},
&cli.StringFlag{
Name: "domain",
Required: true,
EnvVars: []string{"CF_DOMAIN"},
Usage: "Comma separated domain names that should be updated. (i.e. mypage.example.com OR example.com)",
},
&cli.StringFlag{
Name: "ipurl",
Value: "https://domains.google.com/checkip",
EnvVars: []string{"CF_IP_URL"},
Usage: "Alternative ip address service endpoint.",
},
&cli.StringSliceFlag{
Name: "update",
Value: cli.NewStringSlice("ip4", "ip6"),
EnvVars: []string{"CF_IP_UPDATE"},
Usage: "ip4 or ip6",
},
&cli.BoolFlag{
Name: "debug",
Usage: "Enables debug logging.",
},
&cli.BoolFlag{
Name: "json",
Usage: "Enables JSON output for the logging.",
},
}
app.Before = Before
app.Action = Action
if err := app.Run(os.Args); err != nil {
logrus.WithError(err).Fatal()
}
}