-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathoptions.go
67 lines (56 loc) · 1.8 KB
/
options.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
package spice
import (
"context"
"net"
"github.com/sirupsen/logrus"
)
// Option is a functional option handler for Server.
type Option func(*Proxy) error
// SetOption runs a functional option against the server.
func (p *Proxy) SetOption(option Option) error {
return option(p)
}
// WithLogger can be used to provide a custom logger.
// Defaults to a logrus implementation.
func WithLogger(log Logger) Option {
return func(p *Proxy) error {
p.log = log
return nil
}
}
// WithAuthenticator can be provided to implement custom authentication
// By default, "auth-less" no-op mode is enabled.
func WithAuthenticator(a Authenticator) Option {
return func(p *Proxy) error {
if err := a.Init(); err != nil {
return err
}
p.authenticator[a.Method()] = a
return nil
}
}
// WithDialer can be used to provide a custom dialer to reach compute nodes
// the network is always of type 'tcp' and the computeAddress is the compute node
// computeAddress that is return by an Authenticator.
func WithDialer(dial func(ctx context.Context, network, addr string) (net.Conn, error)) Option {
return func(p *Proxy) error {
p.dial = dial
return nil
}
}
func defaultDialer() func(context.Context, string, string) (net.Conn, error) {
dialer := &net.Dialer{}
return dialer.DialContext
}
func defaultLogger() Logger {
return Adapt(logrus.New().WithField("app", "spiceProxy"))
}
// WithConnectionCloseHandler is called when the main channel of a SPICE session is closed.
// The "destination" parameter contains the compute node address returned by "resolveComputeAddress".
// WithConnectionCloseHandler can be used to clean up after a SPICE connection was closed
func WithConnectionCloseHandler(closeCallback func(destination string) error) Option {
return func(p *Proxy) error {
p.closeCallback = closeCallback
return nil
}
}