forked from layeh/radius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
89 lines (74 loc) · 2.41 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
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 shutdown")
// 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 is the local address on which the incoming RADIUS request
// was received.
LocalAddr net.Addr
// RemoteAddr is the address from which the incoming RADIUS request
// was sent.
RemoteAddr net.Addr
// Packet is the RADIUS packet sent in the request.
*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.
//
// Returning an empty secret will discard the incoming packet.
type SecretSource interface {
RADIUSSecret(ctx context.Context, remoteAddr net.Addr, nasIdentifier string) ([]byte, error)
}
// StaticSecretSource returns a SecretSource that uses secret for all requests.
func StaticSecretSource(secret []byte) SecretSource {
return &staticSecretSource{secret}
}
type staticSecretSource struct {
secret []byte
}
func (s *staticSecretSource) RADIUSSecret(ctx context.Context, remoteAddr net.Addr, nasIdentifier string) ([]byte, error) {
return s.secret, nil
}