engine

package
v0.0.0-...-5672997 Latest Latest
Warning

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

Go to latest
Published: May 25, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCaptchaInvalid = errors.New("captcha is invalid")
)
View Source
var (
	ErrLimitExceeded = errors.New("session: retry limit exceeded")
)

Functions

func SessionIDFromContext

func SessionIDFromContext(ctx context.Context) string

func WithSessionID

func WithSessionID(ctx context.Context, gen IDGenerator) context.Context

Types

type CaptchaResolveEngine

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

func NewCaptchaResolveEngine

func NewCaptchaResolveEngine(opts ...Option) (*CaptchaResolveEngine, error)

NewCaptchaResolveEngine creates a new captcha resolve engine.

func (*CaptchaResolveEngine) ResolveFile

func (e *CaptchaResolveEngine) ResolveFile(ctx context.Context, r io.Reader) (*CaptchaResult, error)

ResolveFile resolves the captcha from the file.

func (*CaptchaResolveEngine) ResolveImage

func (e *CaptchaResolveEngine) ResolveImage(ctx context.Context, img image.Image) (*CaptchaResult, error)

ResolveImage resolves the captcha from the image.

type CaptchaResolver

type CaptchaResolver interface {
	ResolveFile(ctx context.Context, r io.Reader) (*CaptchaResult, error)
	ResolveImage(ctx context.Context, img image.Image) (*CaptchaResult, error)
}

type CaptchaResult

type CaptchaResult struct {
	Captcha string `json:"captcha"`
}

type CaptchaSession

type CaptchaSession struct {
	// Captcha is the function to fetch the captcha image, it should use
	// the HTTPDoer to make requests.
	Captcha func(_ context.Context, c HTTPDoer) (io.ReadCloser, error)
	// Main is the function will be invoked after the captcha is resolved.
	Main func(_ context.Context, c HTTPDoer, captcha string) (any, error)
	// Engine is the captcha resolver.
	Engine CaptchaResolver
	// IDGenerator generates a new session ID.
	IDGenerator IDGenerator
	// RetryCount is the number of retries, 0 means no retry.
	RetryCount int
	// Transport is the HTTP transport used to make requests.
	Transport http.RoundTripper
}

func (*CaptchaSession) Start

func (h *CaptchaSession) Start(ctx context.Context) (any, error)

Start starts the captcha session, it will fetch the captcha image, resolve the captcha, and then invoke the main function.

type EngineOption

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

type HTTPDoer

type HTTPDoer interface {
	Do(*http.Request) (*http.Response, error)
}

type IDFunc

type IDFunc func() string

func (IDFunc) NewID

func (fn IDFunc) NewID() string

NewID returns a new session ID.

type IDGenerator

type IDGenerator interface {
	NewID() string
}

type Option

type Option func(*EngineOption) error

func WithBinaryWidth

func WithBinaryWidth(width int) Option

func WithCaptchaLength

func WithCaptchaLength(len int) Option

func WithPreprocessor

func WithPreprocessor(preprocessor Preprocessor) Option

func WithSymbolResolver

func WithSymbolResolver(sr SymbolResolver) Option

type Preprocessor

type Preprocessor interface {
	Transform(ctx context.Context, img image.Image) (image.Image, error)
}

type ResultReporter

type ResultReporter interface {
	Report(ctx context.Context, result *CaptchaResult, correct bool) error
}

type StatsCaptchaResolver

type StatsCaptchaResolver struct {
	CaptchaResolver
}

func (*StatsCaptchaResolver) Report

func (s *StatsCaptchaResolver) Report(ctx context.Context, captcha *CaptchaResult, correct bool) error

func (*StatsCaptchaResolver) ResolveFile

func (s *StatsCaptchaResolver) ResolveFile(ctx context.Context, r io.Reader) (*CaptchaResult, error)

func (*StatsCaptchaResolver) ResolveImage

func (s *StatsCaptchaResolver) ResolveImage(ctx context.Context, img image.Image) (*CaptchaResult, error)

type StatsPreprocessor

type StatsPreprocessor struct {
	Preprocessor
}

func (*StatsPreprocessor) Transform

func (s *StatsPreprocessor) Transform(ctx context.Context, img image.Image) (image.Image, error)

type StatsSymbolResolver

type StatsSymbolResolver struct {
	SymbolResolver
}

func (*StatsSymbolResolver) SymbolResolve

func (s *StatsSymbolResolver) SymbolResolve(ctx context.Context, img image.Image) (string, error)

type SymbolResolver

type SymbolResolver interface {
	SymbolResolve(ctx context.Context, img image.Image) (string, error)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳