-
Notifications
You must be signed in to change notification settings - Fork 646
/
update.go
100 lines (88 loc) 路 2.59 KB
/
update.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
package update
import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
"wgcf/cloudflare/api"
"wgcf/cloudflare/structs/resp"
. "wgcf/cmd/util"
"wgcf/config"
"wgcf/util"
)
var deviceName string
var shortMsg = "Updates the current Cloudflare Warp account, preparing it for connection"
var Cmd = &cobra.Command{
Use: "update",
Short: shortMsg,
Long: FormatMessage(shortMsg, `
If a new/different license key is provided, the current device will be bound to the new key and its parent account.
Please note that there is a maximum limit of 5 active devices linked to the same account at a given time.
Will change various account settings to ensure WireGuard connection will succeed.`),
Run: func(cmd *cobra.Command, args []string) {
if err := updateAccount(); err != nil {
log.Fatal(util.GetErrorMessage(err))
}
},
}
func init() {
Cmd.PersistentFlags().StringVarP(&deviceName, "name", "n", "", "Device name displayed under the 1.1.1.1 app")
}
func updateAccount() error {
if !ValidAccount() {
return errors.New("no account detected")
}
ctx := CreateContext()
deviceData, err := api.GetDeviceData(ctx)
if err != nil {
return err
}
boundDevice, err := api.GetBoundDevice(ctx)
if err != nil {
return err
}
if err := ensureLicenseKeyUpdated(ctx, deviceData, boundDevice); err != nil {
return err
}
if err := EnsureDeviceActive(ctx, boundDevice); err != nil {
return err
}
PrintDeviceData(deviceData, boundDevice)
log.Println("Successfully updated Cloudflare Warp account")
return nil
}
// modifies device pointers if updated
func ensureLicenseKeyUpdated(ctx *config.Context, deviceData *resp.DeviceData, device *resp.BoundDevice) error {
if deviceData.Account.LicenseKey != ctx.LicenseKey {
log.Println("Updated license key detected, re-binding device to new account")
if _, err := updateLicenseKey(ctx); err != nil {
return err
}
if _, err := SetDeviceName(ctx, deviceName); err != nil {
return err
}
// make sure new license key is synced with config file if it came from other stream (e.g. env variable)
if err := viper.WriteConfig(); err != nil {
return err
}
newDeviceData, err := api.GetDeviceData(ctx)
if err != nil {
return err
}
newBoundDevice, err := api.GetBoundDevice(ctx)
if err != nil {
return err
}
*deviceData = *newDeviceData
*device = *newBoundDevice
}
return nil
}
func updateLicenseKey(ctx *config.Context) (*resp.UpdateLicenseData, error) {
accountData, err := api.UpdateLicenseKey(ctx)
if err != nil {
return nil, err
}
//TODO: Check if key actually updated
return accountData, nil
}