增加log

This commit is contained in:
lwch
2021-09-28 11:59:28 +08:00
parent 69a7dad72e
commit 6f78331781
4 changed files with 19 additions and 8 deletions
+1
View File
@@ -24,6 +24,7 @@ func (link *Link) remoteRead() {
link.targetIdx = msg.GetFromIdx()
switch msg.GetXType() {
case network.Msg_shell_created:
logging.Info("shell created")
if msg.GetCrep().GetOk() {
link.OnWork <- struct{}{}
continue
+4
View File
@@ -21,6 +21,10 @@ func (shell *Shell) New(pool *pool.Pool, w http.ResponseWriter, r *http.Request)
conn := pool.Get(id)
conn.SendShellCreate(id, shell.cfg)
conn.AddLink(id)
lk := NewLink(shell, id, shell.cfg.Target, conn)
shell.Lock()
shell.links[id] = lk
shell.Unlock()
logging.Info("new shell: name=%s, id=%s", shell.Name, id)
fmt.Fprint(w, id)
}
+6 -4
View File
@@ -19,7 +19,6 @@ func (shell *Shell) WS(pool *pool.Pool, w http.ResponseWriter, r *http.Request)
id := strings.TrimPrefix(r.URL.Path, "/ws/")
conn := pool.Get(id)
logging.Info("ws: %d", conn.Idx)
local, err := upgrader.Upgrade(w, r, nil)
if err != nil {
logging.Error("upgrade websocket failed: %s, err=%v", shell.Name, err)
@@ -45,7 +44,9 @@ func (shell *Shell) WS(pool *pool.Pool, w http.ResponseWriter, r *http.Request)
func (shell *Shell) localForward(id string, local *websocket.Conn, remote *pool.Conn) {
defer utils.Recover("localForward")
defer local.Close()
logging.Info("localForward: %d", remote.Idx)
shell.RLock()
link := shell.links[id]
shell.RUnlock()
for {
_, data, err := local.ReadMessage()
if err != nil {
@@ -53,8 +54,7 @@ func (shell *Shell) localForward(id string, local *websocket.Conn, remote *pool.
logging.Error("read local data for %s failed: %v", shell.Name, err)
return
}
remote.SendShellData(shell.cfg.Target, remote.Idx, id, data)
logging.Info("send: %d", remote.Idx)
remote.SendShellData(link.target, link.targetIdx, id, data)
logging.Debug("local read %d bytes: name=%s, id=%s", len(data), shell.Name, id)
}
}
@@ -68,6 +68,8 @@ func (shell *Shell) remoteForward(id string, ch <-chan *network.Msg, local *webs
return
}
switch msg.GetXType() {
case network.Msg_shell_created:
logging.Info("shell created")
case network.Msg_shell_data:
err := local.WriteMessage(websocket.TextMessage, msg.GetSdata().GetData())
if err != nil {
+8 -4
View File
@@ -5,21 +5,25 @@ import (
"natpass/code/client/global"
"natpass/code/client/pool"
"net/http"
"sync"
"github.com/lwch/logging"
"github.com/lwch/runtime"
)
type Shell struct {
Name string
cfg global.Tunnel
sync.RWMutex
Name string
cfg global.Tunnel
links map[string]*Link
}
// New new shell
func New(cfg global.Tunnel) *Shell {
return &Shell{
Name: cfg.Name,
cfg: cfg,
Name: cfg.Name,
cfg: cfg,
links: make(map[string]*Link),
}
}