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() {
serverConfig := wgfunc.ServerConfig{
serverConfig := wgfunc.WireGuardConfig{
Name: "sample-server",
IPRanges: []string{"10.0.1.0/24"},
Port: 0,
PrivateKey: "kDUIomFdDOW0VaOMS6Vu8+1rw/8SemeobSa7ZN66J38=\n",
PrivateKey: "kDUIomFdDOW0VaOMS6Vu8+1rw/8SemeobSa7ZN66J38=",
}
server, err := wgfunc.NewServer(serverConfig)
server, err := wgfunc.NewWireGuard(serverConfig)
if err != nil {
panic(err)
}
fmt.Println("Start Server")
fmt.Println("Start WireGuard")
err = server.Up()
if err != nil {
server.Down()
panic(err)
}
defer server.Down()
fmt.Println("Server online")
fmt.Println("WireGuard online")
time.Sleep(5 * time.Second)
fmt.Println("Shoutdown server")
err = server.Down()
if err != nil {
panic(err)
}1
}
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 (
"errors"
"fmt"
"github.com/vishvananda/netlink"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"net"
@ -13,35 +12,35 @@ import (
"time"
)
type Server struct {
name string
iPRanges []string
wgDev *netlink.GenericLink
type WireGuard struct {
Device
wgConfig wgtypes.Config
wgClient *wgctrl.Client
wgUpdate sync.Mutex
port int //Wireguard Server Port
privateKey string //Wireguard Server Private Key
port int //Wireguard WireGuard Port
privateKey string //Wireguard WireGuard Private Key
}
type ServerConfig struct{
type WireGuardConfig struct{
Name string
IPRanges []string
Port int //Wireguard Server Port
PrivateKey string //Wireguard Server Private Key
Port int //Wireguard WireGuard Port
PrivateKey string //Wireguard WireGuard Private Key
}
func NewServer(config ServerConfig) (Server, error){
server := Server{
name: config.Name,
iPRanges: config.IPRanges,
func NewWireGuard(config WireGuardConfig) (WireGuard, error){
wireguard := WireGuard{
port: config.Port,
privateKey: config.PrivateKey,
}
return server, nil
wireguard.name = config.Name
wireguard.iPRanges = config.IPRanges
return wireguard, nil
}
type PeerConfig struct {
@ -51,11 +50,11 @@ type PeerConfig struct {
Endpoint string
}
func (s *Server) Down() (error) {
func (s *WireGuard) Down() (error) {
return s.removeNetworkDevice()
}
func (s *Server) Up() (error) {
func (s *WireGuard) Up() (error) {
err := s.createNetworkDevice()
if err != nil {
return err
@ -91,7 +90,7 @@ func (s *Server) Up() (error) {
return nil
}
func (s *Server) AddPeer(config PeerConfig) (error) {
func (s *WireGuard) AddPeer(config PeerConfig) (error) {
s.wgUpdate.Lock()
defer s.wgUpdate.Unlock()
if s.wgConfig.PrivateKey == nil {
@ -173,7 +172,7 @@ func (s *Server) AddPeer(config PeerConfig) (error) {
return nil
}
func (s *Server) RemovePeer(publicKey string) (error) {
func (s *WireGuard) RemovePeer(publicKey string) (error) {
s.wgUpdate.Lock()
defer s.wgUpdate.Unlock()
@ -202,66 +201,8 @@ func (s *Server) RemovePeer(publicKey string) (error) {
return nil
}
func (s *Server) 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 *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) {
func (s *WireGuard) wgSetup() (error) {
if s.wgConfig.PrivateKey != nil {
return errors.New("Wireguard already setedup")
}
@ -277,10 +218,16 @@ func (s *Server) wgSetup() (error) {
return err
}
var port *int
if s.port > 0 {
port = &s.port
}
s.wgConfig = wgtypes.Config{
PrivateKey: &privkey,
ListenPort: &s.port,
ListenPort: port,
FirewallMark: nil,
ReplacePeers: true,
Peers: nil,

View file

@ -8,37 +8,14 @@ import (
import "github.com/vishvananda/netlink"
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) {
serverConfig := ServerConfig{
serverConfig := WireGuardConfig{
Name: "wgUnit",
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")
err = server.Up()
@ -53,11 +30,11 @@ func TestRemoveDeviceIfIPRangeInvalide(t *testing.T) {
func TestRemoveDeviceIfPrivateKeyIsInvalide(t *testing.T) {
serverConfig := ServerConfig{
serverConfig := WireGuardConfig{
Name: "wgUnit",
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")
err = server.Up()
@ -71,7 +48,7 @@ func TestRemoveDeviceIfPrivateKeyIsInvalide(t *testing.T) {
}
func TestCantRunSetupWireguardTwice(t *testing.T) {
server := Server{}
server := WireGuard{}
server.name = "wgUnit"
key, err := wgtypes.GeneratePrivateKey()
assert.Nil(t, err, "cant create private key")
@ -104,13 +81,13 @@ func TestCreateWireguarderver(t *testing.T) {
key, err := wgtypes.GeneratePrivateKey()
assert.Nil(t, err, "cant create private key")
serverConfig := ServerConfig{
serverConfig := WireGuardConfig{
Name: "wgUnit",
IPRanges: []string{"10.0.0.1/24"},
Port: 54123,
PrivateKey: key.String(),
}
server, err := NewServer(serverConfig)
server, err := NewWireGuard(serverConfig)
assert.Nil(t, err, "cant create server")
err = server.Up()
@ -128,10 +105,10 @@ func TestCreateWireguarderver(t *testing.T) {
}
func TestCantAddPeerIfServerNotUp(t *testing.T) {
serverConfig := ServerConfig{
serverConfig := WireGuardConfig{
Name: "wgUnit",
}
server, err := NewServer(serverConfig)
server, err := NewWireGuard(serverConfig)
assert.Nil(t, err, "cant create server")
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()
assert.Nil(t, err, "cant create private key")
serverConfig := ServerConfig{
serverConfig := WireGuardConfig{
Name: "wgUnit",
IPRanges: []string{"10.0.0.1/24"},
Port: 54123,
PrivateKey: key.String(),
}
server, err := NewServer(serverConfig)
server, err := NewWireGuard(serverConfig)
assert.Nil(t, err, "cant create server")
err = server.Up()
@ -312,7 +289,7 @@ func helperCreateServer(t *testing.T) Server {
return server
}
func helpTearDown(server Server, t *testing.T) {
func helpTearDown(server WireGuard, t *testing.T) {
err := server.Down()
assert.Nil(t, err, "should not return error while shutting down server")