network

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2023 License: MIT Imports: 14 Imported by: 0

README

Network Examples

Download a file from url

package main

import (
  "log"

  "github.com/D3Ext/maldev/network"
)

func main(){
  err := network.DownloadFile("https://example.com/file.txt") // File saved as filename, in this case file.txt
  if err != nil {
    log.Fatal(err)
  }
}

Get status code of url

package main

import (
  "fmt"
  "log"

  "github.com/D3Ext/maldev/network"
)

func main(){
  code, err := network.GetStatusCode("https://google.com")
  if err != nil {
    log.Fatal(err)
  }

  fmt.Println(code) // Output: 200
}

Send http post request with data

package main

import (
  "fmt"
  "log"
  "net/url"

  "github.com/D3Ext/maldev/network"
)

func main(){
  data := url.Values{
    "example": {"value1"},
    "test": {"value2"},
  }

  timeout := 2000 // Request timeout in milliseconds, for default use network.DefaultTimeout
  req, err := network.PostHttpReq("http://example.com/index.php", data, timeout)
  if err != nil {
    log.Fatal(err)
  }

  fmt.Println(req.StatusCode)
}

List all network interfaces

package main

import (
  "fmt"
  "log"

  "github.com/D3Ext/maldev/network"
)

func main(){
  interfaces, err := network.ListAllInterfaces() // func ListAllInterfaces() ([]string, error)
  if err != nil {
    log.Fatal(err)
  }
  
  fmt.Println(interfaces) // Output: [eth0 lo]
}

Get all information about an interface

package main

import (
  "fmt"
  "log"

  "github.com/D3Ext/maldev/network"
)

func main(){
  info, err := network.GetInterfaceInfo("wlan0") // func GetInterfaceInfo(interface_name string) (net.Interface, error)
  if err != nil {
    log.Fatal(err)
  }

  fmt.Println(info)
  format := network.FormatInfo(info)
  fmt.Println(format)
}

Check internet connection

package main

import (
  "fmt"

  "github.com/D3Ext/maldev/network"
)

func main(){
  check := network.CheckInternet()
  fmt.Println(check) // true or false
}

List active ports

package main

import (
  "fmt"

  "github.com/D3Ext/maldev/network"
)

func main(){
  raw_netstat := network.Netstat() // func Netstat() ([]*PortsInfo, error)
  fmt.Println(raw_netstat)

  formated_netstat := network.FormatedNetstat() // func FormatedNetstat() (string, error)
  fmt.Println(formated_netstat) // Represented in columns
}

Get public ip

package main

import (
  "fmt"

  "github.com/D3Ext/maldev/network"
)

func main(){
  ip := network.GetPublicIp()
  fmt.Println(ip) // Example: 163.172.110.176
}

Documentation

Index

Constants

View Source
const (
	DefaultTimeout = 5000
)

Variables

This section is empty.

Functions

func CheckInternet

func CheckInternet() bool

func DownloadFile

func DownloadFile(file_url string) error

func FormatedNetstat

func FormatedNetstat() (string, error)

func GetPublicIp

func GetPublicIp() string

func GetStatusCode

func GetStatusCode(url_to_check string) (int, error)

func ListAllInterfaces

func ListAllInterfaces() ([]string, error)

Types

type CustomInterface added in v0.1.2

type CustomInterface struct {
	Index        int
	MTU          int
	Name         string
	HardwareAddr net.HardwareAddr
	Flags        net.Flags
	Addrs        []string
}

func GetInterfaceInfo

func GetInterfaceInfo(interface_name string) (CustomInterface, error)

func (CustomInterface) Format added in v0.1.2

func (i CustomInterface) Format() string

func (CustomInterface) GetFlags added in v0.1.2

func (i CustomInterface) GetFlags() net.Flags

func (CustomInterface) GetMac added in v0.1.2

func (i CustomInterface) GetMac() string

func (CustomInterface) GetMtu added in v0.1.2

func (i CustomInterface) GetMtu() int

func (CustomInterface) GetName added in v0.1.2

func (i CustomInterface) GetName() string

type IP

type IP struct {
	Query string
}

type PortsInfo

type PortsInfo struct {
	Name  string
	Port  int
	Pid   int
	State netstat.SkState
}

func Netstat

func Netstat() ([]*PortsInfo, error)

type RequestInfo added in v0.1.2

type RequestInfo struct {
	Response      string
	StatusCode    int
	ContentLength int
	Protocol      string
}

func PostHttpReq added in v0.1.2

func PostHttpReq(url string, post_data url.Values, timeout int) (RequestInfo, error)

Jump to

Keyboard shortcuts

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