forked from jpillora/chisel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
159 lines (144 loc) · 3.28 KB
/
users.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package settings
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"regexp"
"sync"
"github.com/fsnotify/fsnotify"
"github.com/jpillora/chisel/share/cio"
)
type Users struct {
sync.RWMutex
inner map[string]*User
}
func NewUsers() *Users {
return &Users{inner: map[string]*User{}}
}
// Len returns the numbers of users
func (u *Users) Len() int {
u.RLock()
l := len(u.inner)
u.RUnlock()
return l
}
// Get user from the index by key
func (u *Users) Get(key string) (*User, bool) {
u.RLock()
user, found := u.inner[key]
u.RUnlock()
return user, found
}
// Set a users into the list by specific key
func (u *Users) Set(key string, user *User) {
u.Lock()
u.inner[key] = user
u.Unlock()
}
// Del ete a users from the list
func (u *Users) Del(key string) {
u.Lock()
delete(u.inner, key)
u.Unlock()
}
// AddUser adds a users to the set
func (u *Users) AddUser(user *User) {
u.Set(user.Name, user)
}
// Reset all users to the given set,
// Use nil to remove all.
func (u *Users) Reset(users []*User) {
m := map[string]*User{}
for _, u := range users {
m[u.Name] = u
}
u.Lock()
u.inner = m
u.Unlock()
}
// UserIndex is a reloadable user source
type UserIndex struct {
*cio.Logger
*Users
configFile string
}
// NewUserIndex creates a source for users
func NewUserIndex(logger *cio.Logger) *UserIndex {
return &UserIndex{
Logger: logger.Fork("users"),
Users: NewUsers(),
}
}
// LoadUsers is responsible for loading users from a file
func (u *UserIndex) LoadUsers(configFile string) error {
u.configFile = configFile
u.Infof("Loading configuration file %s", configFile)
if err := u.loadUserIndex(); err != nil {
return err
}
if err := u.addWatchEvents(); err != nil {
return err
}
return nil
}
// watchEvents is responsible for watching for updates to the file and reloading
func (u *UserIndex) addWatchEvents() error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
if err := watcher.Add(u.configFile); err != nil {
return err
}
go func() {
for e := range watcher.Events {
if e.Op&fsnotify.Write != fsnotify.Write {
continue
}
if err := u.loadUserIndex(); err != nil {
u.Infof("Failed to reload the users configuration: %s", err)
} else {
u.Debugf("Users configuration successfully reloaded from: %s", u.configFile)
}
}
}()
return nil
}
// loadUserIndex is responsible for loading the users configuration
func (u *UserIndex) loadUserIndex() error {
if u.configFile == "" {
return errors.New("configuration file not set")
}
b, err := ioutil.ReadFile(u.configFile)
if err != nil {
return fmt.Errorf("Failed to read auth file: %s, error: %s", u.configFile, err)
}
var raw map[string][]string
if err := json.Unmarshal(b, &raw); err != nil {
return errors.New("Invalid JSON: " + err.Error())
}
users := []*User{}
for auth, remotes := range raw {
user := &User{}
user.Name, user.Pass = ParseAuth(auth)
if user.Name == "" {
return errors.New("Invalid user:pass string")
}
for _, r := range remotes {
if r == "" || r == "*" {
user.Addrs = append(user.Addrs, UserAllowAll)
} else {
re, err := regexp.Compile(r)
if err != nil {
return errors.New("Invalid address regex")
}
user.Addrs = append(user.Addrs, re)
}
}
users = append(users, user)
}
//swap
u.Reset(users)
return nil
}