mirror of https://github.com/ehang-io/nps
http trunk bug
parent
4831e17b38
commit
c01c61fc6b
|
@ -384,3 +384,17 @@ func GetConn(conn net.Conn, cpt, snappy bool, rt *rate.Rate, isServer bool) (io.
|
||||||
}
|
}
|
||||||
return rate.NewRateConn(conn, rt)
|
return rate.NewRateConn(conn, rt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type LenConn struct {
|
||||||
|
conn io.Writer
|
||||||
|
Len int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLenConn(conn io.Writer) *LenConn {
|
||||||
|
return &LenConn{conn: conn}
|
||||||
|
}
|
||||||
|
func (c *LenConn) Write(p []byte) (n int, err error) {
|
||||||
|
n, err = c.conn.Write(p)
|
||||||
|
c.Len += n
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -172,16 +172,12 @@ func (s *httpServer) httpHandle(c *conn.Conn, r *http.Request) {
|
||||||
host.Flow.Add(0, int64(len(b)))
|
host.Flow.Add(0, int64(len(b)))
|
||||||
s.cache.Add(filepath.Join(host.Host, r.URL.Path), b)
|
s.cache.Add(filepath.Join(host.Host, r.URL.Path), b)
|
||||||
} else {
|
} else {
|
||||||
b, err := httputil.DumpResponse(resp, false)
|
lenConn := conn.NewLenConn(c)
|
||||||
if err != nil {
|
if err := resp.Write(lenConn); err != nil {
|
||||||
|
logs.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Write(b)
|
host.Flow.Add(0, int64(lenConn.Len))
|
||||||
if bodyLen, err := common.CopyBuffer(c, resp.Body); err != nil {
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
host.Flow.Add(0, int64(len(b))+bodyLen)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -234,18 +230,14 @@ func (s *httpServer) httpHandle(c *conn.Conn, r *http.Request) {
|
||||||
readReq = true
|
readReq = true
|
||||||
//change the host and header and set proxy setting
|
//change the host and header and set proxy setting
|
||||||
common.ChangeHostAndHeader(r, host.HostChange, host.HeaderChange, c.Conn.RemoteAddr().String())
|
common.ChangeHostAndHeader(r, host.HostChange, host.HeaderChange, c.Conn.RemoteAddr().String())
|
||||||
b, err := httputil.DumpRequest(r, false)
|
|
||||||
if err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
logs.Trace("%s request, method %s, host %s, url %s, remote address %s, target %s", r.URL.Scheme, r.Method, r.Host, r.URL.Path, c.RemoteAddr().String(), lk.Host)
|
logs.Trace("%s request, method %s, host %s, url %s, remote address %s, target %s", r.URL.Scheme, r.Method, r.Host, r.URL.Path, c.RemoteAddr().String(), lk.Host)
|
||||||
//write
|
//write
|
||||||
connClient.Write(b)
|
lenConn := conn.NewLenConn(connClient)
|
||||||
if bodyLen, err := common.CopyBuffer(connClient, r.Body); err != nil {
|
if err := r.Write(lenConn); err != nil {
|
||||||
|
logs.Error(err)
|
||||||
break
|
break
|
||||||
} else {
|
|
||||||
host.Flow.Add(int64(len(b))+bodyLen, 0)
|
|
||||||
}
|
}
|
||||||
|
host.Flow.Add(int64(lenConn.Len), 0)
|
||||||
reqCh <- r
|
reqCh <- r
|
||||||
}
|
}
|
||||||
end:
|
end:
|
||||||
|
|
Loading…
Reference in New Issue