diff --git a/cmd/headscale/cli/preauthkeys.go b/cmd/headscale/cli/preauthkeys.go index 20c23aee..514866e0 100644 --- a/cmd/headscale/cli/preauthkeys.go +++ b/cmd/headscale/cli/preauthkeys.go @@ -142,17 +142,6 @@ var createPreAuthKeyCmd = &cobra.Command{ subnet, _ := cmd.Flags().GetString("subnet") - if subnet != "" { - ipPrefix, err := netaddr.ParseIPPrefix(subnet) - if err != nil { - ErrorOutput(err, fmt.Sprintf("Error parsing subnet: %s", err), output) - - return - } - - subnet = ipPrefix.String() - } - if !reusable && subnet == "" { ip, _ := cmd.Flags().GetString("ip") if ip != "" { diff --git a/preauth_keys.go b/preauth_keys.go index 5d15c472..01c54cc1 100644 --- a/preauth_keys.go +++ b/preauth_keys.go @@ -10,6 +10,7 @@ import ( v1 "github.com/juanfont/headscale/gen/go/headscale/v1" "google.golang.org/protobuf/types/known/timestamppb" "gorm.io/gorm" + "inet.af/netaddr" ) const ( @@ -63,6 +64,15 @@ func (h *Headscale) CreatePreAuthKeyWithSubnet( return nil, err } + if subnet != "" { + ipPrefix, err := netaddr.ParseIPPrefix(subnet) + if err != nil { + return nil, err + } + + subnet = ipPrefix.String() + } + key := PreAuthKey{ Key: kstr, NamespaceID: namespace.ID,