diff --git a/cmd/sample-server/main.go b/cmd/sample-server/main.go index ddf9b1e..7d4971b 100644 --- a/cmd/sample-server/main.go +++ b/cmd/sample-server/main.go @@ -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") } \ No newline at end of file diff --git a/pkg/wgfunc/Readme.md b/pkg/wgfunc/Readme.md index 76273db..cffbabd 100644 --- a/pkg/wgfunc/Readme.md +++ b/pkg/wgfunc/Readme.md @@ -1 +1,4 @@ -Easy way to manage wireaguard "server" and "client" \ No newline at end of file +Todo: + +Server in Connection umbennen, so das clients auch genutzt werden können +Connections ohne port diff --git a/pkg/wgfunc/client.go b/pkg/wgfunc/client.go index 0e94703..f27de4b 100644 --- a/pkg/wgfunc/client.go +++ b/pkg/wgfunc/client.go @@ -11,10 +11,3 @@ type Client struct { } -func NewClient(config ClientConfig) Client { - -} - -func (c *Client)Connect() error { - -} \ No newline at end of file diff --git a/pkg/wgfunc/connection_test.go b/pkg/wgfunc/connection_test.go new file mode 100644 index 0000000..671151f --- /dev/null +++ b/pkg/wgfunc/connection_test.go @@ -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) + +} \ No newline at end of file diff --git a/pkg/wgfunc/device.go b/pkg/wgfunc/device.go new file mode 100644 index 0000000..52da426 --- /dev/null +++ b/pkg/wgfunc/device.go @@ -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 +} \ No newline at end of file diff --git a/pkg/wgfunc/device_test.go b/pkg/wgfunc/device_test.go new file mode 100644 index 0000000..684929b --- /dev/null +++ b/pkg/wgfunc/device_test.go @@ -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") +} \ No newline at end of file diff --git a/pkg/wgfunc/server.go b/pkg/wgfunc/wireguard.go similarity index 68% rename from pkg/wgfunc/server.go rename to pkg/wgfunc/wireguard.go index bb84109..e345883 100644 --- a/pkg/wgfunc/server.go +++ b/pkg/wgfunc/wireguard.go @@ -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, diff --git a/pkg/wgfunc/server_test.go b/pkg/wgfunc/wireguard_test.go similarity index 87% rename from pkg/wgfunc/server_test.go rename to pkg/wgfunc/wireguard_test.go index 68ec0b1..43f227a 100644 --- a/pkg/wgfunc/server_test.go +++ b/pkg/wgfunc/wireguard_test.go @@ -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")