Skip to content

Commit 5c37505

Browse files
committed
Merge remote-tracking branch 'remotes/origin/dev'
# Conflicts: # lib/mux/conn.go # lib/mux/queue.go
2 parents 0a65975 + 099d3fc commit 5c37505

40 files changed

+195
-3112
lines changed

bridge/bridge.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bridge
22

33
import (
4+
"ehang.io/nps-mux"
45
"encoding/binary"
56
"errors"
67
"fmt"
@@ -15,7 +16,6 @@ import (
1516
"ehang.io/nps/lib/conn"
1617
"ehang.io/nps/lib/crypt"
1718
"ehang.io/nps/lib/file"
18-
"ehang.io/nps/lib/mux"
1919
"ehang.io/nps/lib/version"
2020
"ehang.io/nps/server/connection"
2121
"ehang.io/nps/server/tool"
@@ -24,14 +24,14 @@ import (
2424
)
2525

2626
type Client struct {
27-
tunnel *mux.Mux
27+
tunnel *nps_mux.Mux
2828
signal *conn.Conn
29-
file *mux.Mux
29+
file *nps_mux.Mux
3030
Version string
3131
retryTime int // it will be add 1 when ping not ok until to 3 will close the client
3232
}
3333

34-
func NewClient(t, f *mux.Mux, s *conn.Conn, vs string) *Client {
34+
func NewClient(t, f *nps_mux.Mux, s *conn.Conn, vs string) *Client {
3535
return &Client{
3636
signal: s,
3737
tunnel: t,
@@ -50,10 +50,10 @@ type Bridge struct {
5050
CloseClient chan int
5151
SecretChan chan *conn.Secret
5252
ipVerify bool
53-
runList map[int]interface{}
53+
runList sync.Map //map[int]interface{}
5454
}
5555

56-
func NewTunnel(tunnelPort int, tunnelType string, ipVerify bool, runList map[int]interface{}) *Bridge {
56+
func NewTunnel(tunnelPort int, tunnelType string, ipVerify bool, runList sync.Map) *Bridge {
5757
return &Bridge{
5858
TunnelPort: tunnelPort,
5959
tunnelType: tunnelType,
@@ -242,7 +242,7 @@ func (s *Bridge) typeDeal(typeVal string, c *conn.Conn, id int, vs string) {
242242
go s.GetHealthFromClient(id, c)
243243
logs.Info("clientId %d connection succeeded, address:%s ", id, c.Conn.RemoteAddr())
244244
case common.WORK_CHAN:
245-
muxConn := mux.NewMux(c.Conn, s.tunnelType)
245+
muxConn := nps_mux.NewMux(c.Conn, s.tunnelType)
246246
if v, ok := s.Client.LoadOrStore(id, NewClient(muxConn, nil, nil, vs)); ok {
247247
v.(*Client).tunnel = muxConn
248248
}
@@ -263,7 +263,7 @@ func (s *Bridge) typeDeal(typeVal string, c *conn.Conn, id int, vs string) {
263263
logs.Error("secret error, failed to match the key successfully")
264264
}
265265
case common.WORK_FILE:
266-
muxConn := mux.NewMux(c.Conn, s.tunnelType)
266+
muxConn := nps_mux.NewMux(c.Conn, s.tunnelType)
267267
if v, ok := s.Client.LoadOrStore(id, NewClient(nil, muxConn, nil, vs)); ok {
268268
v.(*Client).file = muxConn
269269
}
@@ -321,7 +321,7 @@ func (s *Bridge) SendLinkInfo(clientId int, link *conn.Link, t *file.Tunnel) (ta
321321
}
322322
}
323323
}
324-
var tunnel *mux.Mux
324+
var tunnel *nps_mux.Mux
325325
if t != nil && t.Mode == "file" {
326326
tunnel = v.(*Client).file
327327
} else {
@@ -407,7 +407,8 @@ loop:
407407
})
408408
file.GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
409409
v := value.(*file.Tunnel)
410-
if _, ok := s.runList[v.Id]; ok && v.Client.Id == id {
410+
//if _, ok := s.runList[v.Id]; ok && v.Client.Id == id {
411+
if _, ok := s.runList.Load(v.Id); ok && v.Client.Id == id {
411412
str += v.Remark + common.CONN_DATA_SEQ
412413
}
413414
return true

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#/bash/sh
2-
export VERSION=0.26.0
2+
export VERSION=0.26.1
33

44
sudo apt-get install gcc-mingw-w64-i686
55
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

client/client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package client
33
import (
44
"bufio"
55
"bytes"
6+
"ehang.io/nps-mux"
67
"net"
78
"net/http"
89
"strconv"
@@ -15,7 +16,6 @@ import (
1516
"ehang.io/nps/lib/config"
1617
"ehang.io/nps/lib/conn"
1718
"ehang.io/nps/lib/crypt"
18-
"ehang.io/nps/lib/mux"
1919
)
2020

2121
type TRPClient struct {
@@ -24,7 +24,7 @@ type TRPClient struct {
2424
proxyUrl string
2525
vKey string
2626
p2pAddr map[string]string
27-
tunnel *mux.Mux
27+
tunnel *nps_mux.Mux
2828
signal *conn.Conn
2929
ticker *time.Ticker
3030
cnf *config.Config
@@ -138,22 +138,22 @@ func (s *TRPClient) newUdpConn(localAddr, rAddr string, md5Password string) {
138138
conn.SetUdpSession(udpTunnel)
139139
logs.Trace("successful connection with client ,address %s", udpTunnel.RemoteAddr().String())
140140
//read link info from remote
141-
conn.Accept(mux.NewMux(udpTunnel, s.bridgeConnType), func(c net.Conn) {
141+
conn.Accept(nps_mux.NewMux(udpTunnel, s.bridgeConnType), func(c net.Conn) {
142142
go s.handleChan(c)
143143
})
144144
break
145145
}
146146
}
147147
}
148148

149-
//mux tunnel
149+
//pmux tunnel
150150
func (s *TRPClient) newChan() {
151151
tunnel, err := NewConn(s.bridgeConnType, s.vKey, s.svrAddr, common.WORK_CHAN, s.proxyUrl)
152152
if err != nil {
153153
logs.Error("connect to ", s.svrAddr, "error:", err)
154154
return
155155
}
156-
s.tunnel = mux.NewMux(tunnel.Conn, s.bridgeConnType)
156+
s.tunnel = nps_mux.NewMux(tunnel.Conn, s.bridgeConnType)
157157
for {
158158
src, err := s.tunnel.Accept()
159159
if err != nil {

client/local.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package client
22

33
import (
4+
"ehang.io/nps-mux"
45
"errors"
56
"net"
67
"net/http"
@@ -13,7 +14,6 @@ import (
1314
"ehang.io/nps/lib/conn"
1415
"ehang.io/nps/lib/crypt"
1516
"ehang.io/nps/lib/file"
16-
"ehang.io/nps/lib/mux"
1717
"ehang.io/nps/server/proxy"
1818
"github.com/astaxie/beego/logs"
1919
"github.com/xtaci/kcp-go"
@@ -22,7 +22,7 @@ import (
2222
var (
2323
LocalServer []*net.TCPListener
2424
udpConn net.Conn
25-
muxSession *mux.Mux
25+
muxSession *nps_mux.Mux
2626
fileServer []*http.Server
2727
p2pNetBridge *p2pBridge
2828
lock sync.RWMutex
@@ -73,7 +73,7 @@ func startLocalFileServer(config *config.CommonConfig, t *file.Tunnel, vkey stri
7373
}
7474
logs.Info("start local file system, local path %s, strip prefix %s ,remote port %s ", t.LocalPath, t.StripPre, t.Ports)
7575
fileServer = append(fileServer, srv)
76-
listener := mux.NewMux(remoteConn.Conn, common.CONN_TCP)
76+
listener := nps_mux.NewMux(remoteConn.Conn, common.CONN_TCP)
7777
logs.Error(srv.Serve(listener))
7878
}
7979

@@ -214,6 +214,6 @@ func newUdpConn(localAddr string, config *config.CommonConfig, l *config.LocalSe
214214
logs.Trace("successful create a connection with server", remoteAddress)
215215
conn.SetUdpSession(udpTunnel)
216216
udpConn = udpTunnel
217-
muxSession = mux.NewMux(udpConn, "kcp")
217+
muxSession = nps_mux.NewMux(udpConn, "kcp")
218218
p2pNetBridge = &p2pBridge{}
219219
}

cmd/npc/npc.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,31 @@ var (
3535
debug = flag.Bool("debug", true, "npc debug")
3636
)
3737

38+
const systemdScript = `[Unit]
39+
Description={{.Description}}
40+
ConditionFileIsExecutable={{.Path|cmdEscape}}
41+
{{range $i, $dep := .Dependencies}}
42+
{{$dep}} {{end}}
43+
[Service]
44+
LimitNOFILE=65536
45+
StartLimitInterval=5
46+
StartLimitBurst=10
47+
ExecStart={{.Path|cmdEscape}}{{range .Arguments}} {{.|cmd}}{{end}}
48+
{{if .ChRoot}}RootDirectory={{.ChRoot|cmd}}{{end}}
49+
{{if .WorkingDirectory}}WorkingDirectory={{.WorkingDirectory|cmdEscape}}{{end}}
50+
{{if .UserName}}User={{.UserName}}{{end}}
51+
{{if .ReloadSignal}}ExecReload=/bin/kill -{{.ReloadSignal}} "$MAINPID"{{end}}
52+
{{if .PIDFile}}PIDFile={{.PIDFile|cmd}}{{end}}
53+
{{if and .LogOutput .HasOutputFileSupport -}}
54+
StandardOutput=file:/var/log/{{.Name}}.out
55+
StandardError=file:/var/log/{{.Name}}.err
56+
{{- end}}
57+
Restart=always
58+
RestartSec=120
59+
[Install]
60+
WantedBy=multi-user.target
61+
`
62+
3863
func main() {
3964
flag.Parse()
4065
logs.Reset()
@@ -54,8 +79,6 @@ func main() {
5479

5580
// init service
5681
options := make(service.KeyValue)
57-
options["Restart"] = "on-success"
58-
options["SuccessExitStatus"] = "1 2 8 SIGKILL"
5982
svcConfig := &service.Config{
6083
Name: "Npc",
6184
DisplayName: "nps内网穿透客户端",
@@ -66,6 +89,7 @@ func main() {
6689
svcConfig.Dependencies = []string{
6790
"Requires=network.target",
6891
"After=network-online.target syslog.target"}
92+
svcConfig.Option["SystemdScript"] = systemdScript
6993
}
7094
for _, v := range os.Args[1:] {
7195
switch v {

cmd/nps/nps.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,31 @@ var (
2828
level string
2929
)
3030

31+
const systemdScript = `[Unit]
32+
Description={{.Description}}
33+
ConditionFileIsExecutable={{.Path|cmdEscape}}
34+
{{range $i, $dep := .Dependencies}}
35+
{{$dep}} {{end}}
36+
[Service]
37+
LimitNOFILE=65536
38+
StartLimitInterval=5
39+
StartLimitBurst=10
40+
ExecStart={{.Path|cmdEscape}}{{range .Arguments}} {{.|cmd}}{{end}}
41+
{{if .ChRoot}}RootDirectory={{.ChRoot|cmd}}{{end}}
42+
{{if .WorkingDirectory}}WorkingDirectory={{.WorkingDirectory|cmdEscape}}{{end}}
43+
{{if .UserName}}User={{.UserName}}{{end}}
44+
{{if .ReloadSignal}}ExecReload=/bin/kill -{{.ReloadSignal}} "$MAINPID"{{end}}
45+
{{if .PIDFile}}PIDFile={{.PIDFile|cmd}}{{end}}
46+
{{if and .LogOutput .HasOutputFileSupport -}}
47+
StandardOutput=file:/var/log/{{.Name}}.out
48+
StandardError=file:/var/log/{{.Name}}.err
49+
{{- end}}
50+
Restart=always
51+
RestartSec=120
52+
[Install]
53+
WantedBy=multi-user.target
54+
`
55+
3156
func main() {
3257
flag.Parse()
3358
// init log
@@ -49,8 +74,6 @@ func main() {
4974
}
5075
// init service
5176
options := make(service.KeyValue)
52-
options["Restart"] = "on-success"
53-
options["SuccessExitStatus"] = "1 2 8 SIGKILL"
5477
svcConfig := &service.Config{
5578
Name: "Nps",
5679
DisplayName: "nps内网穿透代理服务器",
@@ -59,14 +82,15 @@ func main() {
5982
}
6083
svcConfig.Arguments = append(svcConfig.Arguments, "service")
6184
if len(os.Args) > 1 && os.Args[1] == "service" {
62-
logs.SetLogger(logs.AdapterFile, `{"level":`+level+`,"filename":"`+logPath+`","daily":false,"maxlines":100000,"color":true}`)
85+
_ = logs.SetLogger(logs.AdapterFile, `{"level":`+level+`,"filename":"`+logPath+`","daily":false,"maxlines":100000,"color":true}`)
6386
} else {
64-
logs.SetLogger(logs.AdapterConsole, `{"level":`+level+`,"color":true}`)
87+
_ = logs.SetLogger(logs.AdapterConsole, `{"level":`+level+`,"color":true}`)
6588
}
6689
if !common.IsWindows() {
6790
svcConfig.Dependencies = []string{
6891
"Requires=network.target",
6992
"After=network-online.target syslog.target"}
93+
svcConfig.Option["SystemdScript"] = systemdScript
7094
}
7195
prg := &nps{}
7296
prg.exit = make(chan struct{})
@@ -82,8 +106,8 @@ func main() {
82106
return
83107
case "install":
84108
// uninstall before
85-
service.Control(s, "stop")
86-
service.Control(s, "uninstall")
109+
_ = service.Control(s, "stop")
110+
_ = service.Control(s, "uninstall")
87111

88112
binPath := install.InstallNps()
89113
svcConfig.Executable = binPath
@@ -111,18 +135,20 @@ func main() {
111135
return
112136
}
113137
}
114-
s.Run()
138+
_ = s.Run()
115139
}
116140

117141
type nps struct {
118142
exit chan struct{}
119143
}
120144

121145
func (p *nps) Start(s service.Service) error {
122-
p.run()
146+
_, _ = s.Status()
147+
_ = p.run()
123148
return nil
124149
}
125150
func (p *nps) Stop(s service.Service) error {
151+
_, _ = s.Status()
126152
close(p.exit)
127153
if service.Interactive() {
128154
os.Exit(0)

conf/nps.conf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ web_key_file=conf/server.key
4949
#web_base_url=/nps
5050

5151
#Web API unauthenticated IP address(the len of auth_crypt_key must be 16)
52-
auth_key=test
52+
#Remove comments if needed
53+
#auth_key=test
5354
auth_crypt_key =1234567812345678
5455

5556
#allow_ports=9001-9009,10001,11000-12000
@@ -73,4 +74,5 @@ system_info_display=false
7374
http_cache=false
7475
http_cache_length=100
7576

76-
77+
#get origin ip
78+
http_add_origin_header=false

docs/_coverpage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
![logo](logo.svg)
22

3-
# NPS <small>0.26.0</small>
3+
# NPS <small>0.26.1</small>
44

55
> 一款轻量级、高性能、功能强大的内网穿透代理服务器
66

docs/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# web api
2+
3+
需要开启请先去掉`nps.conf``auth_key`的注释并配置一个合适的密钥
24
## webAPI验证说明
35
- 采用auth_key的验证方式
46
- 在提交的每个请求后面附带两个参数,`auth_key``timestamp`

docs/description.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# 说明
22
## 获取用户真实ip
3+
如需使用需要在`nps.conf`中设置`http_add_origin_header=true`
34

45
在域名代理模式中,可以通过request请求 header 中的 X-Forwarded-For 和 X-Real-IP 来获取用户真实 IP。
56

@@ -8,9 +9,6 @@
89
## 热更新支持
910
对于绝大多数配置,在web管理中的修改将实时使用,无需重启客户端或者服务端
1011

11-
## web端保护
12-
在一分钟内,如果密码错误次数超过10次,该ip在一分钟内将不能再次登陆。
13-
1412
## 客户端地址显示
1513
在web管理中将显示客户端的连接地址
1614

0 commit comments

Comments
 (0)