tracer

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2024 License: MIT Imports: 12 Imported by: 0

README

Tracer

Network diagnostic tool in Go inspired by Traceroute.

Makes UDP call to target host increasing the TTL(hops) of IP packet and recording the ICMP response for each hop(router address) until it finally reaches the destination or max TTL is reached.

Usage

1. CLI
go install github.com/nirdosh17/tracer/cmd/gotrace@latest

You can find the binaries HERE.

Commands:

# trace route to a host
gotrace example.com

# with options (max hops, timeout, retries)
gotrace route -hops 5 -t 5 -r 5 example.com

# get your public ip
gotrace myip

Get your public IP:

$ gotrace myip
+----------+----------------------------------------+
| IPv4     | 101.129.138.66                         |
| IPv6     | 2404:7c00:41:50ce:755f:69d3:c890:604b  |
| Location | TELIANET (United States)               |
+----------+----------------------------------------+

# just get ipv4
$ gotrace myip --ipv4
101.129.138.66

Trace route:

$ gotrace route example.com
tracing example.com (93.184.215.14), 64 hops max, max retries: 2
1.   192.168.101.1    private range    3.709ms
2.   62.115.42.118    Arelion Sweden AB (Germany)    172.783ms
3.   62.115.124.56    TELIANET (France)    193.725ms
4.   62.115.112.242    TELIANET (United States)    285.389ms
5.   62.115.123.125    TELIANET (United States)    288.032ms
6.   213.248.83.119    Arelion Sweden AB (United States)    261.788ms
7.   152.195.65.153    Edgecast Inc. (United States)    581.99ms
8.   93.184.215.14    Edgecast Inc. (United Kingdom)    286.464ms
2. Package

Install Package:

go get github.com/nirdosh17/tracer
package main

import (
  "fmt"
  "sync"

  "github.com/nirdosh17/tracer"
)

func main() {
  host := "example.com"

  var wg sync.WaitGroup
  wg.Add(1)
  c := make(chan tracer.Hop)
  go liveReader(&wg, c)

  t := tracer.NewTracer(tracer.NewConfig())
  _, err := t.Run(host, c)
  if err != nil {
    fmt.Println("trace err: ", err)
  }
  wg.Wait()
}

// read live hops from channel
func liveReader(wg *sync.WaitGroup, c chan tracer.Hop) {
  for {
    hop, ok := <-c
    // channel closed
    if !ok {
      wg.Done()
      return
    }
    fmt.Printf("%v.   %v    %v    %v\n", hop.TTL, hop.Addr, hop.Location, hop.ElapsedTime)
  }
}

Documentation

Index

Constants

View Source
const (
	DEFAULT_HOPS            int = 64
	DEFAULT_TIMEOUT_SECONDS int = 5
	DEFAULT_MAX_RETRIES     int = 2
)
View Source
const (
	UDPStartPort = 33434
)

Variables

This section is empty.

Functions

func PublicIP added in v1.0.0

func PublicIP() (string, string, string)

PublicIP returns IPv4, Ipv6 and location of the caller

Types

type Hop

type Hop struct {
	// this should be host or IP address
	Addr     string
	Location string
	// current ttl(hops) of the packet
	TTL int
	// total time taken for this hop
	ElapsedTime time.Duration
}

func (Hop) Print added in v1.0.2

func (h Hop) Print()

type NetworkTrace

type NetworkTrace struct {
	RoundTripTime time.Duration
	NetworkHops   []Hop
}

type Tracer

type Tracer struct {
	Config *TracerConfig
}

func NewTracer

func NewTracer(c *TracerConfig) *Tracer

func (Tracer) Run

func (t Tracer) Run(host string, traces chan Hop) (NetworkTrace, error)

Run sends packets to the specified host in loop recording each network hop until it reaches the destination or max hops is reached. It also collects traces in the given channel.

e.g. host = example.com

type TracerConfig

type TracerConfig struct {
	MaxHops        int
	TimeoutSeconds int
	MaxRetries     int
}

func NewConfig

func NewConfig() *TracerConfig

func (*TracerConfig) Hops

func (t *TracerConfig) Hops() int

func (*TracerConfig) Retries

func (t *TracerConfig) Retries() int

func (*TracerConfig) Timeout

func (t *TracerConfig) Timeout() int

func (*TracerConfig) WithHops

func (t *TracerConfig) WithHops(h int) *TracerConfig

func (*TracerConfig) WithMaxRetries

func (t *TracerConfig) WithMaxRetries(n int) *TracerConfig

func (*TracerConfig) WithTimeout

func (t *TracerConfig) WithTimeout(to int) *TracerConfig

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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