Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

统一 domainStrategy 行为. (sockopt) #2043

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions infra/conf/transport_internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ type SocketConfig struct {
TCPKeepAliveIdle int32 `json:"tcpKeepAliveIdle"`
TCPCongestion string `json:"tcpCongestion"`
TCPWindowClamp int32 `json:"tcpWindowClamp"`
TCPMaxSeg int32 `json:"tcpMaxSeg"`
TCPMaxSeg int32 `json:"tcpMaxSeg"`
TCPUserTimeout int32 `json:"tcpUserTimeout"`
V6only bool `json:"v6only"`
Interface string `json:"interface"`
Expand Down Expand Up @@ -652,12 +652,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 All @@ -671,7 +689,7 @@ func (c *SocketConfig) Build() (*internet.SocketConfig, error) {
TcpKeepAliveIdle: c.TCPKeepAliveIdle,
TcpCongestion: c.TCPCongestion,
TcpWindowClamp: c.TCPWindowClamp,
TcpMaxSeg: c.TCPMaxSeg,
TcpMaxSeg: c.TCPMaxSeg,
TcpUserTimeout: c.TCPUserTimeout,
V6Only: c.V6only,
Interface: c.Interface,
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
}
87 changes: 57 additions & 30 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