Skip to content

Commit

Permalink
Apply timeout to dns outbound (#1330)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Oct 15, 2021
1 parent e621175 commit 7b0699e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
8 changes: 5 additions & 3 deletions infra/conf/dns_proxy.go
Expand Up @@ -9,9 +9,10 @@ import (
)

type DNSOutboundConfig struct {
Network cfgcommon.Network `json:"network"`
Address *cfgcommon.Address `json:"address"`
Port uint16 `json:"port"`
Network cfgcommon.Network `json:"network"`
Address *cfgcommon.Address `json:"address"`
Port uint16 `json:"port"`
UserLevel uint32 `json:"userLevel"`
}

func (c *DNSOutboundConfig) Build() (proto.Message, error) {
Expand All @@ -20,6 +21,7 @@ func (c *DNSOutboundConfig) Build() (proto.Message, error) {
Network: c.Network.Build(),
Port: uint32(c.Port),
},
UserLevel: c.UserLevel,
}
if c.Address != nil {
config.Server.Address = c.Address.Build()
Expand Down
30 changes: 20 additions & 10 deletions proxy/dns/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions proxy/dns/config.proto
Expand Up @@ -12,4 +12,5 @@ message Config {
// Server is the DNS server address. If specified, this address overrides the
// original one.
v2ray.core.common.net.Endpoint server = 1;
uint32 user_level = 2;
}
18 changes: 15 additions & 3 deletions proxy/dns/dns.go
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"io"
"sync"
"time"

"golang.org/x/net/dns/dnsmessage"

Expand All @@ -16,17 +17,19 @@ import (
"github.com/v2fly/v2ray-core/v4/common/net"
dns_proto "github.com/v2fly/v2ray-core/v4/common/protocol/dns"
"github.com/v2fly/v2ray-core/v4/common/session"
"github.com/v2fly/v2ray-core/v4/common/signal"
"github.com/v2fly/v2ray-core/v4/common/task"
"github.com/v2fly/v2ray-core/v4/features/dns"
"github.com/v2fly/v2ray-core/v4/features/policy"
"github.com/v2fly/v2ray-core/v4/transport"
"github.com/v2fly/v2ray-core/v4/transport/internet"
)

func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
h := new(Handler)
if err := core.RequireFeatures(ctx, func(dnsClient dns.Client) error {
return h.Init(config.(*Config), dnsClient)
if err := core.RequireFeatures(ctx, func(dnsClient dns.Client, policyManager policy.Manager) error {
return h.Init(config.(*Config), dnsClient, policyManager)
}); err != nil {
return nil, err
}
Expand All @@ -44,10 +47,12 @@ type Handler struct {
ipv6Lookup dns.IPv6Lookup
ownLinkVerifier ownLinkVerifier
server net.Destination
timeout time.Duration
}

func (h *Handler) Init(config *Config, dnsClient dns.Client) error {
func (h *Handler) Init(config *Config, dnsClient dns.Client, policyManager policy.Manager) error {
h.client = dnsClient
h.timeout = policyManager.ForLevel(config.UserLevel).Timeouts.ConnectionIdle

if ipv4lookup, ok := dnsClient.(dns.IPv4Lookup); ok {
h.ipv4Lookup = ipv4lookup
Expand Down Expand Up @@ -160,6 +165,9 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, d internet.
}
}

ctx, cancel := context.WithCancel(ctx)
timer := signal.CancelAfterInactivity(ctx, cancel, h.timeout)

request := func() error {
defer conn.Close()

Expand All @@ -173,6 +181,8 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, d internet.
return err
}

timer.Update()

if !h.isOwnLink(ctx) {
isIPQuery, domain, id, qType := parseIPQuery(b.Bytes())
if isIPQuery {
Expand All @@ -198,6 +208,8 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, d internet.
return err
}

timer.Update()

if err := writer.WriteMessage(b); err != nil {
return err
}
Expand Down

0 comments on commit 7b0699e

Please sign in to comment.