network

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLocalIpAddress

func GetLocalIpAddress() (ipaddr []string)

GetLocalIpAddress returns all non-loopback IPv4 addresses assigned to the local machine's network interfaces.

Example

ExampleGetLocalIpAddress shows how to retrieve all non-loopback IPv4 addresses assigned to the local machine — useful for logging the pod IP at startup.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/network"
)

func main() {
	addrs := network.GetLocalIpAddress()
	for _, ip := range addrs {
		fmt.Println(ip)
	}
}

func GetRemoteIp

func GetRemoteIp(req *http.Request) string

GetRemoteIp returns the remote client IP address from the HTTP request. It inspects the X-Forwarded-For header first (required in AWS Lambda and other reverse-proxy environments), then X-Real-IP, and finally falls back to the TCP remote address. The loopback address "::1" is normalised to "127.0.0.1". When X-Forwarded-For contains multiple addresses, the first one is returned.

Example

ExampleGetRemoteIp shows how to extract the real client IP in a reverse-proxy environment where the original IP is forwarded via X-Forwarded-For.

package main

import (
	"fmt"
	"net/http"

	"github.com/phcp-tech/common-library-golang/network"
)

func main() {
	req, _ := http.NewRequest(http.MethodGet, "/", nil)
	req.Header.Set("X-Forwarded-For", "203.0.113.5")
	req.RemoteAddr = "10.0.0.1:1234"

	fmt.Println(network.GetRemoteIp(req))
}
Output:
203.0.113.5
Example (MultipleProxies)

ExampleGetRemoteIp_multipleProxies shows that when X-Forwarded-For contains multiple addresses (set by a chain of proxies), the first (original client) IP is returned.

package main

import (
	"fmt"
	"net/http"

	"github.com/phcp-tech/common-library-golang/network"
)

func main() {
	req, _ := http.NewRequest(http.MethodGet, "/", nil)
	req.Header.Set("X-Forwarded-For", "203.0.113.5, 10.0.0.2, 10.0.0.3")
	req.RemoteAddr = "10.0.0.1:1234"

	fmt.Println(network.GetRemoteIp(req))
}
Output:
203.0.113.5
Example (XRealIP)

ExampleGetRemoteIp_xRealIP shows the X-Real-IP fallback used by Nginx when X-Forwarded-For is not set.

package main

import (
	"fmt"
	"net/http"

	"github.com/phcp-tech/common-library-golang/network"
)

func main() {
	req, _ := http.NewRequest(http.MethodGet, "/", nil)
	req.Header.Set("X-Real-IP", "198.51.100.7")
	req.RemoteAddr = "10.0.0.1:1234"

	fmt.Println(network.GetRemoteIp(req))
}
Output:
198.51.100.7

func Int2IpWithBigEndian added in v0.1.11

func Int2IpWithBigEndian(ipInt uint32) string

Int2IpWithBigEndian converts a uint32 IP address to string format in big-endian format

Example

ExampleInt2IpWithBigEndian converts a big-endian uint32 (network byte order) to a dotted-decimal IPv4 string.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/network"
)

func main() {
	fmt.Println(network.Int2IpWithBigEndian(0x01020304)) // 0x01020304 = 1.2.3.4
	fmt.Println(network.Int2IpWithBigEndian(0x7F000001)) // 0x7F000001 = 127.0.0.1
}
Output:
1.2.3.4
127.0.0.1

func Int2IpWithLittleEndian added in v0.1.11

func Int2IpWithLittleEndian(ipInt uint32) string

Int2IpWithLittleEndian converts a uint32 IP address to string format in little-endian format

Example

ExampleInt2IpWithLittleEndian converts a little-endian uint32 to a dotted-decimal IPv4 string. This format is used by MT4/MT5 trading platforms.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/network"
)

func main() {
	fmt.Println(network.Int2IpWithLittleEndian(0x04030201)) // 0x04030201 = 1.2.3.4
	fmt.Println(network.Int2IpWithLittleEndian(0))          // 0 → empty
}
Output:
1.2.3.4

func Ip2IntWithBigEndian added in v0.1.11

func Ip2IntWithBigEndian(ipStr string) uint32

Ip2IntWithBigEndian converts an IP address string to uint32 in big-endian format

Example

ExampleIp2IntWithBigEndian converts a dotted-decimal IPv4 string to a big-endian uint32 (network byte order). Returns 0 for empty or invalid input.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/network"
)

func main() {
	fmt.Printf("0x%X\n", network.Ip2IntWithBigEndian("1.2.3.4"))
	fmt.Printf("0x%X\n", network.Ip2IntWithBigEndian("127.0.0.1"))
}
Output:
0x1020304
0x7F000001

func Ip2IntWithLittleEndian added in v0.1.11

func Ip2IntWithLittleEndian(ipStr string) uint32

Ip2IntWithLittleEndian converts an IP address string to uint32 in little-endian format

Example

ExampleIp2IntWithLittleEndian converts a dotted-decimal IPv4 string to a little-endian uint32. Returns 0 for empty or invalid input.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/network"
)

func main() {
	fmt.Printf("0x%X\n", network.Ip2IntWithLittleEndian("1.2.3.4"))
	fmt.Println(network.Ip2IntWithLittleEndian(""))
}
Output:
0x4030201
0

func IsPrivateIP added in v0.1.37

func IsPrivateIP(ipStr string) bool

IsPrivateIP reports whether ipStr is a private IP address, i.e. within RFC 1918 (IPv4: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) or RFC 4193 (IPv6 unique local addresses: fc00::/7). Link-local addresses (e.g. 169.254.0.0/16) are NOT private and return false. An unparsable ipStr also returns false.

func IsValidAddr added in v0.1.11

func IsValidAddr(ipStr string) bool

IsValidAddr checks if the given string is a valid IP address with port or resolvable domain name with port

Example

ExampleIsValidAddr checks whether a string is a valid IP address or resolvable hostname, with or without a port.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/network"
)

func main() {
	fmt.Println(network.IsValidAddr("192.168.1.1:8080")) // IP with port
	fmt.Println(network.IsValidAddr("192.168.1.1"))      // IP without port
	fmt.Println(network.IsValidAddr("999.999.999.999"))  // invalid
}
Output:
true
true
false

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL