mirror of
https://github.com/boringproxy/boringproxy.git
synced 2025-02-25 18:55:29 -06:00
Reuse httpClient
This commit is contained in:
parent
6ee5a5d3f4
commit
57e2e80ed4
2
api.go
2
api.go
@ -44,7 +44,7 @@ func (a *Api) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func (a *Api) handleTunnels(w http.ResponseWriter, r *http.Request) {
|
func (a *Api) handleTunnels(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "GET":
|
case "GET":
|
||||||
body, err := json.Marshal(a.tunMan.GetTunnels())
|
body, err := json.Marshal(a.tunMan.GetTunnels())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
|
@ -118,10 +118,10 @@ func (p *BoringProxy) handleTunnels(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
|
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "POST":
|
case "POST":
|
||||||
p.handleCreateTunnel(w, r)
|
p.handleCreateTunnel(w, r)
|
||||||
case "DELETE":
|
case "DELETE":
|
||||||
if len(query["host"]) != 1 {
|
if len(query["host"]) != 1 {
|
||||||
w.WriteHeader(400)
|
w.WriteHeader(400)
|
||||||
w.Write([]byte("Invalid host parameter"))
|
w.Write([]byte("Invalid host parameter"))
|
||||||
@ -130,11 +130,11 @@ func (p *BoringProxy) handleTunnels(w http.ResponseWriter, r *http.Request) {
|
|||||||
host := query["host"][0]
|
host := query["host"][0]
|
||||||
|
|
||||||
p.tunMan.DeleteTunnel(host)
|
p.tunMan.DeleteTunnel(host)
|
||||||
default:
|
default:
|
||||||
w.WriteHeader(405)
|
w.WriteHeader(405)
|
||||||
w.Write([]byte("Invalid method for /tunnels"))
|
w.Write([]byte("Invalid method for /tunnels"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BoringProxy) handleLogin(w http.ResponseWriter, r *http.Request) {
|
func (p *BoringProxy) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -24,10 +24,11 @@ type SmtpConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type BoringProxy struct {
|
type BoringProxy struct {
|
||||||
config *BoringProxyConfig
|
config *BoringProxyConfig
|
||||||
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)
|
||||||
|
@ -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()
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ func NewTunnelManager(db *Database, certConfig *certmagic.Config) *TunnelManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *TunnelManager) GetTunnels() map[string]Tunnel {
|
func (m *TunnelManager) GetTunnels() map[string]Tunnel {
|
||||||
return m.db.GetTunnels()
|
return m.db.GetTunnels()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *TunnelManager) SetTunnel(host string, port int) error {
|
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")
|
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)
|
authKeysPath := fmt.Sprintf("%s/.ssh/authorized_keys", m.user.HomeDir)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user