Add resolver for outbound dialer

This commit is contained in:
世界
2022-07-07 21:47:21 +08:00
parent ecac383477
commit 538a1f5909
32 changed files with 1058 additions and 222 deletions

View File

@@ -1,6 +1,8 @@
package adapter
import (
"context"
M "github.com/sagernet/sing/common/metadata"
)
@@ -17,6 +19,7 @@ type InboundContext struct {
Destination M.Socksaddr
Domain string
Protocol string
Outbound string
// cache
@@ -26,3 +29,26 @@ type InboundContext struct {
SourceGeoIPCode string
GeoIPCode string
}
type inboundContextKey struct{}
func WithContext(ctx context.Context, inboundContext *InboundContext) context.Context {
return context.WithValue(ctx, (*inboundContextKey)(nil), inboundContext)
}
func ContextFrom(ctx context.Context) *InboundContext {
metadata := ctx.Value((*inboundContextKey)(nil))
if metadata == nil {
return nil
}
return metadata.(*InboundContext)
}
func AppendContext(ctx context.Context) (context.Context, *InboundContext) {
metadata := ContextFrom(ctx)
if metadata != nil {
return ctx, metadata
}
metadata = new(InboundContext)
return WithContext(ctx, metadata), nil
}

View File

@@ -3,11 +3,15 @@ package adapter
import (
"context"
"net"
"net/netip"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing-box/common/geoip"
"github.com/sagernet/sing-box/common/geosite"
C "github.com/sagernet/sing-box/constant"
"golang.org/x/net/dns/dnsmessage"
)
type Router interface {
@@ -18,6 +22,9 @@ type Router interface {
RoutePacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext) error
GeoIPReader() *geoip.Reader
GeositeReader() *geosite.Reader
Exchange(ctx context.Context, message *dnsmessage.Message) (*dnsmessage.Message, error)
Lookup(ctx context.Context, domain string, strategy C.DomainStrategy) ([]netip.Addr, error)
LookupDefault(ctx context.Context, domain string) ([]netip.Addr, error)
}
type Rule interface {