forked from layeh/radius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
80 lines (65 loc) · 2.08 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
package radius
import (
"context"
"errors"
"net"
)
// ErrServerShutdown is returned from server Serve methods when Shutdown
// has been called and handlers are still completing.
var ErrServerShutdown = errors.New("radius: server is shutting down")
// Handler provides a handler to RADIUS server requests. When a RADIUS request
// is received, ServeRADIUS is called.
type Handler interface {
ServeRADIUS(w ResponseWriter, r *Request)
}
// HandlerFunc allows a function to implement Handler.
type HandlerFunc func(w ResponseWriter, r *Request)
// ServeRADIUS calls h(w, p).
func (h HandlerFunc) ServeRADIUS(w ResponseWriter, r *Request) {
h(w, r)
}
// Request is an incoming RADIUS request that is being handled by the server.
type Request struct {
LocalAddr net.Addr
RemoteAddr net.Addr
*Packet
ctx context.Context
}
// Context returns the context of the request. If a context has not been set
// using WithContext, the Background context is returned.
func (r *Request) Context() context.Context {
if r.ctx != nil {
return r.ctx
}
return context.Background()
}
// WithContext returns a shallow copy of the request with the new request's
// context set to the given context.
func (r *Request) WithContext(ctx context.Context) *Request {
if ctx == nil {
panic("nil ctx")
}
req := new(Request)
*req = *r
req.ctx = ctx
return req
}
// ResponseWriter is used by RADIUS servers when replying to a RADIUS request.
type ResponseWriter interface {
Write(packet *Packet) error
}
// SecretSource supplies RADIUS servers with the secret that should be used for
// authorizing and decrypting packets.
//
// ctx is canceled if the server's Shutdown method is called.
type SecretSource interface {
RADIUSSecret(ctx context.Context, remoteAddr net.Addr) ([]byte, error)
}
// StaticSecretSource returns a SecretSource that uses secret for all requests.
func StaticSecretSource(secret []byte) SecretSource {
return staticSecretSource(secret)
}
type staticSecretSource []byte
func (secret staticSecretSource) RADIUSSecret(ctx context.Context, remoteAddr net.Addr) ([]byte, error) {
return []byte(secret), nil
}