Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

packet logging #15

Merged
merged 19 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
module github.com/in4it/wireguard-server

go 1.22.2
go 1.23.0

require (
github.com/go-jose/go-jose/v4 v4.0.2
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0
github.com/gopacket/gopacket v1.3.0
github.com/mdlayher/genetlink v1.3.2
github.com/mdlayher/netlink v1.7.2
github.com/packetcap/go-pcap v0.0.0-20240528124601-8c87ecf5dbc5
github.com/russellhaering/gosaml2 v0.9.1
github.com/russellhaering/goxmldsig v1.4.0
golang.org/x/crypto v0.25.0
golang.org/x/sys v0.22.0
golang.org/x/term v0.22.0
golang.org/x/crypto v0.26.0
golang.org/x/sys v0.24.0
golang.org/x/term v0.23.0
)

require (
Expand All @@ -22,9 +24,8 @@ require (
github.com/josharian/native v1.1.0 // indirect
github.com/mattermost/xml-roundtrip-validator v0.1.0 // indirect
github.com/mdlayher/socket v0.5.1 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/text v0.16.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/text v0.17.0 // indirect
)
47 changes: 20 additions & 27 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs=
github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A=
github.com/beevik/etree v1.4.0 h1:oz1UedHRepuY3p4N5OjE0nK1WLCqtzHf25bxplKOHLs=
github.com/beevik/etree v1.4.0/go.mod h1:cyWiXwGoasx60gHvtnEh5x8+uIjUVnjWqBvEnhnqKDA=
Expand All @@ -14,8 +13,9 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopacket/gopacket v1.3.0 h1:MouZCc+ej0vnqzB0WeiaO/6+tGvb+KU7UczxoQ+X0Yc=
github.com/gopacket/gopacket v1.3.0/go.mod h1:WnFrU1Xkf5lWKV38uKNR9+yYtppn+ZYzOyNqMeH4oNE=
github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
github.com/jonboulle/clockwork v0.3.0 h1:9BSCMi8C+0qdApAp4auwX0RkLGUjs956h0EkuQymUhg=
github.com/jonboulle/clockwork v0.3.0/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc=
Expand All @@ -35,45 +35,38 @@ github.com/mdlayher/netlink v1.7.2 h1:/UtM3ofJap7Vl4QWCPDGXY8d3GIY2UGSDbK+QWmY8/
github.com/mdlayher/netlink v1.7.2/go.mod h1:xraEF7uJbxLhc5fpHL4cPe221LI2bdttWlU+ZGLfQSw=
github.com/mdlayher/socket v0.5.1 h1:VZaqt6RkGkt2OE9l3GcC6nZkqD3xKeQLyfleW/uBcos=
github.com/mdlayher/socket v0.5.1/go.mod h1:TjPLHI1UgwEv5J1B5q0zTZq12A/6H7nKmtTanQE37IQ=
github.com/packetcap/go-pcap v0.0.0-20240528124601-8c87ecf5dbc5 h1:p4VuaitqUAqSZSomd7Wb4BPV/Jj7Hno2/iqtfX7DZJI=
github.com/packetcap/go-pcap v0.0.0-20240528124601-8c87ecf5dbc5/go.mod h1:zIAoVKeWP0mz4zXY50UYQt6NLg2uwKRswMDcGEqOms4=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/russellhaering/gosaml2 v0.9.1 h1:H/whrl8NuSoxyW46Ww5lKPskm+5K+qYLw9afqJ/Zef0=
github.com/russellhaering/gosaml2 v0.9.1/go.mod h1:ja+qgbayxm+0mxBRLMSUuX3COqy+sb0RRhIGun/W2kc=
github.com/russellhaering/goxmldsig v1.3.0 h1:DllIWUgMy0cRUMfGiASiYEa35nsieyD3cigIwLonTPM=
github.com/russellhaering/goxmldsig v1.3.0/go.mod h1:gM4MDENBQf7M+V824SGfyIUVFWydB7n0KkEubVJl+Tw=
github.com/russellhaering/goxmldsig v1.4.0 h1:8UcDh/xGyQiyrW+Fq5t8f+l2DLB1+zlhYzkPUJ7Qhys=
github.com/russellhaering/goxmldsig v1.4.0/go.mod h1:gM4MDENBQf7M+V824SGfyIUVFWydB7n0KkEubVJl+Tw=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
2 changes: 1 addition & 1 deletion latest
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.1.2
v1.1.3
4 changes: 2 additions & 2 deletions pkg/auth/oidc/store/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"

"github.com/in4it/wireguard-server/pkg/auth/oidc"
testingmocks "github.com/in4it/wireguard-server/pkg/testing/mocks"
memorystorage "github.com/in4it/wireguard-server/pkg/storage/memory"
)

func TestGetDiscovery(t *testing.T) {
Expand All @@ -27,7 +27,7 @@ func TestGetDiscovery(t *testing.T) {
}))
defer ts.Close()

store, err := NewStore(&testingmocks.MockMemoryStorage{})
store, err := NewStore(&memorystorage.MockMemoryStorage{})
if err != nil {
t.Fatalf("new store error: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/auth/oidc/store/jwks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"

"github.com/in4it/wireguard-server/pkg/auth/oidc"
testingmocks "github.com/in4it/wireguard-server/pkg/testing/mocks"
memorystorage "github.com/in4it/wireguard-server/pkg/storage/memory"
)

func TestGetJwks(t *testing.T) {
Expand All @@ -32,7 +32,7 @@ func TestGetJwks(t *testing.T) {
}))
defer ts.Close()

store, err := NewStore(&testingmocks.MockMemoryStorage{})
store, err := NewStore(&memorystorage.MockMemoryStorage{})
if err != nil {
t.Fatalf("new store error: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/auth/oidc/store/save_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"testing"

"github.com/in4it/wireguard-server/pkg/auth/oidc"
testingmocks "github.com/in4it/wireguard-server/pkg/testing/mocks"
memorystorage "github.com/in4it/wireguard-server/pkg/storage/memory"
)

func TestSave(t *testing.T) {
storage := &testingmocks.MockMemoryStorage{}
storage := &memorystorage.MockMemoryStorage{}
store, err := NewStore(storage)
if err != nil {
t.Fatalf("error: %s", err)
Expand Down
28 changes: 19 additions & 9 deletions pkg/auth/provisioning/scim/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
"path"
"testing"

testingmocks "github.com/in4it/wireguard-server/pkg/testing/mocks"
memorystorage "github.com/in4it/wireguard-server/pkg/storage/memory"
"github.com/in4it/wireguard-server/pkg/users"
"github.com/in4it/wireguard-server/pkg/wireguard"
)

const USERSTORE_MAX_USERS = 1000

func TestUsersGetCount100EmptyResult(t *testing.T) {
storage := &testingmocks.MockMemoryStorage{}
storage := &memorystorage.MockMemoryStorage{}

userStore, err := users.NewUserStore(storage, USERSTORE_MAX_USERS)
if err != nil {
Expand Down Expand Up @@ -48,7 +48,7 @@ func TestUsersGetCount100EmptyResult(t *testing.T) {
}

func TestUsersGetCount10(t *testing.T) {
storage := &testingmocks.MockMemoryStorage{}
storage := &memorystorage.MockMemoryStorage{}
userStore, err := users.NewUserStore(storage, USERSTORE_MAX_USERS)
if err != nil {
t.Fatalf("cannot create new user store")
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestUsersGetCount10(t *testing.T) {
func TestUsersGetCount10Start5(t *testing.T) {
count := 10
start := 5
storage := &testingmocks.MockMemoryStorage{}
storage := &memorystorage.MockMemoryStorage{}
userStore, err := users.NewUserStore(storage, USERSTORE_MAX_USERS)
if err != nil {
t.Fatalf("cannot create new user store")
Expand Down Expand Up @@ -138,12 +138,12 @@ func TestUsersGetCount10Start5(t *testing.T) {
}

func TestUsersGetNonExistentUser(t *testing.T) {
userStore, err := users.NewUserStore(&testingmocks.MockMemoryStorage{}, USERSTORE_MAX_USERS)
userStore, err := users.NewUserStore(&memorystorage.MockMemoryStorage{}, USERSTORE_MAX_USERS)
if err != nil {
t.Fatalf("cannot create new user stoer")
}

s := New(&testingmocks.MockMemoryStorage{}, userStore, "token")
s := New(&memorystorage.MockMemoryStorage{}, userStore, "token")
req := httptest.NewRequest("GET", "http://example.com/api/scim/v2/Users?filter=userName+eq+%22ward%40in4it.io%22&", nil)
w := httptest.NewRecorder()
s.getUsersHandler(w, req)
Expand All @@ -161,7 +161,7 @@ func TestUsersGetNonExistentUser(t *testing.T) {
}

func TestAddUser(t *testing.T) {
storage := &testingmocks.MockMemoryStorage{}
storage := &memorystorage.MockMemoryStorage{}
userStore, err := users.NewUserStore(storage, USERSTORE_MAX_USERS)
if err != nil {
t.Fatalf("cannot create new user store: %s", err)
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestAddUser(t *testing.T) {
}

func TestCreateUserConnectionDeleteUserFlow(t *testing.T) {
storage := &testingmocks.MockMemoryStorage{}
storage := &memorystorage.MockMemoryStorage{}
userStore, err := users.NewUserStore(storage, USERSTORE_MAX_USERS)
if err != nil {
t.Fatalf("cannot create new user store: %s", err)
Expand All @@ -232,6 +232,11 @@ func TestCreateUserConnectionDeleteUserFlow(t *testing.T) {
w.Write([]byte("OK"))
return
}
if r.RequestURI == "/refresh-server-config" {
w.WriteHeader(http.StatusAccepted)
w.Write([]byte("OK"))
return
}
w.WriteHeader(http.StatusBadRequest)
default:
w.WriteHeader(http.StatusBadRequest)
Expand Down Expand Up @@ -323,7 +328,7 @@ func TestCreateUserConnectionDeleteUserFlow(t *testing.T) {
}
}
func TestCreateUserConnectionSuspendUserFlow(t *testing.T) {
storage := &testingmocks.MockMemoryStorage{}
storage := &memorystorage.MockMemoryStorage{}

userStore, err := users.NewUserStore(storage, USERSTORE_MAX_USERS)
if err != nil {
Expand All @@ -348,6 +353,11 @@ func TestCreateUserConnectionSuspendUserFlow(t *testing.T) {
w.Write([]byte("OK"))
return
}
if r.RequestURI == "/refresh-server-config" {
w.WriteHeader(http.StatusAccepted)
w.Write([]byte("OK"))
return
}
w.WriteHeader(http.StatusBadRequest)
default:
w.WriteHeader(http.StatusBadRequest)
Expand Down
28 changes: 25 additions & 3 deletions pkg/configmanager/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ func (c *ConfigManager) refreshClients(w http.ResponseWriter, r *http.Request) {
}
switch payload.Action {
case wireguard.ACTION_ADD:
err = syncClient(c.Storage, filename)
err = syncClient(c.Storage, filename, c.ClientCache)
if err != nil {
returnError(w, fmt.Errorf("syncClient error: %s", err), http.StatusBadRequest)
return
}
case wireguard.ACTION_DELETE:
err = deleteClient(c.Storage, filename)
err = deleteClient(c.Storage, filename, c.ClientCache)
if err != nil {
returnError(w, fmt.Errorf("deleteClient error: %s", err), http.StatusBadRequest)
return
Expand All @@ -77,6 +77,28 @@ func (c *ConfigManager) refreshClients(w http.ResponseWriter, r *http.Request) {
returnError(w, fmt.Errorf("method not supported"), http.StatusBadRequest)
}
}
func (c *ConfigManager) refreshServerConfig(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
vpnConfig, err := wireguard.GetVPNConfig(c.Storage)
if err != nil {
returnError(w, fmt.Errorf("get vpn config error: %s", err), http.StatusBadRequest)
return
}
startPacketLogger := false
if vpnConfig.EnablePacketLogs && !c.VPNConfig.EnablePacketLogs {
startPacketLogger = true
}
c.VPNConfig.EnablePacketLogs = vpnConfig.EnablePacketLogs
c.VPNConfig.PacketLogsTypes = vpnConfig.PacketLogsTypes
if startPacketLogger {
go wireguard.RunPacketLogger(c.Storage, c.ClientCache, c.VPNConfig)
}
w.WriteHeader(http.StatusAccepted)
default:
returnError(w, fmt.Errorf("method not supported"), http.StatusBadRequest)
}
}

func (c *ConfigManager) upgrade(w http.ResponseWriter, r *http.Request) {
switch r.Method {
Expand Down Expand Up @@ -131,7 +153,7 @@ func (c *ConfigManager) restartVpn(w http.ResponseWriter, r *http.Request) {
returnError(w, fmt.Errorf("vpn start error: %s", err), http.StatusBadRequest)
return
}
err = refreshAllClientsAndServer(c.Storage)
err = refreshAllClientsAndServer(c.Storage, c.ClientCache)
if err != nil {
returnError(w, fmt.Errorf("could not refresh all clients: %s", err), http.StatusBadRequest)
return
Expand Down
46 changes: 43 additions & 3 deletions pkg/configmanager/refresh_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,49 @@
package configmanager

import (
"errors"
"fmt"
"os"

"github.com/in4it/wireguard-server/pkg/storage"
"github.com/in4it/wireguard-server/pkg/wireguard"
)

func refreshAllClientsAndServer(storage storage.Iface) error {
func refreshAllClientsAndServer(storage storage.Iface, clientCache *wireguard.ClientCache) error {
peerConfigPath := storage.ConfigPath(wireguard.VPN_CLIENTS_DIR)

if _, err := os.Stat(peerConfigPath); errors.Is(err, os.ErrNotExist) {
return nil // directory doesn't exist, so no configs to be read
}

entries, err := storage.ReadDir(peerConfigPath)
if err != nil {
return fmt.Errorf("can not list clients from dir %s: %s", peerConfigPath, err)
}

for _, filename := range entries {
peerConfig, err := wireguard.GetPeerConfigByFilename(storage, filename)
if err != nil {
return fmt.Errorf("getClientFile error: %s", err)
}
err = wireguard.UpdateClientCache(peerConfig, clientCache)
if err != nil {
return fmt.Errorf("update client cache error: %s", err)
}
}
fmt.Printf("Warning: not refreshAllClients supported on darwin\n")
return nil
}

func syncClient(storage storage.Iface, filename string) error {
func syncClient(storage storage.Iface, filename string, clientCache *wireguard.ClientCache) error {
peerConfig, err := wireguard.GetPeerConfigByFilename(storage, filename)
if err != nil {
return fmt.Errorf("getClientFile error: %s", err)
}
err = wireguard.UpdateClientCache(peerConfig, clientCache)
if err != nil {
return fmt.Errorf("update client cache error: %s", err)
}
fmt.Printf("Warning: syncClient not supported on darwin. Cannot sync: %s\n", filename)
return nil
}
Expand All @@ -24,7 +56,15 @@ func cleanupClients(storage storage.Iface) error {
return nil
}

func deleteClient(storage storage.Iface, filename string) error {
func deleteClient(storage storage.Iface, filename string, clientCache *wireguard.ClientCache) error {
peerConfig, err := wireguard.GetPeerConfigByFilename(storage, filename)
if err != nil {
return fmt.Errorf("getClientFile error: %s", err)
}
err = wireguard.UpdateClientCache(peerConfig, clientCache)
if err != nil {
return fmt.Errorf("update client cache error: %s", err)
}
fmt.Printf("Warning: deleteClient not supported on darwin. Cannot delete: %s\n", filename)
return nil
}
Loading
Loading