1Panel/backend/app/service/website.go

640 lines
18 KiB
Go
Raw Normal View History

2022-10-28 17:04:57 +08:00
package service
import (
2022-11-03 17:06:48 +08:00
"context"
2022-11-24 17:50:47 +08:00
"crypto/x509"
"encoding/json"
2022-11-24 17:50:47 +08:00
"encoding/pem"
2022-11-03 17:06:48 +08:00
"fmt"
2022-12-13 17:20:13 +08:00
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
"github.com/1Panel-dev/1Panel/backend/buserr"
2022-11-29 17:39:10 +08:00
"os"
"path"
"reflect"
"strings"
"time"
2022-10-28 17:04:57 +08:00
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/constant"
2022-11-19 17:16:02 +08:00
"github.com/1Panel-dev/1Panel/backend/utils/files"
2022-11-02 15:19:14 +08:00
"github.com/pkg/errors"
"gorm.io/gorm"
2022-10-28 17:04:57 +08:00
)
type WebsiteService struct {
}
type IWebsiteService interface {
2022-12-13 17:20:13 +08:00
PageWebsite(req request.WebsiteSearch) (int64, []response.WebsiteDTO, error)
CreateWebsite(create request.WebsiteCreate) error
GetWebsiteOptions() ([]string, error)
Backup(domain string) error
2022-12-13 17:20:13 +08:00
Recover(req request.WebsiteRecover) error
RecoverByUpload(req request.WebsiteRecoverByFile) error
UpdateWebsite(req request.WebsiteUpdate) error
DeleteWebsite(req request.WebsiteDelete) error
GetWebsite(id uint) (response.WebsiteDTO, error)
CreateWebsiteDomain(create request.WebsiteDomainCreate) (model.WebsiteDomain, error)
GetWebsiteDomain(websiteId uint) ([]model.WebsiteDomain, error)
2022-12-06 11:42:11 +08:00
DeleteWebsiteDomain(domainId uint) error
2022-12-13 18:54:46 +08:00
GetNginxConfigByScope(req request.NginxScopeReq) (*response.WebsiteNginxConfig, error)
UpdateNginxConfigByScope(req request.NginxConfigUpdate) error
2022-12-14 19:39:32 +08:00
GetWebsiteNginxConfig(websiteId uint) (response.FileInfo, error)
2022-12-13 17:20:13 +08:00
GetWebsiteHTTPS(websiteId uint) (response.WebsiteHTTPS, error)
OpWebsiteHTTPS(req request.WebsiteHTTPSOp) (response.WebsiteHTTPS, error)
PreInstallCheck(req request.WebsiteInstallCheckReq) ([]response.WebsitePreInstallCheck, error)
GetWafConfig(req request.WebsiteWafReq) (response.WebsiteWafConfig, error)
2022-12-06 11:42:11 +08:00
UpdateWafConfig(req request.WebsiteWafUpdate) error
}
func NewWebsiteService() IWebsiteService {
return &WebsiteService{}
}
2022-12-13 17:20:13 +08:00
func (w WebsiteService) PageWebsite(req request.WebsiteSearch) (int64, []response.WebsiteDTO, error) {
var websiteDTOs []response.WebsiteDTO
2022-11-02 15:19:14 +08:00
total, websites, err := websiteRepo.Page(req.Page, req.PageSize)
if err != nil {
return 0, nil, err
}
for _, web := range websites {
2022-12-13 17:20:13 +08:00
websiteDTOs = append(websiteDTOs, response.WebsiteDTO{
Website: web,
2022-11-02 15:19:14 +08:00
})
}
return total, websiteDTOs, nil
}
2022-10-28 17:04:57 +08:00
2022-12-13 17:20:13 +08:00
func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) error {
if exist, _ := websiteRepo.GetBy(websiteRepo.WithDomain(create.PrimaryDomain)); len(exist) > 0 {
return buserr.New(constant.ErrNameIsExist)
}
if exist, _ := websiteRepo.GetBy(websiteRepo.WithAlias(create.Alias)); len(exist) > 0 {
return buserr.New(constant.ErrAliasIsExist)
}
if exist, _ := websiteDomainRepo.GetBy(websiteDomainRepo.WithDomain(create.PrimaryDomain)); len(exist) > 0 {
return buserr.New(constant.ErrDomainIsExist)
}
2022-10-28 17:04:57 +08:00
defaultDate, _ := time.Parse(constant.DateLayout, constant.DefaultDate)
2022-12-13 17:20:13 +08:00
website := &model.Website{
2022-10-28 17:04:57 +08:00
PrimaryDomain: create.PrimaryDomain,
Type: create.Type,
Alias: create.Alias,
Remark: create.Remark,
Status: constant.WebRunning,
ExpireDate: defaultDate,
AppInstallID: create.AppInstallID,
2022-12-13 17:20:13 +08:00
WebsiteGroupID: create.WebsiteGroupID,
2022-11-21 11:27:56 +08:00
Protocol: constant.ProtocolHTTP,
2022-12-12 15:36:45 +08:00
Proxy: create.Proxy,
2022-10-28 17:04:57 +08:00
}
tx, ctx := getTxAndContext()
2022-12-22 15:26:25 +08:00
var appInstall *model.AppInstall
2022-12-12 15:36:45 +08:00
switch create.Type {
case constant.Deployment:
2022-12-13 17:20:13 +08:00
if create.AppType == constant.NewApp {
var req request.AppInstallCreate
req.Name = create.AppInstall.Name
req.AppDetailId = create.AppInstall.AppDetailId
req.Params = create.AppInstall.Params
install, err := ServiceGroupApp.Install(ctx, req)
if err != nil {
return err
}
website.AppInstallID = install.ID
appInstall = install
} else {
install, err := appInstallRepo.GetFirst(commonRepo.WithByID(create.AppInstallID))
if err != nil {
return err
2022-11-21 16:28:51 +08:00
}
appInstall = &install
2022-11-21 16:28:51 +08:00
}
2022-11-02 15:19:14 +08:00
}
2022-10-28 17:04:57 +08:00
if err := websiteRepo.Create(ctx, website); err != nil {
return err
}
2022-12-13 17:20:13 +08:00
var domains []model.WebsiteDomain
domains = append(domains, model.WebsiteDomain{Domain: website.PrimaryDomain, WebsiteID: website.ID, Port: 80})
2022-11-03 17:06:48 +08:00
otherDomainArray := strings.Split(create.OtherDomains, "\n")
for _, domain := range otherDomainArray {
2022-11-19 17:16:02 +08:00
if domain == "" {
continue
}
2022-10-28 17:04:57 +08:00
domainModel, err := getDomain(domain, website.ID)
if err != nil {
tx.Rollback()
return err
}
2022-12-13 17:20:13 +08:00
if reflect.DeepEqual(domainModel, model.WebsiteDomain{}) {
2022-10-28 17:04:57 +08:00
continue
}
domains = append(domains, domainModel)
}
if len(domains) > 0 {
if err := websiteDomainRepo.BatchCreate(ctx, domains); err != nil {
tx.Rollback()
return err
}
}
2022-12-22 15:26:25 +08:00
if err := configDefaultNginx(website, domains, appInstall); err != nil {
2022-10-28 17:04:57 +08:00
tx.Rollback()
return err
}
tx.Commit()
return nil
}
2022-11-02 15:19:14 +08:00
func (w WebsiteService) GetWebsiteOptions() ([]string, error) {
webs, err := websiteRepo.GetBy()
2022-11-29 17:39:10 +08:00
if err != nil {
return nil, err
2022-11-29 17:39:10 +08:00
}
var datas []string
for _, web := range webs {
datas = append(datas, web.PrimaryDomain)
2022-11-29 17:39:10 +08:00
}
return datas, nil
}
func (w WebsiteService) Backup(domain string) error {
2022-11-29 17:39:10 +08:00
localDir, err := loadLocalDir()
if err != nil {
return err
}
fileName := fmt.Sprintf("%s_%s", domain, time.Now().Format("20060102150405"))
backupDir := fmt.Sprintf("website/%s", domain)
2022-11-29 17:39:10 +08:00
if err := handleWebsiteBackup("LOCAL", localDir, backupDir, domain, fileName); err != nil {
return err
2022-11-29 17:39:10 +08:00
}
return nil
}
2022-12-13 17:20:13 +08:00
func (w WebsiteService) RecoverByUpload(req request.WebsiteRecoverByFile) error {
if err := handleUnTar(fmt.Sprintf("%s/%s", req.FileDir, req.FileName), req.FileDir); err != nil {
2022-11-30 10:34:44 +08:00
return err
}
tmpDir := fmt.Sprintf("%s/%s", req.FileDir, strings.ReplaceAll(req.FileName, ".tar.gz", ""))
webJson, err := os.ReadFile(fmt.Sprintf("%s/website.json", tmpDir))
2022-11-30 10:34:44 +08:00
if err != nil {
return err
}
2022-12-13 17:20:13 +08:00
var websiteInfo WebsiteInfo
if err := json.Unmarshal(webJson, &websiteInfo); err != nil {
2022-11-30 10:34:44 +08:00
return err
}
if websiteInfo.WebsiteName != req.WebsiteName || websiteInfo.WebsiteType != req.Type {
return errors.New("上传文件与选中网站不匹配,无法恢复")
2022-11-30 10:34:44 +08:00
}
website, err := websiteRepo.GetFirst(websiteRepo.WithDomain(req.WebsiteName))
2022-11-30 10:34:44 +08:00
if err != nil {
return err
}
if err := handleWebsiteRecover(&website, tmpDir); err != nil {
2022-11-30 10:34:44 +08:00
return err
}
return nil
}
2022-12-13 17:20:13 +08:00
func (w WebsiteService) Recover(req request.WebsiteRecover) error {
website, err := websiteRepo.GetFirst(websiteRepo.WithDomain(req.WebsiteName))
2022-11-30 10:34:44 +08:00
if err != nil {
return err
}
if !strings.Contains(req.BackupName, "/") {
return errors.New("error path of request")
2022-11-30 10:34:44 +08:00
}
fileDir := req.BackupName[:strings.LastIndex(req.BackupName, "/")]
fileName := strings.ReplaceAll(req.BackupName[strings.LastIndex(req.BackupName, "/"):], ".tar.gz", "")
if err := handleUnTar(req.BackupName, fileDir); err != nil {
return err
2022-11-30 10:34:44 +08:00
}
fileDir = fileDir + "/" + fileName
2022-11-30 10:34:44 +08:00
if err := handleWebsiteRecover(&website, fileDir); err != nil {
return err
2022-11-30 10:34:44 +08:00
}
return nil
}
2022-12-13 17:20:13 +08:00
func (w WebsiteService) UpdateWebsite(req request.WebsiteUpdate) error {
2022-11-08 17:21:13 +08:00
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
if err != nil {
return err
}
website.PrimaryDomain = req.PrimaryDomain
2022-12-13 17:20:13 +08:00
website.WebsiteGroupID = req.WebsiteGroupID
2022-11-08 17:21:13 +08:00
website.Remark = req.Remark
return websiteRepo.Save(context.TODO(), &website)
}
2022-12-13 17:20:13 +08:00
func (w WebsiteService) GetWebsite(id uint) (response.WebsiteDTO, error) {
var res response.WebsiteDTO
2022-11-08 17:21:13 +08:00
website, err := websiteRepo.GetFirst(commonRepo.WithByID(id))
if err != nil {
2022-11-16 10:31:35 +08:00
return res, err
2022-11-08 17:21:13 +08:00
}
2022-12-13 17:20:13 +08:00
res.Website = website
2022-11-16 10:31:35 +08:00
return res, nil
2022-11-08 17:21:13 +08:00
}
2022-12-13 17:20:13 +08:00
func (w WebsiteService) DeleteWebsite(req request.WebsiteDelete) error {
2022-11-02 15:19:14 +08:00
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
if err != nil {
return err
}
2022-12-12 15:36:45 +08:00
if err := delNginxConfig(website, req.ForceDelete); err != nil {
2022-11-02 15:19:14 +08:00
return err
}
tx, ctx := getTxAndContext()
if req.DeleteApp {
2022-11-03 17:06:48 +08:00
websites, _ := websiteRepo.GetBy(websiteRepo.WithAppInstallId(website.AppInstallID))
if len(websites) > 1 {
2022-12-12 15:36:45 +08:00
return buserr.New(constant.ErrAppDelete)
2022-11-03 17:06:48 +08:00
}
2022-11-02 15:19:14 +08:00
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID))
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
if !reflect.DeepEqual(model.AppInstall{}, appInstall) {
if err := deleteAppInstall(ctx, appInstall, true); err != nil && !req.ForceDelete {
2022-11-02 15:19:14 +08:00
return err
}
}
}
if req.DeleteBackup {
backups, _ := backupRepo.ListRecord(backupRepo.WithByType("website-"+website.Type), commonRepo.WithByName(website.PrimaryDomain))
if len(backups) > 0 {
fileOp := files.NewFileOp()
for _, b := range backups {
_ = fileOp.DeleteDir(b.FileDir)
}
if err := backupRepo.DeleteRecord(ctx, backupRepo.WithByType("website-"+website.Type), commonRepo.WithByName(website.PrimaryDomain)); err != nil {
tx.Rollback()
return err
}
}
}
2022-11-02 15:19:14 +08:00
if err := websiteRepo.DeleteBy(ctx, commonRepo.WithByID(req.ID)); err != nil {
tx.Rollback()
return err
}
2022-12-13 17:20:13 +08:00
if err := websiteDomainRepo.DeleteBy(ctx, websiteDomainRepo.WithWebsiteId(req.ID)); err != nil {
2022-11-02 15:19:14 +08:00
tx.Rollback()
return err
}
tx.Commit()
return nil
}
2022-11-03 17:06:48 +08:00
2022-12-13 17:20:13 +08:00
func (w WebsiteService) CreateWebsiteDomain(create request.WebsiteDomainCreate) (model.WebsiteDomain, error) {
var domainModel model.WebsiteDomain
2022-11-03 18:02:07 +08:00
var ports []int
var domains []string
2022-11-03 17:06:48 +08:00
2022-12-13 17:20:13 +08:00
website, err := websiteRepo.GetFirst(commonRepo.WithByID(create.WebsiteID))
2022-11-03 18:02:07 +08:00
if err != nil {
return domainModel, err
}
2022-12-13 17:20:13 +08:00
if oldDomains, _ := websiteDomainRepo.GetBy(websiteDomainRepo.WithWebsiteId(create.WebsiteID), websiteDomainRepo.WithPort(create.Port)); len(oldDomains) == 0 {
2022-11-03 18:02:07 +08:00
ports = append(ports, create.Port)
}
domains = append(domains, create.Domain)
if err := addListenAndServerName(website, ports, domains); err != nil {
return domainModel, err
}
2022-12-13 17:20:13 +08:00
domainModel = model.WebsiteDomain{
2022-11-03 18:02:07 +08:00
Domain: create.Domain,
Port: create.Port,
2022-12-13 17:20:13 +08:00
WebsiteID: create.WebsiteID,
2022-11-03 18:02:07 +08:00
}
return domainModel, websiteDomainRepo.Create(context.TODO(), &domainModel)
2022-11-03 17:06:48 +08:00
}
2022-12-13 17:20:13 +08:00
func (w WebsiteService) GetWebsiteDomain(websiteId uint) ([]model.WebsiteDomain, error) {
return websiteDomainRepo.GetBy(websiteDomainRepo.WithWebsiteId(websiteId))
2022-11-03 17:06:48 +08:00
}
func (w WebsiteService) DeleteWebsiteDomain(domainId uint) error {
webSiteDomain, err := websiteDomainRepo.GetFirst(commonRepo.WithByID(domainId))
if err != nil {
return err
}
2022-12-13 17:20:13 +08:00
if websiteDomains, _ := websiteDomainRepo.GetBy(websiteDomainRepo.WithWebsiteId(webSiteDomain.WebsiteID)); len(websiteDomains) == 1 {
2022-11-03 17:06:48 +08:00
return fmt.Errorf("can not delete last domain")
}
2022-12-13 17:20:13 +08:00
website, err := websiteRepo.GetFirst(commonRepo.WithByID(webSiteDomain.WebsiteID))
2022-11-03 17:06:48 +08:00
if err != nil {
return err
}
var ports []int
2022-12-13 17:20:13 +08:00
if oldDomains, _ := websiteDomainRepo.GetBy(websiteDomainRepo.WithWebsiteId(webSiteDomain.WebsiteID), websiteDomainRepo.WithPort(webSiteDomain.Port)); len(oldDomains) == 1 {
2022-11-03 17:06:48 +08:00
ports = append(ports, webSiteDomain.Port)
}
var domains []string
2022-12-13 17:20:13 +08:00
if oldDomains, _ := websiteDomainRepo.GetBy(websiteDomainRepo.WithWebsiteId(webSiteDomain.WebsiteID), websiteDomainRepo.WithDomain(webSiteDomain.Domain)); len(oldDomains) == 1 {
2022-11-03 17:06:48 +08:00
domains = append(domains, webSiteDomain.Domain)
}
if len(ports) > 0 || len(domains) > 0 {
if err := deleteListenAndServerName(website, ports, domains); err != nil {
return err
}
}
return websiteDomainRepo.DeleteBy(context.TODO(), commonRepo.WithByID(domainId))
}
2022-11-07 16:19:05 +08:00
2022-12-13 18:54:46 +08:00
func (w WebsiteService) GetNginxConfigByScope(req request.NginxScopeReq) (*response.WebsiteNginxConfig, error) {
2022-11-07 16:19:05 +08:00
keys, ok := dto.ScopeKeyMap[req.Scope]
if !ok || len(keys) == 0 {
return nil, nil
}
2022-12-13 17:20:13 +08:00
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
2022-11-07 16:19:05 +08:00
if err != nil {
return nil, err
}
2022-12-13 17:20:13 +08:00
var config response.WebsiteNginxConfig
2022-12-01 00:41:50 +08:00
params, err := getNginxParamsByKeys(constant.NginxScopeServer, keys, &website)
if err != nil {
return nil, err
}
config.Params = params
config.Enable = len(params[0].Params) > 0
2022-11-07 16:19:05 +08:00
2022-12-01 00:41:50 +08:00
return &config, nil
2022-11-07 16:19:05 +08:00
}
2022-12-13 18:54:46 +08:00
func (w WebsiteService) UpdateNginxConfigByScope(req request.NginxConfigUpdate) error {
2022-11-07 16:19:05 +08:00
keys, ok := dto.ScopeKeyMap[req.Scope]
if !ok || len(keys) == 0 {
return nil
}
2022-12-13 17:20:13 +08:00
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
2022-11-07 16:19:05 +08:00
if err != nil {
return err
}
2022-12-13 18:54:46 +08:00
if req.Operate == constant.ConfigDel {
2022-12-01 00:41:50 +08:00
return deleteNginxConfig(constant.NginxScopeServer, keys, &website)
2022-11-08 15:42:31 +08:00
}
2022-12-01 00:41:50 +08:00
params := getNginxParams(req.Params, keys)
2022-12-13 18:54:46 +08:00
if req.Operate == constant.ConfigNew {
2022-12-01 00:41:50 +08:00
if _, ok := dto.StaticFileKeyMap[req.Scope]; ok {
params = getNginxParamsFromStaticFile(req.Scope, params)
}
}
return updateNginxConfig(constant.NginxScopeServer, params, &website)
2022-11-07 16:19:05 +08:00
}
2022-11-19 17:16:02 +08:00
2022-12-14 19:39:32 +08:00
func (w WebsiteService) GetWebsiteNginxConfig(websiteId uint) (response.FileInfo, error) {
2022-11-19 17:16:02 +08:00
website, err := websiteRepo.GetFirst(commonRepo.WithByID(websiteId))
if err != nil {
2022-12-14 19:39:32 +08:00
return response.FileInfo{}, err
2022-11-19 17:16:02 +08:00
}
2022-12-13 20:03:54 +08:00
nginxApp, err := appRepo.GetFirst(appRepo.WithKey(constant.AppNginx))
2022-11-19 17:16:02 +08:00
if err != nil {
2022-12-14 19:39:32 +08:00
return response.FileInfo{}, err
2022-11-19 17:16:02 +08:00
}
nginxInstall, err := appInstallRepo.GetFirst(appInstallRepo.WithAppId(nginxApp.ID))
if err != nil {
2022-12-14 19:39:32 +08:00
return response.FileInfo{}, err
2022-11-19 17:16:02 +08:00
}
2022-12-13 20:03:54 +08:00
configPath := path.Join(constant.AppInstallDir, constant.AppNginx, nginxInstall.Name, "conf", "conf.d", website.Alias+".conf")
2022-11-19 17:16:02 +08:00
info, err := files.NewFileInfo(files.FileOption{
Path: configPath,
Expand: true,
})
if err != nil {
2022-12-14 19:39:32 +08:00
return response.FileInfo{}, err
2022-11-19 17:16:02 +08:00
}
2022-12-14 19:39:32 +08:00
return response.FileInfo{FileInfo: *info}, nil
2022-11-19 17:16:02 +08:00
}
2022-11-20 18:32:56 +08:00
2022-12-13 17:20:13 +08:00
func (w WebsiteService) GetWebsiteHTTPS(websiteId uint) (response.WebsiteHTTPS, error) {
2022-11-20 18:32:56 +08:00
website, err := websiteRepo.GetFirst(commonRepo.WithByID(websiteId))
if err != nil {
2022-12-13 17:20:13 +08:00
return response.WebsiteHTTPS{}, err
2022-11-20 18:32:56 +08:00
}
2022-12-13 17:20:13 +08:00
var res response.WebsiteHTTPS
if website.WebsiteSSLID == 0 {
2022-11-20 18:32:56 +08:00
res.Enable = false
return res, nil
}
2022-12-13 17:20:13 +08:00
websiteSSL, err := websiteSSLRepo.GetFirst(commonRepo.WithByID(website.WebsiteSSLID))
2022-11-20 18:32:56 +08:00
if err != nil {
2022-12-13 17:20:13 +08:00
return response.WebsiteHTTPS{}, err
2022-11-20 18:32:56 +08:00
}
res.SSL = websiteSSL
res.Enable = true
return res, nil
}
2022-12-13 17:20:13 +08:00
func (w WebsiteService) OpWebsiteHTTPS(req request.WebsiteHTTPSOp) (response.WebsiteHTTPS, error) {
2022-11-20 18:32:56 +08:00
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
if err != nil {
2022-12-13 17:20:13 +08:00
return response.WebsiteHTTPS{}, err
2022-11-20 18:32:56 +08:00
}
2022-11-24 17:50:47 +08:00
var (
2022-12-13 17:20:13 +08:00
res response.WebsiteHTTPS
websiteSSL model.WebsiteSSL
2022-11-24 17:50:47 +08:00
)
2022-11-20 18:32:56 +08:00
res.Enable = req.Enable
2022-12-13 17:20:13 +08:00
if req.Type == constant.SSLExisted {
2022-11-24 17:50:47 +08:00
websiteSSL, err = websiteSSLRepo.GetFirst(commonRepo.WithByID(req.WebsiteSSLID))
if err != nil {
2022-12-13 17:20:13 +08:00
return response.WebsiteHTTPS{}, err
2022-11-24 17:50:47 +08:00
}
2022-12-13 17:20:13 +08:00
website.WebsiteSSLID = websiteSSL.ID
2022-11-20 18:32:56 +08:00
if err := websiteRepo.Save(context.TODO(), &website); err != nil {
2022-12-13 17:20:13 +08:00
return response.WebsiteHTTPS{}, err
2022-11-20 18:32:56 +08:00
}
2022-11-24 17:50:47 +08:00
res.SSL = websiteSSL
}
2022-12-13 17:20:13 +08:00
if req.Type == constant.SSLManual {
2022-11-24 17:50:47 +08:00
certBlock, _ := pem.Decode([]byte(req.Certificate))
cert, err := x509.ParseCertificate(certBlock.Bytes)
if err != nil {
2022-12-13 17:20:13 +08:00
return response.WebsiteHTTPS{}, err
2022-11-24 17:50:47 +08:00
}
websiteSSL.ExpireDate = cert.NotAfter
websiteSSL.StartDate = cert.NotBefore
websiteSSL.Type = cert.Issuer.CommonName
websiteSSL.Organization = cert.Issuer.Organization[0]
websiteSSL.PrimaryDomain = cert.Subject.CommonName
if len(cert.Subject.Names) > 0 {
var domains []string
for _, name := range cert.Subject.Names {
if v, ok := name.Value.(string); ok {
if v != cert.Subject.CommonName {
domains = append(domains, v)
}
}
}
if len(domains) > 0 {
websiteSSL.Domains = strings.Join(domains, "")
}
}
2022-12-14 15:26:51 +08:00
websiteSSL.Provider = constant.Manual
2022-11-24 17:50:47 +08:00
websiteSSL.PrivateKey = req.PrivateKey
websiteSSL.Pem = req.Certificate
res.SSL = websiteSSL
2022-11-20 18:32:56 +08:00
}
if req.Enable {
2022-11-21 11:27:56 +08:00
website.Protocol = constant.ProtocolHTTPS
2022-11-24 17:50:47 +08:00
if err := applySSL(website, websiteSSL); err != nil {
2022-12-13 17:20:13 +08:00
return response.WebsiteHTTPS{}, err
2022-11-20 18:32:56 +08:00
}
2022-11-21 11:27:56 +08:00
} else {
website.Protocol = constant.ProtocolHTTP
2022-12-13 17:20:13 +08:00
website.WebsiteSSLID = 0
2022-11-28 15:11:39 +08:00
if err := deleteListenAndServerName(website, []int{443}, []string{}); err != nil {
2022-12-13 17:20:13 +08:00
return response.WebsiteHTTPS{}, err
2022-11-28 15:11:39 +08:00
}
2022-12-01 00:41:50 +08:00
if err := deleteNginxConfig(constant.NginxScopeServer, getKeysFromStaticFile(dto.SSL), &website); err != nil {
2022-12-13 17:20:13 +08:00
return response.WebsiteHTTPS{}, err
2022-11-24 17:50:47 +08:00
}
2022-11-20 18:32:56 +08:00
}
2022-11-24 17:50:47 +08:00
tx, ctx := getTxAndContext()
if websiteSSL.ID == 0 {
if err := websiteSSLRepo.Create(ctx, &websiteSSL); err != nil {
2022-12-13 17:20:13 +08:00
return response.WebsiteHTTPS{}, err
2022-11-24 17:50:47 +08:00
}
2022-12-13 17:20:13 +08:00
website.WebsiteSSLID = websiteSSL.ID
2022-11-24 17:50:47 +08:00
}
if err := websiteRepo.Save(ctx, &website); err != nil {
2022-12-13 17:20:13 +08:00
return response.WebsiteHTTPS{}, err
2022-11-24 17:50:47 +08:00
}
tx.Commit()
2022-11-20 18:32:56 +08:00
return res, nil
}
2022-12-13 17:20:13 +08:00
func (w WebsiteService) PreInstallCheck(req request.WebsiteInstallCheckReq) ([]response.WebsitePreInstallCheck, error) {
2022-12-04 17:45:54 +08:00
var (
2022-12-13 17:20:13 +08:00
res []response.WebsitePreInstallCheck
2022-12-04 17:45:54 +08:00
checkIds []uint
showErr = false
)
2022-12-13 20:03:54 +08:00
app, err := appRepo.GetFirst(appRepo.WithKey(constant.AppNginx))
2022-12-04 17:45:54 +08:00
if err != nil {
return nil, err
}
appInstall, _ := appInstallRepo.GetFirst(appInstallRepo.WithAppId(app.ID))
if reflect.DeepEqual(appInstall, model.AppInstall{}) {
2022-12-13 17:20:13 +08:00
res = append(res, response.WebsitePreInstallCheck{
2022-12-04 17:45:54 +08:00
Name: appInstall.Name,
AppName: app.Name,
Status: buserr.WithMessage(constant.ErrNotInstall, app.Name, nil).Error(),
Version: appInstall.Version,
})
showErr = true
} else {
checkIds = append(req.InstallIds, appInstall.ID)
}
for _, id := range checkIds {
if err := syncById(id); err != nil {
return nil, err
}
}
if len(checkIds) > 0 {
installList, _ := appInstallRepo.GetBy(commonRepo.WithIdsIn(checkIds))
for _, install := range installList {
2022-12-13 17:20:13 +08:00
res = append(res, response.WebsitePreInstallCheck{
2022-12-04 17:45:54 +08:00
Name: install.Name,
Status: install.Status,
Version: install.Version,
AppName: install.App.Name,
})
if install.Status != constant.StatusRunning {
showErr = true
}
}
}
if showErr {
return res, nil
} else {
return nil, nil
}
}
2022-12-06 11:42:11 +08:00
2022-12-13 17:20:13 +08:00
func (w WebsiteService) GetWafConfig(req request.WebsiteWafReq) (response.WebsiteWafConfig, error) {
var res response.WebsiteWafConfig
2022-12-06 11:42:11 +08:00
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
if err != nil {
return res, nil
}
2022-12-06 15:55:42 +08:00
res.Enable = true
if req.Key != "" {
params, err := getNginxParamsByKeys(constant.NginxScopeServer, []string{"set"}, &website)
if err != nil {
return res, nil
}
for _, param := range params {
if param.Params[0] == req.Key {
res.Enable = len(param.Params) > 1 && param.Params[1] == "on"
break
}
2022-12-06 11:42:11 +08:00
}
}
2022-12-06 15:55:42 +08:00
2022-12-06 11:42:11 +08:00
nginxFull, err := getNginxFull(&website)
if err != nil {
return res, nil
}
filePath := path.Join(nginxFull.SiteDir, "sites", website.Alias, "waf", "rules", req.Rule)
content, err := os.ReadFile(filePath)
if err != nil {
return res, nil
}
res.FilePath = filePath
res.Content = string(content)
return res, nil
}
func (w WebsiteService) UpdateWafConfig(req request.WebsiteWafUpdate) error {
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
if err != nil {
return nil
}
updateValue := "on"
if !req.Enable {
updateValue = "off"
}
return updateNginxConfig(constant.NginxScopeServer, []dto.NginxParam{
{Name: "set", Params: []string{req.Key, updateValue}},
}, &website)
}