web

package
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2023 License: MIT Imports: 6 Imported by: 0

README

goutils/v2/web

The web package is a part of goutils library.

It provides utility functions to interact with web applications in Go, including the means to drive a headless browser using chromeDP.


Functions

CancelAll
func CancelAll(cancels ...func())

Executes all provided cancel functions. Typically used for cleaning up or aborting operations that were started earlier and can be cancelled.

GetRandomWait
func GetRandomWait(minWait, maxWait time.Duration) (time.Duration, error)

Return a random duration between the specified minWait and maxWait durations.

Wait
func Wait(near float64) (time.Duration, error)

Generates a random period of time anchored to a given input value. Useful for simulating more human-like interaction in the context of a web application.


Installation

To use the goutils/v2/web package, you need to install it via go get:

go get github.com/l50/goutils/v2/web

Usage

After installation, you can import it in your project:

import "github.com/l50/goutils/v2/web"

Tests

To run the tests for the goutils/v2/web package, navigate to your $GOPATH/src/github.com/l50/goutils/v2/web directory and run go test:

go test -v

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CancelAll

func CancelAll(cancels ...func())

CancelAll executes all provided cancel functions. It is typically used for cleaning up or aborting operations that were started earlier and can be cancelled.

Parameters:

cancels: A slice of cancel functions, each of type func(). These are typically functions returned by context.WithCancel, or similar functions that provide a way to cancel an operation.

Example:

var cancels []func()

ctx, cancel := context.WithCancel(context.Background()) cancels = append(cancels, cancel)

// Later, when all operations need to be cancelled: CancelAll(cancels)

Note: The caller is responsible for handling any errors that may occur during the execution of the cancel functions.

func GetRandomWait

func GetRandomWait(min, max int) (time.Duration, error)

GetRandomWait returns a random duration in seconds between the specified minWait and maxWait durations. The function takes the minimum and maximum wait times as arguments, creates a new random number generator with a seed based on the current Unix timestamp, and calculates the random wait time within the given range.

Parameters:

minWait: The minimum duration to wait.
maxWait: The maximum duration to wait.

Returns:

time.Duration: A random duration between minWait and maxWait.
error: An error if the generation of the random wait time fails.

Example:

minWait := 2
maxWait := 6
randomWaitTime, err := GetRandomWait(minWait, maxWait)

if err != nil {
  log.Fatalf("failed to generate random wait time: %v", err)
}

func IsLogMeOutEnabled added in v2.0.3

func IsLogMeOutEnabled(opts *loginOptions) bool

IsLogMeOutEnabled checks if the option to log out the user after login is enabled in the provided login options.

Parameters:

opts: A pointer to a loginOptions instance.

Returns:

bool: A boolean indicating whether the user is to be logged out after login.

Example:

options := []web.LoginOption{
   web.WithLogMeOut(false),
}

loginOpts := web.SetLoginOptions(options...) isLogMeOutEnabled := web.IsLogMeOutEnabled(loginOpts) // isLogMeOutEnabled is now false.

func IsTwoFacEnabled added in v2.0.3

func IsTwoFacEnabled(opts *loginOptions) bool

IsTwoFacEnabled checks if two-factor authentication is enabled in the provided login options.

Parameters:

opts: A pointer to a loginOptions instance.

Returns:

bool: A boolean indicating whether two-factor authentication is enabled.

Example:

options := []web.LoginOption{
   web.WithTwoFacEnabled(false),
}

loginOpts := web.SetLoginOptions(options...) isTwoFacEnabled := web.IsTwoFacEnabled(loginOpts) // isTwoFacEnabled is now false.

func SetLoginOptions added in v2.0.3

func SetLoginOptions(options ...LoginOption) *loginOptions

SetLoginOptions applies provided login options to a new loginOptions instance with default values and returns a pointer to this instance. This function is primarily used to configure login behavior in the LoginAccount function.

Parameters:

options: A variadic set of LoginOption functions. Each LoginOption is a function that takes a pointer to a loginOptions struct and modifies it in place.

Returns:

*loginOptions: A pointer to a loginOptions struct that has been configured with the provided options.

Example:

options := []web.LoginOption{
   web.WithTwoFacEnabled(false),
   web.WithLogMeOut(true),
}

loginOpts := web.SetLoginOptions(options...)

// Now loginOpts.twoFacEnabled is false and loginOpts.logMeOut is true.

func Wait

func Wait(near float64) (time.Duration, error)

Wait generates a random period of time anchored to a given input value.

Parameters:

near: A float64 value that serves as the base value for generating the random wait time.

Returns:

time.Duration: The calculated random wait time in milliseconds. error: An error if the generation of the random wait time fails.

The function is useful for simulating more human-like interaction in the context of a web application. It first calculates a 'zoom' value by dividing the input 'near' by 10. Then, a random number is generated in the range of [0, zoom), and added to 95% of 'near'. This sum is then converted to a time duration in milliseconds and returned.

Example:

waitTime, err := Wait(1000.0)

if err != nil {
  log.Fatalf("failed to generate random wait time: %v", err)
}

log.Printf("Generated random wait time: %v\n", waitTime)

Types

type Browser

type Browser struct {
	Driver  interface{}
	Cancels []func()
}

Browser defines parameters used for driving a web browser.

type Credential

type Credential struct {
	User       string
	Password   string
	TwoFacCode string
}

Credential contains the information that makes up a credential to authenticate to an application.

type Driver

type Driver interface {
	GetContext() context.Context
	SetContext(context.Context)
	GetOptions() []chromedp.ExecAllocatorOption
	SetOptions([]chromedp.ExecAllocatorOption)
}

Driver is an interface that can be implemented for various browsers in go. It should service to capture information such as context and browser options.

type FormField

type FormField struct {
	Name     string `json:"-"`
	Selector string `json:"-"`
}

FormField contains a form field name and its associated selector.

type LoginOption

type LoginOption func(*loginOptions)

LoginOption is a type for functions that modify the login options. These functions take a pointer to a loginOptions struct and modify it in place.

func WithLogout

func WithLogout(enabled bool) LoginOption

WithLogout is a function that returns a LoginOption function which sets the logMeOut option.

Parameters:

enabled: Determines if the user should be logged out after login.

Returns:

LoginOption: A function that modifies the logMeOut option of a loginOptions struct.

func WithTwoFac

func WithTwoFac(enabled bool) LoginOption

WithTwoFac is a function that returns a LoginOption function which sets the twoFacEnabled option.

Parameters:

enabled: Determines if two-factor authentication should be enabled during login.

Returns:

LoginOption: A function that modifies the twoFacEnabled option of a loginOptions struct.

type Session

type Session struct {
	Credential Credential
	Driver     interface{}
}

Session contains parameters associated with maintaining a session.

type Site

type Site struct {
	LoginURL string
	Session  Session
	Debug    bool
}

Site is used to define parameters for interacting with web applications.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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