mirror of
https://github.com/lwch/natpass
synced 2025-09-27 10:02:07 +08:00
支持自动生成local_port #76
This commit is contained in:
@@ -224,4 +224,5 @@
|
|||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
1. go版本升级到1.20.11
|
1. 支持自动生成local_port
|
||||||
|
2. go版本升级到1.20.11
|
@@ -30,7 +30,7 @@ func (p *program) shellCreate(mgr *rule.Mgr, conn *conn.Conn, msg *network.Msg)
|
|||||||
create := msg.GetCreq()
|
create := msg.GetCreq()
|
||||||
tn := mgr.GetLinked(create.GetName(), msg.GetFrom())
|
tn := mgr.GetLinked(create.GetName(), msg.GetFrom())
|
||||||
if tn == nil {
|
if tn == nil {
|
||||||
tn = shell.New(global.Rule{
|
tn = shell.New(&global.Rule{
|
||||||
Name: create.GetName(),
|
Name: create.GetName(),
|
||||||
Target: msg.GetFrom(),
|
Target: msg.GetFrom(),
|
||||||
Type: "shell",
|
Type: "shell",
|
||||||
@@ -57,7 +57,7 @@ func (p *program) vncCreate(confDir string, mgr *rule.Mgr, conn *conn.Conn, msg
|
|||||||
create := msg.GetCreq()
|
create := msg.GetCreq()
|
||||||
tn := mgr.GetLinked(create.GetName(), msg.GetFrom())
|
tn := mgr.GetLinked(create.GetName(), msg.GetFrom())
|
||||||
if tn == nil {
|
if tn == nil {
|
||||||
tn = vnc.New(global.Rule{
|
tn = vnc.New(&global.Rule{
|
||||||
Name: create.GetName(),
|
Name: create.GetName(),
|
||||||
Target: msg.GetFrom(),
|
Target: msg.GetFrom(),
|
||||||
Type: "vnc",
|
Type: "vnc",
|
||||||
@@ -92,7 +92,7 @@ func (p *program) codeCreate(confDir string, mgr *rule.Mgr, conn *conn.Conn, msg
|
|||||||
create := msg.GetCreq()
|
create := msg.GetCreq()
|
||||||
tn := mgr.GetLinked(create.GetName(), msg.GetFrom())
|
tn := mgr.GetLinked(create.GetName(), msg.GetFrom())
|
||||||
if tn == nil {
|
if tn == nil {
|
||||||
tn = code.New(global.Rule{
|
tn = code.New(&global.Rule{
|
||||||
Name: create.GetName(),
|
Name: create.GetName(),
|
||||||
Target: msg.GetFrom(),
|
Target: msg.GetFrom(),
|
||||||
Type: "code-server",
|
Type: "code-server",
|
||||||
|
@@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// SendConnectReq send connect request message
|
// SendConnectReq send connect request message
|
||||||
func (conn *Conn) SendConnectReq(id string, cfg global.Rule) {
|
func (conn *Conn) SendConnectReq(id string, cfg *global.Rule) {
|
||||||
var msg network.Msg
|
var msg network.Msg
|
||||||
msg.To = cfg.Target
|
msg.To = cfg.Target
|
||||||
msg.XType = network.Msg_connect_req
|
msg.XType = network.Msg_connect_req
|
||||||
@@ -68,7 +68,7 @@ func (conn *Conn) SendConnectReq(id string, cfg global.Rule) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SendConnectVnc send connect vnc request message
|
// SendConnectVnc send connect vnc request message
|
||||||
func (conn *Conn) SendConnectVnc(id string, cfg global.Rule, quality uint64, showCursor bool) {
|
func (conn *Conn) SendConnectVnc(id string, cfg *global.Rule, quality uint64, showCursor bool) {
|
||||||
var msg network.Msg
|
var msg network.Msg
|
||||||
msg.To = cfg.Target
|
msg.To = cfg.Target
|
||||||
msg.XType = network.Msg_connect_req
|
msg.XType = network.Msg_connect_req
|
||||||
|
@@ -42,7 +42,7 @@ type Configure struct {
|
|||||||
DashboardEnabled bool
|
DashboardEnabled bool
|
||||||
DashboardListen string
|
DashboardListen string
|
||||||
DashboardPort uint16
|
DashboardPort uint16
|
||||||
Rules []Rule
|
Rules []*Rule
|
||||||
CodeDir string
|
CodeDir string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,7 +70,7 @@ func LoadConf(dir string) *Configure {
|
|||||||
Listen string `yaml:"listen"`
|
Listen string `yaml:"listen"`
|
||||||
Port uint16 `yaml:"port"`
|
Port uint16 `yaml:"port"`
|
||||||
} `yaml:"dashboard"`
|
} `yaml:"dashboard"`
|
||||||
Rules []Rule `yaml:"rules"`
|
Rules []*Rule `yaml:"rules"`
|
||||||
CodeDir string `yaml:"codedir"`
|
CodeDir string `yaml:"codedir"`
|
||||||
}
|
}
|
||||||
cfg.ID = "unset"
|
cfg.ID = "unset"
|
||||||
|
14
code/client/global/port.go
Normal file
14
code/client/global/port.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package global
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/lwch/runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GeneratePort() uint16 {
|
||||||
|
l, err := net.ListenTCP("tcp", &net.TCPAddr{})
|
||||||
|
runtime.Assert(err)
|
||||||
|
defer l.Close()
|
||||||
|
return uint16(l.Addr().(*net.TCPAddr).Port)
|
||||||
|
}
|
@@ -15,7 +15,7 @@ import (
|
|||||||
// Bench benchmark handler
|
// Bench benchmark handler
|
||||||
type Bench struct {
|
type Bench struct {
|
||||||
Name string
|
Name string
|
||||||
cfg global.Rule
|
cfg *global.Rule
|
||||||
}
|
}
|
||||||
|
|
||||||
// Link bench link
|
// Link bench link
|
||||||
@@ -39,7 +39,7 @@ func (link *Link) GetPackets() (uint64, uint64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New new benchmark handler
|
// New new benchmark handler
|
||||||
func New(cfg global.Rule) *Bench {
|
func New(cfg *global.Rule) *Bench {
|
||||||
return &Bench{
|
return &Bench{
|
||||||
Name: cfg.Name,
|
Name: cfg.Name,
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
@@ -92,6 +92,10 @@ func (bench *Bench) Handle(conn *conn.Conn) {
|
|||||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
bench.http(conn, w, r)
|
bench.http(conn, w, r)
|
||||||
})
|
})
|
||||||
|
if bench.cfg.LocalPort == 0 {
|
||||||
|
bench.cfg.LocalPort = global.GeneratePort()
|
||||||
|
logging.Info("generate port for %s: %d", bench.Name, bench.cfg.LocalPort)
|
||||||
|
}
|
||||||
svr := &http.Server{
|
svr := &http.Server{
|
||||||
Addr: fmt.Sprintf("%s:%d", bench.cfg.LocalAddr, bench.cfg.LocalPort),
|
Addr: fmt.Sprintf("%s:%d", bench.cfg.LocalAddr, bench.cfg.LocalPort),
|
||||||
Handler: mux,
|
Handler: mux,
|
||||||
|
@@ -20,14 +20,14 @@ import (
|
|||||||
type Code struct {
|
type Code struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
Name string
|
Name string
|
||||||
cfg global.Rule
|
cfg *global.Rule
|
||||||
workspace map[string]*Workspace
|
workspace map[string]*Workspace
|
||||||
readTimeout time.Duration
|
readTimeout time.Duration
|
||||||
writeTimeout time.Duration
|
writeTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// New new code-server handler
|
// New new code-server handler
|
||||||
func New(cfg global.Rule, readTimeout, writeTimeout time.Duration) *Code {
|
func New(cfg *global.Rule, readTimeout, writeTimeout time.Duration) *Code {
|
||||||
return &Code{
|
return &Code{
|
||||||
Name: cfg.Name,
|
Name: cfg.Name,
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
@@ -110,6 +110,10 @@ func (code *Code) Handle(c *conn.Conn) {
|
|||||||
mux.HandleFunc("/info", code.Info)
|
mux.HandleFunc("/info", code.Info)
|
||||||
mux.HandleFunc("/forward/", pf(code.Forward))
|
mux.HandleFunc("/forward/", pf(code.Forward))
|
||||||
mux.HandleFunc("/", pf(code.Render))
|
mux.HandleFunc("/", pf(code.Render))
|
||||||
|
if code.cfg.LocalPort == 0 {
|
||||||
|
code.cfg.LocalPort = global.GeneratePort()
|
||||||
|
logging.Info("generate port for %s: %d", code.Name, code.cfg.LocalPort)
|
||||||
|
}
|
||||||
svr := &http.Server{
|
svr := &http.Server{
|
||||||
Addr: fmt.Sprintf("%s:%d", code.cfg.LocalAddr, code.cfg.LocalPort),
|
Addr: fmt.Sprintf("%s:%d", code.cfg.LocalAddr, code.cfg.LocalPort),
|
||||||
Handler: mux,
|
Handler: mux,
|
||||||
|
@@ -18,14 +18,14 @@ import (
|
|||||||
type Shell struct {
|
type Shell struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
Name string
|
Name string
|
||||||
cfg global.Rule
|
cfg *global.Rule
|
||||||
links map[string]*Link
|
links map[string]*Link
|
||||||
readTimeout time.Duration
|
readTimeout time.Duration
|
||||||
writeTimeout time.Duration
|
writeTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// New new shell
|
// New new shell
|
||||||
func New(cfg global.Rule, readTimeout, writeTimeout time.Duration) *Shell {
|
func New(cfg *global.Rule, readTimeout, writeTimeout time.Duration) *Shell {
|
||||||
return &Shell{
|
return &Shell{
|
||||||
Name: cfg.Name,
|
Name: cfg.Name,
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
@@ -113,6 +113,10 @@ func (shell *Shell) Handle(c *conn.Conn) {
|
|||||||
mux.HandleFunc("/ws/", pf(shell.WS))
|
mux.HandleFunc("/ws/", pf(shell.WS))
|
||||||
mux.HandleFunc("/resize", pf(shell.Resize))
|
mux.HandleFunc("/resize", pf(shell.Resize))
|
||||||
mux.HandleFunc("/", shell.Render)
|
mux.HandleFunc("/", shell.Render)
|
||||||
|
if shell.cfg.LocalPort == 0 {
|
||||||
|
shell.cfg.LocalPort = global.GeneratePort()
|
||||||
|
logging.Info("generate port for %s: %d", shell.Name, shell.cfg.LocalPort)
|
||||||
|
}
|
||||||
svr := &http.Server{
|
svr := &http.Server{
|
||||||
Addr: fmt.Sprintf("%s:%d", shell.cfg.LocalAddr, shell.cfg.LocalPort),
|
Addr: fmt.Sprintf("%s:%d", shell.cfg.LocalAddr, shell.cfg.LocalPort),
|
||||||
Handler: mux,
|
Handler: mux,
|
||||||
|
@@ -19,7 +19,7 @@ import (
|
|||||||
type VNC struct {
|
type VNC struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
Name string
|
Name string
|
||||||
cfg global.Rule
|
cfg *global.Rule
|
||||||
link *Link
|
link *Link
|
||||||
readTimeout time.Duration
|
readTimeout time.Duration
|
||||||
writeTimeout time.Duration
|
writeTimeout time.Duration
|
||||||
@@ -27,7 +27,7 @@ type VNC struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New new vnc
|
// New new vnc
|
||||||
func New(cfg global.Rule, readTimeout, writeTimeout time.Duration) *VNC {
|
func New(cfg *global.Rule, readTimeout, writeTimeout time.Duration) *VNC {
|
||||||
return &VNC{
|
return &VNC{
|
||||||
Name: cfg.Name,
|
Name: cfg.Name,
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
@@ -109,6 +109,10 @@ func (v *VNC) Handle(c *conn.Conn) {
|
|||||||
mux.HandleFunc("/clipboard", pf(v.Clipboard))
|
mux.HandleFunc("/clipboard", pf(v.Clipboard))
|
||||||
mux.HandleFunc("/ws/", pf(v.WS))
|
mux.HandleFunc("/ws/", pf(v.WS))
|
||||||
mux.HandleFunc("/", v.Render)
|
mux.HandleFunc("/", v.Render)
|
||||||
|
if v.cfg.LocalPort == 0 {
|
||||||
|
v.cfg.LocalPort = global.GeneratePort()
|
||||||
|
logging.Info("generate port for %s: %d", v.Name, v.cfg.LocalPort)
|
||||||
|
}
|
||||||
svr := &http.Server{
|
svr := &http.Server{
|
||||||
Addr: fmt.Sprintf("%s:%d", v.cfg.LocalAddr, v.cfg.LocalPort),
|
Addr: fmt.Sprintf("%s:%d", v.cfg.LocalAddr, v.cfg.LocalPort),
|
||||||
Handler: mux,
|
Handler: mux,
|
||||||
|
@@ -7,4 +7,4 @@
|
|||||||
target: remote # 目标客户端ID
|
target: remote # 目标客户端ID
|
||||||
type: code-server # code-server
|
type: code-server # code-server
|
||||||
local_addr: 0.0.0.0 # 本地监听地址
|
local_addr: 0.0.0.0 # 本地监听地址
|
||||||
local_port: 8000 # 本地监听端口号
|
# local_port: 8000 # 本地监听端口号
|
@@ -2,7 +2,7 @@
|
|||||||
target: remote # 目标客户端ID
|
target: remote # 目标客户端ID
|
||||||
type: shell # web shell
|
type: shell # web shell
|
||||||
local_addr: 0.0.0.0 # 本地监听地址
|
local_addr: 0.0.0.0 # 本地监听地址
|
||||||
local_port: 2222 # 本地监听端口号
|
# local_port: 2222 # 本地监听端口号
|
||||||
#exec: /bin/bash # 运行命令
|
#exec: /bin/bash # 运行命令
|
||||||
# windows默认powershell或cmd
|
# windows默认powershell或cmd
|
||||||
# 其他系统bash或sh
|
# 其他系统bash或sh
|
||||||
|
@@ -3,5 +3,5 @@
|
|||||||
target: remote # 目标客户端ID
|
target: remote # 目标客户端ID
|
||||||
type: vnc # web vnc
|
type: vnc # web vnc
|
||||||
local_addr: 0.0.0.0 # 本地监听地址
|
local_addr: 0.0.0.0 # 本地监听地址
|
||||||
local_port: 5900 # 本地监听端口号
|
# local_port: 5900 # 本地监听端口号
|
||||||
fps: 10 # 刷新频率
|
fps: 10 # 刷新频率
|
@@ -10,7 +10,7 @@ shell规则用于创建一个网页端的命令行操作页面
|
|||||||
target: that # 目标客户端ID
|
target: that # 目标客户端ID
|
||||||
type: shell # web shell
|
type: shell # web shell
|
||||||
local_addr: 0.0.0.0 # 本地监听地址
|
local_addr: 0.0.0.0 # 本地监听地址
|
||||||
local_port: 8080 # 本地监听端口号
|
#local_port: 8080 # 本地监听端口号
|
||||||
#exec: /bin/bash # 运行命令
|
#exec: /bin/bash # 运行命令
|
||||||
# windows默认powershell或cmd
|
# windows默认powershell或cmd
|
||||||
# 其他系统bash或sh
|
# 其他系统bash或sh
|
||||||
@@ -21,15 +21,13 @@ shell规则用于创建一个网页端的命令行操作页面
|
|||||||
2. `target`: 对端客户端ID
|
2. `target`: 对端客户端ID
|
||||||
3. `type`: shell
|
3. `type`: shell
|
||||||
4. `local_addr`: 本地监听地址,如只允许局域网访问可绑定在局域网IP地址上
|
4. `local_addr`: 本地监听地址,如只允许局域网访问可绑定在局域网IP地址上
|
||||||
5. `local_port`: 本地监听端口号
|
5. `local_port`: 本地监听端口号,可选
|
||||||
6. `exec`: 连接建立成功后的启动命令
|
6. `exec`: 连接建立成功后的启动命令
|
||||||
- 指定该参数:直接使用设定的命令运行
|
- 指定该参数:直接使用设定的命令运行
|
||||||
- linux系统:优先查找bash命令,若没有则查找sh命令,否则报错
|
- linux系统:优先查找bash命令,若没有则查找sh命令,否则报错
|
||||||
- windows系统:优先查找powershell命令,若没有则查找cmd命令,否则报错
|
- windows系统:优先查找powershell命令,若没有则查找cmd命令,否则报错
|
||||||
7. `env`: 进程启动时的环境变量设置
|
7. `env`: 进程启动时的环境变量设置
|
||||||
|
|
||||||
连接成功后即可使用浏览器访问`local_port`所对应的端口来创建shell,如http://127.0.0.1:8080
|
|
||||||
|
|
||||||
## vnc规则
|
## vnc规则
|
||||||
|
|
||||||
vnc规则用于创建一个网页端的远程桌面操作页面
|
vnc规则用于创建一个网页端的远程桌面操作页面
|
||||||
@@ -38,18 +36,16 @@ vnc规则用于创建一个网页端的远程桌面操作页面
|
|||||||
target: that # 目标客户端ID
|
target: that # 目标客户端ID
|
||||||
type: vnc # web vnc
|
type: vnc # web vnc
|
||||||
local_addr: 0.0.0.0 # 本地监听地址
|
local_addr: 0.0.0.0 # 本地监听地址
|
||||||
local_port: 5900 # 本地监听端口号
|
#local_port: 5900 # 本地监听端口号
|
||||||
fps: 10 # 刷新频率
|
fps: 10 # 刷新频率
|
||||||
|
|
||||||
1. `name`: 该规则名称,必须全局唯一
|
1. `name`: 该规则名称,必须全局唯一
|
||||||
2. `target`: 对端客户端ID
|
2. `target`: 对端客户端ID
|
||||||
3. `type`: shell
|
3. `type`: shell
|
||||||
4. `local_addr`: 本地监听地址,如只允许局域网访问可绑定在局域网IP地址上
|
4. `local_addr`: 本地监听地址,如只允许局域网访问可绑定在局域网IP地址上
|
||||||
5. `local_port`: 本地监听端口号
|
5. `local_port`: 本地监听端口号,可选
|
||||||
6. `fps`: 每秒钟截屏多少次,最高50
|
6. `fps`: 每秒钟截屏多少次,最高50
|
||||||
|
|
||||||
连接成功后即可使用浏览器访问`local_port`所对应的端口来创建vnc,如http://127.0.0.1:5900
|
|
||||||
|
|
||||||
注意:
|
注意:
|
||||||
|
|
||||||
1. 创建vnc连接后远端服务会创建一个子进程进行截屏和键鼠操作,
|
1. 创建vnc连接后远端服务会创建一个子进程进行截屏和键鼠操作,
|
||||||
@@ -70,12 +66,10 @@ vnc规则用于创建一个网页端的code-server页面,主要用于远程开
|
|||||||
target: remote # 目标客户端ID
|
target: remote # 目标客户端ID
|
||||||
type: code-server # code-server
|
type: code-server # code-server
|
||||||
local_addr: 0.0.0.0 # 本地监听地址
|
local_addr: 0.0.0.0 # 本地监听地址
|
||||||
local_port: 8000 # 本地监听端口号
|
#local_port: 8000 # 本地监听端口号
|
||||||
|
|
||||||
1. `name`: 该规则名称,必须全局唯一
|
1. `name`: 该规则名称,必须全局唯一
|
||||||
2. `target`: 对端客户端ID
|
2. `target`: 对端客户端ID
|
||||||
3. `type`: code-server
|
3. `type`: code-server
|
||||||
4. `local_addr`: 本地监听地址,如只允许局域网访问可绑定在局域网IP地址上
|
4. `local_addr`: 本地监听地址,如只允许局域网访问可绑定在局域网IP地址上
|
||||||
5. `local_port`: 本地监听端口号
|
5. `local_port`: 本地监听端口号,可选
|
||||||
|
|
||||||
连接成功后即可使用浏览器访问`local_port`所对应的端口来创建code-server,如http://127.0.0.1:8000
|
|
Reference in New Issue
Block a user