浏览文件
dnshosts.go
111 行 · 2.7 KB历史下载| 1 | // SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | // From https://raw.githubusercontent.com/abiosoft/colima/v0.5.5/daemon/process/gvproxy/dnshosts_test.go |
| 5 | /* |
| 6 | MIT License |
| 7 | |
| 8 | Copyright (c) 2021 Abiola Ibrahim |
| 9 | |
| 10 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | of this software and associated documentation files (the "Software"), to deal |
| 12 | in the Software without restriction, including without limitation the rights |
| 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | copies of the Software, and to permit persons to whom the Software is |
| 15 | furnished to do so, subject to the following conditions: |
| 16 | |
| 17 | The above copyright notice and this permission notice shall be included in all |
| 18 | copies or substantial portions of the Software. |
| 19 | |
| 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | package dnshosts |
| 30 | |
| 31 | import ( |
| 32 | "net" |
| 33 | "strings" |
| 34 | |
| 35 | "github.com/containers/gvisor-tap-vsock/pkg/types" |
| 36 | ) |
| 37 | |
| 38 | func ExtractZones(hosts hostMap) []types.Zone { |
| 39 | list := make(map[string]types.Zone) |
| 40 | |
| 41 | for host := range hosts { |
| 42 | h := zoneHost(host) |
| 43 | |
| 44 | zone := types.Zone{Name: h.name()} |
| 45 | if existingZone, ok := list[h.name()]; ok { |
| 46 | zone = existingZone |
| 47 | } |
| 48 | |
| 49 | if h.recordName() == "" { |
| 50 | if zone.DefaultIP == nil { |
| 51 | zone.DefaultIP = hosts.hostIP(host) |
| 52 | } |
| 53 | } else { |
| 54 | zone.Records = append(zone.Records, types.Record{ |
| 55 | Name: h.recordName(), |
| 56 | IP: hosts.hostIP(host), |
| 57 | }) |
| 58 | } |
| 59 | |
| 60 | list[h.name()] = zone |
| 61 | } |
| 62 | |
| 63 | zones := make([]types.Zone, 0, len(list)) |
| 64 | for _, zone := range list { |
| 65 | zones = append(zones, zone) |
| 66 | } |
| 67 | return zones |
| 68 | } |
| 69 | |
| 70 | type hostMap map[string]string |
| 71 | |
| 72 | func (z hostMap) hostIP(host string) net.IP { |
| 73 | for { |
| 74 | // check if host entry exists |
| 75 | h, ok := z[host] |
| 76 | if !ok || h == "" { |
| 77 | return nil |
| 78 | } |
| 79 | |
| 80 | // if it's a valid ip, return |
| 81 | if ip := net.ParseIP(h); ip != nil { |
| 82 | return ip |
| 83 | } |
| 84 | |
| 85 | // otherwise, a string i.e. another host |
| 86 | // loop through the process again. |
| 87 | host = h |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | type zoneHost string |
| 92 | |
| 93 | func (z zoneHost) name() string { |
| 94 | i := z.dotIndex() |
| 95 | if i < 0 { |
| 96 | return string(z) |
| 97 | } |
| 98 | return string(z)[i+1:] + "." |
| 99 | } |
| 100 | |
| 101 | func (z zoneHost) recordName() string { |
| 102 | i := z.dotIndex() |
| 103 | if i < 0 { |
| 104 | return "" |
| 105 | } |
| 106 | return string(z)[:i] |
| 107 | } |
| 108 | |
| 109 | func (z zoneHost) dotIndex() int { |
| 110 | return strings.LastIndex(string(z), ".") |
| 111 | } |

