freeport

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2024 License: MIT Imports: 3 Imported by: 10

README

freeport

Extended and revisited version of https://github.com/phayes/freeport - Get a free port from the OS.

...

tcpPort, err := freeport.GetFreePort("127.0.0.1", freeport.TCP)
if err != nil {
    panic(err)
}
fmt.Println("TCP port: ", tcpPort.Port)

fmt.Println("Multiple ports:")

// get multiple ports
// test with TCP
tcpPorts, err := freeport.GetFreePorts("127.0.0.1", freeport.TCP, 5)
if err != nil {
    panic(err)
}

for _, port := range tcpPorts {
    fmt.Println("TCP port: ", port.Port)
}

...

output:

Single port:
UDP port:  57224
TCP port:  51655
Multiple ports:
TCP port:  51656
TCP port:  51657
TCP port:  51658
TCP port:  51659
TCP port:  51660

License

freeport is distributed under MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Port

type Port struct {
	// Address is the address of the port (e.g. 127.0.0.1)
	Address string
	// Port is the port number
	Port int
	// Protocol is the protocol of the port (TCP or UDP)
	Protocol Protocol
	// Raw is the full OS listenable address (directly usable for net.Listen)
	NetListenAddress string
}

Port obtained from the kernel

func GetFreePort

func GetFreePort(address string, protocol Protocol) (*Port, error)

GetFreePort from ip address and protocol

func GetFreePortInRange

func GetFreePortInRange(address string, protocol Protocol, minPort, maxPort int) (*Port, error)

GetFreePortInRange for protocol within a port range

func GetFreePortOnInterface

func GetFreePortOnInterface(interfaceName string, protocol Protocol) (*Port, error)

GetFreePortOnInterface by name and protocol

func GetFreePorts

func GetFreePorts(address string, protocol Protocol, count int) ([]*Port, error)

GetFreePorts collects "count" free ports of specific protocol

func GetFreeTCPPort

func GetFreeTCPPort(address string) (*Port, error)

GetFreeTCPPort gets a free tcp port on address

func GetFreeUDPPort

func GetFreeUDPPort(address string) (*Port, error)

GetFreeUDPPort gets a free udp port on address

func GetPort

func GetPort(protocol Protocol, address string, port int) (*Port, error)

GetPort for protocol is the specific port is free

func MustGetFreeTCPPort

func MustGetFreeTCPPort(address string) *Port

MustGetFreeTCPPort get a free tcp port for address or panic

func MustGetFreeUDPPort

func MustGetFreeUDPPort(address string) *Port

MustGetFreeUDPPort get a free udp port for address or panic

type Protocol

type Protocol uint8

Protocol represents the supported protocol type

const (
	TCP Protocol = iota
	UDP
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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