mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-02 03:16:53 +00:00
Merge remote-tracking branch 'remotes/origin/master' into mux_test
# Conflicts: # lib/common/util.go # lib/conn/conn.go # lib/conn/snappy.go # lib/mux/conn.go # lib/mux/mux.go # lib/mux/mux_test.go # lib/mux/queue.go # lib/pool/pool.go # server/proxy/p2p.go # server/proxy/udp.go
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"github.com/cnlh/nps/lib/crypt"
|
||||
"html/template"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
@@ -15,6 +14,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/cnlh/nps/lib/crypt"
|
||||
)
|
||||
|
||||
//Get the corresponding IP address through domain name
|
||||
|
@@ -3,10 +3,11 @@ package config
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
)
|
||||
|
||||
type CommonConfig struct {
|
||||
|
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/cnlh/nps/lib/crypt"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
"github.com/cnlh/nps/lib/mux"
|
||||
"github.com/cnlh/nps/lib/pool"
|
||||
"github.com/cnlh/nps/lib/rate"
|
||||
"github.com/cnlh/nps/vender/github.com/xtaci/kcp"
|
||||
"io"
|
||||
@@ -20,6 +21,13 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/crypt"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
"github.com/cnlh/nps/lib/mux"
|
||||
"github.com/cnlh/nps/lib/rate"
|
||||
"github.com/xtaci/kcp-go"
|
||||
)
|
||||
|
||||
type Conn struct {
|
||||
|
@@ -1,10 +1,11 @@
|
||||
package conn
|
||||
|
||||
import (
|
||||
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/vender/github.com/xtaci/kcp"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/xtaci/kcp-go"
|
||||
)
|
||||
|
||||
func NewTcpListenerAndProcess(addr string, f func(c net.Conn), listener *net.Listener) error {
|
||||
|
@@ -1,9 +1,10 @@
|
||||
package conn
|
||||
|
||||
import (
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/vender/github.com/golang/snappy"
|
||||
"io"
|
||||
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/golang/snappy"
|
||||
)
|
||||
|
||||
type SnappyConn struct {
|
||||
|
@@ -2,9 +2,10 @@ package crypt
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/astaxie/beego/logs"
|
||||
)
|
||||
|
||||
var pemPath, keyPath string
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
@@ -9,6 +8,8 @@ import (
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
)
|
||||
|
||||
func InitDaemon(f string, runPath string, pidPath string) {
|
||||
|
@@ -3,12 +3,13 @@
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/vender/github.com/astaxie/beego"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
@@ -3,14 +3,15 @@ package file
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/crypt"
|
||||
"github.com/cnlh/nps/lib/rate"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/crypt"
|
||||
"github.com/cnlh/nps/lib/rate"
|
||||
)
|
||||
|
||||
type DbUtils struct {
|
||||
|
@@ -3,13 +3,14 @@ package file
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/rate"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/rate"
|
||||
)
|
||||
|
||||
func NewJsonDb(runPath string) *JsonDb {
|
||||
|
@@ -1,12 +1,13 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"github.com/cnlh/nps/lib/rate"
|
||||
"github.com/pkg/errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/cnlh/nps/lib/rate"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type Flow struct {
|
||||
|
@@ -3,12 +3,13 @@ package install
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
)
|
||||
|
||||
func InstallNps() {
|
||||
|
@@ -2,12 +2,12 @@ package mux
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
)
|
||||
|
||||
type conn struct {
|
||||
@@ -47,7 +47,6 @@ func NewConn(connId int32, mux *Mux) *conn {
|
||||
}
|
||||
|
||||
func (s *conn) Read(buf []byte) (n int, err error) {
|
||||
//logs.Warn("starting conn read", s.connId)
|
||||
if s.isClose || buf == nil {
|
||||
return 0, errors.New("the conn has closed")
|
||||
}
|
||||
@@ -66,7 +65,6 @@ func (s *conn) Read(buf []byte) (n int, err error) {
|
||||
n = <-s.readCh.nCh
|
||||
err = <-s.readCh.errCh
|
||||
}
|
||||
//logs.Warn("read window finish conn read n err buf", n, err, string(buf[:15]), s.connId)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -78,7 +76,6 @@ func (s *conn) readWindow(buf []byte, nCh chan int, errCh chan error) {
|
||||
// window.Read may be invoked before window.Write, and WindowFull flag change to true
|
||||
// so make sure that receiveWindow is free some space
|
||||
s.receiveWindow.WindowFull = false
|
||||
logs.Warn("defer send mux msg send ok size", s.receiveWindow.Size())
|
||||
s.mux.sendInfo(common.MUX_MSG_SEND_OK, s.connId, s.receiveWindow.Size())
|
||||
// acknowledge other side, have empty some receive window space
|
||||
}
|
||||
@@ -88,18 +85,14 @@ func (s *conn) readWindow(buf []byte, nCh chan int, errCh chan error) {
|
||||
}
|
||||
|
||||
func (s *conn) Write(buf []byte) (n int, err error) {
|
||||
//logs.Warn("write starting", s.connId)
|
||||
//defer logs.Warn("write end ", s.connId)
|
||||
if s.isClose {
|
||||
return 0, errors.New("the conn has closed")
|
||||
}
|
||||
if s.closeFlag {
|
||||
//logs.Warn("conn close by write ", s.connId)
|
||||
//s.Close()
|
||||
return 0, errors.New("io: write on closed conn")
|
||||
}
|
||||
s.sendWindow.SetSendBuf(buf) // set the buf to send window
|
||||
//logs.Warn("write set send buf success")
|
||||
go s.write(s.writeCh.nCh, s.writeCh.errCh)
|
||||
// waiting for send to other side or timeout
|
||||
if t := s.writeTimeOut.Sub(time.Now()); t > 0 {
|
||||
@@ -115,7 +108,6 @@ func (s *conn) Write(buf []byte) (n int, err error) {
|
||||
n = <-s.writeCh.nCh
|
||||
err = <-s.writeCh.errCh
|
||||
}
|
||||
//logs.Warn("write window finish n err buf id", n, err, string(buf[:15]), s.connId)
|
||||
return
|
||||
}
|
||||
func (s *conn) write(nCh chan int, errCh chan error) {
|
||||
@@ -133,7 +125,6 @@ func (s *conn) write(nCh chan int, errCh chan error) {
|
||||
break
|
||||
}
|
||||
n += len(buf)
|
||||
//logs.Warn("send window buf len", len(buf))
|
||||
s.mux.sendInfo(common.MUX_NEW_MSG, s.connId, buf)
|
||||
// send to other side, not send nil data to other side
|
||||
}
|
||||
@@ -150,7 +141,6 @@ func (s *conn) closeProcess() {
|
||||
s.isClose = true
|
||||
s.mux.connMap.Delete(s.connId)
|
||||
if !s.mux.IsClose {
|
||||
//logs.Warn("conn send close", s.connId)
|
||||
// if server or user close the conn while reading, will get a io.EOF
|
||||
// and this Close method will be invoke, send this signal to close other side
|
||||
s.mux.sendInfo(common.MUX_CONN_CLOSE, s.connId, nil)
|
||||
@@ -356,7 +346,6 @@ func (Self *window) Read(p []byte) (n int, err error) {
|
||||
|
||||
func (Self *window) WriteTo() (p []byte, err error) {
|
||||
if Self.closeOp {
|
||||
//logs.Warn("window write to closed")
|
||||
return nil, errors.New("conn.writeWindow: window closed")
|
||||
}
|
||||
if Self.len() == 0 {
|
||||
|
@@ -3,13 +3,13 @@ package mux
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
|
||||
"math"
|
||||
"net"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
)
|
||||
|
||||
type Mux struct {
|
||||
@@ -55,7 +55,6 @@ func (s *Mux) NewConn() (*conn, error) {
|
||||
//it must be set before send
|
||||
s.connMap.Set(conn.connId, conn)
|
||||
s.sendInfo(common.MUX_NEW_CONN, conn.connId, nil)
|
||||
//logs.Warn("send mux new conn ", conn.connId)
|
||||
//set a timer timeout 30 second
|
||||
timer := time.NewTimer(time.Minute * 2)
|
||||
defer timer.Stop()
|
||||
@@ -85,38 +84,13 @@ func (s *Mux) Addr() net.Addr {
|
||||
|
||||
func (s *Mux) sendInfo(flag uint8, id int32, data interface{}) {
|
||||
var err error
|
||||
if flag == common.MUX_NEW_MSG {
|
||||
if len(data.([]byte)) == 0 {
|
||||
//logs.Warn("send info content is nil")
|
||||
}
|
||||
}
|
||||
//buf := common.BuffPool.Get()
|
||||
//defer pool.BuffPool.Put(buf)
|
||||
pack := common.MuxPack.Get()
|
||||
|
||||
err = pack.NewPac(flag, id, data)
|
||||
if err != nil {
|
||||
//logs.Warn("new pack err", err)
|
||||
common.MuxPack.Put(pack)
|
||||
return
|
||||
}
|
||||
s.writeQueue.Push(pack)
|
||||
//err = pack.Pack(buf)
|
||||
//if err != nil {
|
||||
// //logs.Warn("pack err", err)
|
||||
// common.BuffPool.Put(buf)
|
||||
// return
|
||||
//}
|
||||
//if pack.Flag == common.MUX_NEW_CONN {
|
||||
// //logs.Warn("sendinfo mux new conn, insert to write queue", pack.Id)
|
||||
//}
|
||||
//s.writeQueue <- buf
|
||||
//_, err = buf.WriteTo(s.conn)
|
||||
//if err != nil {
|
||||
// s.Close()
|
||||
// logs.Warn("write err, close mux", err)
|
||||
//}
|
||||
//common.BuffPool.Put(buf)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -175,7 +149,6 @@ func (s *Mux) ping() {
|
||||
if (math.MaxInt32 - s.id) < 10000 {
|
||||
s.id = 0
|
||||
}
|
||||
//logs.Warn("send mux ping")
|
||||
s.sendInfo(common.MUX_PING_FLAG, common.MUX_PING, nil)
|
||||
if s.pingOk > 10 && s.connType == "kcp" {
|
||||
s.Close()
|
||||
@@ -197,29 +170,18 @@ func (s *Mux) readSession() {
|
||||
if pack.UnPack(s.conn) != nil {
|
||||
break
|
||||
}
|
||||
if pack.Flag != 0 && pack.Flag != 7 {
|
||||
if pack.Length > 10 {
|
||||
//logs.Warn(pack.Flag, pack.Id, pack.Length, string(pack.Content[:10]))
|
||||
}
|
||||
}
|
||||
if pack.Flag == common.MUX_NEW_CONN {
|
||||
//logs.Warn("unpack mux new connection", pack.Id)
|
||||
}
|
||||
s.pingOk = 0
|
||||
switch pack.Flag {
|
||||
case common.MUX_NEW_CONN: //new connection
|
||||
//logs.Warn("rec mux new connection", pack.Id)
|
||||
connection := NewConn(pack.Id, s)
|
||||
connection := NewConn(pack.Id, s)
|
||||
s.connMap.Set(pack.Id, connection) //it has been set before send ok
|
||||
go func(connection *conn) {
|
||||
connection.sendWindow.SetAllowSize(512) // set the initial receive window
|
||||
}(connection)
|
||||
s.newConnCh <- connection
|
||||
s.sendInfo(common.MUX_NEW_CONN_OK, connection.connId, nil)
|
||||
//logs.Warn("send mux new connection ok", connection.connId)
|
||||
continue
|
||||
case common.MUX_PING_FLAG: //ping
|
||||
//logs.Warn("send mux ping return")
|
||||
go s.sendInfo(common.MUX_PING_RETURN, common.MUX_PING, nil)
|
||||
continue
|
||||
case common.MUX_PING_RETURN:
|
||||
@@ -230,11 +192,9 @@ func (s *Mux) readSession() {
|
||||
case common.MUX_NEW_MSG: //new msg from remote connection
|
||||
//insert wait queue
|
||||
if connection.isClose {
|
||||
logs.Warn("rec mux new msg closed", pack.Id, string(pack.Content[0:15]))
|
||||
continue
|
||||
}
|
||||
connection.receiveWindow.WriteWg.Add(1)
|
||||
//logs.Warn("rec mux new msg ", connection.connId, string(pack.Content[0:15]))
|
||||
go func(connection *conn, content []byte) { // do not block read session
|
||||
_, err := connection.receiveWindow.Write(content)
|
||||
if err != nil {
|
||||
@@ -246,49 +206,38 @@ func (s *Mux) readSession() {
|
||||
connection.receiveWindow.WindowFull = true
|
||||
}
|
||||
s.sendInfo(common.MUX_MSG_SEND_OK, connection.connId, size)
|
||||
//logs.Warn("send mux new msg ok", connection.connId, size)
|
||||
connection.receiveWindow.WriteWg.Done()
|
||||
}(connection, pack.Content)
|
||||
continue
|
||||
case common.MUX_NEW_CONN_OK: //connection ok
|
||||
//logs.Warn("rec mux new connection ok ", pack.Id)
|
||||
connection.connStatusOkCh <- struct{}{}
|
||||
go connection.sendWindow.SetAllowSize(512)
|
||||
// set the initial receive window both side
|
||||
continue
|
||||
case common.MUX_NEW_CONN_Fail:
|
||||
//logs.Warn("rec mux new connection fail", pack.Id)
|
||||
connection.connStatusFailCh <- struct{}{}
|
||||
continue
|
||||
case common.MUX_MSG_SEND_OK:
|
||||
if connection.isClose {
|
||||
//logs.Warn("rec mux msg send ok id window closed!", pack.Id, pack.Window)
|
||||
continue
|
||||
}
|
||||
//logs.Warn("rec mux msg send ok id window", pack.Id, pack.Window)
|
||||
go connection.sendWindow.SetAllowSize(pack.Window)
|
||||
continue
|
||||
case common.MUX_CONN_CLOSE: //close the connection
|
||||
//logs.Warn("rec mux connection close", pack.Id)
|
||||
s.connMap.Delete(pack.Id)
|
||||
connection.closeFlag = true
|
||||
go func(connection *conn) {
|
||||
//logs.Warn("receive mux connection close, wg waiting", connection.connId)
|
||||
connection.receiveWindow.WriteWg.Wait()
|
||||
//logs.Warn("receive mux connection close, wg waited", connection.connId)
|
||||
connection.receiveWindow.WriteEndOp <- struct{}{} // close signal to receive window
|
||||
//logs.Warn("receive mux connection close, finish", connection.connId)
|
||||
}(connection)
|
||||
continue
|
||||
}
|
||||
} else if pack.Flag == common.MUX_CONN_CLOSE {
|
||||
//logs.Warn("rec mux connection close no id ", pack.Id)
|
||||
continue
|
||||
}
|
||||
common.MuxPack.Put(pack)
|
||||
}
|
||||
common.MuxPack.Put(pack)
|
||||
//logs.Warn("read session put pack ", pack.Id)
|
||||
s.Close()
|
||||
}()
|
||||
select {
|
||||
|
@@ -1,14 +1,17 @@
|
||||
package mux
|
||||
|
||||
import (
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/pool"
|
||||
)
|
||||
|
||||
var conn1 net.Conn
|
||||
|
@@ -5,15 +5,16 @@ package mux
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
|
||||
"github.com/pkg/errors"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@@ -1,9 +1,10 @@
|
||||
package mux
|
||||
|
||||
import (
|
||||
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/logs"
|
||||
)
|
||||
|
||||
func TestPortMux_Close(t *testing.T) {
|
||||
@@ -11,7 +12,7 @@ func TestPortMux_Close(t *testing.T) {
|
||||
logs.EnableFuncCallDepth(true)
|
||||
logs.SetLogFuncCallDepth(3)
|
||||
|
||||
pMux := NewPortMux(8888,"Ds")
|
||||
pMux := NewPortMux(8888, "Ds")
|
||||
go func() {
|
||||
if pMux.Start() != nil {
|
||||
logs.Warn("Error")
|
||||
|
Reference in New Issue
Block a user