README

build PkgGoDev Go Report Card

netselect

A CLI tool as well as library to select the fastest host based on the lowest ICMP latency written in Go (golang), inspired by apenwarr/netselect debian package.

Usage

netselect available as Commnad-Line tool and Go library.

Commnad-Line
netselect [options] <host(s)>
Examples
$ netselect google.com google.in google.us
google.com       55 ms  100% ok         ( 3/ 3)
google.in        56 ms  100% ok         ( 3/ 3)
google.us        59 ms  100% ok         ( 3/ 3)

Read the documentation for more information on the CLI usage.

Go Library

Here is a simple example that finds fastest hosts:

hosts := []*netselect.Host{
    &netselect.Host{Address: "google.in"},
    &netselect.Host{Address: "google.com"},
}

netSelector, err :=netselect.NewNetSelector(hosts)
if err != nil {
    panic(err)
}

fastestHosts := netSelector.Hosts // Fastest hosts in ASC order

Read the API documentation for more information on the library usage.

Installation

$ go get github.com/pgollangi/netselect/cmd/netselect
$ netselect
Manual
  1. Download and install binary from the latest release.
  2. Recommended: add netselect executable to your $PATH.

Building from source

netselect CLI is written in the Go programming language, so to build the CLI yourself, you first need to have Go installed and configured on your machine.

Install Go

To download and install Go, please refer to the Go documentation. Please download Go 1.14.x or above.

Clone this repository
$ git clone https://gitlab.com/pgollangi/netselect.git
$ cd netselect
Build
$ go build cmd/netselect/netselect.go
$ netselect

Notice for linux users

netelect implements ICMP ping using both raw socket and UDP. It needs to be run as a root user.

Alternatley, you can use setcap to allow netselect to bind to raw sockets

setcap cap_net_raw=+ep /bin/netselect

If you do not wish to do all this, you can use flag --privileged=false to send an "unprivileged" ping via UDP. This must be enabled by setting

sudo sysctl -w net.ipv4.ping_group_range="0   2147483647"

See this blog and the Go icmp library for more details.

Contributing

Thanks for considering contributing to this project!

Please read the Contributions and Code of conduct.

Feel free to open an issue or submit a pull request!

License

MIT

Copyright © Prasanna Kumar

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Host

type Host struct {
	// Unique ID
	ID string
	// Address of the Host. If URL provided, Host name will be extracted.
	Address string
}

Host represents a input address to NetSelector

func NewHost

func NewHost(id string, address string) (host *Host, err error)

NewHost creates and returns new Host instance

type HostStats

type HostStats struct {
	Host *Host

	Success bool

	Error error
	// PacketsRecv is the number of packets received.
	PacketsRecv int

	// PacketsSent is the number of packets sent.
	PacketsSent int

	// PacketLoss is the percentage of packets lost.
	PacketLoss float64

	// IPAddr is the address of the host being pinged.
	IPAddr *net.IPAddr

	// Addr is the string address of the host being pinged.
	Addr string

	// Rtts is all of the round-trip times sent via this pinger.
	Rtts []time.Duration

	// MinRtt is the minimum round-trip time sent via this pinger.
	MinRtt time.Duration

	// MaxRtt is the maximum round-trip time sent via this pinger.
	MaxRtt time.Duration

	// AvgRtt is the average round-trip time sent via this pinger.
	AvgRtt time.Duration

	// StdDevRtt is the standard deviation of the round-trip times sent via
	// this pinger.
	StdDevRtt time.Duration
}

HostStats represents the results of one particular host

type NetSelector

type NetSelector struct {
	Hosts      []*Host
	Debug      bool
	Attempts   int
	Timeout    time.Duration
	Privileged bool
	Threads    int
}

NetSelector represents the instance of a NetSelector

func NewNetSelector

func NewNetSelector(hosts []*Host) (*NetSelector, error)

NewNetSelector instantiate new instance of NetSelector

func (*NetSelector) Select

func (s *NetSelector) Select() ([]*HostStats, error)

Select finds the faster hosts among the provided inputs, and sort the resulted host in ASC order

Source Files

Directories

Path Synopsis
cmd