Skip to content

Commit

Permalink
fix: don't update private key after updating license key (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
pccr10001 authored Dec 19, 2023
1 parent 0fa9ee3 commit 2fe51a1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 27 deletions.
16 changes: 4 additions & 12 deletions cloudflare/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,16 @@ func GetAccount(ctx *config.Context) (*Account, error) {
return &castResult, err
}

func UpdateLicenseKey(ctx *config.Context, newPublicKey string) (*openapi.UpdateAccount200Response, *Device, error) {
func UpdateLicenseKey(ctx *config.Context) (*openapi.UpdateAccount200Response, error) {
result, _, err := globalClientAuth(ctx.AccessToken).DefaultApi.
UpdateAccount(nil, ctx.DeviceId, ApiVersion).
UpdateAccountRequest(openapi.UpdateAccountRequest{License: ctx.LicenseKey}).
Execute()
if err != nil {
return nil, nil, err
}
// change public key as per official client
result2, _, err := globalClientAuth(ctx.AccessToken).DefaultApi.
UpdateSourceDevice(nil, ApiVersion, ctx.DeviceId).
UpdateSourceDeviceRequest(openapi.UpdateSourceDeviceRequest{Key: newPublicKey}).
Execute()
castResult := Device(result2)
if err != nil {
return nil, nil, err
return nil, err
}
return &result, &castResult, nil

return &result, nil
}

type BoundDevice openapi.GetBoundDevices200Response
Expand Down
16 changes: 1 addition & 15 deletions cmd/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import (
. "github.com/ViRb3/wgcf/cmd/shared"
"github.com/ViRb3/wgcf/config"
"github.com/ViRb3/wgcf/util"
"github.com/ViRb3/wgcf/wireguard"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var deviceName string
Expand Down Expand Up @@ -82,17 +80,8 @@ func ensureLicenseKeyUpToDate(ctx *config.Context, thisDevice *cloudflare.Device
}

func updateLicenseKey(ctx *config.Context) (*cloudflare.Account, *cloudflare.Device, error) {
newPrivateKey, err := wireguard.NewPrivateKey()
if err != nil {
return nil, nil, err
}
newPublicKey := newPrivateKey.Public()
if _, _, err := cloudflare.UpdateLicenseKey(ctx, newPublicKey.String()); err != nil {
return nil, nil, err
}

viper.Set(config.PrivateKey, newPrivateKey.String())
if err := viper.WriteConfig(); err != nil {
if _, err := cloudflare.UpdateLicenseKey(ctx); err != nil {
return nil, nil, err
}

Expand All @@ -108,9 +97,6 @@ func updateLicenseKey(ctx *config.Context) (*cloudflare.Account, *cloudflare.Dev
if account.License != ctx.LicenseKey {
return nil, nil, errors.New("failed to update license key")
}
if thisDevice.Key != newPublicKey.String() {
return nil, nil, errors.New("failed to update public key")
}

return account, thisDevice, nil
}

0 comments on commit 2fe51a1

Please sign in to comment.