netutil

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertMapToQueryString

func ConvertMapToQueryString(param map[string]any) string

ConvertMapToQueryString convert map to sorted url query string. Play: https://go.dev/play/p/jnNt_qoSnRi

func DownloadFile

func DownloadFile(filepath string, url string) error

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

func EncodeUrl

func EncodeUrl(urlStr string) (string, error)

EncodeUrl encode url.

func GetInternalIp

func GetInternalIp() string

GetInternalIp return internal ipv4.

func GetIps

func GetIps() []string

GetIps return all ipv4 of system.

func GetMacAddrs

func GetMacAddrs() []string

GetMacAddrs get mac address.

func GetRequestPublicIp

func GetRequestPublicIp(req *http.Request) string

GetRequestPublicIp return the requested public ip.

func HttpDelete

func HttpDelete(url string, params ...any) (*http.Response, error)

HttpDelete send delete http request.

func HttpGet

func HttpGet(url string, params ...any) (*http.Response, error)

HttpGet send get http request.

func HttpPatch

func HttpPatch(url string, params ...any) (*http.Response, error)

HttpPatch send patch http request.

func HttpPost

func HttpPost(url string, params ...any) (*http.Response, error)

HttpPost send post http request.

func HttpPut

func HttpPut(url string, params ...any) (*http.Response, error)

HttpPut send put http request.

func IsInternalIP

func IsInternalIP(IP net.IP) bool

IsInternalIP verify an ip is intranet or not.

func IsPingConnected

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

func IsTelnetConnected(host string, port string) bool

IsTelnetConnected checks if can telnet specified host or not.

func ParseHttpResponse

func ParseHttpResponse(resp *http.Response, obj any) error

ParseHttpResponse decode http response to specified interface.

func StructToUrlValues

func StructToUrlValues(targetStruct any) (url.Values, error)

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

func UploadFile

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

UploadFile will upload the file to a server.

Types

type File

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

File struct is a combination of file attributes

type HttpClient

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

HttpClient is used for sending http request

func NewHttpClient

func NewHttpClient() *HttpClient

NewHttpClient make a HttpClient instance

func NewHttpClientWithConfig

func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient

NewHttpClientWithConfig make a HttpClient instance with pass config

func (*HttpClient) DecodeResponse

func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error

DecodeResponse decode response into target object. Play: https://go.dev/play/p/jUSgynekH7G

func (*HttpClient) SendRequest

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

SendRequest send http request. Play: https://go.dev/play/p/jUSgynekH7G

type HttpClientConfig

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

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

HttpRequest struct is a composed http request

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

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