|
|
@ -5,14 +5,11 @@ import ( |
|
|
|
"io/fs" |
|
|
|
"io/fs" |
|
|
|
"net/netip" |
|
|
|
"net/netip" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"git.jpi.io/amery/jpictl/pkg/rings" |
|
|
|
"git.jpi.io/amery/jpictl/pkg/wireguard" |
|
|
|
"git.jpi.io/amery/jpictl/pkg/wireguard" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
const ( |
|
|
|
// MaxZoneID indicates the highest ID allowed for a Zone
|
|
|
|
|
|
|
|
MaxZoneID = 0xf |
|
|
|
|
|
|
|
// MaxNodeID indicates the highest Machine ID allowed within a Zone
|
|
|
|
|
|
|
|
MaxNodeID = 0xff - 1 |
|
|
|
|
|
|
|
// RingsCount indicates how many wireguard rings we have
|
|
|
|
// RingsCount indicates how many wireguard rings we have
|
|
|
|
RingsCount = 2 |
|
|
|
RingsCount = 2 |
|
|
|
// RingZeroPort is the port wireguard uses for ring0
|
|
|
|
// RingZeroPort is the port wireguard uses for ring0
|
|
|
@ -81,8 +78,8 @@ func canMergeKeyPairs(p1, p2 wireguard.KeyPair) bool { |
|
|
|
type RingAddressEncoder struct { |
|
|
|
type RingAddressEncoder struct { |
|
|
|
ID int |
|
|
|
ID int |
|
|
|
Port uint16 |
|
|
|
Port uint16 |
|
|
|
Encode func(zoneID, nodeID int) (netip.Addr, bool) |
|
|
|
Encode func(zoneID rings.ZoneID, nodeID rings.NodeID) (netip.Addr, bool) |
|
|
|
Decode func(addr netip.Addr) (zoneID, nodeID int, ok bool) |
|
|
|
Decode func(addr netip.Addr) (zoneID rings.ZoneID, nodeID rings.NodeID, ok bool) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
var ( |
|
|
@ -110,42 +107,34 @@ var ( |
|
|
|
// ValidZoneID checks if the given zoneID is a valid 4 bit zone number.
|
|
|
|
// ValidZoneID checks if the given zoneID is a valid 4 bit zone number.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// 0 is reserved, and only allowed when composing CIDRs.
|
|
|
|
// 0 is reserved, and only allowed when composing CIDRs.
|
|
|
|
func ValidZoneID(zoneID int) bool { |
|
|
|
func ValidZoneID(zoneID rings.ZoneID) bool { |
|
|
|
switch { |
|
|
|
return zoneID == 0 || zoneID.Valid() |
|
|
|
case zoneID < 0 || zoneID > MaxZoneID: |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
return true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ValidNodeID checks if the given nodeID is a valid 8 bit number.
|
|
|
|
// ValidNodeID checks if the given nodeID is a valid 8 bit number.
|
|
|
|
// nodeID is unique within a Zone.
|
|
|
|
// nodeID is unique within a Zone.
|
|
|
|
// 0 is reserved, and only allowed when composing CIDRs.
|
|
|
|
// 0 is reserved, and only allowed when composing CIDRs.
|
|
|
|
func ValidNodeID(nodeID int) bool { |
|
|
|
func ValidNodeID(nodeID rings.NodeID) bool { |
|
|
|
switch { |
|
|
|
return nodeID == 0 || nodeID.Valid() |
|
|
|
case nodeID < 0 || nodeID > MaxNodeID: |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
return true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ParseRingZeroAddress extracts zone and node ID from a wg0 [netip.Addr]
|
|
|
|
// ParseRingZeroAddress extracts zone and node ID from a wg0 [netip.Addr]
|
|
|
|
// wg0 addresses are of the form `10.0.{{zoneID}}.{{nodeID}}`
|
|
|
|
// wg0 addresses are of the form `10.0.{{zoneID}}.{{nodeID}}`
|
|
|
|
func ParseRingZeroAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) { |
|
|
|
func ParseRingZeroAddress(addr netip.Addr) (zoneID rings.ZoneID, nodeID rings.NodeID, ok bool) { |
|
|
|
if addr.IsValid() { |
|
|
|
if addr.IsValid() { |
|
|
|
a4 := addr.As4() |
|
|
|
a4 := addr.As4() |
|
|
|
|
|
|
|
|
|
|
|
if a4[0] == 10 && a4[1] == 0 { |
|
|
|
if a4[0] == 10 && a4[1] == 0 { |
|
|
|
return int(a4[2]), int(a4[3]), true |
|
|
|
zoneID = rings.ZoneID(a4[2]) |
|
|
|
|
|
|
|
nodeID = rings.NodeID(a4[3]) |
|
|
|
|
|
|
|
return zoneID, nodeID, true |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return 0, 0, false |
|
|
|
return 0, 0, false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// RingZeroAddress returns a wg0 IP address
|
|
|
|
// RingZeroAddress returns a wg0 IP address
|
|
|
|
func RingZeroAddress(zoneID, nodeID int) (netip.Addr, bool) { |
|
|
|
func RingZeroAddress(zoneID rings.ZoneID, nodeID rings.NodeID) (netip.Addr, bool) { |
|
|
|
switch { |
|
|
|
switch { |
|
|
|
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID): |
|
|
|
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID): |
|
|
|
return netip.Addr{}, false |
|
|
|
return netip.Addr{}, false |
|
|
@ -157,13 +146,13 @@ func RingZeroAddress(zoneID, nodeID int) (netip.Addr, bool) { |
|
|
|
|
|
|
|
|
|
|
|
// ParseRingOneAddress extracts zone and node ID from a wg1 [netip.Addr]
|
|
|
|
// ParseRingOneAddress extracts zone and node ID from a wg1 [netip.Addr]
|
|
|
|
// wg1 addresses are of the form `10.{{zoneID << 4}}.{{nodeID}}`
|
|
|
|
// wg1 addresses are of the form `10.{{zoneID << 4}}.{{nodeID}}`
|
|
|
|
func ParseRingOneAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) { |
|
|
|
func ParseRingOneAddress(addr netip.Addr) (zoneID rings.ZoneID, nodeID rings.NodeID, ok bool) { |
|
|
|
if addr.IsValid() { |
|
|
|
if addr.IsValid() { |
|
|
|
a4 := addr.As4() |
|
|
|
a4 := addr.As4() |
|
|
|
|
|
|
|
|
|
|
|
if a4[0] == 10 && a4[2] == 0 { |
|
|
|
if a4[0] == 10 && a4[2] == 0 { |
|
|
|
zoneID = int(a4[1] >> 4) |
|
|
|
zoneID = rings.ZoneID(a4[1] >> 4) |
|
|
|
nodeID = int(a4[3]) |
|
|
|
nodeID = rings.NodeID(a4[3]) |
|
|
|
return zoneID, nodeID, true |
|
|
|
return zoneID, nodeID, true |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -171,7 +160,7 @@ func ParseRingOneAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// RingOneAddress returns a wg1 IP address
|
|
|
|
// RingOneAddress returns a wg1 IP address
|
|
|
|
func RingOneAddress(zoneID, nodeID int) (netip.Addr, bool) { |
|
|
|
func RingOneAddress(zoneID rings.ZoneID, nodeID rings.NodeID) (netip.Addr, bool) { |
|
|
|
switch { |
|
|
|
switch { |
|
|
|
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID): |
|
|
|
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID): |
|
|
|
return netip.Addr{}, false |
|
|
|
return netip.Addr{}, false |
|
|
|