net

package module
v1.5.6 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: MIT Imports: 10 Imported by: 0

README

go-net

Introduction

This library contains basic validators and helpers for handling IP addresses, IP networks, IP ranges (from - to IP adress), domain names, FQDN (fully qualified domain names), and URL (unified resource locators).

All methods that work with IP addresses are IPv4 and IPv6 compliant.

Please see the unit tests (xx_test.go) for examples on how to use this library.

Hints

We have received feedback from software developers who were confused about how this library handles specific cases. Examples include but are not limited to:

  • URLs:
    • We consider URLs that do not have a scheme as valid. This is a clear violation of RFC #3986 that says The scheme and path components are required, (...). We are doing this because of several of our own use cases where we had to process large lists of URLs that did not contain a scheme in each end every case. We neither wanted to add a scheme like http:// by default nor simply reject entries. If you, however, need a URL that fully complies to RFC #3986 (e.g. because you want to use it with other libraries), please call NormaliseURLSchema().
  • Domains
    • To get an idea, what a valid domain name looks like, we use the list of TLDs (top-level-domains) from PublicSuffixes.org.
  • FQDNs
    • www.site.tld is not a URL but a FQDN (fully qualified domain name), because it neither has a path component nor a schema (http://, https://, etc.).
    • www.site.tld/about is a valid URL.
  • IP addresses and networks
    • 10.0.0.1/32 although presented as a network address is in fact a single IPv4 address (10.0.0.1).
    • 10.0.0.1/24 is not an IPv4 address, but a URL! This is because we do not need a schema (see URLs above), but when the netmask is /24 the last byte must be 0.
    • 10.0.0.0/24 is a valid network address.

If you find any other behaviour that seems odd, please double check with the code of the unit tests. If something still does not make sense, let us know by starting a discussion or by opening an issue here.

Feedback

We would love to hear from you! Please contact us at [email protected] for feedback and general requests. Kindly raise an issue in Codeberg if you find a problem in the code.

Want to know what we do when we're not developing open-source software? Find out at www.threatint.eu ;-)

License

Release under the MIT License. (see LICENSE)

Documentation

Overview

Package net contains helper function for handling urls

Index

Constants

This section is empty.

Variables

View Source
var PublicSuffix = []string{}/* 6919 elements not displayed */

Functions

func DomainFromFqdn

func DomainFromFqdn(fqdn string) string

DomainFromFqdn returns domain name or empty string

func HostFromURL

func HostFromURL(u string) (string, error)

HostFromURL extraxts hostname from given URL

func IntToIP

func IntToIP(i string) net.IP

IntToIP (int) return net.IP from the integer representation of an ip address (use e.g. for IP2Location databases )

func IsDomain

func IsDomain(domain string) bool

IsDomain (domainname string) returns true if domainname is a valid domain, otherwise false

func IsFQDN

func IsFQDN(fqdn string) bool

IsFQDN (fqdn) returns true if fqdn is a FQDN (Fully Qualified Domain Name) hostname + domainname + tld, otherwise false

func IsIPAddr

func IsIPAddr(ip string) bool

IsIPAddr return true if string ip contains a valid representation of an IPv4 or IPv6 address

func IsIPRange

func IsIPRange(r string) bool

IsIPRange (range) return true if string r contains a valid representation of an ip network (e.g. 192.168.10.1-192.168.10.199)

func IsIPv4

func IsIPv4(ip net.IP) bool

IsIPv4 (ipaddr) return true if string ip contains a valid representation of an IPv4 address

func IsIPv6

func IsIPv6(ip net.IP) bool

IsIPv6 (ipaddr) return true if string ip contains a valid representation of an IPv6 address

func IsNetwork

func IsNetwork(network string) bool

IsNetwork (network) return true if string network contains a valid representation of an ip network

func IsURL

func IsURL(u string) bool

IsURL returns true if string represents a valid URL

func NormaliseIPAddr

func NormaliseIPAddr(ip string) string

NormaliseIPAddr return ip adresse without /32 (IPv4 or /128 (IPv6)

func NormaliseURL

func NormaliseURL(u string) (string, error)

NormaliseURL returns a normalised url (e.g. without default ports like :80 for HTTP or :443 for HTTPS, duplicate slashes, etc.)

func NormaliseURLSchema

func NormaliseURLSchema(u string) (string, error)

NormaliseURLSchema returns normalised URL string that includes a schema.

func ReverseIPAddr

func ReverseIPAddr(ip string) (string, error)

ReverseIPAddr (ipaddr) reverses string ip (use e.g. for DNS blacklists)

func ToPunycode

func ToPunycode(s string) (string, error)

ToPunycode returns string as Punycode

func ToUnicode

func ToUnicode(s string) (string, error)

ToUnicode returns string as Unicode

func URLToPunycode

func URLToPunycode(u string) (string, error)

URLToPunycode returns URL string in punycode

func URLToUnicode

func URLToUnicode(u string) (string, error)

URLToUnicode returns normalised URL string.

Types

This section is empty.

Jump to

Keyboard shortcuts

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