| 1 | |
| 2 | |
| 3 | |
| 4 | package windows |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | "math/rand/v2" |
| 9 | "slices" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | |
| 13 | "golang.org/x/sys/windows/registry" |
| 14 | ) |
| 15 | |
| 16 | const ( |
| 17 | guestCommunicationsPrefix = `SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\GuestCommunicationServices` |
| 18 | magicVSOCKSuffix = "-facb-11e6-bd58-64006a7986d3" |
| 19 | wslDistroInfoPrefix = `SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss` |
| 20 | ) |
| 21 | |
| 22 | |
| 23 | func AddVSockRegistryKey(port int) error { |
| 24 | rootKey, err := getGuestCommunicationServicesKey(true) |
| 25 | if err != nil { |
| 26 | return err |
| 27 | } |
| 28 | defer rootKey.Close() |
| 29 | |
| 30 | used, err := getUsedPorts(rootKey) |
| 31 | if err != nil { |
| 32 | return err |
| 33 | } |
| 34 | |
| 35 | if slices.Contains(used, port) { |
| 36 | return fmt.Errorf("port %d in use", port) |
| 37 | } |
| 38 | |
| 39 | vsockKeyPath := fmt.Sprintf(`%x%s`, port, magicVSOCKSuffix) |
| 40 | vSockKey, _, err := registry.CreateKey( |
| 41 | rootKey, |
| 42 | vsockKeyPath, |
| 43 | registry.ALL_ACCESS, |
| 44 | ) |
| 45 | if err != nil { |
| 46 | return fmt.Errorf( |
| 47 | "failed to create new key (%s%s): %w", |
| 48 | guestCommunicationsPrefix, |
| 49 | vsockKeyPath, |
| 50 | err, |
| 51 | ) |
| 52 | } |
| 53 | defer vSockKey.Close() |
| 54 | |
| 55 | return nil |
| 56 | } |
| 57 | |
| 58 | |
| 59 | func RemoveVSockRegistryKey(port int) error { |
| 60 | rootKey, err := getGuestCommunicationServicesKey(true) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | defer rootKey.Close() |
| 65 | |
| 66 | vsockKeyPath := fmt.Sprintf(`%x%s`, port, magicVSOCKSuffix) |
| 67 | if err := registry.DeleteKey(rootKey, vsockKeyPath); err != nil { |
| 68 | return fmt.Errorf( |
| 69 | "failed to create new key (%s%s): %w", |
| 70 | guestCommunicationsPrefix, |
| 71 | vsockKeyPath, |
| 72 | err, |
| 73 | ) |
| 74 | } |
| 75 | |
| 76 | return nil |
| 77 | } |
| 78 | |
| 79 | |
| 80 | func IsVSockPortFree(port int) (bool, error) { |
| 81 | rootKey, err := getGuestCommunicationServicesKey(false) |
| 82 | if err != nil { |
| 83 | return false, err |
| 84 | } |
| 85 | defer rootKey.Close() |
| 86 | |
| 87 | used, err := getUsedPorts(rootKey) |
| 88 | if err != nil { |
| 89 | return false, err |
| 90 | } |
| 91 | |
| 92 | if slices.Contains(used, port) { |
| 93 | return false, nil |
| 94 | } |
| 95 | |
| 96 | return true, nil |
| 97 | } |
| 98 | |
| 99 | |
| 100 | func GetDistroID(name string) (string, error) { |
| 101 | rootKey, err := registry.OpenKey( |
| 102 | registry.CURRENT_USER, |
| 103 | wslDistroInfoPrefix, |
| 104 | registry.READ, |
| 105 | ) |
| 106 | if err != nil { |
| 107 | return "", fmt.Errorf( |
| 108 | "failed to open Lxss key (%s): %w", |
| 109 | wslDistroInfoPrefix, |
| 110 | err, |
| 111 | ) |
| 112 | } |
| 113 | defer rootKey.Close() |
| 114 | |
| 115 | keys, err := rootKey.ReadSubKeyNames(-1) |
| 116 | if err != nil { |
| 117 | return "", fmt.Errorf("failed to read subkey names for %s: %w", wslDistroInfoPrefix, err) |
| 118 | } |
| 119 | |
| 120 | var out string |
| 121 | for _, k := range keys { |
| 122 | subKey, err := registry.OpenKey( |
| 123 | registry.CURRENT_USER, |
| 124 | fmt.Sprintf(`%s\%s`, wslDistroInfoPrefix, k), |
| 125 | registry.READ, |
| 126 | ) |
| 127 | if err != nil { |
| 128 | return "", fmt.Errorf("failed to read subkey %#q for key %#q: %w", k, wslDistroInfoPrefix, err) |
| 129 | } |
| 130 | dn, _, err := subKey.GetStringValue("DistributionName") |
| 131 | if err != nil { |
| 132 | return "", fmt.Errorf("failed to read 'DistributionName' value for subkey %#q of %#q: %w", k, wslDistroInfoPrefix, err) |
| 133 | } |
| 134 | if dn == name { |
| 135 | out = k |
| 136 | break |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if out == "" { |
| 141 | return "", fmt.Errorf("failed to find matching DistroID for %#q", name) |
| 142 | } |
| 143 | |
| 144 | return out, nil |
| 145 | } |
| 146 | |
| 147 | |
| 148 | func GetRandomFreeVSockPort(minPort, maxPort int) (int, error) { |
| 149 | rootKey, err := getGuestCommunicationServicesKey(false) |
| 150 | if err != nil { |
| 151 | return 0, err |
| 152 | } |
| 153 | defer rootKey.Close() |
| 154 | |
| 155 | used, err := getUsedPorts(rootKey) |
| 156 | if err != nil { |
| 157 | return 0, err |
| 158 | } |
| 159 | |
| 160 | type pair struct{ v, offset int } |
| 161 | tree := make([]pair, 1, len(used)+1) |
| 162 | tree[0] = pair{0, minPort} |
| 163 | |
| 164 | slices.Sort(used) |
| 165 | for i, v := range used { |
| 166 | if tree[len(tree)-1].v+tree[len(tree)-1].offset == v { |
| 167 | tree[len(tree)-1].offset++ |
| 168 | } else { |
| 169 | tree = append(tree, pair{v - minPort - i, minPort + i + 1}) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | v := rand.IntN(maxPort - minPort + 1 - len(used)) |
| 174 | |
| 175 | for len(tree) > 1 { |
| 176 | m := len(tree) / 2 |
| 177 | if v < tree[m].v { |
| 178 | tree = tree[:m] |
| 179 | } else { |
| 180 | tree = tree[m:] |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return tree[0].offset + v, nil |
| 185 | } |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | func getGuestCommunicationServicesKey(allowWrite bool) (registry.Key, error) { |
| 192 | var registryPermissions uint32 = registry.READ |
| 193 | if allowWrite { |
| 194 | registryPermissions = registry.WRITE | registry.READ |
| 195 | } |
| 196 | rootKey, err := registry.OpenKey( |
| 197 | registry.LOCAL_MACHINE, |
| 198 | guestCommunicationsPrefix, |
| 199 | registryPermissions, |
| 200 | ) |
| 201 | if err != nil { |
| 202 | return 0, fmt.Errorf( |
| 203 | "failed to open GuestCommunicationServices key (%s): %w", |
| 204 | guestCommunicationsPrefix, |
| 205 | err, |
| 206 | ) |
| 207 | } |
| 208 | |
| 209 | return rootKey, nil |
| 210 | } |
| 211 | |
| 212 | func getUsedPorts(key registry.Key) ([]int, error) { |
| 213 | keys, err := key.ReadSubKeyNames(-1) |
| 214 | if err != nil { |
| 215 | return nil, fmt.Errorf("failed to read subkey names for %s: %w", guestCommunicationsPrefix, err) |
| 216 | } |
| 217 | |
| 218 | out := []int{} |
| 219 | for _, k := range keys { |
| 220 | split := strings.Split(k, magicVSOCKSuffix) |
| 221 | if len(split) == 2 { |
| 222 | i, err := strconv.Atoi(split[0]) |
| 223 | if err != nil { |
| 224 | return nil, fmt.Errorf("failed convert %#q to int: %w", split[0], err) |
| 225 | } |
| 226 | out = append(out, i) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | return out, nil |
| 231 | } |