add resolver for router

This commit is contained in:
ginuerzh
2021-12-30 23:07:05 +08:00
parent a6671a468e
commit a430384bba
22 changed files with 362 additions and 318 deletions

View File

@ -6,7 +6,6 @@ import (
"net"
"time"
"github.com/go-gost/gost/pkg/chain"
"github.com/go-gost/gost/pkg/handler"
"github.com/go-gost/relay"
)
@ -38,11 +37,7 @@ func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network
return
}
r := (&chain.Router{}).
WithChain(h.chain).
WithRetry(h.md.retryCount).
WithLogger(h.logger)
cc, err := r.Dial(ctx, network, address)
cc, err := h.router.Dial(ctx, network, address)
if err != nil {
resp.Status = relay.StatusNetworkUnreachable
resp.WriteTo(conn)

View File

@ -6,7 +6,6 @@ import (
"net"
"time"
"github.com/go-gost/gost/pkg/chain"
"github.com/go-gost/gost/pkg/handler"
"github.com/go-gost/relay"
)
@ -30,12 +29,7 @@ func (h *relayHandler) handleForward(ctx context.Context, conn net.Conn, network
h.logger.Infof("%s >> %s", conn.RemoteAddr(), target.Addr())
r := (&chain.Router{}).
WithChain(h.chain).
WithRetry(h.md.retryCount).
WithLogger(h.logger)
cc, err := r.Dial(ctx, network, target.Addr())
cc, err := h.router.Dial(ctx, network, target.Addr())
if err != nil {
// TODO: the router itself may be failed due to the failed node in the router,
// the dead marker may be a wrong operation.

View File

@ -21,8 +21,8 @@ func init() {
type relayHandler struct {
group *chain.NodeGroup
chain *chain.Chain
bypass bypass.Bypass
router *chain.Router
logger logger.Logger
md metadata
}
@ -35,17 +35,26 @@ func NewHandler(opts ...handler.Option) handler.Handler {
return &relayHandler{
bypass: options.Bypass,
router: (&chain.Router{}).
WithLogger(options.Logger).
WithResolver(options.Resolver),
logger: options.Logger,
}
}
func (h *relayHandler) Init(md md.Metadata) (err error) {
return h.parseMetadata(md)
if err := h.parseMetadata(md); err != nil {
return err
}
h.router.WithRetry(h.md.retryCount)
return nil
}
// WithChain implements chain.Chainable interface
func (h *relayHandler) WithChain(chain *chain.Chain) {
h.chain = chain
h.router.WithChain(chain)
}
// Forward implements handler.Forwarder.