From 258be8e67a57384cc1a38304cd77bf5ed0a34054 Mon Sep 17 00:00:00 2001 From: ffdfgdfg Date: Tue, 24 Dec 2019 22:00:17 +0800 Subject: [PATCH 1/6] add docker image latest tag --- build.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.sh b/build.sh index c994b98..70c4856 100755 --- a/build.sh +++ b/build.sh @@ -175,5 +175,5 @@ echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin export DOCKER_CLI_EXPERIMENTAL=enabled docker run --rm --privileged docker/binfmt:66f9012c56a8316f9244ffd7622d7c21c1f6f28d docker buildx create --use --name mybuilder -docker buildx build --tag ffdfgdfg/nps:$VERSION --output type=image,push=true --file Dockerfile.nps --platform=linux/amd64,linux/arm64,linux/386,linux/arm . -docker buildx build --tag ffdfgdfg/npc:$VERSION --output type=image,push=true --file Dockerfile.npc --platform=linux/amd64,linux/arm64,linux/386,linux/arm . +docker buildx build --tag ffdfgdfg/nps:$VERSION --tag ffdfgdfg/nps:latest --output type=image,push=true --file Dockerfile.nps --platform=linux/amd64,linux/arm64,linux/386,linux/arm . +docker buildx build --tag ffdfgdfg/npc:$VERSION --tag ffdfgdfg/npc:latest --output type=image,push=true --file Dockerfile.npc --platform=linux/amd64,linux/arm64,linux/386,linux/arm . From 076fc8b2e12cab6613bfa16133627c063ab17b79 Mon Sep 17 00:00:00 2001 From: ffdfgdfg Date: Fri, 27 Dec 2019 00:32:40 +0800 Subject: [PATCH 2/6] fine bandwidth calculation, fix slide window calculated size too large, fix #330 --- lib/common/netpackager.go | 9 +++++++ lib/common/pool.go | 9 ++++--- lib/mux/conn.go | 10 +++++--- lib/mux/mux.go | 52 ++++++++++++++++++++++++++++++++++++--- 4 files changed, 70 insertions(+), 10 deletions(-) diff --git a/lib/common/netpackager.go b/lib/common/netpackager.go index cf4b988..c6956f5 100644 --- a/lib/common/netpackager.go +++ b/lib/common/netpackager.go @@ -246,6 +246,15 @@ func (Self *MuxPackager) UnPack(reader io.Reader) (n uint16, err error) { return } +func (Self *MuxPackager) reset() { + Self.Id = 0 + Self.Flag = 0 + Self.Length = 0 + Self.Content = nil + Self.ReadLength = 0 + Self.Window = 0 +} + const ( ipV4 = 1 domainName = 3 diff --git a/lib/common/pool.go b/lib/common/pool.go index 3ed19dc..31931f9 100644 --- a/lib/common/pool.go +++ b/lib/common/pool.go @@ -93,18 +93,20 @@ type windowBufferPool struct { func (Self *windowBufferPool) New() { Self.pool = sync.Pool{ New: func() interface{} { - return make([]byte, PoolSizeWindow, PoolSizeWindow) + return make([]byte, PoolSizeWindow) }, } } func (Self *windowBufferPool) Get() (buf []byte) { buf = Self.pool.Get().([]byte) - return buf[:PoolSizeWindow] + buf = buf[:PoolSizeWindow] + return buf } func (Self *windowBufferPool) Put(x []byte) { - Self.pool.Put(x[:PoolSizeWindow]) // make buf to full + x = x[:0] // clean buf + Self.pool.Put(x) } type bufferPool struct { @@ -146,6 +148,7 @@ func (Self *muxPackagerPool) Get() *MuxPackager { } func (Self *muxPackagerPool) Put(pack *MuxPackager) { + pack.reset() Self.pool.Put(pack) } diff --git a/lib/mux/conn.go b/lib/mux/conn.go index 28ec1de..0f28acb 100644 --- a/lib/mux/conn.go +++ b/lib/mux/conn.go @@ -2,6 +2,7 @@ package mux import ( "errors" + "github.com/astaxie/beego/logs" "io" "math" "net" @@ -215,10 +216,10 @@ func (Self *ReceiveWindow) calcSize() { if n < common.MAXIMUM_SEGMENT_SIZE*10 { n = common.MAXIMUM_SEGMENT_SIZE * 10 } - bufLen := Self.bufQueue.Len() - if n < bufLen { - n = bufLen - } + //bufLen := Self.bufQueue.Len() + //if n < bufLen { + // n = bufLen + //} if n < Self.maxSize/2 { n = Self.maxSize / 2 } @@ -227,6 +228,7 @@ func (Self *ReceiveWindow) calcSize() { n = 2 * Self.maxSize } if n > (common.MAXIMUM_WINDOW_SIZE / uint32(conns)) { + logs.Warn("window too large", n) n = common.MAXIMUM_WINDOW_SIZE / uint32(conns) } // set the maximum size diff --git a/lib/mux/mux.go b/lib/mux/mux.go index ae9f335..ab02323 100644 --- a/lib/mux/mux.go +++ b/lib/mux/mux.go @@ -5,7 +5,9 @@ import ( "io" "math" "net" + "os" "sync/atomic" + "syscall" "time" "github.com/astaxie/beego/logs" @@ -35,13 +37,17 @@ func NewMux(c net.Conn, connType string) *Mux { //c.(*net.TCPConn).SetReadBuffer(0) //c.(*net.TCPConn).SetWriteBuffer(0) _ = c.SetDeadline(time.Time{}) + fd, err := getConnFd(c) + if err != nil { + logs.Warn(err) + } m := &Mux{ conn: c, connMap: NewConnMap(), id: 0, closeChan: make(chan struct{}, 1), newConnCh: make(chan *conn), - bw: new(bandwidth), + bw: NewBandwidth(fd), IsClose: false, connType: connType, pingCh: make(chan []byte), @@ -58,6 +64,26 @@ func NewMux(c net.Conn, connType string) *Mux { return m } +func getConnFd(c net.Conn) (fd *os.File, err error) { + switch c.(type) { + case *net.TCPConn: + fd, err = c.(*net.TCPConn).File() + if err != nil { + return + } + return + case *net.UDPConn: + fd, err = c.(*net.UDPConn).File() + if err != nil { + return + } + return + default: + err = errors.New("mux:unknown conn type, only tcp or kcp") + return + } +} + func (s *Mux) NewConn() (*conn, error) { if s.IsClose { return nil, errors.New("the mux has closed") @@ -392,13 +418,19 @@ type bandwidth struct { readStart time.Time lastReadStart time.Time bufLength uint32 + fd *os.File + calcThreshold uint32 +} + +func NewBandwidth(fd *os.File) *bandwidth { + return &bandwidth{fd: fd} } func (Self *bandwidth) StartRead() { if Self.readStart.IsZero() { Self.readStart = time.Now() } - if Self.bufLength >= common.MAXIMUM_SEGMENT_SIZE*300 { + if Self.bufLength >= Self.calcThreshold { Self.lastReadStart, Self.readStart = Self.readStart, time.Now() Self.calcBandWidth() } @@ -410,7 +442,21 @@ func (Self *bandwidth) SetCopySize(n uint16) { func (Self *bandwidth) calcBandWidth() { t := Self.readStart.Sub(Self.lastReadStart) - atomic.StoreUint64(&Self.readBandwidth, math.Float64bits(float64(Self.bufLength)/t.Seconds())) + bufferSize, err := syscall.GetsockoptInt(int(Self.fd.Fd()), syscall.SOL_SOCKET, syscall.SO_RCVBUF) + //logs.Warn(bufferSize) + if err != nil { + logs.Warn(err) + Self.bufLength = 0 + return + } + if Self.bufLength >= uint32(bufferSize) { + atomic.StoreUint64(&Self.readBandwidth, math.Float64bits(float64(Self.bufLength)/t.Seconds())) + // calculate the hole socket buffer, the time meaning to fill the buffer + //logs.Warn(Self.Get()) + } else { + Self.calcThreshold = uint32(bufferSize) + } + // socket buffer size is bigger than bufLength, so we don't calculate it Self.bufLength = 0 } From 0b5c903d89c39216979452d30fc50cb93861e67d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B2=B3?= Date: Fri, 27 Dec 2019 10:59:56 +0800 Subject: [PATCH 3/6] fix update --- lib/install/install.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/install/install.go b/lib/install/install.go index 66bb28b..6156e3f 100644 --- a/lib/install/install.go +++ b/lib/install/install.go @@ -91,11 +91,13 @@ func copyStaticFile(srcPath, bin string) string { if _, err := copyFile(filepath.Join(srcPath, bin), "/usr/local/bin/"+bin); err != nil { log.Fatalln(err) } else { - copyFile(filepath.Join(srcPath, "nps"), "/usr/local/bin/"+bin+"-update") + copyFile(filepath.Join(srcPath, bin), "/usr/local/bin/"+bin+"-update") + chMod("/usr/local/bin/"+bin+"-update", 0755) binPath = "/usr/local/bin/" + bin } } else { - copyFile(filepath.Join(srcPath, "nps"), "/usr/bin/"+bin+"-update") + copyFile(filepath.Join(srcPath, bin), "/usr/bin/"+bin+"-update") + chMod("/usr/bin/"+bin+"-update", 0755) binPath = "/usr/bin/" + bin } } else { From 4de24ff13a0fd2523cd2e598bea44f756f0219d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B2=B3?= Date: Fri, 27 Dec 2019 11:12:46 +0800 Subject: [PATCH 4/6] update docs --- docs/_coverpage.md | 2 +- docs/nps_use.md | 4 +++- docs/use.md | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/_coverpage.md b/docs/_coverpage.md index cb361da..68e0cc3 100644 --- a/docs/_coverpage.md +++ b/docs/_coverpage.md @@ -1,6 +1,6 @@ ![logo](logo.svg) -# NPS 0.25.2 +# NPS 0.25.4 > 一款轻量级、高性能、功能强大的内网穿透代理服务器 diff --git a/docs/nps_use.md b/docs/nps_use.md index 4058869..2681e8b 100644 --- a/docs/nps_use.md +++ b/docs/nps_use.md @@ -40,4 +40,6 @@ nps-update.exe update ``` -更新完成后,执行执行`sudo nps start`或者`nps.exe start`重新运行即可完成升级 \ No newline at end of file +更新完成后,执行执行`sudo nps start`或者`nps.exe start`重新运行即可完成升级 + +如果无法更新成功,可以直接自行下载releases压缩包然后覆盖原有的nps二进制文件和web目录 diff --git a/docs/use.md b/docs/use.md index 00547b2..5f775ab 100644 --- a/docs/use.md +++ b/docs/use.md @@ -36,6 +36,8 @@ npc-update.exe update 更新完成后,执行执行`sudo npc start`或者`npc.exe start`重新运行即可完成升级 +如果无法更新成功,可以直接自行下载releases压缩包然后覆盖原有的npc二进制文件 + ## 配置文件模式 此模式使用nps的公钥或者客户端私钥验证,各种配置在客户端完成,同时服务端web也可以进行管理 ``` From 5e1b0be81c62559c9beecee03f9cc369d8a80105 Mon Sep 17 00:00:00 2001 From: ffdfgdfg Date: Fri, 27 Dec 2019 13:02:28 +0800 Subject: [PATCH 5/6] fix mux send window size counting --- lib/mux/conn.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/mux/conn.go b/lib/mux/conn.go index 0f28acb..52fb642 100644 --- a/lib/mux/conn.go +++ b/lib/mux/conn.go @@ -446,7 +446,20 @@ func (Self *SendWindow) allow() { } func (Self *SendWindow) sent(sentSize uint32) { - atomic.AddUint64(&Self.remainingWait, ^(uint64(sentSize)<= sentSize { + atomic.AddUint64(&Self.remainingWait, ^(uint64(sentSize)< Date: Fri, 27 Dec 2019 13:14:49 +0800 Subject: [PATCH 6/6] bump version to 0.25.4 --- build.sh | 2 +- gui/npc/AndroidManifest.xml | 2 +- lib/version/version.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build.sh b/build.sh index 70c4856..5059486 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,5 @@ #/bash/sh -export VERSION=0.25.3 +export VERSION=0.25.4 sudo apt-get install gcc-mingw-w64-i686 env GOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc go build -ldflags "-s -w -extldflags -static -extldflags -static" -buildmode=c-shared -o npc_sdk.dll cmd/npc/sdk.go diff --git a/gui/npc/AndroidManifest.xml b/gui/npc/AndroidManifest.xml index 463634d..901a508 100755 --- a/gui/npc/AndroidManifest.xml +++ b/gui/npc/AndroidManifest.xml @@ -2,7 +2,7 @@ diff --git a/lib/version/version.go b/lib/version/version.go index 78dbbef..c2f1489 100644 --- a/lib/version/version.go +++ b/lib/version/version.go @@ -1,6 +1,6 @@ package version -const VERSION = "0.25.3" +const VERSION = "0.25.4" // Compulsory minimum version, Minimum downward compatibility to this version func GetVersion() string {