From 22535a4cdc7a53d3463b8bab101d81259ba9804d Mon Sep 17 00:00:00 2001 From: UnAfraid Date: Sun, 22 Oct 2023 15:55:21 +0300 Subject: [PATCH] Initialize wireguard devices on boot --- pkg/manage/service.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pkg/manage/service.go b/pkg/manage/service.go index 569f1e7..f2bd179 100644 --- a/pkg/manage/service.go +++ b/pkg/manage/service.go @@ -61,10 +61,37 @@ func NewService( } s.cleanup(context.Background()) + s.init() return s } +func (s *service) init() { + ctx := context.Background() + servers, err := s.serverService.FindServers(ctx, &server.FindOptions{ + Enabled: adapt.ToPointerNilZero(true), + }) + if err != nil { + logrus.WithError(err).Error("failed to find servers") + return + } + + for _, srv := range servers { + peers, err := s.peerService.FindPeers(ctx, &peer.FindOptions{ + ServerId: adapt.ToPointer(srv.Id), + }) + if err != nil { + logrus.WithError(err).WithField("name", srv.Name).Error("failed to find peers for server") + return + } + + if _, err = s.configureDevice(ctx, srv, peers); err != nil { + logrus.WithError(err).WithField("name", srv.Name).Error("failed to configure wireguard device") + return + } + } +} + func (s *service) Authenticate(ctx context.Context, username string, password string) (*user.User, error) { return s.userService.Authenticate(ctx, username, password) }