Skip to content

Commit

Permalink
Merge pull request #8 from lwch/dev
Browse files Browse the repository at this point in the history
v0.6.0
  • Loading branch information
lwch authored Nov 5, 2021
2 parents bfa5a6b + 57cc44d commit 6279f54
Show file tree
Hide file tree
Showing 81 changed files with 4,621 additions and 250 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ jobs:
- name: Build Client
run: |
go run contrib/bindata/main.go -pkg shell -o code/client/tunnel/shell/assets.go -prefix html/shell html/shell/...
go run contrib/bindata/main.go -pkg vnc -o code/client/tunnel/vnc/assets.go -prefix html/vnc html/vnc/...
go run contrib/bindata/main.go -pkg dashboard -o code/client/dashboard/assets.go -prefix html/dashboard html/dashboard/...
go build -v code/client/main.go code/client/connect.go
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@

# v0.5.0

1. 新增dashboard和统一的终端管理页面
1. 新增dashboard和统一的终端管理页面

# v0.6.0

1. 新增vnc隧道支持
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ windows命令行效果

![windows-shell](docs/imgs/shell_win.png)

windows2008远程桌面

![win2008-vnc](docs/imgs/vnc_win2008.png)

windows10远程桌面

![win10-vnc](docs/imgs/vnc_win10.png)

windows11远程桌面

![win11-vnc](docs/imgs/vnc_win11.png)

## iperf3压测对比

使用相同参数,iperf3压测1分钟
Expand All @@ -47,4 +59,5 @@ windows命令行效果
3. ~~所有隧道的dashboard页面~~
4. 文件传输
5. web远程桌面
6. ~~流量监控统计页面,server还是client?~~
6. ~~流量监控统计页面,server还是client?~~
7. web端管理tunnel
22 changes: 8 additions & 14 deletions build
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
#!/bin/sh

VERSION="0.5.0"
HASH=`git log -n1 --pretty=format:%h`
REVERSION=`git log --oneline|wc -l|tr -d ' '`
BUILD_TIME=`date +'%Y-%m-%d %H:%M:%S'`
LDFLAGS="-X 'main.gitHash=$HASH'
-X 'main.gitReversion=$REVERSION'
-X 'main.buildTime=$BUILD_TIME'
-X 'main.version=$VERSION'"
VERSION=0.6.0

go run contrib/bindata/main.go -pkg shell -o code/client/tunnel/shell/assets.go \
-prefix html/shell "$@" html/shell/...
go run contrib/bindata/main.go -pkg dashboard -o code/client/dashboard/assets.go \
-prefix html/dashboard "$@" html/dashboard/...
CGO_ENABLED=0 go build -ldflags "$LDFLAGS" -o bin/np-svr code/server/*.go
CGO_ENABLED=0 go build -ldflags "$LDFLAGS" -o bin/np-cli code/client/*.go
docker build -t natpass -f contrib/build/Dockerfile .
docker run --rm \
-v `pwd`:/build \
-v $HOME/.cache/go-build:/root/.cache/go-build \
-v $HOME/go/pkg/mod:/root/go/pkg/mod \
-e BUILD_VERSION=$VERSION \
natpass /build/contrib/build/build "$@"
11 changes: 11 additions & 0 deletions build_all
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

VERSION=0.6.0

docker build -t natpass -f contrib/build/Dockerfile .
docker run --rm \
-v `pwd`:/build \
-v $HOME/.cache/go-build:/root/.cache/go-build \
-v $HOME/go/pkg/mod:/root/go/pkg/mod \
-e BUILD_VERSION=$VERSION \
natpass go run /build/contrib/build/build_all.go
74 changes: 52 additions & 22 deletions code/client/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"natpass/code/client/tunnel"
"natpass/code/client/tunnel/reverse"
"natpass/code/client/tunnel/shell"
"natpass/code/client/tunnel/vnc"
"natpass/code/network"
"net"
"strconv"
Expand All @@ -16,6 +17,7 @@ import (

func connect(mgr *tunnel.Mgr, conn *pool.Conn, msg *network.Msg) {
req := msg.GetCreq()
// TODO: 创建tcp连接移到reverse包中实现
dial := "tcp"
if req.GetXType() == network.ConnectRequest_udp {
dial = "udp"
Expand All @@ -29,35 +31,39 @@ func connect(mgr *tunnel.Mgr, conn *pool.Conn, msg *network.Msg) {
}
host, pt, _ := net.SplitHostPort(link.LocalAddr().String())
port, _ := strconv.ParseUint(pt, 10, 16)
tn := reverse.New(global.Tunnel{
Name: req.GetName(),
Target: msg.GetFrom(),
Type: dial,
LocalAddr: host,
LocalPort: uint16(port),
RemoteAddr: addr.GetAddr(),
RemotePort: uint16(addr.GetPort()),
})
mgr.Add(tn)
lk := reverse.NewLink(tn, msg.GetLinkId(), msg.GetFrom(), link, conn)
lk.SetTargetIdx(msg.GetFromIdx())
tn := mgr.Get(req.GetName(), msg.GetFrom())
if tn == nil {
tn = reverse.New(global.Tunnel{
Name: req.GetName(),
Target: msg.GetFrom(),
Type: dial,
LocalAddr: host,
LocalPort: uint16(port),
RemoteAddr: addr.GetAddr(),
RemotePort: uint16(addr.GetPort()),
})
mgr.Add(tn)
}
lk := tn.NewLink(msg.GetLinkId(), msg.GetFrom(), msg.GetFromIdx(), link, conn).(*reverse.Link)
conn.SendConnectOK(msg.GetFrom(), msg.GetFromIdx(), msg.GetLinkId())
lk.Forward()
lk.OnWork <- struct{}{}
}

func shellCreate(mgr *tunnel.Mgr, conn *pool.Conn, msg *network.Msg) {
create := msg.GetCreq()
sh := shell.New(global.Tunnel{
Name: create.GetName(),
Target: msg.GetFrom(),
Type: "shell",
Exec: create.GetCshell().GetExec(),
Env: create.GetCshell().GetEnv(),
})
mgr.Add(sh)
lk := shell.NewLink(sh, msg.GetLinkId(), msg.GetFrom(), conn)
lk.SetTargetIdx(msg.GetFromIdx())
tn := mgr.Get(create.GetName(), msg.GetFrom())
if tn == nil {
tn = shell.New(global.Tunnel{
Name: create.GetName(),
Target: msg.GetFrom(),
Type: "shell",
Exec: create.GetCshell().GetExec(),
Env: create.GetCshell().GetEnv(),
})
mgr.Add(tn)
}
lk := tn.NewLink(msg.GetLinkId(), msg.GetFrom(), msg.GetFromIdx(), nil, conn).(*shell.Link)
err := lk.Exec()
if err != nil {
logging.Error("create shell failed: %v", err)
Expand All @@ -67,3 +73,27 @@ func shellCreate(mgr *tunnel.Mgr, conn *pool.Conn, msg *network.Msg) {
conn.SendConnectOK(msg.GetFrom(), msg.GetFromIdx(), msg.GetLinkId())
lk.Forward()
}

func vncCreate(confDir string, mgr *tunnel.Mgr, conn *pool.Conn, msg *network.Msg) {
create := msg.GetCreq()
tn := mgr.Get(create.GetName(), msg.GetFrom())
if tn == nil {
tn = vnc.New(global.Tunnel{
Name: create.GetName(),
Target: msg.GetFrom(),
Type: "vnc",
Fps: create.GetCvnc().GetFps(),
})
mgr.Add(tn)
}
lk := tn.NewLink(msg.GetLinkId(), msg.GetFrom(), msg.GetFromIdx(), nil, conn).(*vnc.Link)
lk.SetQuality(create.GetCvnc().GetQuality())
err := lk.Fork(confDir)
if err != nil {
logging.Error("create vnc failed: %v", err)
conn.SendConnectError(msg.GetFrom(), msg.GetFromIdx(), msg.GetLinkId(), err.Error())
return
}
conn.SendConnectOK(msg.GetFrom(), msg.GetFromIdx(), msg.GetLinkId())
lk.Forward()
}
23 changes: 13 additions & 10 deletions code/client/global/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ import (

// Tunnel tunnel config
type Tunnel struct {
Name string `yaml:"name"`
Target string `yaml:"target"`
Type string `yaml:"type"`
LocalAddr string `yaml:"local_addr"`
LocalPort uint16 `yaml:"local_port"`
RemoteAddr string `yaml:"remote_addr"`
RemotePort uint16 `yaml:"remote_port"`
Exec string `yaml:"exec"`
Env []string `yaml:"env"`
Name string `yaml:"name"`
Target string `yaml:"target"`
Type string `yaml:"type"`
LocalAddr string `yaml:"local_addr"`
LocalPort uint16 `yaml:"local_port"`
RemoteAddr string `yaml:"remote_addr"`
RemotePort uint16 `yaml:"remote_port"`
// shell
Exec string `yaml:"exec"`
Env []string `yaml:"env"`
// vnc
Fps uint32 `yaml:"fps"`
}

// Configure client configure
Expand Down Expand Up @@ -65,7 +68,7 @@ func LoadConf(dir string) *Configure {
runtime.Assert(yaml.Decode(dir, &cfg))
for i, t := range cfg.Tunnel {
switch t.Type {
case "tcp", "shell":
case "tcp", "shell", "vnc":
default:
t.Type = "udp"
}
Expand Down
42 changes: 39 additions & 3 deletions code/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"natpass/code/client/tunnel"
"natpass/code/client/tunnel/reverse"
"natpass/code/client/tunnel/shell"
"natpass/code/client/tunnel/vnc"
"natpass/code/network"
"natpass/code/utils"
"os"
"path/filepath"
rt "runtime"
Expand Down Expand Up @@ -38,7 +40,8 @@ func showVersion() {
}

type app struct {
cfg *global.Configure
confDir string
cfg *global.Configure
}

func (a *app) Start(s service.Service) error {
Expand All @@ -51,7 +54,11 @@ func (a *app) run() {
// http.ListenAndServe(":9000", nil)
// }()

logging.SetSizeRotate(a.cfg.LogDir, "np-cli", int(a.cfg.LogSize.Bytes()), a.cfg.LogRotate, true)
stdout := true
if rt.GOOS == "windows" {
stdout = false
}
logging.SetSizeRotate(a.cfg.LogDir, "np-cli", int(a.cfg.LogSize.Bytes()), a.cfg.LogRotate, stdout)
defer logging.Flush()

pl := pool.New(a.cfg)
Expand All @@ -67,6 +74,10 @@ func (a *app) run() {
sh := shell.New(t)
mgr.Add(sh)
go sh.Handle(pl)
case "vnc":
v := vnc.New(t)
mgr.Add(v)
go v.Handle(pl)
}
}

Expand All @@ -91,6 +102,8 @@ func (a *app) run() {
connect(mgr, conn, msg)
case network.ConnectRequest_shell:
shellCreate(mgr, conn, msg)
case network.ConnectRequest_vnc:
vncCreate(a.confDir, mgr, conn, msg)
}
default:
linkID = msg.GetLinkId()
Expand Down Expand Up @@ -124,6 +137,9 @@ func main() {
conf := flag.String("conf", "", "configure file path")
version := flag.Bool("version", false, "show version info")
act := flag.String("action", "", "install or uninstall")
name := flag.String("name", "", "tunnel name")
vport := flag.Uint("vport", 6155, "vnc worker listen port")
vcursor := flag.Bool("vcursor", false, "vnc show cursor")
flag.Parse()

if *version {
Expand All @@ -136,6 +152,11 @@ func main() {
os.Exit(1)
}

// for test
// work := worker.NewWorker()
// work.TestCapture()
// return

dir, err := filepath.Abs(*conf)
runtime.Assert(err)

Expand All @@ -155,7 +176,22 @@ func main() {

cfg := global.LoadConf(*conf)

app := &app{cfg: cfg}
if *act == "vnc.worker" {
defer utils.Recover("vnc.worker")
stdout := true
if rt.GOOS == "windows" {
stdout = false
}
// go func() {
// http.ListenAndServe(":9001", nil)
// }()
logging.SetSizeRotate(cfg.LogDir, "np-cli.vnc."+*name, int(cfg.LogSize.Bytes()), cfg.LogRotate, stdout)
defer logging.Flush()
vnc.RunWorker(uint16(*vport), *vcursor)
return
}

app := &app{confDir: *conf, cfg: cfg}
sv, err := service.New(app, appCfg)
runtime.Assert(err)

Expand Down
3 changes: 2 additions & 1 deletion code/client/pool/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (conn *Conn) AddLink(id string) {

// RemoveLink detach read message
func (conn *Conn) RemoveLink(id string) {
logging.Info("remove link %s from %d", id, conn.Idx)
conn.Lock()
ch := conn.read[id]
if ch != nil {
Expand Down Expand Up @@ -98,7 +99,7 @@ func (conn *Conn) loopRead(cancel context.CancelFunc) {
defer cancel()
var timeout int
for {
msg, err := conn.conn.ReadMessage(conn.parent.cfg.ReadTimeout)
msg, _, err := conn.conn.ReadMessage(conn.parent.cfg.ReadTimeout)
if err != nil {
if strings.Contains(err.Error(), "i/o timeout") {
timeout++
Expand Down
Loading

0 comments on commit 6279f54

Please sign in to comment.