gosessionclient

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2025 License: MIT Imports: 15 Imported by: 0

README

Go-Session-Client

Go-Session-Client is a simple and lightweight HTTP client for Go, with support for sessions, cookies and proxies.

Features

  • Persistent sessions with cookies
  • Support for SOCKS4(A), SOCKS5, and HTTP proxies
  • Simplified request methods (GET, POST, etc.)
  • A lot of user agent (mobile and PC)

Installation

go get github.com/yan00s/go-session-client

Example Usage:

package main

import (
	"fmt"
	"time"

	session "github.com/yan00s/go-session-client"
)

func main() {
	ses := session.CreateSession(true)

	/// Trying to set Proxy
	proxyStr := "http://username:passw@100.100.100.100:2000"
	timeout := 3 // seconds
	if err := ses.SetProxy(proxyStr, timeout); err != nil {
		fmt.Println(fmt.Errorf("Error in setting up proxy: %w", err))
	}
	///

	fmt.Println()

	/// Trying to make a get request
	fmt.Println("Trying to make request with 1 try on icanhazip.com with a 10 second timeout per request")
	resp := ses.SendReq("https://icanhazip.com", "GET", 5*time.Second)

	if resp.Err != nil {
		fmt.Println("Error in making request:", resp.Err.Error())
		return
	}

	fmt.Println("Response:", resp.String())
	fmt.Println("Status:", resp.Status)
	///
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateHeaders

func GenerateHeaders(pcAgent bool) http.Header

func GenerateMobileAgent

func GenerateMobileAgent() string

func GeneratePcAgent

func GeneratePcAgent() string

Types

type Response

type Response struct {
	Body    []byte
	Headers http.Header
	Cookies []*http.Cookie
	Status  int
	Err     error
}

func (*Response) String

func (res *Response) String() string

type Session

type Session struct {
	Client  *http.Client
	Headers http.Header
}

func CreateSession

func CreateSession(pcAgent bool) Session

func (*Session) SendReq

func (session *Session) SendReq(url, method string, timeout time.Duration, dataStr ...string) *Response

SendReq sends an HTTP request without retry logic. This function allows specifying the HTTP method, timeout duration, and optional request body data.

Parameters: - url: The target URL for the request. - method: The HTTP method to use (e.g., GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, TRACE). - timeout: The maximum duration for the request context (e.g., 30 * time.Second). - dataStr: (Optional) A string to be sent as the request body, if provided.

Returns: A pointer to a Response struct containing the result of the request, including any errors.

func (*Session) SendReqWithRetry added in v1.1.0

func (session *Session) SendReqWithRetry(url, method string, timeout time.Duration, retryCount int, retryDelay time.Duration, dataStr ...string) *Response

SendReqWithRetry sends an HTTP request with retry logic. This function allows specifying the HTTP method, timeout duration, number of retries, delay between retries, and optional request body data.

Parameters: - url: The target URL for the request. - method: The HTTP method to use (e.g., GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, TRACE). - timeout: The maximum duration for the one request context (e.g., 30 * time.Second). - retryCount: The number of retry attempts if the request fails. - retryDelay: The duration to wait between retries (e.g., 2 * time.Second). - dataStr: (Optional) A string to be sent as the request body, if provided.

Returns: A pointer to a Response struct containing the result of the request, including any errors.

func (*Session) SetProxy

func (session *Session) SetProxy(proxy string, timeout int) error

Scheme: protocol://username:passw@ip:port or protocol://ip:port Example: http://kyabx3d:hjehxapxxa@178.0.9.209:4791 And also need enter timeout in seconds

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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