You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.7 KiB
73 lines
1.7 KiB
package network |
|
|
|
import ( |
|
"crypto/tls" |
|
"git.hpds.cc/Component/network/auth" |
|
"git.hpds.cc/Component/network/log" |
|
"github.com/quic-go/quic-go" |
|
) |
|
|
|
const ( |
|
// DefaultListenAddr is the default address to listen. |
|
DefaultListenAddr = "0.0.0.0:9000" |
|
) |
|
|
|
// ServerOption is the option for server. |
|
type ServerOption func(*serverOptions) |
|
|
|
// ServerOptions are the options for HPDS Network server. |
|
type serverOptions struct { |
|
quicConfig *quic.Config |
|
tlsConfig *tls.Config |
|
addr string |
|
auths map[string]auth.Authentication |
|
alpnHandler func(proto string) error |
|
} |
|
|
|
func defaultServerOptions() *serverOptions { |
|
opts := &serverOptions{ |
|
quicConfig: DefaultQuicConfig, |
|
tlsConfig: nil, |
|
addr: DefaultListenAddr, |
|
auths: map[string]auth.Authentication{}, |
|
} |
|
opts.alpnHandler = func(proto string) error { |
|
log.Infof("%s client alpn proto %s, %s, %s, %s", ServerLogPrefix, "component", "server", "proto", proto) |
|
return nil |
|
} |
|
return opts |
|
} |
|
|
|
// WithAddr sets the server address. |
|
func WithAddr(addr string) ServerOption { |
|
return func(o *serverOptions) { |
|
o.addr = addr |
|
} |
|
} |
|
|
|
// WithAuth sets the server authentication method. |
|
func WithAuth(name string, args ...string) ServerOption { |
|
return func(o *serverOptions) { |
|
if a, ok := auth.GetAuth(name); ok { |
|
a.Init(args...) |
|
if o.auths == nil { |
|
o.auths = make(map[string]auth.Authentication) |
|
} |
|
o.auths[a.Name()] = a |
|
} |
|
} |
|
} |
|
|
|
// WithServerTLSConfig sets the TLS configuration for the server. |
|
func WithServerTLSConfig(tc *tls.Config) ServerOption { |
|
return func(o *serverOptions) { |
|
o.tlsConfig = tc |
|
} |
|
} |
|
|
|
// WithServerQuicConfig sets the QUIC configuration for the server. |
|
func WithServerQuicConfig(qc *quic.Config) ServerOption { |
|
return func(o *serverOptions) { |
|
o.quicConfig = qc |
|
} |
|
}
|
|
|