4
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2025-07-04 15:57:44 +00:00

Add TLSConfig to nctalk (#1195)

Signed-off-by: Gary Kim <gary@garykim.dev>
This commit is contained in:
Gary Kim
2020-08-30 07:49:26 -04:00
committed by GitHub
parent c63f08c811
commit a0741d99b8
52 changed files with 12389 additions and 368 deletions

View File

@ -1,16 +0,0 @@
language: go
go:
- 1.14.x
- tip
sudo: false
before_install:
- go get -t -v ./...
script:
- go test -race -coverprofile=coverage.txt -covermode=atomic
after_success:
- bash <(curl -s https://codecov.io/bash) -t 6cf6f7ab-26e6-429c-ac44-f0ad85c1e586

View File

@ -2,7 +2,6 @@
<img align="right" width="159px" src="https://raw.githubusercontent.com/gin-gonic/logo/master/color.png">
[![Build Status](https://travis-ci.org/monaco-io/request.svg?branch=master)](https://travis-ci.org/monaco-io/request)
[![GoDoc](https://godoc.org/github.com/monaco-io/request?status.svg)](https://pkg.go.dev/github.com/monaco-io/request?tab=doc)
[![codecov](https://codecov.io/gh/monaco-io/request/branch/master/graph/badge.svg)](https://codecov.io/gh/monaco-io/request)
[![Release](https://img.shields.io/github/release/monaco-io/request.svg?style=flat-square)](https://github.com/monaco-io/request/releases)
@ -192,6 +191,31 @@ func main() {
}
```
### TLS
```go
package main
import (
"log"
"crypto/tls"
"github.com/monaco-io/request"
)
func main() {
client := request.Client{
URL: "https://google.com",
TLSConfig: &tls.Config{InsecureSkipVerify: true},
}
resp, err := client.Do()
log.Println(resp.Code, string(resp.Data), err)
}
```
## License
[MIT](LICENSE)

98
vendor/github.com/monaco-io/request/build.go generated vendored Normal file
View File

@ -0,0 +1,98 @@
package request
import (
"bytes"
"net/http"
"net/http/cookiejar"
"net/url"
"time"
)
// TODO: func unit test coverage
func (c *Client) buildRequest() (err error) {
if err = c.applyRequest(); err != nil {
return
}
c.transport = &http.Transport{}
c.applyHTTPHeader()
c.applyBasicAuth()
c.applyClient()
c.applyTimeout()
c.applyCookies()
c.applyTLSConfig()
err = c.applyProxy()
c.client.Transport = c.transport
return
}
func (c *Client) applyRequest() (err error) {
// encode requestURL.httpURL like https://google.com?hello=world&package=request
c.requestURL = requestURL{
urlString: c.URL,
parameters: c.Params,
}
if err = c.requestURL.EncodeURL(); err != nil {
return
}
c.req, err = http.NewRequest(c.Method, c.requestURL.string(), bytes.NewReader(c.Body))
return
}
func (c *Client) applyHTTPHeader() {
if c.Method == POST {
if c.ContentType == emptyString {
c.ContentType = ApplicationJSON
}
c.req.Header.Set(contentType, string(c.ContentType))
}
for k, v := range c.Header {
c.req.Header.Add(k, v)
}
}
func (c *Client) applyBasicAuth() {
if c.BasicAuth.Username != emptyString && c.BasicAuth.Password != emptyString {
c.req.SetBasicAuth(c.BasicAuth.Username, c.BasicAuth.Password)
}
}
func (c *Client) applyClient() {
c.client = &http.Client{}
}
func (c *Client) applyTimeout() {
if c.Timeout > 0 {
c.client.Timeout = c.Timeout * time.Second
}
}
func (c *Client) applyCookies() {
if c.Cookies != nil {
jar, _ := cookiejar.New(nil)
jar.SetCookies(&url.URL{Scheme: c.requestURL.scheme(), Host: c.requestURL.host()}, c.Cookies)
c.client.Jar = jar
}
}
// TODO: test case
func (c *Client) applyProxy() (err error) {
if c.ProxyURL != emptyString {
var proxy *url.URL
if proxy, err = url.Parse(c.ProxyURL); err != nil {
return
} else if proxy != nil {
c.transport.Proxy = http.ProxyURL(proxy)
}
}
return
}
func (c *Client) applyTLSConfig() {
// &tls.Config{InsecureSkipVerify: true}
if c.TLSConfig != nil {
c.transport.TLSClientConfig = c.TLSConfig
}
}

46
vendor/github.com/monaco-io/request/const.go generated vendored Normal file
View File

@ -0,0 +1,46 @@
package request
const (
// ApplicationJSON application/json
ApplicationJSON ContentType = "application/json"
// ApplicationXWwwFormURLEncoded application/x-www-form-urlencoded
ApplicationXWwwFormURLEncoded ContentType = "application/x-www-form-urlencoded"
// MultipartFormData multipart/form-data
MultipartFormData ContentType = "multipart/form-data"
)
const (
// OPTIONS http options
OPTIONS = "OPTIONS"
// GET http get
GET = "GET"
// HEAD http head
HEAD = "HEAD"
// POST http post
POST = "POST"
// PUT http put
PUT = "PUT"
// DELETE http delete
DELETE = "DELETE"
// TRACE http trace
TRACE = "TRACE"
// CONNECT http connect
CONNECT = "CONNECT"
// PATCH http patch
PATCH = "PATCH"
)
const (
emptyString = ""
contentType = "Content-Type"
)

View File

@ -1,24 +1,18 @@
package request
import (
"crypto/tls"
"net/http"
"time"
)
const (
// ApplicationJSON application/json
ApplicationJSON ContentType = "application/json"
// ApplicationXWwwFormURLEncoded application/x-www-form-urlencoded
ApplicationXWwwFormURLEncoded ContentType = "application/x-www-form-urlencoded"
// MultipartFormData multipart/form-data
MultipartFormData ContentType = "multipart/form-data"
)
// ContentType Content-Type
type ContentType string
// Method http method
// TODO:
type Method string
// Client Method
/*
Method = "OPTIONS" ; Section 9.2
@ -44,10 +38,13 @@ type Client struct {
ProxyURL string
ContentType ContentType
Cookies []*http.Cookie
TLSConfig *tls.Config
// private
client *http.Client
req *http.Request
client *http.Client
requestURL requestURL
req *http.Request
transport *http.Transport
}
// BasicAuth Add Username:Password as Basic Auth

View File

@ -1,98 +1,33 @@
package request
import (
"bytes"
"crypto/tls"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"time"
)
// Do send http request
func (c *Client) Do() (resp SugaredResp, err error) {
defer resp.Close()
if err := c.buildRequest(); err != nil {
return resp, err
if err = c.buildRequest(); err != nil {
return
}
// send request and close on func call end
if resp.resp, err = c.client.Do(c.req); err != nil {
return resp, err
return
}
// read response data form resp
resp.Data, err = ioutil.ReadAll(resp.resp.Body)
resp.Code = resp.resp.StatusCode
return resp, err
}
func (c *Client) buildRequest() (err error) {
// encode requestURL.httpURL like https://google.com?hello=world&package=request
ru := requestURL{
urlString: c.URL,
parameters: c.Params,
}
if err := ru.EncodeURL(); err != nil {
return err
}
// build request
c.req, err = http.NewRequest(c.Method, ru.string(), bytes.NewReader(c.Body))
if err != nil {
return err
}
// apply Header to request
if c.Method == "POST" {
if c.ContentType == "" {
c.ContentType = ApplicationJSON
}
c.req.Header.Set("Content-Type", string(c.ContentType))
}
for k, v := range c.Header {
c.req.Header.Add(k, v)
}
// apply basic Auth of request header
if c.BasicAuth.Username != "" && c.BasicAuth.Password != "" {
c.req.SetBasicAuth(c.BasicAuth.Username, c.BasicAuth.Password)
}
c.client = &http.Client{}
// apply timeout
if c.Timeout > 0 {
c.client.Timeout = c.Timeout * time.Second
}
// apply cookies
if c.Cookies != nil {
jar, _ := cookiejar.New(nil)
jar.SetCookies(&url.URL{Scheme: ru.scheme(), Host: ru.host()}, c.Cookies)
c.client.Jar = jar
}
// apply proxy
if c.ProxyURL != "" {
if proxy, err := url.Parse(c.ProxyURL); err == nil && proxy != nil {
c.client.Transport = &http.Transport{
Proxy: http.ProxyURL(proxy),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
}
return err
return
}
// Resp do request and get original http response struct
func (c *Client) Resp() (resp *http.Response, err error) {
if err = c.buildRequest(); err != nil {
return resp, err
return
}
return c.client.Do(c.req)
}

View File

@ -12,14 +12,14 @@ type requestURL struct {
func (ru *requestURL) EncodeURL() (err error) {
ru.httpURL, err = url.Parse(ru.urlString)
if err != nil {
return err
return
}
query := ru.httpURL.Query()
for k := range ru.parameters {
query.Set(k, ru.parameters[k])
}
ru.httpURL.RawQuery = query.Encode()
return err
return
}
// String return example: https://www.google.com/search?a=1&b=2