netutil

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 21 Imported by: 2

Documentation

Overview

Package netutil implements some basic functions to send http request and get ip info. Note: HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `url` is required. HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `params` is variable, the order is: params[0] is header which type should be http.Header or map[string]string, params[1] is query string param which type should be url.Values or map[string]string, when content-type header is multipart/form-data or application/x-www-form-urlencoded params[2] is post body which type should be []byte. params[3] is http client which type should be http.Client.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertMapToQueryString

func ConvertMapToQueryString(param map[string]interface{}) string

ConvertMapToQueryString convert map to sorted url query string

func DownloadFile added in v1.3.8

func DownloadFile(filepath string, url string) error

DownloadFile will download the file exist in url to a local file.

func EncodeUrl added in v1.3.1

func EncodeUrl(urlStr string) (string, error)

EncodeUrl encode url

func GetInternalIp

func GetInternalIp() string

GetInternalIp return internal ipv4

func GetIps added in v1.2.4

func GetIps() []string

GetIps return all ipv4 of system

func GetMacAddrs added in v1.2.4

func GetMacAddrs() []string

GetMacAddrs get mac address

func GetRequestPublicIp added in v1.3.1

func GetRequestPublicIp(req *http.Request) string

GetRequestPublicIp return the requested public ip

func HttpDelete

func HttpDelete(url string, params ...interface{}) (*http.Response, error)

HttpDelete send delete http request

func HttpGet

func HttpGet(url string, params ...interface{}) (*http.Response, error)

HttpGet send get http request

func HttpPatch

func HttpPatch(url string, params ...interface{}) (*http.Response, error)

HttpPatch send patch http request

func HttpPost

func HttpPost(url string, params ...interface{}) (*http.Response, error)

HttpPost send post http request

func HttpPut

func HttpPut(url string, params ...interface{}) (*http.Response, error)

HttpPut send put http request

func IsInternalIP added in v1.3.1

func IsInternalIP(IP net.IP) bool

IsInternalIP verify an ip is intranet or not

func IsPingConnected added in v1.3.8

func IsPingConnected(host string) bool

IsPingConnected checks if can ping specified host or not.

func IsPublicIP

func IsPublicIP(IP net.IP) bool

IsPublicIP verify a ip is public or not

func IsTelnetConnected added in v1.3.8

func IsTelnetConnected(host string, port string) bool

IsTelnetConnected checks if can telnet specified host or not.

func ParseHttpResponse added in v1.0.8

func ParseHttpResponse(resp *http.Response, obj interface{}) error

ParseHttpResponse decode http response to specified interface

func StructToUrlValues added in v1.3.2

func StructToUrlValues(targetStruct interface{}) url.Values

StructToUrlValues convert struct to url valuse, only convert the field which is exported and has `json` tag

func UploadFile added in v1.3.8

func UploadFile(filepath string, server string) (bool, error)

DownloadFile will upload the file to a server.

Types

type File added in v1.4.0

type File struct {
	Content   []byte
	Path      string
	FieldName string
	FileName  string
}

File struct is a combination of file attributes

type HttpClient added in v1.3.2

type HttpClient struct {
	*http.Client
	TLS     *tls.Config
	Request *http.Request
	Config  HttpClientConfig
}

HttpClient is used for sending http request

func NewHttpClient added in v1.3.2

func NewHttpClient() *HttpClient

NewHttpClient make a HttpClient instance

func NewHttpClientWithConfig added in v1.3.2

func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient

NewHttpClientWithConfig make a HttpClient instance with pass config

func (*HttpClient) DecodeResponse added in v1.3.2

func (client *HttpClient) DecodeResponse(resp *http.Response, target interface{}) error

DecodeResponse decode response into target object

func (*HttpClient) SendRequest added in v1.3.2

func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)

SendRequest send http request

type HttpClientConfig added in v1.3.2

type HttpClientConfig struct {
	SSLEnabled       bool
	TLSConfig        *tls.Config
	Compressed       bool
	HandshakeTimeout time.Duration
	ResponseTimeout  time.Duration
	Verbose          bool
}

HttpClientConfig contains some configurations for http client

type HttpRequest added in v1.3.2

type HttpRequest struct {
	RawURL      string
	Method      string
	Headers     http.Header
	QueryParams url.Values
	FormData    url.Values
	File        *File
	Body        []byte
}

type PublicIpInfo

type PublicIpInfo struct {
	Status      string  `json:"status"`
	Country     string  `json:"country"`
	CountryCode string  `json:"countryCode"`
	Region      string  `json:"region"`
	RegionName  string  `json:"regionName"`
	City        string  `json:"city"`
	Lat         float64 `json:"lat"`
	Lon         float64 `json:"lon"`
	Isp         string  `json:"isp"`
	Org         string  `json:"org"`
	As          string  `json:"as"`
	Ip          string  `json:"query"`
}

PublicIpInfo public ip info: country, region, isp, city, lat, lon, ip

func GetPublicIpInfo

func GetPublicIpInfo() (*PublicIpInfo, error)

GetPublicIpInfo return public ip information return the PublicIpInfo struct

type SetFileFunc added in v1.4.0

type SetFileFunc func(req *http.Request, values url.Values) error

Jump to

Keyboard shortcuts

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