fscan/WebScan/lib/client.go

89 lines
2.1 KiB
Go
Raw Normal View History

2021-05-30 19:03:01 -07:00
package lib
import (
2022-05-07 08:46:22 -07:00
"context"
2021-05-30 19:03:01 -07:00
"crypto/tls"
2022-05-07 08:46:22 -07:00
"errors"
2021-05-30 19:03:01 -07:00
"github.com/shadow1ng/fscan/common"
2022-05-07 08:46:22 -07:00
"golang.org/x/net/proxy"
2021-05-30 19:03:01 -07:00
"log"
"net"
"net/http"
"net/url"
"strings"
"time"
)
var (
Client *http.Client
ClientNoRedirect *http.Client
dialTimout = 5 * time.Second
2022-01-07 00:59:05 -08:00
keepAlive = 5 * time.Second
2021-05-30 19:03:01 -07:00
)
func Inithttp(PocInfo common.PocInfo) {
//PocInfo.Proxy = "http://127.0.0.1:8080"
2022-07-03 08:41:39 -07:00
err := InitHttpClient(PocInfo.Num, common.Proxy, time.Duration(common.WebTimeout)*time.Second)
2021-05-30 19:03:01 -07:00
if err != nil {
log.Fatal(err)
}
}
func InitHttpClient(ThreadsNum int, DownProxy string, Timeout time.Duration) error {
2022-05-07 08:46:22 -07:00
type DialContext = func(ctx context.Context, network, addr string) (net.Conn, error)
2021-05-30 19:03:01 -07:00
dialer := &net.Dialer{
Timeout: dialTimout,
KeepAlive: keepAlive,
}
tr := &http.Transport{
2022-05-08 21:08:29 -07:00
DialContext: dialer.DialContext,
2022-01-07 01:45:13 -08:00
MaxConnsPerHost: 5,
2021-05-30 19:03:01 -07:00
MaxIdleConns: 0,
2022-04-26 20:48:22 -07:00
MaxIdleConnsPerHost: ThreadsNum * 2,
2021-05-30 19:03:01 -07:00
IdleConnTimeout: keepAlive,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
TLSHandshakeTimeout: 5 * time.Second,
2022-01-07 01:45:13 -08:00
DisableKeepAlives: false,
2021-05-30 19:03:01 -07:00
}
2022-05-08 21:08:29 -07:00
if common.Socks5Proxy != "" {
dialSocksProxy, err := common.Socks5Dailer(dialer)
if err != nil {
return err
}
if contextDialer, ok := dialSocksProxy.(proxy.ContextDialer); ok {
tr.DialContext = contextDialer.DialContext
} else {
return errors.New("Failed type assertion to DialContext")
}
2022-07-03 08:41:39 -07:00
} else if DownProxy != "" {
2021-05-30 19:03:01 -07:00
if DownProxy == "1" {
DownProxy = "http://127.0.0.1:8080"
} else if DownProxy == "2" {
DownProxy = "socks5://127.0.0.1:1080"
2021-05-30 19:03:01 -07:00
} else if !strings.Contains(DownProxy, "://") {
DownProxy = "http://127.0.0.1:" + DownProxy
}
2022-07-03 08:41:39 -07:00
if !strings.HasPrefix(DownProxy, "socks") && !strings.HasPrefix(DownProxy, "http") {
2022-05-08 21:08:29 -07:00
return errors.New("no support this proxy")
}
2021-05-30 19:03:01 -07:00
u, err := url.Parse(DownProxy)
if err != nil {
return err
}
tr.Proxy = http.ProxyURL(u)
}
Client = &http.Client{
Transport: tr,
Timeout: Timeout,
}
ClientNoRedirect = &http.Client{
2021-06-08 20:16:23 -07:00
Transport: tr,
Timeout: Timeout,
CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse },
2021-05-30 19:03:01 -07:00
}
return nil
}