package maker import ( "context" "fmt" "os" "path/filepath" "gitea.loveuer.com/yizhisec/pkg3/logger" "yizhisec.com/hsv2/forge/pkg/downloader" "yizhisec.com/hsv2/forge/pkg/resource" ) type YosguardOpt func(*yosguardOpt) type yosguardOpt struct { Pkg bool } func WithYosguardPkg(makePkg bool) YosguardOpt { return func(o *yosguardOpt) { o.Pkg = makePkg } } func (m *maker) Yosguard(ctx context.Context, opts ...YosguardOpt) error { const ( _config = ` Web: Host: __ip__ Port: 7788 Database: SQLite: DBPath: /etc/yosguard/db/yosguard.db HeartbeatDuration: 5 UUIDFilePath: /etc/yosguard/uuid ` systemdService = ` [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` binURL = "https://artifactory.yizhisec.com:443/artifactory/filestore/hsv2/bin/yosguard" ) var ( location = filepath.Join(m.workdir, "dependency", "yosguard") o = &yosguardOpt{} ) for _, fn := range opts { fn(o) } logger.Info("☑️ maker.Yosguard: 开始构建 yosguard...") logger.Debug("☑️ maker.Yosguard: 创建目录 %s", location) if err := os.RemoveAll(location); err != nil { logger.Debug("❌ maker.Yosguard: 删除 yosguard 目录失败, dir = %s, err = %v", location, err) return err } if err := os.MkdirAll(location, 0755); err != nil { logger.Debug("❌ maker.Yosguard: 创建 yosguard 目录失败: %v", err) return err } logger.Debug("✅ maker.Yosguard: 创建目录 %s 成功", location) logger.Debug("☑️ maker.Yosguard: 下载 yosguard 二进制文件..., url = %s, dest = %s", binURL, filepath.Join(location, "yosguard")) if err := downloader.Download( ctx, binURL, filepath.Join(location, "yosguard"), downloader.WithInsecureSkipVerify(), downloader.WithFileMode(0755), ); err != nil { logger.Debug("❌ maker.Yosguard: 下载 yosguard 失败, url = %s, err = %v", binURL, err) return err } logger.Debug("✅ maker.Yosguard: 下载 yosguard 成功, url = %s", binURL) logger.Debug("☑️ maker.Yosguard: 写入 config.yml 文件..., dest = %s", filepath.Join(location, "config.yml")) if err := os.WriteFile(filepath.Join(location, "config.yml"), []byte(_config), 0644); err != nil { logger.Debug("❌ maker.Yosguard: 写入 config.yml 失败, dest = %s, err = %v", filepath.Join(location, "config.yml"), err) return err } logger.Debug("✅ maker.Yosguard: 写入 config.yml 文件成功, dest = %s", filepath.Join(location, "config.yml")) logger.Debug("☑️ maker.Yosguard: 写入 create.sql 文件..., dest = %s", filepath.Join(location, "create.sql")) if err := os.WriteFile(filepath.Join(location, "create.sql"), resource.SQLYosguard, 0644); err != nil { logger.Debug("❌ maker.Yosguard: 写入 create.sql 失败, dest = %s, err = %v", filepath.Join(location, "create.sql"), err) return err } logger.Debug("✅ maker.Yosguard: 写入 create.sql 文件成功, dest = %s", filepath.Join(location, "create.sql")) logger.Debug("☑️ maker.Yosguard: 写入 yosguard.service 文件..., dest = %s", filepath.Join(location, "yosguard.service")) if err := os.WriteFile(filepath.Join(location, "yosguard.service"), []byte(systemdService), 0644); err != nil { logger.Debug("❌ maker.Yosguard: 写入 yosguard.service 失败, dest = %s, err = %v", filepath.Join(location, "yosguard.service"), err) return err } logger.Debug("✅ maker.Yosguard: 写入 yosguard.service 文件成功, dest = %s", filepath.Join(location, "yosguard.service")) if o.Pkg { _cmd := fmt.Sprintf("cd %s && 7za a -pRrPt7Uo9WM1dkXOJmHps56T8BZY2qA4g -mhe=on yosguard.pkg *", location) logger.Debug("☑️ maker.Yosguard: start create yosguard.pkg by 7zip, cmd = '%s'", _cmd) if err := m.RunCommand(ctx, location, _cmd); err != nil { logger.Debug("❌ maker.Yosguard: 创建 yosguard.pkg 失败, err = %v", err) return err } } logger.Info("✅ maker.Yosguard: 构建 yosguard 成功!!!") return nil }