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

2
api.go
View File

@ -44,7 +44,7 @@ func (a *Api) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (a *Api) handleTunnels(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
case "GET":
body, err := json.Marshal(a.tunMan.GetTunnels())
if err != nil {
w.WriteHeader(500)

View File

@ -118,10 +118,10 @@ func (p *BoringProxy) handleTunnels(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
switch r.Method {
case "POST":
switch r.Method {
case "POST":
p.handleCreateTunnel(w, r)
case "DELETE":
case "DELETE":
if len(query["host"]) != 1 {
w.WriteHeader(400)
w.Write([]byte("Invalid host parameter"))
@ -130,11 +130,11 @@ func (p *BoringProxy) handleTunnels(w http.ResponseWriter, r *http.Request) {
host := query["host"][0]
p.tunMan.DeleteTunnel(host)
default:
w.WriteHeader(405)
w.Write([]byte("Invalid method for /tunnels"))
return
}
default:
w.WriteHeader(405)
w.Write([]byte("Invalid method for /tunnels"))
return
}
}
func (p *BoringProxy) handleLogin(w http.ResponseWriter, r *http.Request) {

View File

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

View File

@ -46,7 +46,7 @@ func NewTunnelManager(db *Database, certConfig *certmagic.Config) *TunnelManager
}
func (m *TunnelManager) GetTunnels() map[string]Tunnel {
return m.db.GetTunnels()
return m.db.GetTunnels()
}
func (m *TunnelManager) SetTunnel(host string, port int) error {
@ -99,7 +99,7 @@ func (m *TunnelManager) DeleteTunnel(domain string) error {
return errors.New("Tunnel doesn't exist")
}
m.db.DeleteTunnel(domain)
m.db.DeleteTunnel(domain)
authKeysPath := fmt.Sprintf("%s/.ssh/authorized_keys", m.user.HomeDir)