webreplay

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2025 License: BSD-3-Clause Imports: 38 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

Functions

func CompressBody

func CompressBody(ae string, uncompressed []byte) ([]byte, string, error)

CompressBody reads a response body and compresses according to the given Accept-Encoding. The chosen compressed encoding is returned along with the compressed body.

func ConnStateHook added in v1.1.0

func ConnStateHook(c net.Conn, state http.ConnState)

func DecompressResponse

func DecompressResponse(resp *http.Response) error

Decompresses Response Body in place.

func ExcludeRequest

func ExcludeRequest(req *http.Request, websites []string) bool

func GetTCPKeepAliveListener added in v1.1.0

func GetTCPKeepAliveListener(host string, port int, ma *MultipleArchive) (net.Listener, error)

func Is2xxOr3xx

func Is2xxOr3xx(resp *http.Response) bool

func IsSubdomain

func IsSubdomain(root string, sub string) bool

func LevenshteinDistance

func LevenshteinDistance(a, b string) int

ComputeDistance computes the levenshtein distance between the two strings passed as an argument. The return value is the levenshtein distance

Works on runes (Unicode code points) but does not normalize the input strings. See https://blog.golang.org/normalization and the golang.org/x/text/unicode/norm package.

func MintDummyCertificate

func MintDummyCertificate(serverName string, leafCert *x509.Certificate, intCert *x509.Certificate, intKey crypto.PrivateKey) ([]byte, string, error)

Mints a dummy server cert when the real one is not recorded.

func MintServerCert

func MintServerCert(serverName string, leafCert *x509.Certificate, intCert *x509.Certificate, intKey crypto.PrivateKey) ([]byte, string, error)

Returns DER encoded server cert.

func NewRecordingProxy

func NewRecordingProxy(mwa *MultipleWritableArchive, scheme string, siteLog *SiteLog, proxyServerURL *url.URL) http.Handler

NewRecordingProxy constructs an HTTP proxy that records responses into an archive. The proxy is listening for requests on a port that uses the given scheme (e.g., http, https).

func NewReplayingProxy

func NewReplayingProxy(ma *MultipleArchive, scheme string, quietMode bool,
	excludesList string, disableReqDelay bool, siteLog *SiteLog) http.Handler

NewReplayingProxy constructs an HTTP proxy that replays responses from an archive. The proxy is listening for requests on a port that uses the given scheme (e.g., http, https).

func RecordTLSConfig

func RecordTLSConfig(leafs []tls.Certificate, tls_int tls.Certificate, mwa *MultipleWritableArchive) (*tls.Config, error)

Returns a TLS configuration that serves a server leaf cert fetched over the network on demand.

func ReplayTLSConfig

func ReplayTLSConfig(leafs []tls.Certificate, tls_int tls.Certificate, ma *MultipleArchive) (*tls.Config, error)

Returns a TLS configuration that serves a recorded server leaf cert signed by root CA.

func ShouldIncludeTransformation

func ShouldIncludeTransformation(req *http.Request, filename string) bool

Types

type AddMode

type AddMode int
const (
	AddModeAppend            AddMode = 0
	AddModeOverwriteExisting AddMode = 1
	AddModeSkipExisting      AddMode = 2
)

type Archive

type Archive struct {
	// Requests maps host(url) => url => []request.
	// The two-level mapping makes it easier to search for similar requests.
	// There may be multiple requests for a given URL.
	Requests map[string]map[string][]*ArchivedRequest
	// Maps host string to DER encoded certs.
	Certs map[string][]byte
	// Map host string to server timeout duration
	IdleTimeouts map[string]time.Duration
	// Maps host string to the negotiated protocol. eg. "http/1.1" or "h2"
	// If absent, will default to "http/1.1".
	NegotiatedProtocol map[string]string
	// The time seed that was used to initialize deterministic.js.
	DeterministicTimeSeedMs int64
	// When an incoming request matches multiple recorded responses, whether to
	// serve the responses in the chronological sequence in which wpr_go
	// recorded them.
	ServeResponseInChronologicalSequence bool
	// Records the current session id.
	// Archive can serve responses in chronological order. If a client wants to
	// reset the Archive to serve responses from the start, the client may do so
	// by incrementing its session id.
	CurrentSessionId uint32
	// If an incoming URL doesn't exactly match an entry in the archive,
	// skip fuzzy matching and return nothing.
	DisableFuzzyURLMatching bool
}

Archive contains an archive of requests. Immutable except when embedded in a WritableArchive. Fields are exported to enabled JSON encoding.

func OpenArchive

func OpenArchive(path string) (*Archive, error)

OpenArchive opens an archive file previously written by OpenWritableArchive.

func (*Archive) Add

func (a *Archive) Add(method string, urlString string, mode AddMode) error

Add the result of a get request to the receiver.

func (*Archive) Edit

func (a *Archive) Edit(edit func(req *http.Request, resp *http.Response) (*http.Request, *http.Response, error)) (*Archive, error)

Edit iterates over all requests in the archive. For each request, it calls f to edit the request. If f returns a nil pair, the request is deleted. The edited archive is returned, leaving the current archive is unchanged.

func (*Archive) FindHostTlsConfig

func (a *Archive) FindHostTlsConfig(host string) ([]byte, string, error)

Returns the der encoded cert and negotiated protocol.

func (*Archive) FindIdleTimeout added in v1.1.0

func (a *Archive) FindIdleTimeout(serverName string) (time.Duration, error)

func (*Archive) FindRequest

func (a *Archive) FindRequest(req *http.Request) (*http.Request, *http.Response, time.Duration, error)

FindRequest searches for the given request in the archive. Returns ErrNotFound if the request could not be found.

Does not use the request body, but reads the request body to prevent WPR from issuing a Connection Reset error when handling large upload requests. (https://bugs.chromium.org/p/chromium/issues/detail?id=1215668)

TODO: conditional requests

func (*Archive) ForEach

func (a *Archive) ForEach(f func(req *http.Request, resp *http.Response, dur time.Duration) error) error

ForEach applies f to all requests in the archive.

func (Archive) MarshalEasyJSON

func (v Archive) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (Archive) MarshalJSON

func (v Archive) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*Archive) Merge

func (a *Archive) Merge(youtubeOnly bool, other *Archive) error

Merge adds all the request of the provided archive to the receiver.

func (*Archive) Serialize

func (a *Archive) Serialize(w io.Writer) error

Serialize serializes this archive to the given writer.

func (*Archive) StartNewReplaySession

func (a *Archive) StartNewReplaySession()

Start a new replay session so that the archive serves responses from the start. If an archive contains multiple identical requests with different responses, the archive can serve the responses in chronological order. This function resets the archive serving order to the start.

func (*Archive) Trim

func (a *Archive) Trim(trimMatch func(req *http.Request, resp *http.Response) (bool, error)) (*Archive, error)

Trim iterates over all requests in the archive. For each request, it calls f to see if the request should be removed the archive. The trimmed archive is returned, leaving the current archive unchanged.

func (*Archive) UnmarshalEasyJSON

func (v *Archive) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*Archive) UnmarshalJSON

func (v *Archive) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type ArchivedRequest

type ArchivedRequest struct {
	SerializedRequest   []byte
	SerializedResponse  []byte // if empty, the request failed
	LastServedSessionId uint32
	Duration            time.Duration
}

ArchivedRequest contains a single request and its response. Immutable after creation.

type Installer

type Installer struct {
	AndroidDeviceId    string
	AdbBinaryPath      string
	CertUtilBinaryPath string
}

func (*Installer) AdbInstallRoot

func (i *Installer) AdbInstallRoot(certPath string) error

func (*Installer) AdbUninstallRoot

func (i *Installer) AdbUninstallRoot() error

func (*Installer) InstallRoot

func (i *Installer) InstallRoot(certFile string, keyFile string) error

TODO: Implement root CA installation for platforms other than Linux and Android.

func (*Installer) RemoveRoot

func (i *Installer) RemoveRoot()

type MultipleArchive

type MultipleArchive struct {
	Archives     []*Archive
	Transformers [][]ResponseTransformer
	Names        []string
	CurrentIndex uint32
	InitIndex    int32
	PrevIndex    int32
}

func (*MultipleArchive) ChangeArchive

func (ma *MultipleArchive) ChangeArchive(nextName string)

func (*MultipleArchive) CurrentArchive

func (ma *MultipleArchive) CurrentArchive() *Archive

func (*MultipleArchive) CurrentName

func (ma *MultipleArchive) CurrentName() string

func (*MultipleArchive) CurrentTransformers

func (ma *MultipleArchive) CurrentTransformers() []ResponseTransformer

func (*MultipleArchive) FindHostTlsConfig

func (ma *MultipleArchive) FindHostTlsConfig(host string) ([]byte, string, error)

func (*MultipleArchive) FindIdleTimeout added in v1.1.0

func (ma *MultipleArchive) FindIdleTimeout(serverName string) (time.Duration, error)

func (*MultipleArchive) InitArchive

func (ma *MultipleArchive) InitArchive() *Archive

func (*MultipleArchive) PrevArchive

func (ma *MultipleArchive) PrevArchive() *Archive

type MultipleWritableArchive

type MultipleWritableArchive struct {
	IsDir bool
	Dir   string

	WritableArchives []*WritableArchive
	Transformers     []ResponseTransformer
	Names            []string
	CurrentIndex     uint32
	// contains filtered or unexported fields
}

func (*MultipleWritableArchive) ChangeArchive

func (mwa *MultipleWritableArchive) ChangeArchive(nextName string)

func (*MultipleWritableArchive) Close

func (mwa *MultipleWritableArchive) Close() error

func (*MultipleWritableArchive) CurrentArchive

func (mwa *MultipleWritableArchive) CurrentArchive() *WritableArchive

func (*MultipleWritableArchive) CurrentName

func (mwa *MultipleWritableArchive) CurrentName() string

func (*MultipleWritableArchive) CurrentTransformers

func (mwa *MultipleWritableArchive) CurrentTransformers() []ResponseTransformer

type PushPromiseRule

type PushPromiseRule struct {
	// URL to push.
	URL string

	// Header for the request being simulated by this push. If empty, a default
	// set of headers are created by cloning the current request's headers and
	// setting
	// "referer" to the URL of the current (pusher) request.
	Headers http.Header
}

PushPromiseRule is a rule that adds pushes into the response stream.

type RequestMatch

type RequestMatch struct {
	Match      *ArchivedRequest
	Request    *http.Request
	Response   *http.Response
	MatchRatio float64
}

RequestMatch represents a match when querying the archive for responses to a request

func (*RequestMatch) SetMatch

func (requestMatch *RequestMatch) SetMatch(
	match *ArchivedRequest,
	request *http.Request,
	response *http.Response,
	ratio float64)

type ResponseTransformer

type ResponseTransformer interface {
	// Transform applies transformations to the response. for example, by
	// updating resp.Header or wrapping resp.Body. The transformer may inspect
	// the request but should not modify the request.
	Transform(req *http.Request, resp *http.Response)
}

ResponseTransformer is an interface for transforming HTTP responses.

func NewRuleBasedTransformer

func NewRuleBasedTransformer(filename string) (ResponseTransformer, error)

NewRuleBasedTransformer creates a transformer that is controlled by a rules file. Rules are specified as a JSON-encoded array of TransformerRule objects.

func NewScriptInjector

func NewScriptInjector(
	filename string, script []byte, replacements map[string]string) ResponseTransformer

NewScriptInjector constructs a transformer that injects the given script after the first <head>, <html>, or <!doctype html> tag. Statements in script must be ';' terminated. The script is lightly minified before injection.

func NewScriptInjectorFromFile

func NewScriptInjectorFromFile(
	filepath string, filename string, replacements map[string]string) (
	ResponseTransformer, error)

NewScriptInjectorFromFile creates a script injector from a script stored in a file.

type SiteLog added in v1.1.0

type SiteLog struct {
	// contains filtered or unexported fields
}

func CreateSiteLog added in v1.1.0

func CreateSiteLog(outDir string) *SiteLog

type TransformerRule

type TransformerRule struct {
	// How to match URLs: exactly one of URL and URLPattern must be specified.
	URL        string
	URLPattern string

	// Rules to apply to these URLs.
	// Inject these extra headers into the response
	ExtraHeaders http.Header
	// Inject these HTTP/2 PUSH_PROMISE frames into the response
	Push []PushPromiseRule
	// contains filtered or unexported fields
}

TransformerRule is a single JSON-encoded rule. Each rule matches either a specific URL (via URL) or a regexp pattern (via URLPattern).

type WritableArchive

type WritableArchive struct {
	Archive
	// contains filtered or unexported fields
}

WriteableArchive wraps an Archive with writable methods for recording. The file is not flushed until Close is called. All methods are thread-safe.

func OpenWritableArchive

func OpenWritableArchive(path string) (*WritableArchive, error)

OpenWritableArchive opens an archive file for writing. The output is gzipped JSON.

func (*WritableArchive) Close

func (a *WritableArchive) Close() error

Close flushes the the archive and closes the output file.

func (*WritableArchive) RecordRequest

func (a *WritableArchive) RecordRequest(req *http.Request, resp *http.Response, dur time.Duration) error

RecordRequest records a request/response pair in the archive.

func (*WritableArchive) RecordTlsConfig

func (a *WritableArchive) RecordTlsConfig(host string, der_bytes []byte, negotiatedProtocol string)

RecordTlsConfig records the cert used and protocol negotiated for a host.

Jump to

Keyboard shortcuts

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