Reuse httpClient

This commit is contained in:
Anders Pitman 2020-10-06 10:22:03 -06:00
parent 6ee5a5d3f4
commit 57e2e80ed4
5 changed files with 23 additions and 19 deletions

View File

@ -28,6 +28,7 @@ type BoringProxy struct {
db *Database db *Database
auth *Auth auth *Auth
tunMan *TunnelManager tunMan *TunnelManager
httpClient *http.Client
} }
func Listen() { func Listen() {
@ -65,7 +66,9 @@ func Listen() {
auth := NewAuth(db) auth := NewAuth(db)
p := &BoringProxy{config, db, auth, tunMan} httpClient := &http.Client{}
p := &BoringProxy{config, db, auth, tunMan, httpClient}
api := NewApi(config, auth, tunMan) api := NewApi(config, auth, tunMan)
http.Handle("/api/", http.StripPrefix("/api", api)) http.Handle("/api/", http.StripPrefix("/api", api))
@ -94,6 +97,8 @@ func Listen() {
func (p *BoringProxy) proxyRequest(w http.ResponseWriter, r *http.Request) { func (p *BoringProxy) proxyRequest(w http.ResponseWriter, r *http.Request) {
log.Println("proxy conn")
port, err := p.tunMan.GetPort(r.Host) port, err := p.tunMan.GetPort(r.Host)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
@ -103,8 +108,6 @@ func (p *BoringProxy) proxyRequest(w http.ResponseWriter, r *http.Request) {
return return
} }
httpClient := &http.Client{}
downstreamReqHeaders := r.Header.Clone() downstreamReqHeaders := r.Header.Clone()
upstreamAddr := fmt.Sprintf("localhost:%d", port) upstreamAddr := fmt.Sprintf("localhost:%d", port)
@ -121,7 +124,7 @@ func (p *BoringProxy) proxyRequest(w http.ResponseWriter, r *http.Request) {
upstreamReq.Header = downstreamReqHeaders upstreamReq.Header = downstreamReqHeaders
upstreamRes, err := httpClient.Do(upstreamReq) upstreamRes, err := p.httpClient.Do(upstreamReq)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
errMessage := fmt.Sprintf("%s", err) errMessage := fmt.Sprintf("%s", err)

View File

@ -103,6 +103,7 @@ func (c *BoringProxyClient) Run() {
} }
func (c *BoringProxyClient) handleConnection(conn net.Conn, port int) { func (c *BoringProxyClient) handleConnection(conn net.Conn, port int) {
log.Println("new conn")
defer conn.Close() defer conn.Close()