-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
81 lines (75 loc) · 1.91 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package updater
import (
"github.com/COSAE-FR/ripradius/pkg/freeradius"
"github.com/COSAE-FR/ripradius/pkg/local/client"
"github.com/COSAE-FR/ripradius/pkg/updater/fetcher"
log "github.com/sirupsen/logrus"
"time"
)
type Server struct {
config *Configuration
fetcher fetcher.Fetcher
radius *freeradius.Freeradius
certificateDate *time.Time
tick *time.Ticker
done chan bool
manual chan bool
log *log.Entry
}
func New(logger *log.Entry, config *Configuration, client *client.Client) (*Server, error) {
f := fetcher.NewHttpFetcher(client)
return &Server{config: config, log: logger.WithField("component", "updater"), fetcher: f}, nil
}
func (s *Server) Start() error {
if err := s.startWithoutRemote(); err != nil {
s.log.Errorf("cannot start initial radius server with default certificate")
}
go func() {
for {
select {
case <-s.done:
return
case <-s.tick.C:
if err := s.applyUpdate(); err != nil {
s.log.Errorf("cannot update radius certificate: %s", err)
if err := s.startWithoutRemote(); err != nil {
s.log.Errorf("cannot start radius server with default certificate")
}
}
case <-s.manual:
if err := s.applyUpdate(); err != nil {
s.log.Errorf("cannot update radius certificate: %s", err)
if err := s.startWithoutRemote(); err != nil {
s.log.Errorf("cannot start radius server with default certificate")
}
}
}
}
}()
s.manual <- true
return nil
}
func (s *Server) Stop() error {
if s.tick != nil {
s.tick.Stop()
}
if s.done != nil {
s.done <- true
}
if s.radius != nil {
return s.radius.Stop()
}
return nil
}
func (s *Server) Configure() error {
if s.done != nil {
_ = s.Stop()
}
s.tick = time.NewTicker(s.config.Interval)
s.done = make(chan bool)
s.manual = make(chan bool)
if err := s.createCacheDirectory(); err != nil {
return err
}
return nil
}