fix: Assign IP to wg device
Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
parent
ca29b792fb
commit
4bab068c10
@ -165,7 +165,7 @@ func createCliApp() *cli.App {
|
||||
app.Flags = append(app.Flags, &wgPort)
|
||||
|
||||
httpListenAddr := cli.StringFlag{
|
||||
Name: "host",
|
||||
Name: "http-host",
|
||||
Usage: "IP address to listen on for HTTP API requests",
|
||||
Value: "0.0.0.0",
|
||||
Action: func(ctx *cli.Context, s string) error {
|
||||
@ -362,6 +362,12 @@ func setup(ctx *cli.Context) error {
|
||||
IPPool = ipPool
|
||||
wgLink.IPPool = ipPool
|
||||
|
||||
err = wgLink.SetIP()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
slog.Debug("Assigned IP to Wiregaurd interface")
|
||||
|
||||
//defer wgLink.Close()
|
||||
cInput := make(chan struct{})
|
||||
go handleStdin(cInput)
|
||||
|
@ -12,6 +12,7 @@ type IPPool interface {
|
||||
Allocate() (net.IP, error)
|
||||
Free(net.IP) error
|
||||
Remove(...net.IP) error
|
||||
Network() net.IPNet
|
||||
}
|
||||
|
||||
// Pool is a pool of available IP numbers for allocation.
|
||||
@ -94,6 +95,10 @@ func (p *Pool) Free(ip net.IP) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Pool) Network() net.IPNet {
|
||||
return *p.network
|
||||
}
|
||||
|
||||
// ip4To6 will prefix IPv4 with the IPv6 network to create an IPv6 address.
|
||||
func ip4To6(ip4 net.IP, ip6prefix *net.IPNet) (ip6 net.IP) {
|
||||
b6 := ip6prefix.IP.To16()
|
||||
|
22
link.go
22
link.go
@ -2,8 +2,6 @@ package hvpnnode3
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
@ -86,9 +84,27 @@ func (wg *WGLink) initClient() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (wg *WGLink) SetIP() error {
|
||||
ip, err := wg.Allocate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ipnet := net.IPNet{
|
||||
IP: ip,
|
||||
Mask: wg.Network().Mask,
|
||||
}
|
||||
netlinkIP, err := netlink.ParseAddr(ipnet.String())
|
||||
|
||||
err = netlink.AddrAdd(wg, netlinkIP)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Adds a peer to the wireguard netlink.
|
||||
func (wg *WGLink) AddPeer(publicKey string) (*wgtypes.Peer, error) {
|
||||
slog.Debug(fmt.Sprintf("Trying to add peer %s", publicKey))
|
||||
pubKey, err := wgtypes.ParseKey(publicKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
Loading…
Reference in New Issue
Block a user