Categorygithub.com/stack1ng/tls-client
modulepackage
0.0.0-20240722210210-9e76226ebb64
Repository: https://github.com/stack1ng/tls-client.git
Documentation: pkg.go.dev

# README

TLS-Client

Preface

This TLS Client is built upon https://github.com/Carcraftz/fhttp and https://github.com/Carcraftz/utls (https://github.com/refraction-networking/utls). Big thanks to all contributors so far. Sadly it seems that the original repositories from Carcraftz are not maintained anymore.

What is TLS Fingerprinting?

Some people think it is enough to change the user-agent header of a request to let the server think that the client requesting a resource is a specific browser. Nowadays this is not enough, because the server might use a technique to detect the client browser which is called TLS Fingerprinting.

Even though this article is about TLS Fingerprinting in NodeJS it well describes the technique in general. https://httptoolkit.tech/blog/tls-fingerprinting-node-js/#how-does-tls-fingerprinting-work

Why is this library needed?

With this library you are able to create a http client implementing an interface which is similar to golangs net/http client interface. This TLS Client allows you to specify the Client (Browser and Version) you want to use, when requesting a server.

The Interface of the HTTP Client looks like the following and extends the base net/http Client Interface by some useful functions. Most likely you will use the Do() function like you did before with net/http Client.

type HttpClient interface {
    GetCookies(u *url.URL) []*http.Cookie
    SetCookies(u *url.URL, cookies []*http.Cookie)
    SetCookieJar(jar http.CookieJar)
    GetCookieJar() http.CookieJar
    SetProxy(proxyUrl string) error
    GetProxy() string
    SetFollowRedirect(followRedirect bool)
    GetFollowRedirect() bool
    CloseIdleConnections()
    Do(req *http.Request) (*http.Response, error)
    Get(url string) (resp *http.Response, err error)
    Head(url string) (resp *http.Response, err error)
    Post(url, contentType string, body io.Reader) (resp *http.Response, err error)
}

Quick Usage Example

package main

import (
	"fmt"
	"io"
	"log"

	http "github.com/bogdanfinn/fhttp"
	tls_client "github.com/stack1ng/tls-client"
	"github.com/stack1ng/tls-client/profiles"
)

func main() {
    jar := tls_client.NewCookieJar()
	options := []tls_client.HttpClientOption{
		tls_client.WithTimeoutSeconds(30),
		tls_client.WithClientProfile(profiles.Chrome_120),
		tls_client.WithNotFollowRedirects(),
		tls_client.WithCookieJar(jar), // create cookieJar instance and pass it as argument
	}

	client, err := tls_client.NewHttpClient(tls_client.NewNoopLogger(), options...)
	if err != nil {
		log.Println(err)
		return
	}

	req, err := http.NewRequest(http.MethodGet, "https://tls.peet.ws/api/all", nil)
	if err != nil {
		log.Println(err)
		return
	}

	req.Header = http.Header{
		"accept":                    {"*/*"},
		"accept-language":           {"de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7"},
		"user-agent":                {"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"},
		http.HeaderOrderKey: {
			"accept",
			"accept-language",
			"user-agent",
		},
	}

	resp, err := client.Do(req)
	if err != nil {
		log.Println(err)
		return
	}

	defer resp.Body.Close()

	log.Println(fmt.Sprintf("status code: %d", resp.StatusCode))

	readBytes, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Println(err)
		return
	}

	log.Println(string(readBytes))
}

Detailed Documentation

https://bogdanfinn.gitbook.io/open-source-oasis/

Questions?

Join my discord support server for free: https://discord.gg/7Ej9eJvHqk No Support in DMs!

Appreciate my work?

"Buy Me A Coffee"

# Packages

No description provided by the author
tls_client_cffi_src provides and manages a CFFI (C Foreign Function Interface) which allows code in other languages to interact with the module.
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewHttpClient constructs a new HTTP client with the given logger and client options.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
WithCatchPanics configures a client to catch all go panics happening during a request and not print the stacktrace.
WithCertificatePinning enables SSL Pinning for the client and will throw an error if the SSL Pin is not matched.
WithCharlesProxy configures the HTTP client to use a local running charles as proxy.
WithClientProfile configures a TLS client to use the specified client profile.
/Use useragent in the CONNECT request.
WithCookieJar configures a HTTP client to use the specified cookie jar.
WithCustomRedirectFunc configures an HTTP client to use a custom redirect func.
WithDebug configures a client to log debugging information.
No description provided by the author
WithDefaultHeaders configures a TLS client to use a set of default headers if none are specified on the request.
WithDialer configures an HTTP client to use the specified dialer.
WithDisableIPV6 configures a dialer to use tcp4 network argument.
WithForceHttp1 configures a client to force HTTP/1.1 as the used protocol.
WithInsecureSkipVerify configures a client to skip SSL certificate verification.
WithLocalAddr configures an HTTP client to use the specified local address.
No description provided by the author
WithNotFollowRedirects configures an HTTP client to not follow HTTP redirects.
WithProxyUrl configures a HTTP client to use the specified proxy URL.
WithRandomTLSExtensionOrder configures a TLS client to randomize the order of TLS extensions being sent in the ClientHello.
WithServerNameOverwrite configures a TLS client to overwrite the server name being used for certificate verification and in the client hello.
No description provided by the author
WithTimeout configures an HTTP client to use the specified request timeout.
WithTimeoutMilliseconds configures an HTTP client to use the specified request timeout.
WithTimeoutSeconds configures an HTTP client to use the specified request timeout.
WithTransportOptions configures a client to use the specified transport options.

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
Users of context.WithValue should define their own types for keys.
No description provided by the author

# Interfaces

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author