@@ -4,13 +4,14 @@ import (
4
4
"log"
5
5
6
6
"github.com/NHAS/wag/internal/acls"
7
+ "github.com/NHAS/wag/internal/config"
7
8
"github.com/NHAS/wag/internal/data"
8
9
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
9
10
)
10
11
11
- func handleEvents () {
12
+ func handleEvents (erroChan chan <- error ) {
12
13
data .RegisterAclsWatcher (aclsChanges )
13
- data .RegisterClusterHealthWatcher (clusterState )
14
+ data .RegisterClusterHealthWatcher (clusterState ( erroChan ) )
14
15
data .RegisterDeviceWatcher (deviceChanges )
15
16
data .RegisterGroupsWatcher (groupChanges )
16
17
data .RegisterUserWatcher (userChanges )
@@ -41,14 +42,23 @@ func deviceChanges(device data.BasicEvent[data.Device], state int) {
41
42
}
42
43
}
43
44
44
- if (device .CurrentValue .Attempts != device .Previous .Attempts && device .CurrentValue .Attempts > 5 ) ||
45
+ if (device .CurrentValue .Attempts != device .Previous .Attempts && device .CurrentValue .Attempts > config . Values (). Lockout ) ||
45
46
device .CurrentValue .Endpoint .String () != device .Previous .Endpoint .String () {
46
47
err := Deauthenticate (device .CurrentValue .Address )
47
48
if err != nil {
48
49
log .Println (err )
49
50
}
50
51
}
51
52
53
+ if device .CurrentValue .Authorised != device .Previous .Authorised {
54
+ if device .CurrentValue .Attempts <= config .Values ().Lockout {
55
+ err := SetAuthorized (device .CurrentValue .Address , device .CurrentValue .Username )
56
+ if err != nil {
57
+ log .Println (err )
58
+ }
59
+ }
60
+ }
61
+
52
62
default :
53
63
panic ("unknown state" )
54
64
}
@@ -117,19 +127,20 @@ func groupChanges(groupChange data.TargettedEvent[[]string], state int) {
117
127
}
118
128
}
119
129
120
- func clusterState (stateText string , state int ) {
121
- switch stateText {
122
- case "dead" :
123
- TearDown ()
124
- case "healthy" :
125
- errors := make (chan error )
126
- go func () {
127
- <- errors
128
- // TODO fix this
129
- }()
130
- err := Setup (errors , true )
131
- if err != nil {
132
- log .Fatal (err )
130
+ func clusterState (errorsChan chan <- error ) data.ClusterHealthFunc {
131
+
132
+ return func (stateText string , state int ) {
133
+ switch stateText {
134
+ case "dead" :
135
+ log .Println ("Cluster has entered dead state, tearing down" )
136
+ TearDown ()
137
+ case "healthy" :
138
+ err := Setup (errorsChan , true )
139
+ if err != nil {
140
+ errorsChan <- err
141
+ log .Println ("was unable to return wag member to healthy state, dying: " , err )
142
+ return
143
+ }
133
144
}
134
145
}
135
146
}
0 commit comments