Files
natpass/code/client/global/conf.go
T
lwch 303b1faa99 1. 修正重启时code-server配置丢失问题
2. 修正code-server的websocket连接断开时无法重连的问题
2022-07-27 14:54:27 +08:00

114 lines
2.8 KiB
Go

package global
import (
"crypto/md5"
"fmt"
"os"
"path/filepath"
"time"
"github.com/lwch/natpass/code/utils"
"github.com/lwch/runtime"
"github.com/lwch/yaml"
)
// Rule rule config
type Rule struct {
Name string `yaml:"name"`
Target string `yaml:"target"`
Type string `yaml:"type"`
LocalAddr string `yaml:"local_addr"`
LocalPort uint16 `yaml:"local_port"`
// shell
Exec string `yaml:"exec"`
Env []string `yaml:"env"`
// vnc
Fps uint32 `yaml:"fps"`
}
// Configure client configure
type Configure struct {
ID string
Server string
UseSSL bool
Enc [md5.Size]byte
Links int
LogDir string
LogSize utils.Bytes
LogRotate int
ReadTimeout time.Duration
WriteTimeout time.Duration
DashboardEnabled bool
DashboardListen string
DashboardPort uint16
Rules []Rule
CodeDir string
}
// LoadConf load configure file
func LoadConf(dir string) *Configure {
var cfg struct {
ID string `yaml:"id"`
Server string `yaml:"server"`
Secret string `yaml:"secret"`
SSL bool `yaml:"ssl"`
Link struct {
ReadTimeout time.Duration `yaml:"read_timeout"`
WriteTimeout time.Duration `yaml:"write_timeout"`
} `yaml:"link"`
Log struct {
Dir string `yaml:"dir"`
Size utils.Bytes `yaml:"size"`
Rotate int `yaml:"rotate"`
} `yaml:"log"`
Dashboard struct {
Enabled bool `yaml:"enabled"`
Listen string `yaml:"listen"`
Port uint16 `yaml:"port"`
} `yaml:"dashboard"`
Rules []Rule `yaml:"rules"`
CodeDir string `yaml:"codedir"`
}
runtime.Assert(yaml.Decode(dir, &cfg))
for i, t := range cfg.Rules {
switch t.Type {
case "shell", "vnc", "bench", "code-server":
default:
panic(fmt.Sprintf("unsupported type: %s", t.Type))
}
cfg.Rules[i] = t
}
if cfg.Link.ReadTimeout <= 0 {
cfg.Link.ReadTimeout = 5 * time.Second
}
if cfg.Link.WriteTimeout <= 0 {
cfg.Link.WriteTimeout = 5 * time.Second
}
if !filepath.IsAbs(cfg.Log.Dir) {
dir, err := os.Executable()
runtime.Assert(err)
cfg.Log.Dir = filepath.Join(filepath.Dir(dir), cfg.Log.Dir)
}
if !filepath.IsAbs(cfg.CodeDir) {
dir, err := os.Executable()
runtime.Assert(err)
cfg.CodeDir = filepath.Join(filepath.Dir(dir), cfg.CodeDir)
}
return &Configure{
ID: cfg.ID,
Server: cfg.Server,
UseSSL: cfg.SSL,
Enc: md5.Sum([]byte(cfg.Secret)),
ReadTimeout: cfg.Link.ReadTimeout,
WriteTimeout: cfg.Link.WriteTimeout,
LogDir: cfg.Log.Dir,
LogSize: cfg.Log.Size,
LogRotate: cfg.Log.Rotate,
DashboardEnabled: cfg.Dashboard.Enabled,
DashboardListen: cfg.Dashboard.Listen,
DashboardPort: cfg.Dashboard.Port,
Rules: cfg.Rules,
CodeDir: cfg.CodeDir,
}
}