|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +// dependencies that can be injected from testing |
| 10 | +var ( |
| 11 | + interfaceByName = net.InterfaceByName |
| 12 | + interfaceAddrs = net.InterfaceAddrs |
| 13 | + dial = net.Dial |
| 14 | + ifaceAddrs = func(iface *net.Interface) ([]net.Addr, error) { |
| 15 | + return iface.Addrs() |
| 16 | + } |
| 17 | +) |
| 18 | + |
| 19 | +// fetchAgentIP guesses the non-loopback IP address of the Agent host, according to the |
| 20 | +// user-provided configuration: |
| 21 | +// - If AgentIP is provided, this value is used whatever is the real IP of the Agent. |
| 22 | +// - AgentIPIface specifies which interface this function should look into in order to pickup an address. |
| 23 | +// - AgentIPType specifies which type of IP address should the agent pickup ("any" to pickup whichever |
| 24 | +// ipv4 or ipv6 address is found first) |
| 25 | +func fetchAgentIP(cfg *Config) (net.IP, error) { |
| 26 | + if cfg.AgentIP != "" { |
| 27 | + if ip := net.ParseIP(cfg.AgentIP); ip != nil { |
| 28 | + return ip, nil |
| 29 | + } |
| 30 | + return nil, fmt.Errorf("can't parse provided IP %v", cfg.AgentIP) |
| 31 | + } |
| 32 | + |
| 33 | + if cfg.AgentIPType != IPTypeAny && |
| 34 | + cfg.AgentIPType != IPTypeIPV6 && |
| 35 | + cfg.AgentIPType != IPTypeIPV4 { |
| 36 | + return nil, fmt.Errorf("invalid IP type %q. Valid values are: %s, %s or %s", |
| 37 | + cfg.AgentIPType, IPTypeIPV4, IPTypeIPV6, IPTypeAny) |
| 38 | + } |
| 39 | + |
| 40 | + switch cfg.AgentIPIface { |
| 41 | + case IPIfaceLocal: |
| 42 | + return fromLocal(cfg.AgentIPType) |
| 43 | + case IPIfaceExternal: |
| 44 | + return fromExternal(cfg.AgentIPType) |
| 45 | + default: |
| 46 | + if !strings.HasPrefix(cfg.AgentIPIface, IPIfaceNamedPrefix) { |
| 47 | + return nil, fmt.Errorf( |
| 48 | + "invalid IP interface %q. Valid values are: %s, %s or %s<iface_name>", |
| 49 | + cfg.AgentIPIface, IPIfaceLocal, IPIfaceExternal, IPIfaceNamedPrefix) |
| 50 | + } |
| 51 | + return fromInterface(cfg.AgentIPIface[len(IPIfaceNamedPrefix):], cfg.AgentIPType) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func fromInterface(ifName, ipType string) (net.IP, error) { |
| 56 | + iface, err := interfaceByName(ifName) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + addrs, err := ifaceAddrs(iface) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + if ip, ok := findAddress(addrs, ipType); ok { |
| 65 | + return ip, nil |
| 66 | + } |
| 67 | + return nil, fmt.Errorf("no matching %q addresses found at interface %v", ipType, ifName) |
| 68 | +} |
| 69 | + |
| 70 | +func fromLocal(ipType string) (net.IP, error) { |
| 71 | + addrs, err := interfaceAddrs() |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + if ip, ok := findAddress(addrs, ipType); ok { |
| 76 | + return ip, nil |
| 77 | + } |
| 78 | + return nil, fmt.Errorf("no matching local %q addresses found", ipType) |
| 79 | +} |
| 80 | + |
| 81 | +func fromExternal(ipType string) (net.IP, error) { |
| 82 | + // We don't really care about the existence or nonexistence of the addresses. |
| 83 | + // This will just establish an external dialer where we can pickup the external |
| 84 | + // host address |
| 85 | + addrStr := "8.8.8.8:80" |
| 86 | + if ipType == IPTypeIPV6 { |
| 87 | + addrStr = "[2001:4860:4860::8888]:80" |
| 88 | + } |
| 89 | + conn, err := dial("udp", addrStr) |
| 90 | + if err != nil { |
| 91 | + return nil, fmt.Errorf("can't establish an external connection %w", err) |
| 92 | + } |
| 93 | + if addr, ok := conn.LocalAddr().(*net.UDPAddr); !ok { |
| 94 | + return nil, fmt.Errorf("unexpected local address type %T for external connection", |
| 95 | + conn.LocalAddr()) |
| 96 | + } else if ip, ok := getIP(addr.IP, ipType); ok { |
| 97 | + return ip, nil |
| 98 | + } |
| 99 | + return nil, fmt.Errorf("no matching %q external addresses found", ipType) |
| 100 | +} |
| 101 | + |
| 102 | +func findAddress(addrs []net.Addr, ipType string) (net.IP, bool) { |
| 103 | + for _, addr := range addrs { |
| 104 | + if ipnet, ok := addr.(*net.IPNet); ok && ipnet != nil { |
| 105 | + if ip, ok := getIP(ipnet.IP, ipType); ok { |
| 106 | + return ip, true |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + return nil, false |
| 111 | +} |
| 112 | + |
| 113 | +func getIP(pip net.IP, ipType string) (net.IP, bool) { |
| 114 | + if pip == nil || pip.IsLoopback() { |
| 115 | + return nil, false |
| 116 | + } |
| 117 | + switch ipType { |
| 118 | + case IPTypeIPV4: |
| 119 | + if ip := pip.To4(); ip != nil { |
| 120 | + return ip, true |
| 121 | + } |
| 122 | + case IPTypeIPV6: |
| 123 | + // as any IP4 address can be converted to IP6, we only return any |
| 124 | + // address that can be converted to IP6 but not to IP4 |
| 125 | + if ip := pip.To16(); ip != nil && pip.To4() == nil { |
| 126 | + return ip, true |
| 127 | + } |
| 128 | + default: // Any |
| 129 | + if ip := pip.To4(); ip != nil { |
| 130 | + return ip, true |
| 131 | + } |
| 132 | + if ip := pip.To16(); ip != nil { |
| 133 | + return ip, true |
| 134 | + } |
| 135 | + } |
| 136 | + return nil, false |
| 137 | +} |
0 commit comments