Skip to content

Commit

Permalink
use default http client
Browse files Browse the repository at this point in the history
  • Loading branch information
ls0f committed Nov 19, 2017
1 parent 218656f commit 25f100c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ server/server
bin/
src/github.com/
src/gopkg.in/
dist/*
38 changes: 13 additions & 25 deletions local.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,12 @@ import (
"net/http"
"os"
"time"

"context"
g "github.com/golang/glog"
"gopkg.in/bufio.v1"
)

const (
PerHostNum = 10
)

var tr = &http.Transport{

DisableKeepAlives: false,
MaxIdleConnsPerHost: PerHostNum,
Proxy: http.ProxyFromEnvironment,
TLSHandshakeTimeout: time.Second * timeout / 2,
IdleConnTimeout: time.Second * 90,
}
var hc = &http.Client{}

func Init(cert string) {
if f, err := os.Stat(cert); err == nil && !f.IsDir() {
Expand All @@ -42,8 +31,9 @@ func Init(cert string) {
return
}
CAPOOL.AppendCertsFromPEM(serverCert)
tp := hc.Transport.(*http.Transport)
config := &tls.Config{RootCAs: CAPOOL}
tr.TLSClientConfig = config
tp.TLSClientConfig = config
g.Infof("load %s success ... ", cert)
} else if err != nil {
g.Error(err)
Expand All @@ -70,7 +60,6 @@ func (c *localProxyConn) gen_sign(req *http.Request) {
}

func (c *localProxyConn) push(data []byte, typ string) error {
hc := &http.Client{Transport: tr, Timeout: time.Second * timeout}
buf := bufio.NewBuffer(data)
req, _ := http.NewRequest("POST", c.server+PUSH, buf)
c.gen_sign(req)
Expand All @@ -92,11 +81,13 @@ func (c *localProxyConn) push(data []byte, typ string) error {
}

func (c *localProxyConn) connect(dstHost, dstPort string) (uuid string, err error) {
hc := &http.Client{Transport: tr, Timeout: time.Second * timeout}
req, _ := http.NewRequest("GET", c.server+CONNECT, nil)
c.gen_sign(req)
req.Header.Set("DSTHOST", dstHost)
req.Header.Set("DSTPORT", dstPort)
cxt, cancel := context.WithTimeout(context.Background(), time.Second*timeout)
defer cancel()
req.WithContext(cxt)
res, err := hc.Do(req)
if err != nil {
return "", err
Expand All @@ -112,18 +103,15 @@ func (c *localProxyConn) connect(dstHost, dstPort string) (uuid string, err erro

func (c *localProxyConn) pull() error {

var (
hc *http.Client
)
if c.interval > 0 {
hc = &http.Client{Transport: tr, Timeout: time.Second * timeout}
} else {
hc = &http.Client{Transport: tr}
}

req, _ := http.NewRequest("GET", c.server+PULL, nil)
req.Header.Set("Interval", fmt.Sprintf("%d", c.interval))
c.gen_sign(req)
if c.interval > 0 {
cxt, cancel := context.WithTimeout(context.Background(), time.Second*timeout)
defer cancel()
req.WithContext(cxt)

}
res, err := hc.Do(req)
if err != nil {
return err
Expand Down

0 comments on commit 25f100c

Please sign in to comment.