|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package gxnet |
| 19 | + |
| 20 | +import ( |
| 21 | + "net" |
| 22 | + "strings" |
| 23 | +) |
| 24 | + |
| 25 | +import ( |
| 26 | + perrors "github.com/pkg/errors" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + privateBlocks []*net.IPNet |
| 31 | +) |
| 32 | + |
| 33 | +func init() { |
| 34 | + for _, b := range []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"} { |
| 35 | + if _, block, err := net.ParseCIDR(b); err == nil { |
| 36 | + privateBlocks = append(privateBlocks, block) |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func GetLocalIP() (string, error) { |
| 42 | + faces, err := net.Interfaces() |
| 43 | + if err != nil { |
| 44 | + return "", perrors.WithStack(err) |
| 45 | + } |
| 46 | + |
| 47 | + var addr net.IP |
| 48 | + for _, face := range faces { |
| 49 | + if !isValidNetworkInterface(face) { |
| 50 | + continue |
| 51 | + } |
| 52 | + |
| 53 | + addrs, err := face.Addrs() |
| 54 | + if err != nil { |
| 55 | + return "", perrors.WithStack(err) |
| 56 | + } |
| 57 | + |
| 58 | + if ipv4, ok := getValidIPv4(addrs); ok { |
| 59 | + addr = ipv4 |
| 60 | + if isPrivateIP(ipv4) { |
| 61 | + return ipv4.String(), nil |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if addr == nil { |
| 67 | + return "", perrors.Errorf("can not get local IP") |
| 68 | + } |
| 69 | + |
| 70 | + return addr.String(), nil |
| 71 | +} |
| 72 | + |
| 73 | +func isPrivateIP(ip net.IP) bool { |
| 74 | + for _, priv := range privateBlocks { |
| 75 | + if priv.Contains(ip) { |
| 76 | + return true |
| 77 | + } |
| 78 | + } |
| 79 | + return false |
| 80 | +} |
| 81 | + |
| 82 | +func getValidIPv4(addrs []net.Addr) (net.IP, bool) { |
| 83 | + for _, addr := range addrs { |
| 84 | + var ip net.IP |
| 85 | + |
| 86 | + switch v := addr.(type) { |
| 87 | + case *net.IPNet: |
| 88 | + ip = v.IP |
| 89 | + case *net.IPAddr: |
| 90 | + ip = v.IP |
| 91 | + } |
| 92 | + |
| 93 | + if ip == nil || ip.IsLoopback() { |
| 94 | + continue |
| 95 | + } |
| 96 | + |
| 97 | + ip = ip.To4() |
| 98 | + if ip == nil { |
| 99 | + // not an valid ipv4 address |
| 100 | + continue |
| 101 | + } |
| 102 | + |
| 103 | + return ip, true |
| 104 | + } |
| 105 | + return nil, false |
| 106 | +} |
| 107 | + |
| 108 | +func isValidNetworkInterface(face net.Interface) bool { |
| 109 | + if face.Flags&net.FlagUp == 0 { |
| 110 | + // interface down |
| 111 | + return false |
| 112 | + } |
| 113 | + |
| 114 | + if face.Flags&net.FlagLoopback != 0 { |
| 115 | + // loopback interface |
| 116 | + return false |
| 117 | + } |
| 118 | + |
| 119 | + if strings.Contains(strings.ToLower(face.Name), "docker") { |
| 120 | + return false |
| 121 | + } |
| 122 | + |
| 123 | + return true |
| 124 | +} |
0 commit comments