http

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NewClient = func(conf *configs.HTTPTaskConfig, proxyMap map[string]string) Client {
	cj, err := cookiejar.New(nil)
	if err != nil {
		logger.Errorf("create cookiejar failed: %v", err)
	}
	dialer := net.Dialer{
		Timeout: conf.Timeout,
	}
	transport := &http.Transport{
		MaxResponseHeaderBytes: int64(conf.BufferSize),
		DisableKeepAlives:      true,
		TLSClientConfig: &tls.Config{

			InsecureSkipVerify: conf.InsecureSkipVerify,
			Renegotiation:      tls.RenegotiateFreelyAsClient,
		},
		Proxy: func(_ *http.Request) (*url.URL, error) {
			if conf.Proxy != "" {
				return url.Parse(conf.Proxy)
			}
			return nil, nil
		},
		DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
			network = "tcp"
			switch conf.TargetIPType {
			case configs.IPv4:
				network = "tcp4"
			case configs.IPv6:
				network = "tcp6"
			}
			logger.Debugf("http dial with network: %s", network)

			if len(proxyMap) > 0 {
				host, port, err := net.SplitHostPort(addr)
				if err != nil {
					logger.Errorf("parse addr %s failed: %v", addr, err)
					host = addr
				}

				if proxyIP, ok := proxyMap[host]; ok {
					addr = net.JoinHostPort(proxyIP, port)
				}
			}
			return dialer.DialContext(ctx, network, addr)
		},
	}
	client := &http.Client{
		Transport: transport,
		Jar:       cj,
		Timeout:   conf.GetTimeout(),
	}
	return client
}

NewClient proxyMap代理配置 key: host value: proxy ip, 如{"example.com": "127.0.0.1"}

Functions

func New

func New(globalConfig define.Config, taskConfig define.TaskConfig) define.Task

Types

type Client

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

Client 请求客户端

type Event

type Event struct {
	*tasks.Event
	URL           string
	Index         int
	Steps         int
	Method        string
	ResponseCode  int
	Message       string
	Charset       string
	ContentLength int
	MediaType     string
	ResolvedIP    string
}

func NewEvent

func NewEvent(g *Gather) *Event

func (*Event) AsMapStr

func (e *Event) AsMapStr() common.MapStr

func (*Event) Fail

func (e *Event) Fail(code define.NamedCode)

func (*Event) FailFromError

func (e *Event) FailFromError(err error)

func (*Event) OK

func (e *Event) OK() bool

func (*Event) ToStep

func (e *Event) ToStep(index int, method, url string)

type Gather

type Gather struct {
	tasks.BaseTask
	// contains filtered or unexported fields
}

func (*Gather) GatherURL

func (g *Gather) GatherURL(ctx context.Context, event *Event, step *configs.HTTPTaskStepConfig, url, host string) bool

GatherURL 测试链接并设置结果事件,url为请求的链接,proxyHost和proxyIP为需要代理的host和ip

func (*Gather) Run

func (g *Gather) Run(ctx context.Context, e chan<- define.Event)

func (*Gather) UpdateEventByResponse

func (g *Gather) UpdateEventByResponse(event *Event, response *http.Response)

UpdateEventByResponse 根据返回写入结果数据

Jump to

Keyboard shortcuts

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