-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.go
250 lines (213 loc) · 5.72 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Package api contains the Unix Domain Socket API.
package api
import (
"fmt"
"net"
"os"
"os/user"
"strconv"
"time"
log "github.com/sirupsen/logrus"
)
const (
// ServerShuttingDown is the error reply to client requests when the
// API server is shutting down.
ServerShuttingDown = "server is shutting down"
)
// Server is a Daemon API server.
type Server struct {
config *Config
listen net.Listener
requests chan *Request
shutdown chan struct{}
done chan struct{}
closed chan struct{}
}
// isStopping returns whether the server is stopping.
func (s *Server) isStopping() bool {
select {
case <-s.done:
return true
default:
return false
}
}
// sendShuttingDownError sends the "shutting down" error to the client and
// closes the connection.
func (s *Server) sendShuttingDownError(conn net.Conn) {
// tell client server is not accepting connections anymore
e := NewError([]byte(ServerShuttingDown))
if err := WriteMessage(conn, e); err != nil {
log.WithError(err).Error("Daemon message send error")
}
_ = conn.Close()
}
// handleRequest handles a request from the client.
func (s *Server) handleRequest(conn net.Conn) {
// set timeout for entire request/response exchange
deadline := time.Now().Add(s.config.RequestTimeout)
if err := conn.SetDeadline(deadline); err != nil {
log.WithError(err).Error("Daemon error setting deadline")
_ = conn.Close()
return
}
// read message from client
msg, err := ReadMessage(conn)
if err != nil {
log.WithError(err).Error("Daemon receive message error")
_ = conn.Close()
return
}
// check if its a known message type
switch msg.Type {
case TypeVPNConfigUpdate:
default:
// send Error and disconnect
e := NewError([]byte("invalid message"))
if err := WriteMessage(conn, e); err != nil {
log.WithError(err).Error("Daemon message send error")
}
_ = conn.Close()
return
}
// forward client's request to daemon
select {
case s.requests <- &Request{
msg: msg,
conn: conn,
}:
case <-s.shutdown:
s.sendShuttingDownError(conn)
case <-s.done:
s.sendShuttingDownError(conn)
}
}
// handleClients handles client connections.
func (s *Server) handleClients() {
defer func() {
_ = s.listen.Close()
close(s.requests)
close(s.closed)
}()
for {
// wait for new client connection
conn, err := s.listen.Accept()
if err != nil {
if s.isStopping() {
// ignore error when shutting down
return
}
log.WithError(err).Error("Daemon listener error")
return
}
// read request from client connection and handle it
s.handleRequest(conn)
}
}
// setSocketOwner sets the owner of the socket file.
func (s *Server) setSocketOwner() {
if s.config.SocketOwner == "" {
// do not change owner
return
}
user, err := user.Lookup(s.config.SocketOwner)
if err != nil {
log.WithError(err).Error("Daemon could not get UID of socket file owner")
return
}
uid, err := strconv.Atoi(user.Uid)
if err != nil {
log.WithError(err).Error("Daemon could not convert UID of socket file owner to int")
return
}
if err := os.Chown(s.config.SocketFile, uid, -1); err != nil {
log.WithError(err).Error("Daemon could not change owner of socket file")
}
}
// setSocketGroup sets the group of the socket file.
func (s *Server) setSocketGroup() {
if s.config.SocketGroup == "" {
// do not change group
return
}
group, err := user.LookupGroup(s.config.SocketGroup)
if err != nil {
log.WithError(err).Error("Daemon could not get GID of socket file group")
return
}
gid, err := strconv.Atoi(group.Gid)
if err != nil {
log.WithError(err).Error("Daemon could not convert GID of socket file group to int")
return
}
if err := os.Chown(s.config.SocketFile, -1, gid); err != nil {
log.WithError(err).Error("Daemon could not change group of socket file")
}
}
// setSocketPermissions sets the file permissions of the socket file.
func (s *Server) setSocketPermissions() {
if s.config.SocketPermissions == "" {
// do not change permissions
return
}
perm, err := strconv.ParseUint(s.config.SocketPermissions, 8, 32)
if err != nil {
log.WithError(err).Error("Daemon could not convert permissions of sock file to uint")
return
}
if err := os.Chmod(s.config.SocketFile, os.FileMode(perm)); err != nil {
log.WithError(err).Error("Daemon could not set permissions of sock file")
}
}
// Start starts the API server.
func (s *Server) Start() error {
// cleanup existing sock file, this should normally fail
if err := os.Remove(s.config.SocketFile); err == nil {
log.Warn("Removed existing unix socket file")
}
// start listener
listen, err := net.Listen("unix", s.config.SocketFile)
if err != nil {
return fmt.Errorf("could not start unix listener: %w", err)
}
s.listen = listen
// set owner of sock file
s.setSocketOwner()
// set group of sock file
s.setSocketGroup()
// set file permissions of sock file
s.setSocketPermissions()
// handle client connections
go s.handleClients()
return nil
}
// Stop stops the API server.
func (s *Server) Stop() {
close(s.done)
// stop listener
err := s.listen.Close()
if err != nil {
log.WithError(err).Error("Daemon could not close unix listener")
}
<-s.closed
}
// Shutdown stops accepting client requests in the API server. This is called
// before Stop to tell clients the server is shutting down. Client requests
// should not be read from the requests channel after calling this.
func (s *Server) Shutdown() {
close(s.shutdown)
}
// Requests returns the clients channel.
func (s *Server) Requests() chan *Request {
return s.requests
}
// NewServer returns a new API server.
func NewServer(config *Config) *Server {
return &Server{
config: config,
requests: make(chan *Request),
shutdown: make(chan struct{}),
done: make(chan struct{}),
closed: make(chan struct{}),
}
}