-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathwsl.go
37 lines (32 loc) · 1014 Bytes
/
wsl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"net"
"strings"
"github.com/containers/common/pkg/machine"
rkport "github.com/rootless-containers/rootlesskit/v2/pkg/port"
)
// WSL machines do not relay ipv4 traffic to dual-stack ports, simulate instead
func splitDualStackSpecIfWsl(spec rkport.Spec) []rkport.Spec {
specs := []rkport.Spec{spec}
protocol := spec.Proto
if machine.HostType() != machine.Wsl || strings.HasSuffix(protocol, "4") || strings.HasSuffix(protocol, "6") {
return specs
}
ip := net.ParseIP(spec.ParentIP)
splitLoopback := ip.IsLoopback() && ip.To4() == nil
// Map ::1 and 0.0.0.0/:: to ipv4 + ipv6 to simulate dual-stack
if ip.IsUnspecified() || splitLoopback {
specs = append(specs, spec)
specs[0].Proto = protocol + "4"
specs[1].Proto = protocol + "6"
if splitLoopback {
// Hacky, but we will only have one ipv4 loopback with WSL config
specs[0].ParentIP = "127.0.0.1"
}
if ip.IsUnspecified() {
specs[0].ParentIP = "0.0.0.0"
specs[1].ParentIP = "::"
}
}
return specs
}