121 lines
3.2 KiB
Go
121 lines
3.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"gitea.loveuer.com/yizhisec/pkg3/logger"
|
|
"github.com/spf13/cobra"
|
|
"yizhisec.com/hsv2/forge/internal/opt"
|
|
"yizhisec.com/hsv2/forge/pkg/downloader"
|
|
"yizhisec.com/hsv2/forge/pkg/resource"
|
|
)
|
|
|
|
func makeYosguard() *cobra.Command {
|
|
const (
|
|
_configTemplate = `
|
|
Web:
|
|
# default listen in docker0
|
|
Host: 172.17.0.1
|
|
Port: 7788
|
|
|
|
UUIDFilePath: /etc/yosguard/uuid
|
|
|
|
# 心跳间隔: 单位秒,默认为5
|
|
HeartbeatDuration: 5
|
|
|
|
# 控制器 yosguard 地址
|
|
ControllerServer:
|
|
Host: dasheng.zhsftech.debug
|
|
Port: 443
|
|
|
|
# True: 作为控制器运行; False: 不作为控制器运行
|
|
AsController: true
|
|
|
|
# True: 作为网关运行; False: 不作为网关运行
|
|
AsGateway: false
|
|
|
|
Database:
|
|
SQLite:
|
|
DBPath: "/etc/yosguard/db/yosguard.db"
|
|
SQLPath: "/etc/yosguard/db/create.sql"`
|
|
_service = `
|
|
[Unit]
|
|
Description=YiZhiSec YOSGuard
|
|
After=network.target
|
|
|
|
[Service]
|
|
Type=simple
|
|
User=root
|
|
ExecStart=/usr/local/bin/yosguard web --host __host__
|
|
StandardOutput=journal
|
|
StandardError=journal
|
|
Nice=-20
|
|
Restart=always
|
|
RestartSec=15
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target`
|
|
)
|
|
_cmd := &cobra.Command{
|
|
Use: "yosguard",
|
|
Aliases: []string{"YOS"},
|
|
Short: "Make Yosguard",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
var (
|
|
err error
|
|
location = filepath.Join(opt.Cfg.Make.Dir, "dependency", "yosguard")
|
|
)
|
|
|
|
logger.Info("MakeYosguard: 开始构建 yosguard...")
|
|
|
|
if err = os.MkdirAll(location, 0755); err != nil {
|
|
logger.Error("MakeYosguard: 创建 yosguard 目录失败")
|
|
logger.Debug("MakeYosguard: 创建 yosguard 目录失败: %v", err)
|
|
return err
|
|
}
|
|
|
|
// 1. download yosguard bin: https://artifactory.yizhisec.com:443/artifactory/filestore/hsv2/bin/yosguard
|
|
_url := "https://artifactory.yizhisec.com:443/artifactory/filestore/hsv2/bin/yosguard"
|
|
logger.Debug("MakeYosguard: start download bin from %s", _url)
|
|
if err = downloader.Download(
|
|
cmd.Context(),
|
|
_url,
|
|
filepath.Join(location, "yosguard"),
|
|
downloader.WithInsecureSkipVerify(),
|
|
downloader.WithFileMode(0755),
|
|
); err != nil {
|
|
logger.Error("MakeYosguard: 下载 yosguard 失败")
|
|
logger.Debug("MakeYosguard: 下载 yosguard 失败: %v", err)
|
|
return err
|
|
}
|
|
|
|
// 2. generate config_template.yml
|
|
if err = os.WriteFile(filepath.Join(location, "config_template.yml"), []byte(_configTemplate), 0644); err != nil {
|
|
logger.Error("MakeYosguard: 生成 config_template.yml 失败")
|
|
logger.Debug("MakeYosguard: 生成 config_template.yml 失败: %v", err)
|
|
return err
|
|
}
|
|
|
|
// 3. generate create.sql
|
|
if err = os.WriteFile(filepath.Join(location, "create.sql"), resource.SQLYosguard, 0644); err != nil {
|
|
logger.Error("MakeYosguard: 生成 create.sql 失败")
|
|
logger.Debug("MakeYosguard: 生成 create.sql 失败: %v", err)
|
|
return err
|
|
}
|
|
|
|
// 4. generate systemd file
|
|
if err = os.WriteFile(filepath.Join(location, "yosguard.service"), []byte(_service), 0644); err != nil {
|
|
logger.Error("MakeYosguard: 生成 yosguard.service 失败")
|
|
logger.Debug("MakeYosguard: 生成 yosguard.service 失败: %v", err)
|
|
return err
|
|
}
|
|
|
|
logger.Info("MakeYosguard: 构建 yosguard成功!!!")
|
|
return nil
|
|
},
|
|
}
|
|
|
|
return _cmd
|
|
}
|