-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarp_account.go
100 lines (81 loc) · 2.66 KB
/
warp_account.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 main
import (
"errors"
"fmt"
"os"
wgcfCloudflare "github.com/ViRb3/wgcf/v2/cloudflare"
wgcfCmdShared "github.com/ViRb3/wgcf/v2/cmd/shared"
wgcfConfig "github.com/ViRb3/wgcf/v2/config"
wgcfWireguard "github.com/ViRb3/wgcf/v2/wireguard"
"github.com/pelletier/go-toml/v2"
)
type Account struct {
PrivateKey string `toml:"private_key"`
DeviceId string `toml:"device_id"`
AccessToken string `toml:"access_token"`
LicenseKey string `toml:"license_key"`
ThisDevice *wgcfCloudflare.Device `toml:"this_device"`
}
func (acc *Account) tryToLoadFromConfig(accountConfigPath string) (wasSuccessful bool) {
wgcfAccountToml, err := os.ReadFile(accountConfigPath)
if err != nil {
return false
}
err = toml.Unmarshal(wgcfAccountToml, acc)
if err != nil {
return false
}
if acc.PrivateKey == "" || acc.DeviceId == "" || acc.AccessToken == "" || acc.LicenseKey == "" || acc.ThisDevice == nil {
return false
}
return true
}
func (acc *Account) saveToConfig(accountConfigPath string) error {
file, err := os.Create(accountConfigPath)
if err != nil {
return fmt.Errorf("could not create a config file at '%s': %v", accountConfigPath, err)
}
defer file.Close() // todo: handle .Close() errors?
err = toml.NewEncoder(file).Encode(acc)
if err != nil {
return fmt.Errorf("could not marshall an Account into '%s': %v", accountConfigPath, err)
}
return nil
}
func (acc *Account) register() error {
var err error
privateKey, err := wgcfWireguard.NewPrivateKey()
if err != nil {
return fmt.Errorf("could not generate a wireguard private key: %v", err)
}
acc.PrivateKey = privateKey.String()
device, err := wgcfCloudflare.Register(privateKey.Public(), "PC")
if err != nil {
return err
}
acc.DeviceId = device.Id
acc.AccessToken = device.Token
acc.LicenseKey = device.Account.License
wgcfAccountContext := &wgcfConfig.Context{
DeviceId: acc.DeviceId,
AccessToken: acc.AccessToken,
PrivateKey: acc.PrivateKey,
LicenseKey: acc.LicenseKey,
}
_, err = wgcfCmdShared.SetDeviceName(wgcfAccountContext, "")
if err != nil {
return fmt.Errorf("could not set cloudflare warp device name: %v", err)
}
acc.ThisDevice, err = wgcfCloudflare.GetSourceDevice(wgcfAccountContext)
if err != nil {
return fmt.Errorf("could not get cloudflare warp source device: %v", err)
}
boundDevice, err := wgcfCloudflare.UpdateSourceBoundDeviceActive(wgcfAccountContext, true)
if err != nil {
return fmt.Errorf("could not set current device to active: %v", err)
}
if !boundDevice.Active {
return errors.New("current cloudflare warp device did not become active after activating")
}
return nil
}