fast_proxy

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2022 License: MIT Imports: 7 Imported by: 0

README

Fast Proxy Stars Watchers

banner

Why FastProxy?

FastProxy is lightweight, easy-to-use proxy dialing module that makes using proxies with fasthttp easier. FastProxy also includes proxy formatting for Proxy-Authentication and a connection cache system.

Quick Usage

package main

// Import Fasthttp and FastProxy
import (
    FastProxy "github.com/realTristan/FastProxy"
    "github.com/valyala/fasthttp"
)

// Main Function
func main() {
    // Enable proxy formatting (ex: user:pass@ip:port -> ip:port@user:pass)
    var enableProxyFormatting bool = true

    // Create new request object
    var request *fasthttp.Request = fasthttp.AcquireRequest()
    request.SetRequestURI("URL")

    // Create client and use FastProxy Dial
    var client *fasthttp.Client = &fasthttp.Client{}
    client.Dial = FastProxy.Dial("ip:port@user:pass", enableProxyFormatting)
    
    // Create Response Object
    var response *fasthttp.Response = fasthttp.AcquireResponse()
    
    // Release Response when not needed anymore
    defer fasthttp.ReleaseResponse(response)

    // Send the http request
    var err = client.Do(request, response)
    if err != nil {
        // Handle Error
        return
    }

    // Print the request Body, StatusCode
    println(string(response.Body()))
    println(response.StatusCode())
}

Request Proxy

package main

// Import FastProxy and fasthttp
import (
	FastProxy "github.com/realTristan/FastProxy"

	"github.com/valyala/fasthttp"
)

// Main Function
func main() {
    // Create an empty error variable
	var err error

	// Create a fasthttp request object
	var req *fasthttp.Request = fasthttp.AcquireRequest()
	req.SetRequestURI("URL")

	// Create a fasthttp response object
	var resp *fasthttp.Response = fasthttp.AcquireResponse()
	defer fasthttp.ReleaseResponse(resp)

	// Create a FastProxy Request Object
	var FastProxyRequest FastProxy.Request = FastProxy.Request{
		Client:      &fasthttp.Client{},
		Request:     req,
		Response:    resp,
		FormatProxy: true,
		Proxy:       "ip:port@user:pass",
	}

	// Send the request
	resp, err = FastProxy.SendRequest(FastProxyRequest)

	// Handle Error
	if err != nil {
		return
	}

	// Print Response body and Response status code
	println(resp.Body())
	println(resp.StatusCode())
}

Proxy Formatting

// Format proxies to ip:port@user:pass
func FormatProxyList() {

    // Create a new proxy slice
    var proxies []string = []string{
        "ip:port@user:pass",
        "user:pass@ip:port",
    }

    // Iterate through the proxy slice
    for i := 0; i < len(proxies); i++ {
        var proxy string = proxies[i]

        // Format the proxy
        proxies[i] = FastProxy.FormatProxy(&proxy)
    }

    // Print the formatted proxies
    println(proxies)
    
    // Output -> [
    // 	  "ip:port@user:pass", "ip:port@user:pass"
    // ]
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ConnectionCache     map[string]net.Conn = map[string]net.Conn{}
	FormattedProxyCache map[string]*string  = map[string]*string{}
)

Cache Map Variables

  • ConnectionCache holds the proxy connections
  • FormattedProxyCache holds the formatted proxies

Functions

func Base64Encode

func Base64Encode(s string) string

Base64 encode a string

func CacheConnection

func CacheConnection(c net.Conn, p string)

The CacheConnection() function will add the proxy to the

ConnectionCache with it's pre-made connection

func Dial

func Dial(proxy string, formatProxy bool) fasthttp.DialFunc

Function to use a proxy dia [user:pass@proxy:port] Usage -> Dial(proxy, (whether to format proxy if using auth))

func EstablishConnection

func EstablishConnection(addr string, _proxy *string, formatProxy bool) (net.Conn, error)

The EstablishConnection() function is the dial function that returns

a fasthttp.Dial object

This function will create a connection url then send a connection request

func FormatProxy

func FormatProxy(proxy *string) *string

The FormatProxy() function will check whether the provided proxy

is already in the formatted proxy cache

If the provided proxy is not already in the cache, it will reformat it

and add it to the cache

func FormatProxyAuth

func FormatProxyAuth(proxy *string) *string

The FormatProxyAuth() function will split the proxy+auth by the @

then it will determine which side is the ip:port and which side is
the user:password

After it has reformatted the proxy to ip:port@user:pass, it will add the

formatted proxy to the cache for any later use

func GenerateConnectUrl

func GenerateConnectUrl(addr string, proxy *string, formatProxy bool) (string, *string)

The GenerateConnectUrl() function will create the Dial Url for the proxy If the proxy contains an @user:pass, it will split the proxy and format it

if formatProxy is set to true

After the proxy is or is not formatted, it will base64 encode it for the

Proxy-Authentication

func GetConnection

func GetConnection(addr string, proxy *string, formatProxy bool) (net.Conn, error)

The GetConnection() function will iterate through the ConnectionCache If the proxy exists in the ConnectionCache, it will return it, else

it will establish a new connection

func SendRequest

func SendRequest(FastProxyRequest Request) (*fasthttp.Response, error)

The SendRequest() function takes in the Request struct as it's sole parameter It will then make an ordinary fasthttp client request and return the response

and request error

func SetResponse

func SetResponse(sB bool) *fasthttp.Response

Function to return a fasthttp response object

  • sB bool -> SkipBody

func ValidProxy

func ValidProxy(proxy string) bool

The ValidProxy() function will iterate through the provided proxy

and check how many periods are in the string

If there are 3 periods and no letters, then the provided proxy is valid

Types

type Request

type Request struct {
	Client      *fasthttp.Client
	Request     *fasthttp.Request
	Response    *fasthttp.Response
	FormatProxy bool
	Proxy       string
}

Request Structure The Request Structure holds five keys

  • Client: *fasthttp.Client -> The fasthttp client to use
  • Request: *fasthttp.Request -> The fasthttp request object to act off of
  • Response: *fasthttp.Response -> The fasthttp response object to return
  • FormatProxy: bool -> Whether the user wants the provided proxy formatted
  • Proxy: string -> The proxy to connect to

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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