Skip to content

Commit

Permalink
allocation working inellegagntly but not adding peer
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgmiller committed Dec 30, 2023
1 parent e114ec7 commit fb1162f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
18 changes: 1 addition & 17 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -36,21 +35,6 @@ func init() {
//probably have to pass in public ip and maybe cidr?
}

// move to udpjoinut i guess
type cidrAllocatorImpl struct{}

func (c cidrAllocatorImpl) Allocate() (net.IP, error) {
return net.ParseIP("10.0.0.100"), nil
}

func (c cidrAllocatorImpl) CIDR() *net.IPNet {
_, net, err := net.ParseCIDR("10.0.0.0/24")
if err != nil {
panic(err)
}
return net
}

// this is for testing please don't use
type dumbpassword string

Expand Down Expand Up @@ -85,7 +69,7 @@ func serve(cmd *cobra.Command, args []string) error {
//get this lazily for each add.
wg, err := wghelpers.WithCidr("10.0.0.0/24")
if err != nil {
return err
return fmt.Errorf("error getting wg device: %s", err)
}
joiner := udpjoin.New(t, wg, wg)
if password != "" {
Expand Down
2 changes: 1 addition & 1 deletion pretty/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func New(p wgtypes.Peer) Peer {

func Ini(iniFile *ini.File, peers ...Peer) error {
for _, p := range peers {
sec, err := iniFile.NewSection("PEER")
sec, err := iniFile.NewSection("Peer")
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion udpjoin/udpjoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ func (j *joiner) GenerateResponse(jreq Request) (Response, error) {
//if we crash here we lose the ip. Combine allocator and wg device?
//so that allocate takes a public key and adds the peer
//wierd to add the slash /32.
j.dev.AddPeer(jreq.PublicKey, assignedip)
if err := j.dev.AddPeer(jreq.PublicKey, assignedip); err != nil {
return Response{}, err
}
}

//add the peer to us before we return anything
Expand Down
40 changes: 35 additions & 5 deletions wghelpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wghelpers

import (
"fmt"
"log"
"net"

"github.com/paulgmiller/wg-sync/nethelpers"
Expand Down Expand Up @@ -32,24 +33,52 @@ func (wg *wghelper) CIDR() *net.IPNet {
}

func (wg *wghelper) Allocate() (net.IP, error) {
var candidate net.IP
var candidate net.IP = make([]byte, len(wg.firstip))
log.Printf("checking %s", wg.firstip)
copy(candidate, wg.firstip)

myAddr, err := nethelpers.GetWireGaurdCIDR(wg.d.Name)
if err != nil {
return net.IP{}, fmt.Errorf("no more ips left in %s", wg.cidr)
}
myip, _, err := net.ParseCIDR(myAddr.String())
if err != nil {
return net.IP{}, fmt.Errorf("couldn't parse %s", myAddr.String())
}
log.Printf("my ip is %s", myip.String())

for {
inc(candidate)
log.Printf("checking %s", candidate)

if myip.String() == candidate.String() {
continue
}

if !wg.cidr.Contains(candidate) {
return net.IP{}, fmt.Errorf("no more ips left in %s", wg.cidr)
}

inUse := false
for _, p := range wg.d.Peers {
for _, used := range p.AllowedIPs {
if !used.Contains(candidate) {
return candidate, nil
log.Printf("checking %s", used.String())
if used.Contains(candidate) {
log.Printf("ip %s already in use by %s", candidate, p.PublicKey.String())
inUse = true
break
}
}
if inUse {
break
}
}
if !inUse {
return candidate, nil
}
inc(candidate)
}
}

// just use https://pkg.go.dev/net/netip#Addr.Next
func inc(ip net.IP) {
for i := len(ip) - 1; i >= 0; i-- {
ip[i]++
Expand Down Expand Up @@ -109,6 +138,7 @@ func (wg *wghelper) AddPeer(publickey, cidr string) error {
if err != nil {
return err
}
log.Printf("adding peer %s -> %v", peer.PublicKey, peer.AllowedIPs[0].String())
return wgc.ConfigureDevice(wg.d.Name, wgtypes.Config{

Peers: []wgtypes.PeerConfig{
Expand Down

0 comments on commit fb1162f

Please sign in to comment.