Skip to content

Commit

Permalink
统一 domainStrategy 行为. (#2720)
Browse files Browse the repository at this point in the history
* 统一 `domainStrategy` 行为.

* Update proto

---------

Co-authored-by: rui0572 <[email protected]>
  • Loading branch information
yuhan6665 and rui0572 authored Nov 12, 2023
1 parent d9fd3f8 commit 7523f7f
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 49 deletions.
24 changes: 21 additions & 3 deletions infra/conf/transport_internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,12 +654,30 @@ func (c *SocketConfig) Build() (*internet.SocketConfig, error) {

dStrategy := internet.DomainStrategy_AS_IS
switch strings.ToLower(c.DomainStrategy) {
case "useip", "use_ip":
case "asis", "":
dStrategy = internet.DomainStrategy_AS_IS
case "useip":
dStrategy = internet.DomainStrategy_USE_IP
case "useip4", "useipv4", "use_ipv4", "use_ip_v4", "use_ip4":
case "useipv4":
dStrategy = internet.DomainStrategy_USE_IP4
case "useip6", "useipv6", "use_ipv6", "use_ip_v6", "use_ip6":
case "useipv6":
dStrategy = internet.DomainStrategy_USE_IP6
case "useipv4v6":
dStrategy = internet.DomainStrategy_USE_IP46
case "useipv6v4":
dStrategy = internet.DomainStrategy_USE_IP64
case "forceip":
dStrategy = internet.DomainStrategy_FORCE_IP
case "forceipv4":
dStrategy = internet.DomainStrategy_FORCE_IP4
case "forceipv6":
dStrategy = internet.DomainStrategy_FORCE_IP6
case "forceipv4v6":
dStrategy = internet.DomainStrategy_FORCE_IP46
case "forceipv6v4":
dStrategy = internet.DomainStrategy_FORCE_IP64
default:
return nil, newError("unsupported domain strategy: ", c.DomainStrategy)
}

return &internet.SocketConfig{
Expand Down
43 changes: 43 additions & 0 deletions transport/internet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ var (
globalTransportSettings []*TransportConfig
)

var strategy = [][]byte{
// name strategy, prefer, fallback
{0, 0, 0}, // AsIs none, /, /
{1, 0, 0}, // UseIP use, both, none
{1, 4, 0}, // UseIPv4 use, 4, none
{1, 6, 0}, // UseIPv6 use, 6, none
{1, 4, 6}, // UseIPv4v6 use, 4, 6
{1, 6, 4}, // UseIPv6v4 use, 6, 4
{2, 0, 0}, // ForceIP force, both, none
{2, 4, 0}, // ForceIPv4 force, 4, none
{2, 6, 0}, // ForceIPv6 force, 6, none
{2, 4, 6}, // ForceIPv4v6 force, 4, 6
{2, 6, 4}, // ForceIPv6v4 force, 6, 4
}

const unknownProtocol = "unknown"

func transportProtocolToString(protocol TransportProtocol) string {
Expand Down Expand Up @@ -122,3 +137,31 @@ func (c *ProxyConfig) HasTag() bool {
func (m SocketConfig_TProxyMode) IsEnabled() bool {
return m != SocketConfig_Off
}

func (s DomainStrategy) hasStrategy() bool {
return strategy[s][0] != 0
}

func (s DomainStrategy) forceIP() bool {
return strategy[s][0] == 2
}

func (s DomainStrategy) preferIP4() bool {
return strategy[s][1] == 4 || strategy[s][1] == 0
}

func (s DomainStrategy) preferIP6() bool {
return strategy[s][1] == 6 || strategy[s][1] == 0
}

func (s DomainStrategy) hasFallback() bool {
return strategy[s][2] != 0
}

func (s DomainStrategy) fallbackIP4() bool {
return strategy[s][2] == 4
}

func (s DomainStrategy) fallbackIP6() bool {
return strategy[s][2] == 6
}
75 changes: 51 additions & 24 deletions transport/internet/config.pb.go

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

7 changes: 7 additions & 0 deletions transport/internet/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ enum DomainStrategy {
USE_IP = 1;
USE_IP4 = 2;
USE_IP6 = 3;
USE_IP46 = 4;
USE_IP64 = 5;
FORCE_IP = 6;
FORCE_IP4 = 7;
FORCE_IP6 = 8;
FORCE_IP46 = 9;
FORCE_IP64 = 10;
}

message TransportConfig {
Expand Down
34 changes: 12 additions & 22 deletions transport/internet/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,37 +78,27 @@ func lookupIP(domain string, strategy DomainStrategy, localAddr net.Address) ([]
return nil, nil
}

option := dns.IPOption{
IPv4Enable: true,
IPv6Enable: true,
FakeEnable: false,
}

switch {
case strategy == DomainStrategy_USE_IP4 || (localAddr != nil && localAddr.Family().IsIPv4()):
option = dns.IPOption{
IPv4Enable: true,
IPv6Enable: false,
FakeEnable: false,
ips, err := dnsClient.LookupIP(domain, dns.IPOption{
IPv4Enable: (localAddr == nil || localAddr.Family().IsIPv4()) && strategy.preferIP4(),
IPv6Enable: (localAddr == nil || localAddr.Family().IsIPv6()) && strategy.preferIP6(),
})
{ // Resolve fallback
if (len(ips) == 0 || err != nil) && strategy.hasFallback() && localAddr == nil {
ips, err = dnsClient.LookupIP(domain, dns.IPOption{
IPv4Enable: strategy.fallbackIP4(),
IPv6Enable: strategy.fallbackIP6(),
})
}
case strategy == DomainStrategy_USE_IP6 || (localAddr != nil && localAddr.Family().IsIPv6()):
option = dns.IPOption{
IPv4Enable: false,
IPv6Enable: true,
FakeEnable: false,
}
case strategy == DomainStrategy_AS_IS:
return nil, nil
}

return dnsClient.LookupIP(domain, option)
return ips, err
}

func canLookupIP(ctx context.Context, dst net.Destination, sockopt *SocketConfig) bool {
if dst.Address.Family().IsIP() || dnsClient == nil {
return false
}
return sockopt.DomainStrategy != DomainStrategy_AS_IS
return sockopt.DomainStrategy.hasStrategy()
}

func redirect(ctx context.Context, dst net.Destination, obt string) net.Conn {
Expand Down

1 comment on commit 7523f7f

@chika0801
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原讨论链接:#2026

Please sign in to comment.