mirror of https://github.com/ehang-io/nps
Http handle change
parent
4b7b2f4c27
commit
144f102935
|
@ -108,12 +108,11 @@ func (s *httpServer) handleTunneling(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusServiceUnavailable)
|
http.Error(w, err.Error(), http.StatusServiceUnavailable)
|
||||||
}
|
}
|
||||||
s.httpHandle(conn.NewConn(c), r)
|
s.handleHttp(conn.NewConn(c), r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *httpServer) httpHandle(c *conn.Conn, r *http.Request) {
|
func (s *httpServer) handleHttp(c *conn.Conn, r *http.Request) {
|
||||||
var (
|
var (
|
||||||
isConn = false
|
|
||||||
host *file.Host
|
host *file.Host
|
||||||
target net.Conn
|
target net.Conn
|
||||||
lastHost *file.Host
|
lastHost *file.Host
|
||||||
|
@ -122,46 +121,58 @@ func (s *httpServer) httpHandle(c *conn.Conn, r *http.Request) {
|
||||||
scheme = r.URL.Scheme
|
scheme = r.URL.Scheme
|
||||||
lk *conn.Link
|
lk *conn.Link
|
||||||
targetAddr string
|
targetAddr string
|
||||||
readReq bool
|
lenConn *conn.LenConn
|
||||||
reqCh = make(chan *http.Request)
|
isReset bool
|
||||||
|
wg sync.WaitGroup
|
||||||
)
|
)
|
||||||
|
defer func() {
|
||||||
|
if connClient != nil {
|
||||||
|
s.writeConnFail(c.Conn)
|
||||||
|
connClient.Close()
|
||||||
|
}
|
||||||
|
c.Close()
|
||||||
|
}()
|
||||||
if host, err = file.GetDb().GetInfoByHost(r.Host, r); err != nil {
|
if host, err = file.GetDb().GetInfoByHost(r.Host, r); err != nil {
|
||||||
logs.Notice("the url %s %s %s can't be parsed!", r.URL.Scheme, r.Host, r.RequestURI)
|
logs.Notice("the url %s %s %s can't be parsed!", r.URL.Scheme, r.Host, r.RequestURI)
|
||||||
goto end
|
return
|
||||||
}
|
}
|
||||||
if err := s.CheckFlowAndConnNum(host.Client); err != nil {
|
if err := s.CheckFlowAndConnNum(host.Client); err != nil {
|
||||||
logs.Warn("client id %d, host id %d, error %s, when https connection", host.Client.Id, host.Id, err.Error())
|
logs.Warn("client id %d, host id %d, error %s, when https connection", host.Client.Id, host.Id, err.Error())
|
||||||
c.Close()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer host.Client.AddConn()
|
defer host.Client.AddConn()
|
||||||
lastHost = host
|
|
||||||
for {
|
|
||||||
start:
|
|
||||||
if isConn {
|
|
||||||
if err = s.auth(r, c, host.Client.Cnf.U, host.Client.Cnf.P); err != nil {
|
if err = s.auth(r, c, host.Client.Cnf.U, host.Client.Cnf.P); err != nil {
|
||||||
logs.Warn("auth error", err, r.RemoteAddr)
|
logs.Warn("auth error", err, r.RemoteAddr)
|
||||||
break
|
return
|
||||||
}
|
}
|
||||||
|
reset:
|
||||||
if targetAddr, err = host.Target.GetRandomTarget(); err != nil {
|
if targetAddr, err = host.Target.GetRandomTarget(); err != nil {
|
||||||
logs.Warn(err.Error())
|
logs.Warn(err.Error())
|
||||||
break
|
return
|
||||||
}
|
}
|
||||||
lk = conn.NewLink("http", targetAddr, host.Client.Cnf.Crypt, host.Client.Cnf.Compress, r.RemoteAddr, host.Target.LocalProxy)
|
lk = conn.NewLink("http", targetAddr, host.Client.Cnf.Crypt, host.Client.Cnf.Compress, r.RemoteAddr, host.Target.LocalProxy)
|
||||||
if target, err = s.bridge.SendLinkInfo(host.Client.Id, lk, nil); err != nil {
|
if target, err = s.bridge.SendLinkInfo(host.Client.Id, lk, nil); err != nil {
|
||||||
logs.Notice("connect to target %s error %s", lk.Host, err)
|
logs.Notice("connect to target %s error %s", lk.Host, err)
|
||||||
break
|
return
|
||||||
}
|
}
|
||||||
connClient = conn.GetConn(target, lk.Crypt, lk.Compress, host.Client.Rate, true)
|
connClient = conn.GetConn(target, lk.Crypt, lk.Compress, host.Client.Rate, true)
|
||||||
isConn = false
|
lastHost = host
|
||||||
|
|
||||||
|
//read from inc-client
|
||||||
go func() {
|
go func() {
|
||||||
|
wg.Add(1)
|
||||||
|
isReset = false
|
||||||
defer connClient.Close()
|
defer connClient.Close()
|
||||||
defer c.Close()
|
defer func() {
|
||||||
|
wg.Done()
|
||||||
|
if !isReset {
|
||||||
|
c.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
for {
|
for {
|
||||||
if resp, err := http.ReadResponse(bufio.NewReader(connClient), r); err != nil {
|
if resp, err := http.ReadResponse(bufio.NewReader(connClient), r); err != nil {
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
r := <-reqCh
|
|
||||||
//if the cache is start and the response is in the extension,store the response to the cache list
|
//if the cache is start and the response is in the extension,store the response to the cache list
|
||||||
if s.useCache && strings.Contains(r.URL.Path, ".") {
|
if s.useCache && strings.Contains(r.URL.Path, ".") {
|
||||||
b, err := httputil.DumpResponse(resp, true)
|
b, err := httputil.DumpResponse(resp, true)
|
||||||
|
@ -182,29 +193,8 @@ func (s *httpServer) httpHandle(c *conn.Conn, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
} else if readReq {
|
|
||||||
r, err = http.ReadRequest(bufio.NewReader(c))
|
for {
|
||||||
if err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
r.URL.Scheme = scheme
|
|
||||||
//What happened ,Why one character less???
|
|
||||||
if r.Method == "ET" {
|
|
||||||
r.Method = "GET"
|
|
||||||
}
|
|
||||||
if r.Method == "OST" {
|
|
||||||
r.Method = "POST"
|
|
||||||
}
|
|
||||||
if hostTmp, err := file.GetDb().GetInfoByHost(r.Host, r); err != nil {
|
|
||||||
logs.Notice("the url %s %s %s can't be parsed!", r.URL.Scheme, r.Host, r.RequestURI)
|
|
||||||
break
|
|
||||||
} else if host != lastHost {
|
|
||||||
host = hostTmp
|
|
||||||
lastHost = host
|
|
||||||
isConn = true
|
|
||||||
goto start
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//if the cache start and the request is in the cache list, return the cache
|
//if the cache start and the request is in the cache list, return the cache
|
||||||
if s.useCache {
|
if s.useCache {
|
||||||
if v, ok := s.cache.Get(filepath.Join(host.Host, r.URL.Path)); ok {
|
if v, ok := s.cache.Get(filepath.Join(host.Host, r.URL.Path)); ok {
|
||||||
|
@ -215,39 +205,54 @@ func (s *httpServer) httpHandle(c *conn.Conn, r *http.Request) {
|
||||||
logs.Trace("%s request, method %s, host %s, url %s, remote address %s, return cache", r.URL.Scheme, r.Method, r.Host, r.URL.Path, c.RemoteAddr().String())
|
logs.Trace("%s request, method %s, host %s, url %s, remote address %s, return cache", r.URL.Scheme, r.Method, r.Host, r.URL.Path, c.RemoteAddr().String())
|
||||||
host.Flow.Add(0, int64(n))
|
host.Flow.Add(0, int64(n))
|
||||||
//if return cache and does not create a new conn with client and Connection is not set or close, close the connection.
|
//if return cache and does not create a new conn with client and Connection is not set or close, close the connection.
|
||||||
if connClient == nil && (strings.ToLower(r.Header.Get("Connection")) == "close" || strings.ToLower(r.Header.Get("Connection")) == "") {
|
if strings.ToLower(r.Header.Get("Connection")) == "close" || strings.ToLower(r.Header.Get("Connection")) == "" {
|
||||||
c.Close()
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
readReq = true
|
goto readReq
|
||||||
goto start
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if connClient == nil {
|
|
||||||
isConn = true
|
|
||||||
goto start
|
|
||||||
}
|
|
||||||
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())
|
||||||
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
|
||||||
lenConn := conn.NewLenConn(connClient)
|
lenConn = conn.NewLenConn(connClient)
|
||||||
if err := r.Write(lenConn); err != nil {
|
if err := r.Write(lenConn); err != nil {
|
||||||
logs.Error(err)
|
logs.Error(err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
host.Flow.Add(int64(lenConn.Len), 0)
|
host.Flow.Add(int64(lenConn.Len), 0)
|
||||||
reqCh <- r
|
|
||||||
|
readReq:
|
||||||
|
//read req from connection
|
||||||
|
if r, err = http.ReadRequest(bufio.NewReader(c)); err != nil {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
end:
|
r.URL.Scheme = scheme
|
||||||
if !readReq {
|
//What happened ,Why one character less???
|
||||||
s.writeConnFail(c.Conn)
|
r.Method = resetReqMethod(r.Method)
|
||||||
|
if hostTmp, err := file.GetDb().GetInfoByHost(r.Host, r); err != nil {
|
||||||
|
logs.Notice("the url %s %s %s can't be parsed!", r.URL.Scheme, r.Host, r.RequestURI)
|
||||||
|
break
|
||||||
|
} else if host != lastHost {
|
||||||
|
host = hostTmp
|
||||||
|
lastHost = host
|
||||||
|
isReset = true
|
||||||
|
connClient.Close()
|
||||||
|
goto reset
|
||||||
}
|
}
|
||||||
c.Close()
|
|
||||||
if target != nil {
|
|
||||||
target.Close()
|
|
||||||
}
|
}
|
||||||
|
wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
func resetReqMethod(method string) string {
|
||||||
|
if method == "ET" {
|
||||||
|
return "GET"
|
||||||
|
}
|
||||||
|
if method == "OST" {
|
||||||
|
return "POST"
|
||||||
|
}
|
||||||
|
return method
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *httpServer) NewServer(port int, scheme string) *http.Server {
|
func (s *httpServer) NewServer(port int, scheme string) *http.Server {
|
||||||
|
|
Loading…
Reference in New Issue