Compare commits
2
Commits
v0.26.12.1
...
v0.26.12.3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f7d86c524d | ||
|
|
00315a8892 |
+13
-12
@@ -45,14 +45,17 @@ func newConnGroup(dst, src io.ReadWriteCloser, wg *sync.WaitGroup, n *int64, flo
|
||||
func CopyBuffer(dst io.Writer, src io.Reader, flow *file.Flow, task *file.Tunnel, remote string) (err error) {
|
||||
buf := common.CopyBuff.Get()
|
||||
defer common.CopyBuff.Put(buf)
|
||||
i := 0
|
||||
for {
|
||||
if len(buf) <= 0 {
|
||||
break
|
||||
}
|
||||
nr, er := src.Read(buf)
|
||||
|
||||
//if len(pr)>0 && pr[0] && nr > 50 {
|
||||
// logs.Warn(string(buf[:50]))
|
||||
//}
|
||||
|
||||
if task != nil && i == 0 {
|
||||
if task != nil {
|
||||
task.IsHttp = false
|
||||
firstLine := string(buf[0:nr])
|
||||
if len(firstLine) > 3 {
|
||||
@@ -63,28 +66,26 @@ func CopyBuffer(dst io.Writer, src io.Reader, flow *file.Flow, task *file.Tunnel
|
||||
if len(heads) >= 2 {
|
||||
logs.Info("HTTP Request method %s, %s, remote address %s, target %s", heads[0], heads[1], remote, task.Target.TargetStr)
|
||||
}
|
||||
//logs.Info("HTTP Request: " + firstLine[0:strings.Index(firstLine, "\n")])
|
||||
//logs.Info("%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)
|
||||
|
||||
}
|
||||
|
||||
task.IsHttp = true
|
||||
}
|
||||
}
|
||||
i++
|
||||
}
|
||||
|
||||
if nr > 0 {
|
||||
nw, ew := dst.Write(buf[0:nr])
|
||||
if nw > 0 {
|
||||
//written += int64(nw)
|
||||
flow.Add(int64(nw), int64(nw))
|
||||
|
||||
// <<20 = 1024 * 1024
|
||||
if flow.FlowLimit > 0 && (flow.FlowLimit<<20) < (flow.ExportFlow+flow.InletFlow) {
|
||||
logs.Info("流量已经超出.........")
|
||||
break
|
||||
if flow != nil {
|
||||
flow.Add(int64(nw), int64(nw))
|
||||
// <<20 = 1024 * 1024
|
||||
if flow.FlowLimit > 0 && (flow.FlowLimit<<20) < (flow.ExportFlow+flow.InletFlow) {
|
||||
logs.Info("流量已经超出.........")
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if ew != nil {
|
||||
err = ew
|
||||
|
||||
@@ -97,7 +97,7 @@ func in(target string, str_array []string) bool {
|
||||
func (s *BaseServer) DealClient(c *conn.Conn, client *file.Client, addr string,
|
||||
rb []byte, tp string, f func(), flow *file.Flow, localProxy bool, task *file.Tunnel) error {
|
||||
|
||||
// 判断访问地址是否在黑明单内
|
||||
// 判断访问地址是否在黑名单内
|
||||
if common.IsBlackIp(c.RemoteAddr().String(), client.VerifyKey, client.BlackIpList) {
|
||||
c.Close()
|
||||
return nil
|
||||
|
||||
+40
-14
@@ -17,6 +17,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -121,7 +122,6 @@ func (s *httpServer) handleTunneling(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (s *httpServer) handleHttp(c *conn.Conn, r *http.Request) {
|
||||
|
||||
var (
|
||||
host *file.Host
|
||||
target net.Conn
|
||||
@@ -191,34 +191,60 @@ reset:
|
||||
}
|
||||
}()
|
||||
|
||||
if true {
|
||||
// http 这里使用数据包交换
|
||||
wg1 := new(sync.WaitGroup)
|
||||
wg1.Add(1)
|
||||
err := goroutine.CopyConnsPool.Invoke(goroutine.NewConns(connClient, c, host.Client.Flow, wg1, nil))
|
||||
wg1.Wait()
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
}
|
||||
|
||||
err1 := goroutine.CopyBuffer(c, connClient, host.Client.Flow, nil, "")
|
||||
if err1 != nil {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := http.ReadResponse(bufio.NewReader(connClient), r)
|
||||
if err != nil || resp == nil || r == nil {
|
||||
// if there got broken pipe, http.ReadResponse will get a nil
|
||||
//break
|
||||
return
|
||||
} else {
|
||||
lenConn := conn.NewLenConn(c)
|
||||
if err := resp.Write(lenConn); err != nil {
|
||||
logs.Error(err)
|
||||
//break
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
//if the cache start and the request is in the cache list, return the cache
|
||||
if s.useCache {
|
||||
if v, ok := s.cache.Get(filepath.Join(host.Host, r.URL.Path)); ok {
|
||||
n, err := c.Write(v.([]byte))
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
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.Client.Flow.Add(int64(n), 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 strings.ToLower(r.Header.Get("Connection")) == "close" || strings.ToLower(r.Header.Get("Connection")) == "" {
|
||||
break
|
||||
}
|
||||
goto readReq
|
||||
}
|
||||
}
|
||||
|
||||
//change the host and header and set proxy setting
|
||||
common.ChangeHostAndHeader(r, host.HostChange, host.HeaderChange, c.Conn.RemoteAddr().String(), s.addOrigin)
|
||||
logs.Info("%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
|
||||
lenConn = conn.NewLenConn(connClient)
|
||||
//lenConn = conn.LenConn
|
||||
if err := r.Write(lenConn); err != nil {
|
||||
logs.Error(err)
|
||||
break
|
||||
}
|
||||
host.Client.Flow.Add(int64(lenConn.Len), int64(lenConn.Len))
|
||||
|
||||
//readReq:
|
||||
readReq:
|
||||
//read req from connection
|
||||
if r, err = http.ReadRequest(bufio.NewReader(c)); err != nil {
|
||||
r, err = http.ReadRequest(bufio.NewReader(c))
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
r.URL.Scheme = scheme
|
||||
|
||||
+11
-12
@@ -5,6 +5,7 @@ import (
|
||||
"ehang.io/nps/lib/common"
|
||||
"ehang.io/nps/lib/conn"
|
||||
"ehang.io/nps/lib/file"
|
||||
"ehang.io/nps/lib/goroutine"
|
||||
"errors"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"io"
|
||||
@@ -61,22 +62,18 @@ func (rp *HttpReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request)
|
||||
req = req.WithContext(context.WithValue(req.Context(), "target", targetAddr))
|
||||
req = req.WithContext(context.WithValue(req.Context(), "req", req))
|
||||
|
||||
rp.proxy.ServeHTTP(rw, req)
|
||||
rp.proxy.ServeHTTP(rw, req, host)
|
||||
|
||||
defer host.Client.AddConn()
|
||||
}
|
||||
|
||||
func (c *flowConn) Read(p []byte) (n int, err error) {
|
||||
n, err = c.ReadWriteCloser.Read(p)
|
||||
c.host.Client.Flow.Add(int64(n), int64(n))
|
||||
//c.flowIn += int64(n)
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (c *flowConn) Write(p []byte) (n int, err error) {
|
||||
n, err = c.ReadWriteCloser.Write(p)
|
||||
//c.flowOut += int64(n)
|
||||
c.host.Client.Flow.Add(int64(n), int64(n))
|
||||
return n, err
|
||||
}
|
||||
|
||||
@@ -217,13 +214,13 @@ func (p *ReverseProxy) errHandler(rw http.ResponseWriter, r *http.Request, e err
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request, host *file.Host) {
|
||||
if IsWebsocketRequest(req) {
|
||||
p.serveWebSocket(rw, req)
|
||||
p.serveWebSocket(rw, req, host)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ReverseProxy) serveWebSocket(rw http.ResponseWriter, req *http.Request) {
|
||||
func (p *ReverseProxy) serveWebSocket(rw http.ResponseWriter, req *http.Request, host *file.Host) {
|
||||
if p.WebSocketDialContext == nil {
|
||||
rw.WriteHeader(500)
|
||||
return
|
||||
@@ -250,20 +247,22 @@ func (p *ReverseProxy) serveWebSocket(rw http.ResponseWriter, req *http.Request)
|
||||
defer conn.Close()
|
||||
|
||||
req.Write(targetConn)
|
||||
Join(conn, targetConn)
|
||||
|
||||
Join(conn, targetConn, host)
|
||||
}
|
||||
|
||||
func Join(c1 io.ReadWriteCloser, c2 io.ReadWriteCloser) (inCount int64, outCount int64) {
|
||||
func Join(c1 io.ReadWriteCloser, c2 io.ReadWriteCloser, host *file.Host) (inCount int64, outCount int64) {
|
||||
var wait sync.WaitGroup
|
||||
pipe := func(to io.ReadWriteCloser, from io.ReadWriteCloser, count *int64) {
|
||||
defer to.Close()
|
||||
defer from.Close()
|
||||
defer wait.Done()
|
||||
|
||||
*count, _ = io.Copy(to, from)
|
||||
goroutine.CopyBuffer(to, from, host.Client.Flow, nil, "")
|
||||
//*count, _ = io.Copy(to, from)
|
||||
}
|
||||
|
||||
wait.Add(2)
|
||||
|
||||
go pipe(c1, c2, &inCount)
|
||||
go pipe(c2, c1, &outCount)
|
||||
wait.Wait()
|
||||
|
||||
Reference in New Issue
Block a user