network

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2024 License: MIT Imports: 13 Imported by: 0

README

Network Examples

Download a file from url

package main

import (
  "log"

  "github.com/D3Ext/maldev/src/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/src/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/src/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/src/network"
)

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

Get all information about an interface

package main

import (
  "fmt"
  "log"

  "github.com/D3Ext/maldev/src/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/src/network"
)

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

List active ports

package main

import (
  "fmt"

  "github.com/D3Ext/maldev/src/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/src/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 GetAllInterfaces

func GetAllInterfaces() ([]string, error)

func GetPublicIp

func GetPublicIp() string

func GetStatusCode

func GetStatusCode(url_to_check string) (int, error)

Types

type CustomInterface

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

func (i CustomInterface) Format() string

func (CustomInterface) GetFlags

func (i CustomInterface) GetFlags() net.Flags

func (CustomInterface) GetMac

func (i CustomInterface) GetMac() string

func (CustomInterface) GetMtu

func (i CustomInterface) GetMtu() int

func (CustomInterface) GetName

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

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

func PostHttpReq

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