cookie

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package cookie is the file a warmed session is kept in.

Warming a session costs something: a browser, a challenge, sometimes a person. Once it is warm it is worth keeping, and worth reusing across runs and machines, which means it has to be written down in a shape that survives the trip.

A file holds SETS rather than cookies, because a session is the unit that was warmed. Twenty sessions in one file are twenty identities to spread a run over, not one pile of cookies to mix.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cookie struct {
	Name     string    `json:"name"`
	Value    string    `json:"value"`
	Domain   string    `json:"domain,omitempty"`
	Path     string    `json:"path,omitempty"`
	Secure   bool      `json:"secure,omitempty"`
	HTTPOnly bool      `json:"http_only,omitempty"`
	Expires  time.Time `json:"expires,omitempty"`
}

Cookie is one cookie as a browser holds it.

More than a name and a value, because a warmed session is not portable without the rest: the domain decides what it is sent to, Secure and HttpOnly are part of what the server set, and an expiry that has passed is a cookie that will be ignored.

func Parse

func Parse(pair string) (Cookie, error)

Parse reads a cookie given on a command line, as `name=value`.

Only the two halves, because that is all a command line can carry without becoming its own format. Anything needing a domain or an expiry belongs in a file.

func (Cookie) Expired

func (c Cookie) Expired(at time.Time) bool

Expired reports whether this cookie is past its date. A cookie with no date is a session cookie and never expires on its own.

func (Cookie) MarshalJSON

func (c Cookie) MarshalJSON() ([]byte, error)

MarshalJSON leaves out an expiry that was never set.

encoding/json's omitempty has no opinion about a struct, so a session cookie would otherwise be written down as expiring in the year one, which is both noise and a lie.

type File

type File struct {
	// Version is written so a later format can be told from this one. Nothing
	// is rejected for it: a file from the future is more likely to be readable
	// than not, and refusing to try is not the reader's call to make.
	Version int   `json:"version"`
	Sets    []Set `json:"sets"`
}

File is what a cookies.json holds.

func Load

func Load(data []byte) (*File, error)

Load reads a cookie file.

Three shapes are accepted, because a warmed session arrives from wherever it was warmed: this file's own object, a bare array of sets, and a bare array of cookies, which is what a browser extension exports and becomes one set.

func (*File) Add

func (f *File) Add(set Set)

Add puts a set in the file under an id nothing else is using.

Unique because --cookie-set takes one: two sets answering to the same name make the flag a coin toss. The stamp a caller offers is only a starting point, since two runs can finish inside one second.

func (*File) Encode

func (f *File) Encode() []byte

Encode writes the file back out.

No error to return: encoding/json fails on types this format does not have and cannot grow without someone noticing, and a signature that promises a failure nobody can produce is a branch no test can reach.

func (*File) IDs

func (f *File) IDs() []string

IDs are the sets in the file, in order.

func (*File) Pick

func (f *File) Pick(id string, pick *rand.Rand) (Set, error)

Pick chooses a set: the one named, or one at random when no name is given.

Random rather than the first, because a file of warmed sessions exists to be spread over. Deterministic when there is only one, which is the common case and should not need a seed to be repeatable.

type Set

type Set struct {
	// ID names it, so a run can ask for this one.
	ID string `json:"id"`
	// Note is for whoever opens the file in six months.
	Note    string    `json:"note,omitempty"`
	Warmed  time.Time `json:"warmed,omitempty"`
	Cookies []Cookie  `json:"cookies"`
}

Set is one warmed session.

func (Set) EncodeNetscape

func (s Set) EncodeNetscape() []byte

EncodeNetscape writes a set as a cookies.txt, for handing to curl or to anything else that reads them.

One set, because the format cannot hold two: it is a jar, and a jar is one identity. The caller chooses which.

func (Set) Live

func (s Set) Live(at time.Time) Set

Live is the set with its expired cookies left out.

func (Set) MarshalJSON

func (s Set) MarshalJSON() ([]byte, error)

MarshalJSON leaves out a warming date that was never set, for the reason Cookie.MarshalJSON does.

Jump to

Keyboard shortcuts

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