Get public IP in Golang

Shubhra :

How do we get public IP in golang? Method req.Header.Get("X-Forwarded-For") returns array of IPs. How do we identify which one is public and which on internal? Is there any other method to fetch public(external) IP?

Sarath Sadasivan Pillai :

The following IP blocks are reserved for private IP addresses.

Class        Starting IPAddress    Ending IP Address    # of Hosts
A            10.0.0.0              10.255.255.255       16,777,216
B            172.16.0.0            172.31.255.255       1,048,576
C            192.168.0.0           192.168.255.255      65,536
Link-local-u 169.254.0.0           169.254.255.255      65,536
Link-local-m 224.0.0.0             224.0.0.255          256
Local        127.0.0.0             127.255.255.255      16777216

You may write a function that checks for if the ip comes under these

Here is an attempt to do the same,the code below is not handling ipv6 please add if needed

func IsPublicIP(IP net.IP) bool {
    if IP.IsLoopback() || IP.IsLinkLocalMulticast() || IP.IsLinkLocalUnicast() {
        return false
    }
    if ip4 := IP.To4(); ip4 != nil {
        switch {
        case ip4[0] == 10:
            return false
        case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31:
            return false
        case ip4[0] == 192 && ip4[1] == 168:
            return false
        default:
            return true
        }
    }
    return false
}

Here is play link

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Public IP works but with DynDNS I get an ERR_CONNECTION_REFUSED

分類Dev

how can i get clients ip address on UDP server in golang?

分類Dev

AWS ECS : get public IP of the instance container when start task is called

分類Dev

How to get the public IP address of localhost using asp.net c#?

分類Dev

exposing CockroachDB on Kubernetes to public IP

分類Dev

Can access port on public IP but not private

分類Dev

Is it possible to assign a public IP to a Lambda function in AWS?

分類Dev

Redis Cluster Create Replicas Bind Public IP

分類Dev

Could not run nodejs on my public ip address

分類Dev

How to contact a person knowing their public IP address?

分類Dev

ssh client not connecting through public ip

分類Dev

ssh client not connecting through public ip

分類Dev

Debian 9 - Fail ping my public IP

分類Dev

Assign public ip to every openvpn client

分類Dev

iptables dnat mapping a private ip to public to specific ip

分類Dev

Golang http.Get()

分類Dev

Channels in Golang with TCP/IP socket not working

分類Dev

Get IP of wifi gateway?

分類Dev

Azure AKS Public IP in Non-standard Resource Group

分類Dev

How are local ip addresses separated from public ones?

分類Dev

How to find CIDR subnet for a public Internet IP address?

分類Dev

Chrome Extension to Modify request headers based on Public IP

分類Dev

Java RMI: running RMI application over the Internet with public IP of router

分類Dev

Can i use a DNS service to change my public ip?

分類Dev

How to monitor public ip and the location(country, state, city) simultaneously in terminal

分類Dev

Make my current Public IP address unchangeable.

分類Dev

How to get the time of public transport in maps api

分類Dev

Get public link to file with Google Drive SDK

分類Dev

Problem with public key on apt-get update

Related 関連記事

  1. 1

    Public IP works but with DynDNS I get an ERR_CONNECTION_REFUSED

  2. 2

    how can i get clients ip address on UDP server in golang?

  3. 3

    AWS ECS : get public IP of the instance container when start task is called

  4. 4

    How to get the public IP address of localhost using asp.net c#?

  5. 5

    exposing CockroachDB on Kubernetes to public IP

  6. 6

    Can access port on public IP but not private

  7. 7

    Is it possible to assign a public IP to a Lambda function in AWS?

  8. 8

    Redis Cluster Create Replicas Bind Public IP

  9. 9

    Could not run nodejs on my public ip address

  10. 10

    How to contact a person knowing their public IP address?

  11. 11

    ssh client not connecting through public ip

  12. 12

    ssh client not connecting through public ip

  13. 13

    Debian 9 - Fail ping my public IP

  14. 14

    Assign public ip to every openvpn client

  15. 15

    iptables dnat mapping a private ip to public to specific ip

  16. 16

    Golang http.Get()

  17. 17

    Channels in Golang with TCP/IP socket not working

  18. 18

    Get IP of wifi gateway?

  19. 19

    Azure AKS Public IP in Non-standard Resource Group

  20. 20

    How are local ip addresses separated from public ones?

  21. 21

    How to find CIDR subnet for a public Internet IP address?

  22. 22

    Chrome Extension to Modify request headers based on Public IP

  23. 23

    Java RMI: running RMI application over the Internet with public IP of router

  24. 24

    Can i use a DNS service to change my public ip?

  25. 25

    How to monitor public ip and the location(country, state, city) simultaneously in terminal

  26. 26

    Make my current Public IP address unchangeable.

  27. 27

    How to get the time of public transport in maps api

  28. 28

    Get public link to file with Google Drive SDK

  29. 29

    Problem with public key on apt-get update

ホットタグ

アーカイブ