Skip to content

Commit 842be95

Browse files
authored
Merge pull request #69 from lwch/dev
v0.12.1
2 parents 3611859 + 583ec2e commit 842be95

File tree

18 files changed

+386
-90
lines changed

18 files changed

+386
-90
lines changed

.github/workflows/build-builder-image.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
- cron: '30 0 * * *'
88

99
env:
10-
GO_VERSION: 1.19.5
10+
GO_VERSION: "1.20.1"
1111

1212
jobs:
1313

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
- '1.17'
2626
- '1.18'
2727
- '1.19'
28+
- '1.20'
2829
steps:
2930
- uses: actions/checkout@v3
3031

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,12 @@
199199
1. 命令行交互切换到cobra库
200200
2. 新增start、stop、restart、status命令行交互命令
201201
3. 升级第三方库
202-
4. 补充代码注释
202+
4. 补充代码注释
203+
204+
## v0.12.1
205+
206+
1. 启动时增加logo输出
207+
2. 重写网络数据编码逻辑
208+
3. go版本升级到1.20.1
209+
4. 补充代码注释
210+
5. 更新第三方库

code/client/app/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (a *App) Status(*cobra.Command, []string) {
163163
}
164164
}
165165

166-
// Vnc handle vnc child process
166+
// Vnc handle vnc child process handler
167167
func (a *App) Vnc(*cobra.Command, []string) {
168168
defer utils.Recover("vnc.worker")
169169

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ import (
1111
"github.com/lwch/natpass/code/network"
1212
)
1313

14+
/*
15+
this is the handler function file like http.HandlerFunc.
16+
17+
for linked rule:
18+
1. get rule from manager
19+
2. create rule if not exists by its type
20+
3. create link call NewLink
21+
4. do the initialize logic for the link
22+
5. response connect ok message
23+
6. loop forward
24+
25+
no linked rule:
26+
TODO
27+
*/
28+
1429
func (p *program) shellCreate(mgr *rule.Mgr, conn *conn.Conn, msg *network.Msg) {
1530
create := msg.GetCreq()
1631
tn := mgr.GetLinked(create.GetName(), msg.GetFrom())

code/client/app/program.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55
rt "runtime"
66

7+
"github.com/common-nighthawk/go-figure"
78
"github.com/kardianos/service"
89
"github.com/lwch/logging"
910
"github.com/lwch/natpass/code/client/conn"
@@ -69,6 +70,10 @@ func (p *program) run() {
6970
})
7071
defer logging.Flush()
7172

73+
fg := figure.NewFigure("NatPass", "alligator2", false)
74+
figure.Write(&logging.DefaultLogger, fg)
75+
logging.DefaultLogger.Write(nil)
76+
7277
// create connection and handshake
7378
p.conn = conn.New(p.cfg)
7479

code/client/conn/conn.go

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ const dropBlockTimeout = 10 * time.Minute
2020
// Conn connection
2121
type Conn struct {
2222
sync.RWMutex
23-
cfg *global.Configure
24-
conn *network.Conn
23+
cfg *global.Configure // configure
24+
conn *network.Conn // connection wrap, read write with timeout
2525
read map[string]chan *network.Msg // link id => channel
2626
unknownRead chan *network.Msg // read message without link
27-
onDisconnect chan string
28-
write chan *network.Msg
29-
lockDrop sync.RWMutex
30-
drop map[string]time.Time
27+
onDisconnect chan string // on disconnect channel, the value is clientid
28+
write chan *network.Msg // write queue
29+
lockDrop sync.RWMutex // drop mutex
30+
drop map[string]time.Time // drop cache, drop message when this link is closed
3131
// runtime
3232
ctx context.Context
3333
cancel context.CancelFunc
@@ -52,11 +52,12 @@ func New(cfg *global.Configure) *Conn {
5252
return conn
5353
}
5454

55+
// connect connect server and write handshake packet
5556
func (conn *Conn) connect() error {
5657
var dial net.Conn
5758
var err error
5859
if conn.cfg.UseSSL {
59-
if conn.cfg.SSLInsecure {
60+
if conn.cfg.SSLInsecure { // disable sni
6061
rawConn, err := net.Dial("tcp", conn.cfg.Server)
6162
if err != nil {
6263
logging.Error("raw dial: %v", err)
@@ -96,6 +97,7 @@ func (conn *Conn) close() {
9697
}
9798
}
9899

100+
// writeHandshake send handshake message, default timeout is 5 seconds
99101
func writeHandshake(conn *network.Conn, cfg *global.Configure) error {
100102
var msg network.Msg
101103
msg.XType = network.Msg_handshake
@@ -109,13 +111,22 @@ func writeHandshake(conn *network.Conn, cfg *global.Configure) error {
109111
return conn.WriteMessage(&msg, 5*time.Second)
110112
}
111113

114+
// isDrop check the message is dropped by linkid
112115
func (conn *Conn) isDrop(linkID string) bool {
113116
conn.lockDrop.RLock()
114117
defer conn.lockDrop.RUnlock()
115118
_, ok := conn.drop[linkID]
116119
return ok
117120
}
118121

122+
// addDrop add to drop queue
123+
func (conn *Conn) addDrop(linkID string) {
124+
conn.lockDrop.Lock()
125+
defer conn.lockDrop.Unlock()
126+
conn.drop[linkID] = time.Now().Add(dropBlockTimeout)
127+
}
128+
129+
// getChan get read channel by linkid
119130
func (conn *Conn) getChan(linkID string) chan *network.Msg {
120131
conn.RLock()
121132
ch := conn.read[linkID]
@@ -126,50 +137,66 @@ func (conn *Conn) getChan(linkID string) chan *network.Msg {
126137
return ch
127138
}
128139

129-
func (conn *Conn) hookDispatch(ch chan *network.Msg, msg *network.Msg) bool {
140+
// hookDispatch hook message before dispatcher
141+
func (conn *Conn) hookDispatch(msg *network.Msg) bool {
130142
switch msg.GetXType() {
143+
// if disconnected add linkid to drop list, and break the handle chain
131144
case network.Msg_disconnect:
132-
conn.lockDrop.Lock()
133-
conn.drop[msg.GetLinkId()] = time.Now().Add(dropBlockTimeout)
134-
conn.lockDrop.Unlock()
135-
conn.onDisconnect <- msg.GetLinkId()
145+
conn.addDrop(msg.GetLinkId())
146+
// TODO: no need will block
147+
// conn.onDisconnect <- msg.GetLinkId()
136148
logging.Info("connection %s disconnected", msg.GetLinkId())
137149
return false
138150
}
139151
return true
140152
}
141153

154+
// handleLinkedMessage linked message handler, return false means break read loop
155+
func (conn *Conn) handleLinkedMessage(msg *network.Msg) bool {
156+
linkID := msg.GetLinkId()
157+
if conn.isDrop(linkID) {
158+
return true
159+
}
160+
if !conn.hookDispatch(msg) {
161+
return true
162+
}
163+
ch := conn.getChan(linkID)
164+
select {
165+
case ch <- msg:
166+
case <-time.After(conn.cfg.WriteTimeout):
167+
logging.Error("drop message: %s", msg.GetXType().String())
168+
conn.addDrop(linkID)
169+
case <-conn.ctx.Done():
170+
return false
171+
}
172+
return true
173+
}
174+
175+
// handleUnlinkedMessage unlinked message handler, return false means break read loop
176+
func (conn *Conn) handleUnlinkedMessage(msg *network.Msg) bool {
177+
// TODO
178+
return true
179+
}
180+
181+
// loopRead loop read message
142182
func (conn *Conn) loopRead() {
143183
defer utils.Recover("loopRead")
144184
defer conn.close()
145185
defer conn.cancel()
146186
var timeout int
147187
run := func(msg *network.Msg) bool {
148188
timeout = 0
189+
// skip keepalive message
149190
if msg.GetXType() == network.Msg_keepalive {
150191
return true
151192
}
152193
logging.Debug("read message %s(%s) from %s",
153194
msg.GetXType().String(), msg.GetLinkId(), msg.GetFrom())
154195
linkID := msg.GetLinkId()
155-
if conn.isDrop(linkID) {
156-
return true
157-
}
158-
ch := conn.getChan(linkID)
159-
if !conn.hookDispatch(ch, msg) {
160-
return true
161-
}
162-
select {
163-
case ch <- msg:
164-
case <-time.After(conn.cfg.WriteTimeout):
165-
logging.Error("drop message: %s", msg.GetXType().String())
166-
conn.lockDrop.Lock()
167-
conn.drop[msg.GetLinkId()] = time.Now().Add(dropBlockTimeout)
168-
conn.lockDrop.Unlock()
169-
case <-conn.ctx.Done():
170-
return false
196+
if len(linkID) > 0 {
197+
return conn.handleLinkedMessage(msg)
171198
}
172-
return true
199+
return conn.handleUnlinkedMessage(msg)
173200
}
174201
for {
175202
msg, _, err := conn.conn.ReadMessage(conn.cfg.ReadTimeout)
@@ -191,6 +218,7 @@ func (conn *Conn) loopRead() {
191218
}
192219
}
193220

221+
// loopWrite loop write message
194222
func (conn *Conn) loopWrite() {
195223
defer utils.Recover("loopWrite")
196224
defer conn.close()
@@ -212,6 +240,7 @@ func (conn *Conn) loopWrite() {
212240
}
213241
}
214242

243+
// keepalive loop send keepalive message
215244
func (conn *Conn) keepalive() {
216245
defer utils.Recover("keepalive")
217246
defer conn.close()
@@ -237,8 +266,8 @@ func (conn *Conn) AddLink(id string) {
237266
conn.Unlock()
238267
}
239268

240-
// Reset reset message next read
241-
func (conn *Conn) Reset(id string, msg *network.Msg) {
269+
// Requeue requeue for next read
270+
func (conn *Conn) Requeue(id string, msg *network.Msg) {
242271
conn.RLock()
243272
ch := conn.read[id]
244273
conn.RUnlock()
@@ -262,6 +291,7 @@ func (conn *Conn) ChanDisconnect() <-chan string {
262291
return conn.onDisconnect
263292
}
264293

294+
// checkDrop clear timeouted drop queue
265295
func (conn *Conn) checkDrop() {
266296
for {
267297
time.Sleep(time.Second)
@@ -298,7 +328,5 @@ func (conn *Conn) ChanClose(id string) {
298328
delete(conn.read, id)
299329
conn.Unlock()
300330

301-
conn.lockDrop.Lock()
302-
conn.drop[id] = time.Now().Add(dropBlockTimeout)
303-
conn.lockDrop.Unlock()
331+
conn.addDrop(id)
304332
}

code/client/rule/code/code.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (code *Code) new(conn *conn.Conn) (string, error) {
143143
return "", errWaitingTimeout
144144
}
145145
if msg.GetXType() != network.Msg_connect_rep {
146-
conn.Reset(id, msg)
146+
conn.Requeue(id, msg)
147147
time.Sleep(code.readTimeout / 10)
148148
continue
149149
}

code/client/rule/shell/h_new.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (shell *Shell) New(conn *conn.Conn, w http.ResponseWriter, r *http.Request)
3535
return
3636
}
3737
if msg.GetXType() != network.Msg_connect_rep {
38-
conn.Reset(id, msg)
38+
conn.Requeue(id, msg)
3939
time.Sleep(shell.readTimeout / 10)
4040
continue
4141
}

code/client/rule/vnc/h_new.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (v *VNC) New(conn *conn.Conn, w http.ResponseWriter, r *http.Request) {
5151
return
5252
}
5353
if msg.GetXType() != network.Msg_connect_rep {
54-
conn.Reset(id, msg)
54+
conn.Requeue(id, msg)
5555
time.Sleep(v.readTimeout / 10)
5656
continue
5757
}

0 commit comments

Comments
 (0)