add chain.Chainable interface

This commit is contained in:
ginuerzh
2021-11-23 21:44:32 +08:00
parent ece51cb0b8
commit 7beb5bf6d7
18 changed files with 125 additions and 63 deletions

View File

@ -1,5 +1,9 @@
package chain
type Chainable interface {
WithChain(chain *Chain)
}
type Chain struct {
groups []*NodeGroup
}

View File

@ -33,7 +33,6 @@ func NewHandler(opts ...handler.Option) handler.Handler {
}
return &forwardHandler{
chain: options.Chain,
bypass: options.Bypass,
logger: options.Logger,
}
@ -43,6 +42,11 @@ func (h *forwardHandler) Init(md md.Metadata) (err error) {
return h.parseMetadata(md)
}
// implements chain.Chainable interface
func (h *forwardHandler) WithChain(chain *chain.Chain) {
h.chain = chain
}
// Forward implements handler.Forwarder.
func (h *forwardHandler) Forward(group *chain.NodeGroup) {
h.group = group

View File

@ -41,7 +41,6 @@ func NewHandler(opts ...handler.Option) handler.Handler {
}
return &httpHandler{
chain: options.Chain,
bypass: options.Bypass,
logger: options.Logger,
}
@ -51,6 +50,11 @@ func (h *httpHandler) Init(md md.Metadata) error {
return h.parseMetadata(md)
}
// implements chain.Chainable interface
func (h *httpHandler) WithChain(chain *chain.Chain) {
h.chain = chain
}
func (h *httpHandler) Handle(ctx context.Context, conn net.Conn) {
defer conn.Close()

View File

@ -2,12 +2,10 @@ package handler
import (
"github.com/go-gost/gost/pkg/bypass"
"github.com/go-gost/gost/pkg/chain"
"github.com/go-gost/gost/pkg/logger"
)
type Options struct {
Chain *chain.Chain
Bypass bypass.Bypass
Logger logger.Logger
}
@ -20,12 +18,6 @@ func LoggerOption(logger logger.Logger) Option {
}
}
func ChainOption(chain *chain.Chain) Option {
return func(opts *Options) {
opts.Chain = chain
}
}
func BypassOption(bypass bypass.Bypass) Option {
return func(opts *Options) {
opts.Bypass = bypass

View File

@ -34,7 +34,6 @@ func NewHandler(opts ...handler.Option) handler.Handler {
}
return &relayHandler{
chain: options.Chain,
bypass: options.Bypass,
logger: options.Logger,
}
@ -44,6 +43,11 @@ func (h *relayHandler) Init(md md.Metadata) (err error) {
return h.parseMetadata(md)
}
// implements chain.Chainable interface
func (h *relayHandler) WithChain(chain *chain.Chain) {
h.chain = chain
}
// Forward implements handler.Forwarder.
func (h *relayHandler) Forward(group *chain.NodeGroup) {
h.group = group

View File

@ -33,7 +33,6 @@ func NewHandler(opts ...handler.Option) handler.Handler {
}
return &socks4Handler{
chain: options.Chain,
bypass: options.Bypass,
logger: options.Logger,
}
@ -43,6 +42,11 @@ func (h *socks4Handler) Init(md md.Metadata) (err error) {
return h.parseMetadata(md)
}
// implements chain.Chainable interface
func (h *socks4Handler) WithChain(chain *chain.Chain) {
h.chain = chain
}
func (h *socks4Handler) Handle(ctx context.Context, conn net.Conn) {
defer conn.Close()

View File

@ -35,7 +35,6 @@ func NewHandler(opts ...handler.Option) handler.Handler {
}
return &socks5Handler{
chain: options.Chain,
bypass: options.Bypass,
logger: options.Logger,
}
@ -56,6 +55,11 @@ func (h *socks5Handler) Init(md md.Metadata) (err error) {
return
}
// implements chain.Chainable interface
func (h *socks5Handler) WithChain(chain *chain.Chain) {
h.chain = chain
}
func (h *socks5Handler) Handle(ctx context.Context, conn net.Conn) {
defer conn.Close()

View File

@ -35,7 +35,6 @@ func NewHandler(opts ...handler.Option) handler.Handler {
}
return &ssHandler{
chain: options.Chain,
bypass: options.Bypass,
logger: options.Logger,
}
@ -45,6 +44,11 @@ func (h *ssHandler) Init(md md.Metadata) (err error) {
return h.parseMetadata(md)
}
// implements chain.Chainable interface
func (h *ssHandler) WithChain(chain *chain.Chain) {
h.chain = chain
}
func (h *ssHandler) Handle(ctx context.Context, conn net.Conn) {
defer conn.Close()

View File

@ -34,7 +34,6 @@ func NewHandler(opts ...handler.Option) handler.Handler {
}
return &ssuHandler{
chain: options.Chain,
bypass: options.Bypass,
logger: options.Logger,
}
@ -44,6 +43,11 @@ func (h *ssuHandler) Init(md md.Metadata) (err error) {
return h.parseMetadata(md)
}
// implements chain.Chainable interface
func (h *ssuHandler) WithChain(chain *chain.Chain) {
h.chain = chain
}
func (h *ssuHandler) Handle(ctx context.Context, conn net.Conn) {
defer conn.Close()

View File

@ -4,7 +4,6 @@ import (
"errors"
"net"
"github.com/go-gost/gost/pkg/chain"
"github.com/go-gost/gost/pkg/metadata"
)
@ -22,7 +21,3 @@ type Listener interface {
type Accepter interface {
Accept() (net.Conn, error)
}
type Chainable interface {
Chain(chain *chain.Chain)
}

View File

@ -38,8 +38,8 @@ func NewListener(opts ...listener.Option) listener.Listener {
}
}
// implements listener.Chainable interface
func (l *rtcpListener) Chain(chain *chain.Chain) {
// implements chain.Chainable interface
func (l *rtcpListener) WithChain(chain *chain.Chain) {
l.chain = chain
}

View File

@ -38,8 +38,8 @@ func NewListener(opts ...listener.Option) listener.Listener {
}
}
// implements listener.Chainable interface
func (l *rudpListener) Chain(chain *chain.Chain) {
// implements chain.Chainable interface
func (l *rudpListener) WithChain(chain *chain.Chain) {
l.chain = chain
}

53
pkg/logger/nop_logger.go Normal file
View File

@ -0,0 +1,53 @@
package logger
var (
nop = &nopLogger{}
)
func Nop() Logger {
return nop
}
type nopLogger struct{}
func (l *nopLogger) WithFields(fields map[string]interface{}) Logger {
return l
}
func (l *nopLogger) Debug(args ...interface{}) {
}
func (l *nopLogger) Debugf(format string, args ...interface{}) {
}
func (l *nopLogger) Info(args ...interface{}) {
}
func (l *nopLogger) Infof(format string, args ...interface{}) {
}
func (l *nopLogger) Warn(args ...interface{}) {
}
func (l *nopLogger) Warnf(format string, args ...interface{}) {
}
func (l *nopLogger) Error(args ...interface{}) {
}
func (l *nopLogger) Errorf(format string, args ...interface{}) {
}
func (l *nopLogger) Fatal(args ...interface{}) {
}
func (l *nopLogger) Fatalf(format string, args ...interface{}) {
}
func (l *nopLogger) GetLevel() LogLevel {
return ""
}
func (l *nopLogger) IsLevelEnabled(level LogLevel) bool {
return false
}