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