实现shell数据转发

This commit is contained in:
lwch
2021-09-27 18:28:26 +08:00
parent 2f7696348f
commit bd0a1ab3c5
13 changed files with 301 additions and 61 deletions

View File

@@ -8,11 +8,9 @@ import (
"natpass/code/client/shell"
"natpass/code/client/tunnel"
"natpass/code/network"
"net"
"os"
"path/filepath"
rt "runtime"
"strconv"
"time"
_ "net/http/pprof"
@@ -83,9 +81,9 @@ func (a *app) run() {
var linkID string
switch msg.GetXType() {
case network.Msg_connect_req:
connect(pl, conn, msg.GetLinkId(), msg.GetFrom(), msg.GetTo(),
msg.GetFromIdx(), msg.GetToIdx(), msg.GetCreq())
connect(conn, msg)
case network.Msg_shell_create:
shellCreate(conn, msg)
case network.Msg_connect_rep,
network.Msg_disconnect,
network.Msg_forward:
@@ -159,31 +157,3 @@ func main() {
runtime.Assert(sv.Run())
}
}
func connect(pool *pool.Pool, conn *pool.Conn, id, from, to string, fromIdx, toIdx uint32, req *network.ConnectRequest) {
dial := "tcp"
if req.GetXType() == network.ConnectRequest_udp {
dial = "udp"
}
link, err := net.Dial(dial, fmt.Sprintf("%s:%d", req.GetAddr(), req.GetPort()))
if err != nil {
conn.SendConnectError(from, fromIdx, id, err.Error())
return
}
host, pt, _ := net.SplitHostPort(link.LocalAddr().String())
port, _ := strconv.ParseUint(pt, 10, 16)
tn := tunnel.New(global.Tunnel{
Name: req.GetName(),
Target: from,
Type: dial,
LocalAddr: host,
LocalPort: uint16(port),
RemoteAddr: req.GetAddr(),
RemotePort: uint16(req.GetPort()),
})
lk := tunnel.NewLink(tn, id, from, link, conn)
lk.SetTargetIdx(fromIdx)
conn.SendConnectOK(from, fromIdx, id)
lk.Forward()
lk.OnWork <- struct{}{}
}