forked from bronze1man/radius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
90 lines (77 loc) · 1.75 KB
/
client.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
82
83
84
85
86
87
88
89
90
package radius
import "sync"
func NewClientList(cs []Client) *ClientList {
cl := new(ClientList)
cl.SetHerd(cs)
return cl
}
// ClientList are list of client allowed to communicate with server
type ClientList struct {
herd map[string]Client
sync.RWMutex
}
// Get client from list of clients based on host
func (cls *ClientList) Get(host string) Client {
cls.RLock()
defer cls.RUnlock()
cl, _ := cls.herd[host]
return cl
}
// Add new client or reset existing client based on host
func (cls *ClientList) AddOrUpdate(cl Client) {
cls.Lock()
defer cls.Unlock()
cls.herd[cl.GetHost()] = cl
}
// Remove client based on host
func (cls *ClientList) Remove(host string) {
cls.Lock()
defer cls.Unlock()
delete(cls.herd, host)
}
// SetHerd reset/initialize the herd of clients
func (cls *ClientList) SetHerd(herd []Client) {
cls.Lock()
defer cls.Unlock()
if cls.herd == nil {
cls.herd = make(map[string]Client)
}
for _, v := range herd {
cls.herd[v.GetHost()] = v
}
}
func (cls *ClientList) GetHerd() []Client {
cls.RLock()
defer cls.RUnlock()
herd := make([]Client, len(cls.herd))
i := 0
for _, v := range cls.herd {
herd[i] = v
i++
}
return herd
}
// Client represent a client to connect to radius server
type Client interface {
// GetHost get the client host
GetHost() string
// GetSecret get shared secret
GetSecret() string
}
// NewClient return new client
func NewClient(host, secret string) Client {
return &DefaultClient{host, secret}
}
// DefaultClient is default client implementation
type DefaultClient struct {
Host string
Secret string
}
// GetSecret get shared secret
func (cl *DefaultClient) GetSecret() string {
return cl.Secret
}
// GetHost get the client host
func (cl *DefaultClient) GetHost() string {
return cl.Host
}