You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.4 KiB

package nwweb
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
)
var DefaultClient = NewClient()
type Client struct {
httpClient *http.Client
httpCookieJar http.CookieJar
}
func NewClient() *Client {
httpCookieJar, _ := cookiejar.New(nil)
return &Client{
httpClient: &http.Client{
Transport: &http.Transport{},
Jar: httpCookieJar,
},
httpCookieJar: httpCookieJar,
}
}
func (c Client) Do(request *Request) *Response {
request.finalize()
httpResponse, err := c.httpClient.Do(request.httpRequest)
if err != nil {
return nil
}
return newResponseClient(httpResponse)
}
func (c *Client) SetProxy(hostname string, port int) {
proxyUrl, _ := url.Parse(fmt.Sprintf("%s:%d", hostname, port))
c.httpClient.Transport.(*http.Transport).Proxy = http.ProxyURL(proxyUrl)
}
func (c *Client) SetSSLChecks(enabled bool) {
c.httpClient.Transport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: !enabled}
}
func (c *Client) SetTLSVersion(maxVersion uint16) {
tlsConfig := &tls.Config{MaxVersion: maxVersion}
c.httpClient.Transport.(*http.Transport).DialTLS = func(network, addr string) (net.Conn, error) {
return tls.Dial(network, addr, tlsConfig)
}
}
func (c *Client) DisableHTTP2() {
c.httpClient.Transport.(*http.Transport).TLSNextProto = make(map[string]func(authority string, c *tls.Conn) http.RoundTripper)
}