Skip to content

Commit

Permalink
Merge pull request #6 from lwch/dev
Browse files Browse the repository at this point in the history
v0.4.0
  • Loading branch information
lwch authored Oct 11, 2021
2 parents 01f1b8e + ccc6edc commit 8f77a5d
Show file tree
Hide file tree
Showing 37 changed files with 516 additions and 602 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@

# v0.3.0

1. 新增shell隧道的支持
1. 新增shell隧道的支持

# v0.4.0

1. 配置文件支持include语法
2. 通用的握手方式,支持tcp和shell
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ windows命令行效果

## TODO

1. 支持include的yaml配置文件
2. 通用的connect、connect_response、disconnect消息
1. ~~支持include的yaml配置文件~~
2. ~~通用的connect、connect_response、disconnect消息~~
3. 所有隧道的portal页面
4. web远程桌面
5. 流量监控统计页面,server还是client?
4. 文件传输
5. web远程桌面
6. 流量监控统计页面,server还是client?
2 changes: 1 addition & 1 deletion build
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

VERSION="0.3.0"
VERSION="0.4.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'`
Expand Down
21 changes: 17 additions & 4 deletions build_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/lwch/runtime"
)

const version = "0.3.0"
const version = "0.4.0"
const buildDir = "tmp"
const releaseDir = "release"

Expand All @@ -30,6 +30,7 @@ type target struct {
packExt string
}

// go tool dist list
var targets = []target{
// freebsd
{"freebsd", "386", "", ".tar.gz"},
Expand All @@ -41,6 +42,11 @@ var targets = []target{
{"linux", "amd64", "", ".tar.gz"},
{"linux", "arm", "", ".tar.gz"},
{"linux", "arm64", "", ".tar.gz"},
// mips
{"linux", "mips", "", ".tar.gz"},
{"linux", "mips64", "", ".tar.gz"},
{"linux", "mips64le", "", ".tar.gz"},
{"linux", "mipsle", "", ".tar.gz"},
// netbsd
{"netbsd", "386", "", ".tar.gz"},
{"netbsd", "amd64", "", ".tar.gz"},
Expand Down Expand Up @@ -82,9 +88,16 @@ func build(t target) {
os.RemoveAll(buildDir)
runtime.Assert(os.MkdirAll(buildDir, 0755))

err := copyFile(path.Join("conf", "client.yaml"), path.Join(buildDir, "client.yaml"))
runtime.Assert(err)
err = copyFile(path.Join("conf", "server.yaml"), path.Join(buildDir, "server.yaml"))
err := filepath.Walk("conf", func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
path = strings.TrimPrefix(path, "conf")
if info.IsDir() {
return os.MkdirAll(filepath.Join(buildDir, path), 0755)
}
return copyFile("conf"+path, filepath.Join(buildDir, path))
})
runtime.Assert(err)
err = copyFile("CHANGELOG.md", path.Join(buildDir, "CHANGELOG.md"))
runtime.Assert(err)
Expand Down
19 changes: 10 additions & 9 deletions code/client/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ func connect(conn *pool.Conn, msg *network.Msg) {
if req.GetXType() == network.ConnectRequest_udp {
dial = "udp"
}
link, err := net.Dial(dial, fmt.Sprintf("%s:%d", req.GetAddr(), req.GetPort()))
addr := req.GetCaddr()
link, err := net.Dial(dial, fmt.Sprintf("%s:%d", addr.GetAddr(), addr.GetPort()))
if err != nil {
logging.Error("connect to %s:%d failed, err=%v", req.GetAddr(), req.GetPort(), err)
logging.Error("connect to %s:%d failed, err=%v", addr.GetAddr(), addr.GetPort(), err)
conn.SendConnectError(msg.GetFrom(), msg.GetFromIdx(), msg.GetLinkId(), err.Error())
return
}
Expand All @@ -33,8 +34,8 @@ func connect(conn *pool.Conn, msg *network.Msg) {
Type: dial,
LocalAddr: host,
LocalPort: uint16(port),
RemoteAddr: req.GetAddr(),
RemotePort: uint16(req.GetPort()),
RemoteAddr: addr.GetAddr(),
RemotePort: uint16(addr.GetPort()),
})
lk := tunnel.NewLink(tn, msg.GetLinkId(), msg.GetFrom(), link, conn)
lk.SetTargetIdx(msg.GetFromIdx())
Expand All @@ -44,22 +45,22 @@ func connect(conn *pool.Conn, msg *network.Msg) {
}

func shellCreate(conn *pool.Conn, msg *network.Msg) {
create := msg.GetScreate()
create := msg.GetCreq()
sh := shell.New(global.Tunnel{
Name: create.GetName(),
Target: msg.GetFrom(),
Type: "shell",
Exec: create.GetExec(),
Env: create.GetEnv(),
Exec: create.GetCshell().GetExec(),
Env: create.GetCshell().GetEnv(),
})
lk := shell.NewLink(sh, msg.GetLinkId(), msg.GetFrom(), conn)
lk.SetTargetIdx(msg.GetFromIdx())
err := lk.Exec()
if err != nil {
logging.Error("create shell failed: %v", err)
conn.SendShellCreatedError(msg.GetFrom(), msg.GetFromIdx(), msg.GetLinkId(), err.Error())
conn.SendConnectError(msg.GetFrom(), msg.GetFromIdx(), msg.GetLinkId(), err.Error())
return
}
conn.SendShellCreatedOK(msg.GetFrom(), msg.GetFromIdx(), msg.GetLinkId())
conn.SendConnectOK(msg.GetFrom(), msg.GetFromIdx(), msg.GetLinkId())
lk.Forward()
}
8 changes: 2 additions & 6 deletions code/client/global/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package global
import (
"crypto/md5"
"natpass/code/utils"
"os"
"time"

"github.com/lwch/runtime"
"gopkg.in/yaml.v2"
"github.com/lwch/yaml"
)

// Tunnel tunnel config
Expand Down Expand Up @@ -55,10 +54,7 @@ func LoadConf(dir string) *Configure {
} `yaml:"log"`
Tunnel []Tunnel `yaml:"tunnel"`
}
f, err := os.Open(dir)
runtime.Assert(err)
defer f.Close()
runtime.Assert(yaml.NewDecoder(f).Decode(&cfg))
runtime.Assert(yaml.Decode(dir, &cfg))
for i, t := range cfg.Tunnel {
switch t.Type {
case "tcp", "shell":
Expand Down
16 changes: 7 additions & 9 deletions code/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,13 @@ func (a *app) run() {
var linkID string
switch msg.GetXType() {
case network.Msg_connect_req:
connect(conn, msg)
case network.Msg_shell_create:
shellCreate(conn, msg)
case network.Msg_connect_rep,
network.Msg_disconnect,
network.Msg_forward,
network.Msg_shell_close,
network.Msg_shell_resize,
network.Msg_shell_data:
switch msg.GetCreq().GetXType() {
case network.ConnectRequest_tcp, network.ConnectRequest_udp:
connect(conn, msg)
case network.ConnectRequest_shell:
shellCreate(conn, msg)
}
default:
linkID = msg.GetLinkId()
}
if len(linkID) > 0 {
Expand Down
2 changes: 2 additions & 0 deletions code/client/pool/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/lwch/logging"
)

// Conn pool connection
type Conn struct {
sync.RWMutex
Idx uint32
Expand Down Expand Up @@ -163,6 +164,7 @@ func (conn *Conn) Reset(id string, msg *network.Msg) {
ch <- msg
}

// ChanUnknown get channel of unknown link id
func (conn *Conn) ChanUnknown() <-chan *network.Msg {
return conn.unknownRead
}
Expand Down
1 change: 1 addition & 0 deletions code/client/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (p *Pool) onClose(idx uint32) {
p.Unlock()
}

// Size get pool size
func (p *Pool) Size() int {
return len(p.conns)
}
75 changes: 0 additions & 75 deletions code/client/pool/send.go
Original file line number Diff line number Diff line change
@@ -1,85 +1,10 @@
package pool

import (
"natpass/code/client/global"
"natpass/code/network"
"time"
)

// SendConnectReq send connect request message
func (conn *Conn) SendConnectReq(id string, cfg global.Tunnel) {
tp := network.ConnectRequest_tcp
if cfg.Type != "tcp" {
tp = network.ConnectRequest_udp
}
var msg network.Msg
msg.To = cfg.Target
msg.XType = network.Msg_connect_req
msg.LinkId = id
msg.Payload = &network.Msg_Creq{
Creq: &network.ConnectRequest{
Name: cfg.Name,
XType: tp,
Addr: cfg.RemoteAddr,
Port: uint32(cfg.RemotePort),
},
}
select {
case conn.write <- &msg:
case <-time.After(conn.parent.cfg.WriteTimeout):
}
}

// SendConnectError send connect error response message
func (conn *Conn) SendConnectError(to string, toIdx uint32, id, info string) {
var msg network.Msg
msg.To = to
msg.ToIdx = toIdx
msg.XType = network.Msg_connect_rep
msg.LinkId = id
msg.Payload = &network.Msg_Crep{
Crep: &network.ConnectResponse{
Ok: false,
Msg: info,
},
}
select {
case conn.write <- &msg:
case <-time.After(conn.parent.cfg.WriteTimeout):
}
}

// SendConnectOK send connect success response message
func (conn *Conn) SendConnectOK(to string, toIdx uint32, id string) {
var msg network.Msg
msg.To = to
msg.ToIdx = toIdx
msg.XType = network.Msg_connect_rep
msg.LinkId = id
msg.Payload = &network.Msg_Crep{
Crep: &network.ConnectResponse{
Ok: true,
},
}
select {
case conn.write <- &msg:
case <-time.After(conn.parent.cfg.WriteTimeout):
}
}

// SendDisconnect send disconnect message
func (conn *Conn) SendDisconnect(to string, toIdx uint32, id string) {
var msg network.Msg
msg.To = to
msg.ToIdx = toIdx
msg.XType = network.Msg_disconnect
msg.LinkId = id
select {
case conn.write <- &msg:
case <-time.After(conn.parent.cfg.WriteTimeout):
}
}

// SendData send forward data
func (conn *Conn) SendData(to string, toIdx uint32, id string, data []byte) {
dup := func(data []byte) []byte {
Expand Down
98 changes: 98 additions & 0 deletions code/client/pool/send_conn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package pool

import (
"natpass/code/client/global"
"natpass/code/network"
"time"
)

// SendConnectReq send connect request message
func (conn *Conn) SendConnectReq(id string, cfg global.Tunnel) {
var msg network.Msg
msg.To = cfg.Target
msg.XType = network.Msg_connect_req
msg.LinkId = id
switch cfg.Type {
case "tcp":
msg.Payload = &network.Msg_Creq{
Creq: &network.ConnectRequest{
Name: cfg.Name,
XType: network.ConnectRequest_tcp,
Payload: &network.ConnectRequest_Caddr{
Caddr: &network.ConnectAddr{
Addr: cfg.RemoteAddr,
Port: uint32(cfg.RemotePort),
},
},
},
}
case "udp":
case "shell":
msg.Payload = &network.Msg_Creq{
Creq: &network.ConnectRequest{
Name: cfg.Name,
XType: network.ConnectRequest_shell,
Payload: &network.ConnectRequest_Cshell{
Cshell: &network.ConnectShell{
Exec: cfg.Exec,
Env: cfg.Env,
},
},
},
}
}
select {
case conn.write <- &msg:
case <-time.After(conn.parent.cfg.WriteTimeout):
}
}

// SendConnectError send connect error response message
func (conn *Conn) SendConnectError(to string, toIdx uint32, id, info string) {
var msg network.Msg
msg.To = to
msg.ToIdx = toIdx
msg.XType = network.Msg_connect_rep
msg.LinkId = id
msg.Payload = &network.Msg_Crep{
Crep: &network.ConnectResponse{
Ok: false,
Msg: info,
},
}
select {
case conn.write <- &msg:
case <-time.After(conn.parent.cfg.WriteTimeout):
}
}

// SendConnectOK send connect success response message
func (conn *Conn) SendConnectOK(to string, toIdx uint32, id string) {
var msg network.Msg
msg.To = to
msg.ToIdx = toIdx
msg.XType = network.Msg_connect_rep
msg.LinkId = id
msg.Payload = &network.Msg_Crep{
Crep: &network.ConnectResponse{
Ok: true,
},
}
select {
case conn.write <- &msg:
case <-time.After(conn.parent.cfg.WriteTimeout):
}
}

// SendDisconnect send disconnect message
func (conn *Conn) SendDisconnect(to string, toIdx uint32, id string) {
var msg network.Msg
msg.To = to
msg.ToIdx = toIdx
msg.XType = network.Msg_disconnect
msg.LinkId = id
select {
case conn.write <- &msg:
case <-time.After(conn.parent.cfg.WriteTimeout):
}
}
Loading

0 comments on commit 8f77a5d

Please sign in to comment.