webpagereplay

package
v0.0.0-...-221ecf7 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: BSD-3-Clause Imports: 30 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 DecompressResponse

func DecompressResponse(resp *http.Response) error

Decompresses Response Body in place.

func MintDummyCertificate

func MintDummyCertificate(serverName string, rootCert *x509.Certificate, rootKey crypto.PrivateKey) ([]byte, string, error)

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

func MintServerCert

func MintServerCert(serverName string, rootCert *x509.Certificate, rootKey crypto.PrivateKey) ([]byte, string, error)

Returns DER encoded server cert.

func NewRecordingProxy

func NewRecordingProxy(a *WritableArchive, scheme string, transformers []ResponseTransformer) 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(a *Archive, scheme string, transformers []ResponseTransformer, quietMode bool) 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(roots []tls.Certificate, w *WritableArchive) (*tls.Config, error)

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

func ReplayTLSConfig

func ReplayTLSConfig(roots []tls.Certificate, a *Archive) (*tls.Config, error)

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

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
	// 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) FindRequest

func (a *Archive) FindRequest(req *http.Request) (*http.Request, *http.Response, 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) error) error

ForEach applies f to all requests in the archive.

func (*Archive) Merge

func (a *Archive) Merge(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.

type ArchivedRequest

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

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

type ConvertorConfig

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

func (*ConvertorConfig) Convert

func (r *ConvertorConfig) Convert(c *cli.Context)

func (*ConvertorConfig) Flags

func (cfg *ConvertorConfig) Flags() []cli.Flag

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 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(
	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(
	filename string, replacements map[string]string) (
	ResponseTransformer, error)

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

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) 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