Refactoring

This commit is contained in:
Kekskurse 2022-02-27 05:08:06 +01:00
parent 4e08e8410a
commit f7c8ef9bfc
Signed by: kekskurse
GPG key ID: 728ACCB59341E7E4
8 changed files with 215 additions and 129 deletions

View file

@ -7,30 +7,31 @@ import (
) )
func main() { func main() {
serverConfig := wgfunc.ServerConfig{ serverConfig := wgfunc.WireGuardConfig{
Name: "sample-server", Name: "sample-server",
IPRanges: []string{"10.0.1.0/24"}, IPRanges: []string{"10.0.1.0/24"},
Port: 0, Port: 0,
PrivateKey: "kDUIomFdDOW0VaOMS6Vu8+1rw/8SemeobSa7ZN66J38=\n", PrivateKey: "kDUIomFdDOW0VaOMS6Vu8+1rw/8SemeobSa7ZN66J38=",
} }
server, err := wgfunc.NewServer(serverConfig) server, err := wgfunc.NewWireGuard(serverConfig)
if err != nil { if err != nil {
panic(err) panic(err)
} }
fmt.Println("Start Server") fmt.Println("Start WireGuard")
err = server.Up() err = server.Up()
if err != nil { if err != nil {
server.Down() server.Down()
panic(err) panic(err)
} }
defer server.Down()
fmt.Println("Server online") fmt.Println("WireGuard online")
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
fmt.Println("Shoutdown server") fmt.Println("Shoutdown server")
err = server.Down() err = server.Down()
if err != nil { if err != nil {
panic(err) panic(err)
}1 }
fmt.Println("bye") fmt.Println("bye")
} }

View file

@ -1 +1,4 @@
Easy way to manage wireaguard "server" and "client" Todo:
Server in Connection umbennen, so das clients auch genutzt werden können
Connections ohne port

View file

@ -11,10 +11,3 @@ type Client struct {
} }
func NewClient(config ClientConfig) Client {
}
func (c *Client)Connect() error {
}

View file

@ -0,0 +1,65 @@
package wgfunc
import (
"github.com/stretchr/testify/assert"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"testing"
"time"
)
func TestConnection(t *testing.T) {
//sinnlos aber hey entiwkclung und so
key, _ := wgtypes.GeneratePrivateKey()
clientKey, _ := wgtypes.GeneratePrivateKey()
serverConfig := WireGuardConfig{
Name: "wgUnit",
IPRanges: []string{"10.0.0.1/24"},
Port: 54123,
PrivateKey: key.String(),
}
server, err := NewWireGuard(serverConfig)
assert.Nil(t, err, "cant create server")
err = server.Up()
defer server.Down()
foundDevice := checkForNetworkDevice("wgUnit", t)
assert.True(t, foundDevice, "should fount device with name wgUnit")
peerConfigServer := PeerConfig{
PublicKey: clientKey.PublicKey().String(),
PreSharedKey: "",
AllowedIPs: []string{"10.0.0.4/32"},
Endpoint: "",
}
server.AddPeer(peerConfigServer)
//Client
time.Sleep(10*time.Second)
clientConfig := WireGuardConfig{
Name: "wgclient",
IPRanges: []string{"10.0.0.4/32"},
PrivateKey: clientKey.String(),
}
client, err := NewWireGuard(clientConfig)
assert.Nil(t, err, "should be able to create client config")
client.Up()
defer client.Down()
peerConfigClient := PeerConfig{
PublicKey: key.PublicKey().String(),
PreSharedKey: "",
AllowedIPs: []string{"10.0.0.1/24"},
Endpoint: "127.0.0.1:54123",
}
client.AddPeer(peerConfigClient)
}

69
pkg/wgfunc/device.go Normal file
View file

@ -0,0 +1,69 @@
package wgfunc
import "github.com/vishvananda/netlink"
type Device struct {
name string
iPRanges []string
wgDev *netlink.GenericLink
}
func (s *Device) createNetworkDevice() (error) {
la := netlink.NewLinkAttrs()
la.Name = s.name
wgDev := &netlink.GenericLink{
LinkAttrs: la,
LinkType: "wireguard",
}
err := netlink.LinkAdd(wgDev)
if err != nil {
return err
}
s.wgDev = wgDev
return nil
}
func (s *Device) upNetworkDevice() (error) {
err := netlink.LinkSetUp(s.wgDev) //todo: test
if err != nil {
return err
}
return nil
}
func (s *Device) removeNetworkDevice() (error) {
la := netlink.NewLinkAttrs()
la.Name = s.name
wgDev := &netlink.GenericLink{
LinkAttrs: la,
LinkType: "wireguard",
}
err := netlink.LinkDel(wgDev)
return err
}
func (s *WireGuard) addIPToNetwork() (error) {
var ips []*netlink.Addr
for _, iprange := range s.iPRanges {
ip, err := netlink.ParseAddr(iprange)
if err != nil {
return err
}
ips = append(ips, ip)
}
for _, ip := range ips {
err := netlink.AddrAdd(s.wgDev, ip)
if err != nil { //ToDo: Check how to test this case
return err
}
}
return nil
}

31
pkg/wgfunc/device_test.go Normal file
View file

@ -0,0 +1,31 @@
package wgfunc
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestCreateAndDeleteDevice(t *testing.T) {
serverConfig := WireGuardConfig{
Name: "wgUnit",
}
server, err := NewWireGuard(serverConfig)
err = server.createNetworkDevice()
if err != nil {
assert.Nil(t, err, "shouldn't return error creating the network Device: %s", err)
}
foundDevice := checkForNetworkDevice("wgUnit", t)
assert.Truef(t, foundDevice, "should fount device with name wgUnit")
err = server.removeNetworkDevice()
if err != nil {
assert.Nil(t, err, "shouldn't return an error doing remove the network device again")
}
foundDevice = checkForNetworkDevice("wgUnit", t)
assert.Falsef(t, foundDevice, "should not fount device with name wgUnit")
}

View file

@ -3,7 +3,6 @@ package wgfunc
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/vishvananda/netlink"
"golang.zx2c4.com/wireguard/wgctrl" "golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"net" "net"
@ -13,35 +12,35 @@ import (
"time" "time"
) )
type Server struct {
name string
iPRanges []string
wgDev *netlink.GenericLink type WireGuard struct {
Device
wgConfig wgtypes.Config wgConfig wgtypes.Config
wgClient *wgctrl.Client wgClient *wgctrl.Client
wgUpdate sync.Mutex wgUpdate sync.Mutex
port int //Wireguard Server Port port int //Wireguard WireGuard Port
privateKey string //Wireguard Server Private Key privateKey string //Wireguard WireGuard Private Key
} }
type ServerConfig struct{ type WireGuardConfig struct{
Name string Name string
IPRanges []string IPRanges []string
Port int //Wireguard Server Port Port int //Wireguard WireGuard Port
PrivateKey string //Wireguard Server Private Key PrivateKey string //Wireguard WireGuard Private Key
} }
func NewServer(config ServerConfig) (Server, error){ func NewWireGuard(config WireGuardConfig) (WireGuard, error){
server := Server{ wireguard := WireGuard{
name: config.Name,
iPRanges: config.IPRanges,
port: config.Port, port: config.Port,
privateKey: config.PrivateKey, privateKey: config.PrivateKey,
} }
return server, nil wireguard.name = config.Name
wireguard.iPRanges = config.IPRanges
return wireguard, nil
} }
type PeerConfig struct { type PeerConfig struct {
@ -51,11 +50,11 @@ type PeerConfig struct {
Endpoint string Endpoint string
} }
func (s *Server) Down() (error) { func (s *WireGuard) Down() (error) {
return s.removeNetworkDevice() return s.removeNetworkDevice()
} }
func (s *Server) Up() (error) { func (s *WireGuard) Up() (error) {
err := s.createNetworkDevice() err := s.createNetworkDevice()
if err != nil { if err != nil {
return err return err
@ -91,7 +90,7 @@ func (s *Server) Up() (error) {
return nil return nil
} }
func (s *Server) AddPeer(config PeerConfig) (error) { func (s *WireGuard) AddPeer(config PeerConfig) (error) {
s.wgUpdate.Lock() s.wgUpdate.Lock()
defer s.wgUpdate.Unlock() defer s.wgUpdate.Unlock()
if s.wgConfig.PrivateKey == nil { if s.wgConfig.PrivateKey == nil {
@ -173,7 +172,7 @@ func (s *Server) AddPeer(config PeerConfig) (error) {
return nil return nil
} }
func (s *Server) RemovePeer(publicKey string) (error) { func (s *WireGuard) RemovePeer(publicKey string) (error) {
s.wgUpdate.Lock() s.wgUpdate.Lock()
defer s.wgUpdate.Unlock() defer s.wgUpdate.Unlock()
@ -202,66 +201,8 @@ func (s *Server) RemovePeer(publicKey string) (error) {
return nil return nil
} }
func (s *Server) createNetworkDevice() (error) {
la := netlink.NewLinkAttrs()
la.Name = s.name
wgDev := &netlink.GenericLink{ func (s *WireGuard) wgSetup() (error) {
LinkAttrs: la,
LinkType: "wireguard",
}
err := netlink.LinkAdd(wgDev)
if err != nil {
return err
}
s.wgDev = wgDev
return nil
}
func (s *Server) upNetworkDevice() (error) {
err := netlink.LinkSetUp(s.wgDev) //todo: test
if err != nil {
return err
}
return nil
}
func (s *Server) removeNetworkDevice() (error) {
la := netlink.NewLinkAttrs()
la.Name = s.name
wgDev := &netlink.GenericLink{
LinkAttrs: la,
LinkType: "wireguard",
}
err := netlink.LinkDel(wgDev)
return err
}
func (s *Server) addIPToNetwork() (error) {
var ips []*netlink.Addr
for _, iprange := range s.iPRanges {
ip, err := netlink.ParseAddr(iprange)
if err != nil {
return err
}
ips = append(ips, ip)
}
for _, ip := range ips {
err := netlink.AddrAdd(s.wgDev, ip)
if err != nil { //ToDo: Check how to test this case
return err
}
}
return nil
}
func (s *Server) wgSetup() (error) {
if s.wgConfig.PrivateKey != nil { if s.wgConfig.PrivateKey != nil {
return errors.New("Wireguard already setedup") return errors.New("Wireguard already setedup")
} }
@ -277,10 +218,16 @@ func (s *Server) wgSetup() (error) {
return err return err
} }
var port *int
if s.port > 0 {
port = &s.port
}
s.wgConfig = wgtypes.Config{ s.wgConfig = wgtypes.Config{
PrivateKey: &privkey, PrivateKey: &privkey,
ListenPort: &s.port, ListenPort: port,
FirewallMark: nil, FirewallMark: nil,
ReplacePeers: true, ReplacePeers: true,
Peers: nil, Peers: nil,

View file

@ -8,37 +8,14 @@ import (
import "github.com/vishvananda/netlink" import "github.com/vishvananda/netlink"
import "github.com/stretchr/testify/assert" import "github.com/stretchr/testify/assert"
func TestCreateAndDeleteDevice(t *testing.T) {
serverConfig := ServerConfig{
Name: "wgUnit",
}
server, err := NewServer(serverConfig)
err = server.createNetworkDevice()
if err != nil {
assert.Nil(t, err, "shouldn't return error creating the network Device: %s", err)
}
foundDevice := checkForNetworkDevice("wgUnit", t)
assert.Truef(t, foundDevice, "should fount device with name wgUnit")
err = server.removeNetworkDevice()
if err != nil {
assert.Nil(t, err, "shouldn't return an error doing remove the network device again")
}
foundDevice = checkForNetworkDevice("wgUnit", t)
assert.Falsef(t, foundDevice, "should not fount device with name wgUnit")
}
func TestRemoveDeviceIfIPRangeInvalide(t *testing.T) { func TestRemoveDeviceIfIPRangeInvalide(t *testing.T) {
serverConfig := ServerConfig{ serverConfig := WireGuardConfig{
Name: "wgUnit", Name: "wgUnit",
IPRanges: []string{"10.0.0.1/24", "abc/24"}, IPRanges: []string{"10.0.0.1/24", "abc/24"},
} }
server, err := NewServer(serverConfig) server, err := NewWireGuard(serverConfig)
assert.Nil(t, err, "shouldn't return error while creating server") assert.Nil(t, err, "shouldn't return error while creating server")
err = server.Up() err = server.Up()
@ -53,11 +30,11 @@ func TestRemoveDeviceIfIPRangeInvalide(t *testing.T) {
func TestRemoveDeviceIfPrivateKeyIsInvalide(t *testing.T) { func TestRemoveDeviceIfPrivateKeyIsInvalide(t *testing.T) {
serverConfig := ServerConfig{ serverConfig := WireGuardConfig{
Name: "wgUnit", Name: "wgUnit",
IPRanges: []string{"10.0.0.1/24"}, IPRanges: []string{"10.0.0.1/24"},
} }
server, err := NewServer(serverConfig) server, err := NewWireGuard(serverConfig)
assert.Nil(t, err, "shouldn't return error while creating server") assert.Nil(t, err, "shouldn't return error while creating server")
err = server.Up() err = server.Up()
@ -71,7 +48,7 @@ func TestRemoveDeviceIfPrivateKeyIsInvalide(t *testing.T) {
} }
func TestCantRunSetupWireguardTwice(t *testing.T) { func TestCantRunSetupWireguardTwice(t *testing.T) {
server := Server{} server := WireGuard{}
server.name = "wgUnit" server.name = "wgUnit"
key, err := wgtypes.GeneratePrivateKey() key, err := wgtypes.GeneratePrivateKey()
assert.Nil(t, err, "cant create private key") assert.Nil(t, err, "cant create private key")
@ -104,13 +81,13 @@ func TestCreateWireguarderver(t *testing.T) {
key, err := wgtypes.GeneratePrivateKey() key, err := wgtypes.GeneratePrivateKey()
assert.Nil(t, err, "cant create private key") assert.Nil(t, err, "cant create private key")
serverConfig := ServerConfig{ serverConfig := WireGuardConfig{
Name: "wgUnit", Name: "wgUnit",
IPRanges: []string{"10.0.0.1/24"}, IPRanges: []string{"10.0.0.1/24"},
Port: 54123, Port: 54123,
PrivateKey: key.String(), PrivateKey: key.String(),
} }
server, err := NewServer(serverConfig) server, err := NewWireGuard(serverConfig)
assert.Nil(t, err, "cant create server") assert.Nil(t, err, "cant create server")
err = server.Up() err = server.Up()
@ -128,10 +105,10 @@ func TestCreateWireguarderver(t *testing.T) {
} }
func TestCantAddPeerIfServerNotUp(t *testing.T) { func TestCantAddPeerIfServerNotUp(t *testing.T) {
serverConfig := ServerConfig{ serverConfig := WireGuardConfig{
Name: "wgUnit", Name: "wgUnit",
} }
server, err := NewServer(serverConfig) server, err := NewWireGuard(serverConfig)
assert.Nil(t, err, "cant create server") assert.Nil(t, err, "cant create server")
peer := PeerConfig{} peer := PeerConfig{}
@ -290,17 +267,17 @@ func TestRemovePeer(t *testing.T) {
} }
func helperCreateServer(t *testing.T) Server { func helperCreateServer(t *testing.T) WireGuard {
key, err := wgtypes.GeneratePrivateKey() key, err := wgtypes.GeneratePrivateKey()
assert.Nil(t, err, "cant create private key") assert.Nil(t, err, "cant create private key")
serverConfig := ServerConfig{ serverConfig := WireGuardConfig{
Name: "wgUnit", Name: "wgUnit",
IPRanges: []string{"10.0.0.1/24"}, IPRanges: []string{"10.0.0.1/24"},
Port: 54123, Port: 54123,
PrivateKey: key.String(), PrivateKey: key.String(),
} }
server, err := NewServer(serverConfig) server, err := NewWireGuard(serverConfig)
assert.Nil(t, err, "cant create server") assert.Nil(t, err, "cant create server")
err = server.Up() err = server.Up()
@ -312,7 +289,7 @@ func helperCreateServer(t *testing.T) Server {
return server return server
} }
func helpTearDown(server Server, t *testing.T) { func helpTearDown(server WireGuard, t *testing.T) {
err := server.Down() err := server.Down()
assert.Nil(t, err, "should not return error while shutting down server") assert.Nil(t, err, "should not return error while shutting down server")