The client package provides an HTTP client with advanced features for browser emulation, including TLS fingerprinting, HTTP/2 fingerprint control, custom cookie handling, and proxy support.
Installation
import "github.com/aarock1234/go-template/pkg/client"
Quick Start
Create a client and make a request:
package main
import (
"context"
"fmt"
"net/http"
"github.com/aarock1234/go-template/pkg/client"
)
func main() {
// Create a new client with default settings
c, err := client.New()
if err != nil {
panic(err)
}
// Make a request
req, _ := http.NewRequest("GET", "https://example.com", nil)
resp, err := c.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("Status:", resp.StatusCode)
}
Client Creation
New
Creates a new Client with the given options.
func New(opts ...Option) (*Client, error)
Optional configuration functions to customize the client
Default Settings:
- Browser: Chrome
- Platform: Windows
- Keep-alives: Disabled
- Session tickets: Disabled
- TLS verification: Skipped
Configuration Options
Customize the client with these options:
WithBrowser
Sets the browser identity for TLS and HTTP/2 fingerprinting.
func WithBrowser(b Browser) Option
Browser identity. Available options:
BrowserChrome - Google Chrome
BrowserEdge - Microsoft Edge
BrowserBrave - Brave browser
BrowserSafari - Apple Safari
BrowserFirefox - Mozilla Firefox
Example:
c, err := client.New(
client.WithBrowser(client.BrowserFirefox),
)
WithBrowserVersion
Sets the browser version for TLS fingerprinting.
func WithBrowserVersion(v string) Option
Browser version string (e.g., “131.0.0.0”)
Sets the OS platform for TLS and HTTP/2 fingerprinting.
func WithPlatform(p Platform) Option
Operating system platform. Available options:
PlatformWindows - Windows
PlatformMac - macOS
PlatformLinux - Linux
PlatformIOS - iOS
PlatformIPadOS - iPadOS
WithProxy
Configures proxy settings for the client.
func WithProxy(p *Proxy) Option
Example:
proxy, err := client.ParseProxy("127.0.0.1:8080:user:pass", client.ProxySchemeHTTP)
if err != nil {
panic(err)
}
c, err := client.New(
client.WithProxy(proxy),
)
WithInsecureSkipVerify
Controls whether TLS certificate verification is skipped.
func WithInsecureSkipVerify(d bool) Option
Set to false to enable TLS certificate verification
WithDisableKeepAlives
Controls whether HTTP keep-alives are disabled.
func WithDisableKeepAlives(d bool) Option
Set to false to enable HTTP keep-alives
Overrides browser profile default headers.
func WithDefaultHeaderOverrides(h http.Header) Option
Headers to override in the browser profile
Making Requests
Sends an HTTP request and returns an HTTP response.
func (c *Client) Do(req *http.Request) (*http.Response, error)
Redirects are followed automatically (up to 10), and cookies from intermediate responses are captured and applied to subsequent requests.
Example:
req, _ := http.NewRequestWithContext(ctx, "POST", "https://api.example.com/data", body)
req.Header.Set("Content-Type", "application/json")
resp, err := c.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
Cookie Management
SetCookies
Stores cookies for a given URL.
func (c *Client) SetCookies(u *url.URL, cookies []*http.Cookie)
The URL to associate cookies with
SetCookieString
Sets cookies with a specific domain for subdomain sharing.
func (c *Client) SetCookieString(domain string, cookieString string, exclude ...string) error
Domain with leading dot (e.g., “.example.com”)
Cookie string in “name=value; name2=value2” format
Optional cookie names to filter out
Example:
err := c.SetCookieString(".example.com", "session_id=abc123; user_id=456", "old_cookie")
GetCookies
Returns cookies for a given domain.
func (c *Client) GetCookies(domain string) []*http.Cookie
Domain to retrieve cookies for
GetCookieString
Returns cookies as a semicolon-separated string.
func (c *Client) GetCookieString(domain string) string
Domain to retrieve cookies for
FindCookie
Finds a specific cookie by name.
func (c *Client) FindCookie(name string, domain string) (*http.Cookie, bool)
Cookie name to search for
Returns the cookie and true if found, or nil and false otherwise.
ClearCookies
Clears all cookies by creating a new jar.
func (c *Client) ClearCookies() error
Proxy Support
Proxy Types
The client supports multiple proxy schemes:
const (
ProxySchemeHTTP ProxyScheme = "http" // HTTP CONNECT proxy
ProxySchemeHTTPS ProxyScheme = "https" // HTTPS CONNECT proxy
ProxySchemeSOCKS5 ProxyScheme = "socks5" // SOCKS5 with local DNS
ProxySchemeSOCKS5H ProxyScheme = "socks5h" // SOCKS5 with remote DNS
)
ParseProxy
Parses a proxy string into a Proxy struct.
func ParseProxy(proxy string, scheme ProxyScheme) (*Proxy, error)
Proxy in “host:port” or “host:port:user:pass” format
Proxy scheme (http, https, socks5, socks5h)
Example:
// Without authentication
proxy, err := client.ParseProxy("127.0.0.1:8080", client.ProxySchemeHTTP)
// With authentication
proxy, err := client.ParseProxy("proxy.example.com:3128:user:pass", client.ProxySchemeHTTP)
ImportProxies
Reads and parses proxy configurations from a file.
func ImportProxies(filename string, scheme ProxyScheme) ([]*Proxy, error)
Path to file containing proxies (one per line)
Proxy scheme to use for all proxies
Example:
proxies, err := client.ImportProxies("proxies.txt", client.ProxySchemeHTTP)
if err != nil {
panic(err)
}
for _, proxy := range proxies {
c, _ := client.New(client.WithProxy(proxy))
// Use client...
}
Utility Methods
UserAgent
Returns the default User-Agent header.
func (c *Client) UserAgent() string
ClientHint
Returns the default sec-ch-ua header.
func (c *Client) ClientHint() string
Proxy
Returns the proxy configuration.
func (c *Client) Proxy() *Proxy
Advanced Cookie Parsing
ParseCookies
Parses a raw cookie string into an http.Cookie slice.
func ParseCookies(cookieStr string) []*http.Cookie
Cookie string in “name=value; name2=value2” format or JSON array format
ParseSetCookie
Parses a single Set-Cookie header with relaxed validation.
func ParseSetCookie(line string) (*http.Cookie, error)
This function uses relaxed validation that allows double-quote characters in cookie values.
Complete Example
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/aarock1234/go-template/pkg/client"
)
func main() {
// Parse proxy
proxy, err := client.ParseProxy("127.0.0.1:8080:user:pass", client.ProxySchemeHTTP)
if err != nil {
panic(err)
}
// Create client with custom configuration
c, err := client.New(
client.WithBrowser(client.BrowserChrome),
client.WithPlatform(client.PlatformMac),
client.WithProxy(proxy),
client.WithInsecureSkipVerify(false), // Enable TLS verification
)
if err != nil {
panic(err)
}
// Set cookies
c.SetCookieString(".example.com", "session=abc123; user_id=456", nil)
// Make authenticated request
payload := map[string]string{"key": "value"}
body, _ := json.Marshal(payload)
req, _ := http.NewRequestWithContext(context.Background(), "POST", "https://api.example.com/data", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
resp, err := c.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("Status:", resp.StatusCode)
fmt.Println("User-Agent:", c.UserAgent())
// Retrieve cookies after request
cookies := c.GetCookies(".example.com")
for _, cookie := range cookies {
fmt.Printf("Cookie: %s=%s\n", cookie.Name, cookie.Value)
}
}
Error Handling
The client defines specific errors:
var ErrTooManyRedirects = errors.New("too many redirects")
This error is returned when a request exceeds 10 redirects.