Documentation
¶
Index ¶
- func CancelAll(cancels ...func())
- func GetRandomWait(min, max int) (time.Duration, error)
- func IsLogMeOutEnabled(opts *loginOptions) bool
- func IsTwoFacEnabled(opts *loginOptions) bool
- func SetLoginOptions(options ...LoginOption) *loginOptions
- func Wait(near float64) (time.Duration, error)
- type Browser
- type Credential
- type Driver
- type FormField
- type LoginOption
- type Session
- type Site
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 ¶
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 ¶
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 ¶
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 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.