浏览代码

minor bug fix, docker

ffdfgdfg 5 年之前
父节点
当前提交
78ebeba1bb
共有 6 个文件被更改,包括 42 次插入9 次删除
  1. 10 0
      Dockerfile.npc
  2. 11 0
      Dockerfile.nps
  3. 6 1
      README.md
  4. 7 0
      lib/conn/listener.go
  5. 7 7
      lib/mux/mux.go
  6. 1 1
      lib/mux/queue.go

+ 10 - 0
Dockerfile.npc

@@ -0,0 +1,10 @@
+FROM golang as builder
+WORKDIR /go/src/github.com/cnlh/nps
+COPY . .
+RUN go get -d -v ./... 
+RUN CGO_ENABLED=0 go build -ldflags="-w -s -extldflags -static" ./cmd/npc/npc.go
+
+FROM scratch
+COPY --from=builder /go/src/github.com/cnlh/nps/npc /
+VOLUME /conf
+ENTRYPOINT ["/npc"]

+ 11 - 0
Dockerfile.nps

@@ -0,0 +1,11 @@
+FROM golang as builder
+WORKDIR /go/src/github.com/cnlh/nps
+COPY . .
+RUN go get -d -v ./... 
+RUN CGO_ENABLED=0 go build -ldflags="-w -s -extldflags -static" ./cmd/nps/nps.go
+
+FROM scratch
+COPY --from=builder /go/src/github.com/cnlh/nps/nps /
+COPY --from=builder /go/src/github.com/cnlh/nps/web /web
+VOLUME /conf
+CMD ["/nps"]

+ 6 - 1
README.md

@@ -26,6 +26,7 @@ nps是一款轻量级、高性能、功能强大的**内网穿透**代理服务
 * [安装](#安装)
     * [编译安装](#源码安装)
     * [release安装](#release安装)
+    * [docker安装](#docker安装)
 * [使用示例(以web主控模式为主)](#使用示例)
     * [统一准备工作](#统一准备工作(必做))
     * [http|https域名解析](#域名解析)
@@ -121,7 +122,7 @@ nps是一款轻量级、高性能、功能强大的**内网穿透**代理服务
 
 ## 安装
 
-### releases安装
+### release安装
 > [releases](https://github.com/cnlh/nps/releases)
 
 下载对应的系统版本即可,服务端和客户端是单独的
@@ -134,6 +135,10 @@ nps是一款轻量级、高性能、功能强大的**内网穿透**代理服务
 
 > go build cmd/npc/npc.go
 
+### docker安装
+> [server](https://hub.docker.com/r/ffdfgdfg/nps)
+> [client](https://hub.docker.com/r/ffdfgdfg/npc)
+
 ## 使用示例
 
 ### 统一准备工作(必做)

+ 7 - 0
lib/conn/listener.go

@@ -43,9 +43,16 @@ func Accept(l net.Listener, f func(c net.Conn)) {
 			if strings.Contains(err.Error(), "use of closed network connection") {
 				break
 			}
+			if strings.Contains(err.Error(), "the mux has closed") {
+				break
+			}
 			logs.Warn(err)
 			continue
 		}
+		if c == nil {
+			logs.Warn("nil connection")
+			break
+		}
 		go f(c)
 	}
 }

+ 7 - 7
lib/mux/mux.go

@@ -21,7 +21,7 @@ type Mux struct {
 	id            int32
 	closeChan     chan struct{}
 	IsClose       bool
-	pingOk        int
+	pingOk        uint32
 	counter       *latencyCounter
 	bw            *bandwidth
 	pingCh        chan []byte
@@ -101,7 +101,7 @@ func (s *Mux) sendInfo(flag uint8, id int32, data ...interface{}) {
 	err = pack.NewPac(flag, id, data...)
 	if err != nil {
 		common.MuxPack.Put(pack)
-		logs.Error("mux: new pack err")
+		logs.Error("mux: new pack err", err)
 		s.Close()
 		return
 	}
@@ -191,12 +191,12 @@ func (s *Mux) ping() {
 			now, _ := time.Now().UTC().MarshalText()
 			s.sendInfo(common.MUX_PING_FLAG, common.MUX_PING, now)
 			atomic.AddUint32(&s.pingCheckTime, 1)
-			if s.pingOk > 10 && s.connType == "kcp" {
+			if atomic.LoadUint32(&s.pingOk) > 10 && s.connType == "kcp" {
 				logs.Error("mux: kcp ping err")
 				s.Close()
 				break
 			}
-			s.pingOk++
+			atomic.AddUint32(&s.pingOk, 1)
 		}
 	}()
 }
@@ -256,12 +256,12 @@ func (s *Mux) readSession() {
 			pack = common.MuxPack.Get()
 			s.bw.StartRead()
 			if l, err = pack.UnPack(s.conn); err != nil {
-				logs.Error("mux: read session unpack from connection err")
+				logs.Error("mux: read session unpack from connection err", err)
 				s.Close()
 				break
 			}
 			s.bw.SetCopySize(l)
-			s.pingOk = 0
+			atomic.StoreUint32(&s.pingOk, 0)
 			switch pack.Flag {
 			case common.MUX_NEW_CONN: //new connection
 				connection := NewConn(pack.Id, s)
@@ -282,7 +282,7 @@ func (s *Mux) readSession() {
 				case common.MUX_NEW_MSG, common.MUX_NEW_MSG_PART: //new msg from remote connection
 					err = s.newMsg(connection, pack)
 					if err != nil {
-						logs.Error("mux: read session connection new msg err")
+						logs.Error("mux: read session connection new msg err", err)
 						connection.Close()
 					}
 					continue

+ 1 - 1
lib/mux/queue.go

@@ -209,10 +209,10 @@ func NewListElement(buf []byte, l uint16, part bool) (element *common.ListElemen
 }
 
 type ReceiveWindowQueue struct {
+	lengthWait uint64
 	chain      *bufChain
 	stopOp     chan struct{}
 	readOp     chan struct{}
-	lengthWait uint64
 	timeout    time.Time
 }