mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-03 04:26:53 +00:00
merge master
This commit is contained in:
@@ -5,9 +5,9 @@ import (
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"ehang.io/nps/lib/mux"
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/lib/mux"
|
||||
)
|
||||
|
||||
var pMux *mux.PortMux
|
||||
|
@@ -6,11 +6,11 @@ import (
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"ehang.io/nps/bridge"
|
||||
"ehang.io/nps/lib/common"
|
||||
"ehang.io/nps/lib/conn"
|
||||
"ehang.io/nps/lib/file"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/bridge"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/conn"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
|
@@ -13,13 +13,13 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"ehang.io/nps/bridge"
|
||||
"ehang.io/nps/lib/cache"
|
||||
"ehang.io/nps/lib/common"
|
||||
"ehang.io/nps/lib/conn"
|
||||
"ehang.io/nps/lib/file"
|
||||
"ehang.io/nps/server/connection"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/bridge"
|
||||
"github.com/cnlh/nps/lib/cache"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/conn"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
"github.com/cnlh/nps/server/connection"
|
||||
)
|
||||
|
||||
type httpServer struct {
|
||||
@@ -171,11 +171,11 @@ reset:
|
||||
}
|
||||
}()
|
||||
for {
|
||||
if resp, err := http.ReadResponse(bufio.NewReader(connClient), r); err != nil {
|
||||
if resp, err := http.ReadResponse(bufio.NewReader(connClient), r); err != nil || resp == nil {
|
||||
return
|
||||
} else {
|
||||
//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 && r.URL != nil && strings.Contains(r.URL.Path, ".") {
|
||||
b, err := httputil.DumpResponse(resp, true)
|
||||
if err != nil {
|
||||
return
|
||||
|
@@ -6,13 +6,13 @@ import (
|
||||
"net/url"
|
||||
"sync"
|
||||
|
||||
"ehang.io/nps/lib/cache"
|
||||
"ehang.io/nps/lib/common"
|
||||
"ehang.io/nps/lib/conn"
|
||||
"ehang.io/nps/lib/crypt"
|
||||
"ehang.io/nps/lib/file"
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/lib/cache"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/conn"
|
||||
"github.com/cnlh/nps/lib/crypt"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
|
@@ -5,8 +5,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ehang.io/nps/lib/common"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
)
|
||||
|
||||
type P2PServer struct {
|
||||
|
@@ -7,10 +7,10 @@ import (
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"ehang.io/nps/lib/common"
|
||||
"ehang.io/nps/lib/conn"
|
||||
"ehang.io/nps/lib/file"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/conn"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -154,27 +154,129 @@ func (s *Sock5ModeServer) handleConnect(c net.Conn) {
|
||||
// passive mode
|
||||
func (s *Sock5ModeServer) handleBind(c net.Conn) {
|
||||
}
|
||||
func (s *Sock5ModeServer) sendUdpReply(writeConn net.Conn, c net.Conn, rep uint8, serverIp string) {
|
||||
reply := []byte{
|
||||
5,
|
||||
rep,
|
||||
0,
|
||||
1,
|
||||
}
|
||||
localHost, localPort, _ := net.SplitHostPort(c.LocalAddr().String())
|
||||
localHost = serverIp
|
||||
ipBytes := net.ParseIP(localHost).To4()
|
||||
nPort, _ := strconv.Atoi(localPort)
|
||||
reply = append(reply, ipBytes...)
|
||||
portBytes := make([]byte, 2)
|
||||
binary.BigEndian.PutUint16(portBytes, uint16(nPort))
|
||||
reply = append(reply, portBytes...)
|
||||
writeConn.Write(reply)
|
||||
|
||||
}
|
||||
|
||||
//udp
|
||||
func (s *Sock5ModeServer) handleUDP(c net.Conn) {
|
||||
/*
|
||||
+----+------+------+----------+----------+----------+
|
||||
|RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
|
||||
+----+------+------+----------+----------+----------+
|
||||
| 2 | 1 | 1 | Variable | 2 | Variable |
|
||||
+----+------+------+----------+----------+----------+
|
||||
*/
|
||||
buf := make([]byte, 3)
|
||||
c.Read(buf)
|
||||
// relay udp datagram silently, without any notification to the requesting client
|
||||
if buf[2] != 0 {
|
||||
// does not support fragmentation, drop it
|
||||
logs.Warn("does not support fragmentation, drop")
|
||||
dummy := make([]byte, maxUDPPacketSize)
|
||||
c.Read(dummy)
|
||||
defer c.Close()
|
||||
addrType := make([]byte, 1)
|
||||
c.Read(addrType)
|
||||
var host string
|
||||
switch addrType[0] {
|
||||
case ipV4:
|
||||
ipv4 := make(net.IP, net.IPv4len)
|
||||
c.Read(ipv4)
|
||||
host = ipv4.String()
|
||||
case ipV6:
|
||||
ipv6 := make(net.IP, net.IPv6len)
|
||||
c.Read(ipv6)
|
||||
host = ipv6.String()
|
||||
case domainName:
|
||||
var domainLen uint8
|
||||
binary.Read(c, binary.BigEndian, &domainLen)
|
||||
domain := make([]byte, domainLen)
|
||||
c.Read(domain)
|
||||
host = string(domain)
|
||||
default:
|
||||
s.sendReply(c, addrTypeNotSupported)
|
||||
return
|
||||
}
|
||||
//读取端口
|
||||
var port uint16
|
||||
binary.Read(c, binary.BigEndian, &port)
|
||||
logs.Warn(host, string(port))
|
||||
replyAddr, err := net.ResolveUDPAddr("udp", s.task.ServerIp+":0")
|
||||
if err != nil {
|
||||
logs.Error("build local reply addr error", err)
|
||||
return
|
||||
}
|
||||
reply, err := net.ListenUDP("udp", replyAddr)
|
||||
if err != nil {
|
||||
s.sendReply(c, addrTypeNotSupported)
|
||||
logs.Error("listen local reply udp port error")
|
||||
return
|
||||
}
|
||||
// reply the local addr
|
||||
s.sendUdpReply(c, reply, succeeded, common.GetServerIpByClientIp(c.RemoteAddr().(*net.TCPAddr).IP))
|
||||
defer reply.Close()
|
||||
// new a tunnel to client
|
||||
link := conn.NewLink("udp5", "", s.task.Client.Cnf.Crypt, s.task.Client.Cnf.Compress, c.RemoteAddr().String(), false)
|
||||
target, err := s.bridge.SendLinkInfo(s.task.Client.Id, link, s.task)
|
||||
if err != nil {
|
||||
logs.Warn("get connection from client id %d error %s", s.task.Client.Id, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
s.doConnect(c, associateMethod)
|
||||
var clientAddr net.Addr
|
||||
// copy buffer
|
||||
go func() {
|
||||
b := common.BufPoolUdp.Get().([]byte)
|
||||
defer common.BufPoolUdp.Put(b)
|
||||
defer c.Close()
|
||||
|
||||
for {
|
||||
n, laddr, err := reply.ReadFrom(b)
|
||||
if err != nil {
|
||||
logs.Error("read data from %s err %s", reply.LocalAddr().String(), err.Error())
|
||||
return
|
||||
}
|
||||
if clientAddr == nil {
|
||||
clientAddr = laddr
|
||||
}
|
||||
if _, err := target.Write(b[:n]); err != nil {
|
||||
logs.Error("write data to client error", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
var l int32
|
||||
b := common.BufPoolUdp.Get().([]byte)
|
||||
defer common.BufPoolUdp.Put(b)
|
||||
defer c.Close()
|
||||
for {
|
||||
if err := binary.Read(target, binary.LittleEndian, &l); err != nil || l >= common.PoolSizeUdp || l <= 0 {
|
||||
logs.Warn("read len bytes error", err.Error())
|
||||
return
|
||||
}
|
||||
binary.Read(target, binary.LittleEndian, b[:l])
|
||||
if err != nil {
|
||||
logs.Warn("read data form client error", err.Error())
|
||||
return
|
||||
}
|
||||
if _, err := reply.WriteTo(b[:l], clientAddr); err != nil {
|
||||
logs.Warn("write data to user ", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
b := common.BufPoolUdp.Get().([]byte)
|
||||
defer common.BufPoolUdp.Put(b)
|
||||
for {
|
||||
_, err := c.Read(b)
|
||||
if err != nil {
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//new conn
|
||||
|
@@ -7,13 +7,13 @@ import (
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"ehang.io/nps/bridge"
|
||||
"ehang.io/nps/lib/common"
|
||||
"ehang.io/nps/lib/conn"
|
||||
"ehang.io/nps/lib/file"
|
||||
"ehang.io/nps/server/connection"
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/bridge"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/conn"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
"github.com/cnlh/nps/server/connection"
|
||||
)
|
||||
|
||||
type TunnelModeServer struct {
|
||||
@@ -63,15 +63,23 @@ func (s *WebServer) Start() error {
|
||||
<-stop
|
||||
}
|
||||
beego.BConfig.WebConfig.Session.SessionOn = true
|
||||
beego.SetStaticPath("/static", filepath.Join(common.GetRunPath(), "web", "static"))
|
||||
beego.SetStaticPath(beego.AppConfig.String("web_base_url")+"/static", filepath.Join(common.GetRunPath(), "web", "static"))
|
||||
beego.SetViewsPath(filepath.Join(common.GetRunPath(), "web", "views"))
|
||||
if l, err := connection.GetWebManagerListener(); err == nil {
|
||||
err := errors.New("Web management startup failure ")
|
||||
var l net.Listener
|
||||
if l, err = connection.GetWebManagerListener(); err == nil {
|
||||
beego.InitBeforeHTTPRun()
|
||||
http.Serve(l, beego.BeeApp.Handlers)
|
||||
if beego.AppConfig.String("web_open_ssl") == "true" {
|
||||
keyPath := beego.AppConfig.String("web_key_file")
|
||||
certPath := beego.AppConfig.String("web_cert_file")
|
||||
err = http.ServeTLS(l, beego.BeeApp.Handlers, certPath, keyPath)
|
||||
} else {
|
||||
err = http.Serve(l, beego.BeeApp.Handlers)
|
||||
}
|
||||
} else {
|
||||
logs.Error(err)
|
||||
}
|
||||
return errors.New("Web management startup failure")
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *WebServer) Close() error {
|
||||
|
@@ -7,8 +7,8 @@ import (
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/conn"
|
||||
"ehang.io/nps/lib/common"
|
||||
"ehang.io/nps/lib/conn"
|
||||
)
|
||||
|
||||
func HandleTrans(c *conn.Conn, s *TunnelModeServer) error {
|
||||
|
@@ -3,7 +3,7 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"github.com/cnlh/nps/lib/conn"
|
||||
"ehang.io/nps/lib/conn"
|
||||
)
|
||||
|
||||
func HandleTrans(c *conn.Conn, s *TunnelModeServer) error {
|
||||
|
@@ -4,11 +4,11 @@ import (
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"ehang.io/nps/bridge"
|
||||
"ehang.io/nps/lib/common"
|
||||
"ehang.io/nps/lib/conn"
|
||||
"ehang.io/nps/lib/file"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/bridge"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/conn"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
)
|
||||
|
||||
type UdpModeServer struct {
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"ehang.io/nps/lib/version"
|
||||
"errors"
|
||||
"math"
|
||||
"os"
|
||||
@@ -8,13 +9,13 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ehang.io/nps/bridge"
|
||||
"ehang.io/nps/lib/common"
|
||||
"ehang.io/nps/lib/file"
|
||||
"ehang.io/nps/server/proxy"
|
||||
"ehang.io/nps/server/tool"
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/cnlh/nps/bridge"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
"github.com/cnlh/nps/server/proxy"
|
||||
"github.com/cnlh/nps/server/tool"
|
||||
"github.com/shirou/gopsutil/cpu"
|
||||
"github.com/shirou/gopsutil/load"
|
||||
"github.com/shirou/gopsutil/mem"
|
||||
@@ -109,6 +110,7 @@ func StartNewServer(bridgePort int, cnf *file.Tunnel, bridgeType string) {
|
||||
|
||||
func dealClientFlow() {
|
||||
ticker := time.NewTicker(time.Minute)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
@@ -270,8 +272,9 @@ func GetClientList(start, length int, search, sort, order string, clientId int)
|
||||
func dealClientData() {
|
||||
file.GetDb().JsonDb.Clients.Range(func(key, value interface{}) bool {
|
||||
v := value.(*file.Client)
|
||||
if _, ok := Bridge.Client.Load(v.Id); ok {
|
||||
if vv, ok := Bridge.Client.Load(v.Id); ok {
|
||||
v.IsConnect = true
|
||||
v.Version = vv.(*bridge.Client).Version
|
||||
} else {
|
||||
v.IsConnect = false
|
||||
}
|
||||
@@ -337,8 +340,12 @@ func DelClientConnect(clientId int) {
|
||||
|
||||
func GetDashboardData() map[string]interface{} {
|
||||
data := make(map[string]interface{})
|
||||
data["version"] = version.VERSION
|
||||
data["hostCount"] = common.GeSynctMapLen(file.GetDb().JsonDb.Hosts)
|
||||
data["clientCount"] = common.GeSynctMapLen(file.GetDb().JsonDb.Clients) - 1 //Remove the public key client
|
||||
data["clientCount"] = common.GeSynctMapLen(file.GetDb().JsonDb.Clients)
|
||||
if beego.AppConfig.String("public_vkey") != "" { //remove public vkey
|
||||
data["clientCount"] = data["clientCount"].(int) - 1
|
||||
}
|
||||
dealClientData()
|
||||
c := 0
|
||||
var in, out int64
|
||||
@@ -430,6 +437,7 @@ func GetDashboardData() map[string]interface{} {
|
||||
|
||||
func flowSession(m time.Duration) {
|
||||
ticker := time.NewTicker(m)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
|
@@ -5,9 +5,9 @@ import (
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"ehang.io/nps/lib/common"
|
||||
"ehang.io/nps/lib/file"
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/cnlh/nps/lib/file"
|
||||
)
|
||||
|
||||
func TestServerConfig() {
|
||||
|
@@ -5,8 +5,8 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"ehang.io/nps/lib/common"
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/cnlh/nps/lib/common"
|
||||
"github.com/shirou/gopsutil/cpu"
|
||||
"github.com/shirou/gopsutil/load"
|
||||
"github.com/shirou/gopsutil/mem"
|
||||
@@ -31,6 +31,9 @@ func InitAllowPort() {
|
||||
}
|
||||
|
||||
func TestServerPort(p int, m string) (b bool) {
|
||||
if m == "p2p" || m == "secret" {
|
||||
return true
|
||||
}
|
||||
if p > 65535 || p < 0 {
|
||||
return false
|
||||
}
|
||||
|
Reference in New Issue
Block a user