-
Notifications
You must be signed in to change notification settings - Fork 646
/
generate.go
73 lines (64 loc) 路 1.72 KB
/
generate.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
package generate
import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
"wgcf/cloudflare/api"
. "wgcf/cmd/util"
"wgcf/config"
"wgcf/util"
"wgcf/wireguard"
)
var profileFile string
var shortMsg = "Generates a WireGuard profile from the current Cloudflare Warp account"
var Cmd = &cobra.Command{
Use: "generate",
Short: shortMsg,
Long: FormatMessage(shortMsg, `
Will change various account settings to ensure WireGuard connection will succeed.`),
Run: func(cmd *cobra.Command, args []string) {
if err := generateProfile(); err != nil {
log.Fatal(util.GetErrorMessage(err))
}
},
}
func init() {
Cmd.PersistentFlags().StringVarP(&profileFile, "profile", "p", "wgcf-profile.conf", "WireGuard profile file")
}
func generateProfile() 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 := EnsureWarpEnabled(ctx, deviceData); err != nil {
return err
}
if err := EnsureDeviceActive(ctx, boundDevice); err != nil {
return err
}
profile, err := wireguard.NewProfile(&wireguard.ProfileData{
PrivateKey: viper.GetString(config.PrivateKey),
Address1: deviceData.Config.Interface.Addresses.V4,
Address2: deviceData.Config.Interface.Addresses.V6,
PublicKey: deviceData.Config.Peers[0].PublicKey,
Endpoint: deviceData.Config.Peers[0].Endpoint.Host,
})
if err != nil {
return err
}
if err := profile.Save(profileFile); err != nil {
return err
}
PrintDeviceData(deviceData, boundDevice)
log.Println("Successfully generated WireGuard profile:", profileFile)
return nil
}