go-socks5/option.go

120 lines
3.1 KiB
Go
Raw Normal View History

2020-04-20 13:17:38 +00:00
package socks5
import (
"context"
2020-04-22 02:15:40 +00:00
"io"
2020-04-20 13:17:38 +00:00
"net"
2022-10-17 01:36:23 +00:00
"git.tcp.direct/kayos/go-socks5/bufferpool"
2020-04-20 13:17:38 +00:00
)
2020-04-22 02:15:40 +00:00
// Option user's option
2020-04-20 13:17:38 +00:00
type Option func(s *Server)
2020-08-06 02:12:46 +00:00
// WithBufferPool can be provided to implement custom buffer pool
// By default, buffer pool use size is 32k
func WithBufferPool(bufferPool bufferpool.BufPool) Option {
2020-08-06 02:12:46 +00:00
return func(s *Server) {
s.bufferPool = bufferPool
}
}
2020-04-22 02:15:40 +00:00
// WithAuthMethods can be provided to implement custom authentication
2020-04-20 13:17:38 +00:00
// By default, "auth-less" mode is enabled.
// For password-based auth use UserPassAuthenticator.
func WithAuthMethods(authMethods []Authenticator) Option {
return func(s *Server) {
if len(authMethods) != 0 {
s.authCustomMethods = make([]Authenticator, 0, len(authMethods))
s.authCustomMethods = append(s.authCustomMethods, authMethods...)
}
}
}
2020-04-22 02:15:40 +00:00
// WithCredential If provided, username/password authentication is enabled,
2020-04-20 13:17:38 +00:00
// by appending a UserPassAuthenticator to AuthMethods. If not provided,
// and AUthMethods is nil, then "auth-less" mode is enabled.
func WithCredential(cs CredentialStore) Option {
return func(s *Server) {
2020-08-06 02:12:46 +00:00
s.credentials = cs
2020-04-20 13:17:38 +00:00
}
}
2020-04-22 02:15:40 +00:00
// WithResolver can be provided to do custom name resolution.
2020-04-20 13:17:38 +00:00
// Defaults to DNSResolver if not provided.
func WithResolver(res NameResolver) Option {
return func(s *Server) {
2020-08-06 02:12:46 +00:00
s.resolver = res
2020-04-20 13:17:38 +00:00
}
}
2020-04-22 02:15:40 +00:00
// WithRule is provided to enable custom logic around permitting
2020-08-05 06:40:07 +00:00
// various commands. If not provided, NewPermitAll is used.
2020-04-20 13:17:38 +00:00
func WithRule(rule RuleSet) Option {
return func(s *Server) {
2020-08-06 02:12:46 +00:00
s.rules = rule
2020-04-20 13:17:38 +00:00
}
}
2020-04-22 02:15:40 +00:00
// WithRewriter can be used to transparently rewrite addresses.
2020-04-20 13:17:38 +00:00
// This is invoked before the RuleSet is invoked.
// Defaults to NoRewrite.
func WithRewriter(rew AddressRewriter) Option {
return func(s *Server) {
2020-08-06 02:12:46 +00:00
s.rewriter = rew
2020-04-20 13:17:38 +00:00
}
}
2020-04-22 02:15:40 +00:00
// WithBindIP is used for bind or udp associate
2020-04-20 13:17:38 +00:00
func WithBindIP(ip net.IP) Option {
return func(s *Server) {
2020-04-20 13:27:12 +00:00
if len(ip) != 0 {
s.bindIP = make(net.IP, 0, len(ip))
2020-04-20 13:29:50 +00:00
s.bindIP = append(s.bindIP, ip...)
2020-04-20 13:17:38 +00:00
}
}
}
2020-04-22 02:15:40 +00:00
// WithLogger can be used to provide a custom log target.
2020-04-20 13:17:38 +00:00
// Defaults to ioutil.Discard.
func WithLogger(l Logger) Option {
return func(s *Server) {
2020-08-06 02:12:46 +00:00
s.logger = l
2020-04-20 13:17:38 +00:00
}
}
2020-04-22 02:15:40 +00:00
// WithDial Optional function for dialing out
2020-04-20 13:17:38 +00:00
func WithDial(dial func(ctx context.Context, network, addr string) (net.Conn, error)) Option {
return func(s *Server) {
2020-08-06 02:12:46 +00:00
s.dial = dial
2020-04-20 13:17:38 +00:00
}
}
2020-04-22 02:15:40 +00:00
// WithGPool can be provided to do custom goroutine pool.
2020-04-20 13:17:38 +00:00
func WithGPool(pool GPool) Option {
return func(s *Server) {
2020-08-06 02:12:46 +00:00
s.gPool = pool
2020-04-20 13:17:38 +00:00
}
}
2020-04-22 02:15:40 +00:00
// WithConnectHandle is used to handle a user's connect command
2020-04-24 07:52:47 +00:00
func WithConnectHandle(h func(ctx context.Context, writer io.Writer, request *Request) error) Option {
2020-04-22 02:15:40 +00:00
return func(s *Server) {
s.userConnectHandle = h
}
}
// WithBindHandle is used to handle a user's bind command
2020-04-24 07:52:47 +00:00
func WithBindHandle(h func(ctx context.Context, writer io.Writer, request *Request) error) Option {
2020-04-22 02:15:40 +00:00
return func(s *Server) {
s.userBindHandle = h
}
}
// WithAssociateHandle is used to handle a user's associate command
2020-04-24 07:52:47 +00:00
func WithAssociateHandle(h func(ctx context.Context, writer io.Writer, request *Request) error) Option {
2020-04-22 02:15:40 +00:00
return func(s *Server) {
s.userAssociateHandle = h
}
}