Skip to content

Commit

Permalink
predefined network
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziomal12 committed Apr 10, 2024
1 parent 3ed2f08 commit ba1ea87
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions management/server/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net"
"sync"
"time"
"os"
"encoding/json"

Check failure on line 9 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (386, jsonfile)

"encoding/json" imported and not used

Check failure on line 9 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test_client_on_docker

"encoding/json" imported and not used

Check failure on line 9 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (386, sqlite)

"encoding/json" imported and not used

Check failure on line 9 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

"encoding/json" imported and not used) (typecheck)

Check failure on line 9 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

"encoding/json" imported and not used) (typecheck)

Check failure on line 9 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

"encoding/json" imported and not used) (typecheck)

Check failure on line 9 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

"encoding/json" imported and not used) (typecheck)

Check failure on line 9 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

"encoding/json" imported and not used) (typecheck)

"github.com/c-robinson/iplib"
"github.com/rs/xid"
Expand Down Expand Up @@ -47,20 +49,46 @@ type Network struct {

// NewNetwork creates a new Network initializing it with a Serial=0
// It takes a random /16 subnet from 100.64.0.0/10 (64 different subnets)
func NewNetwork() *Network {

n := iplib.NewNet4(net.ParseIP("100.64.0.0"), NetSize)
sub, _ := n.Subnet(SubnetSize)

s := rand.NewSource(time.Now().Unix())
r := rand.New(s)
intn := r.Intn(len(sub))
// GetPredefinedNetwork parses the environment variable 'NETBIRD_PREDEFINED_NETWORK' if set
// and returns the subnet in CIDR notation, otherwise returns an empty string
func GetPredefinedNetwork() string {
network := os.Getenv("NETBIRD_PREDEFINED_NETWORK")
return network
}

return &Network{
Identifier: xid.New().String(),
Net: sub[intn].IPNet,
Dns: "",
Serial: 0}
// NewNetwork creates a new Network initializing it with a Serial=0
// If the 'NETBIRD_PREDEFINED_NETWORK' environment variable is set, it uses that network, otherwise generates a random /16 subnet from 100.64.0.0/10
func NewNetwork() *Network {
predefinedNetwork := GetPredefinedNetwork()

if predefinedNetwork != "" {
_, ipNet, err := net.ParseCIDR(predefinedNetwork)
if err != nil {
// Handle error parsing the subnet
panic(err)
}

return &Network{
Identifier: xid.New().String(),
Net: *ipNet,
Dns: "",
Serial: 0,
}
} else {
n := iplib.NewNet4(net.ParseIP("100.64.0.0"), NetSize)
sub, _ := n.Subnet(SubnetSize)

s := rand.NewSource(time.Now().Unix())
r := rand.New(s)
intn := r.Intn(len(sub))

return &Network{
Identifier: xid.New().String(),
Net: sub[intn].IPNet,
Dns: "",
Serial: 0,
}
}
}

// IncSerial increments Serial by 1 reflecting that the network state has been changed
Expand Down

0 comments on commit ba1ea87

Please sign in to comment.