mirror of
https://github.com/juanfont/headscale.git
synced 2025-08-14 13:51:01 +02:00
Parse IP with netaddr
This commit is contained in:
parent
167925b264
commit
3eeb2757e0
@ -3,7 +3,6 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
||||||
@ -11,6 +10,7 @@ import (
|
|||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"google.golang.org/protobuf/types/known/timestamppb"
|
"google.golang.org/protobuf/types/known/timestamppb"
|
||||||
|
"inet.af/netaddr"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -142,15 +142,30 @@ var createPreAuthKeyCmd = &cobra.Command{
|
|||||||
|
|
||||||
subnet, _ := cmd.Flags().GetString("subnet")
|
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 == "" {
|
if !reusable && subnet == "" {
|
||||||
ip, _ := cmd.Flags().GetString("ip")
|
ip, _ := cmd.Flags().GetString("ip")
|
||||||
if ip != "" {
|
if ip != "" {
|
||||||
// If IP is in CIDR notation, strip the last octet
|
// Parse IP and convert to prefix
|
||||||
if strings.Contains(ip, "/") {
|
ipAddr, err := netaddr.ParseIP(ip)
|
||||||
ip = strings.Split(ip, "/")[0]
|
|
||||||
|
if err != nil {
|
||||||
|
ErrorOutput(err, fmt.Sprintf("Error parsing IP address: %s", err), output)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
subnet = ip + "/32"
|
ipPrefix := netaddr.IPPrefixFrom(ipAddr, 32)
|
||||||
|
subnet = ipPrefix.String()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user