2022-10-28 09:04:57 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2023-04-05 16:09:58 +00:00
|
|
|
"bufio"
|
2023-04-14 08:01:06 +00:00
|
|
|
"bytes"
|
2022-11-03 09:06:48 +00:00
|
|
|
"context"
|
2022-11-24 09:50:47 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2023-04-12 06:22:29 +00:00
|
|
|
"errors"
|
2022-11-03 09:06:48 +00:00
|
|
|
"fmt"
|
2023-04-12 06:16:29 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
|
2023-04-18 10:46:58 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2023-05-06 07:27:34 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
2023-04-23 10:15:38 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/nginx"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/nginx/components"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/nginx/parser"
|
2023-04-14 08:01:06 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/cmd/server/nginx_conf"
|
2023-04-25 10:10:17 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2023-05-24 08:31:18 +00:00
|
|
|
"gopkg.in/ini.v1"
|
2023-04-12 06:22:29 +00:00
|
|
|
"gorm.io/gorm"
|
2022-11-29 09:39:10 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"reflect"
|
2023-04-04 10:54:04 +00:00
|
|
|
"regexp"
|
2023-05-04 06:28:38 +00:00
|
|
|
"strconv"
|
2022-11-29 09:39:10 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-02-21 11:06:24 +00: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/app/repo"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
2023-03-02 05:54:07 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-02-21 11:06:24 +00:00
|
|
|
|
2022-10-28 09:04:57 +00: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 09:16:02 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
2022-10-28 09:04:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type WebsiteService struct {
|
|
|
|
}
|
|
|
|
|
2022-11-30 07:47:11 +00:00
|
|
|
type IWebsiteService interface {
|
2022-12-13 09:20:13 +00:00
|
|
|
PageWebsite(req request.WebsiteSearch) (int64, []response.WebsiteDTO, error)
|
2023-01-03 08:56:36 +00:00
|
|
|
GetWebsites() ([]response.WebsiteDTO, error)
|
2023-04-12 06:16:29 +00:00
|
|
|
CreateWebsite(create request.WebsiteCreate) error
|
2022-12-26 08:09:21 +00:00
|
|
|
OpWebsite(req request.WebsiteOp) error
|
2022-11-30 07:47:11 +00:00
|
|
|
GetWebsiteOptions() ([]string, error)
|
2022-12-13 09:20:13 +00:00
|
|
|
UpdateWebsite(req request.WebsiteUpdate) error
|
2023-04-12 06:22:29 +00:00
|
|
|
DeleteWebsite(req request.WebsiteDelete) error
|
2022-12-13 09:20:13 +00:00
|
|
|
GetWebsite(id uint) (response.WebsiteDTO, error)
|
|
|
|
CreateWebsiteDomain(create request.WebsiteDomainCreate) (model.WebsiteDomain, error)
|
|
|
|
GetWebsiteDomain(websiteId uint) ([]model.WebsiteDomain, error)
|
2022-12-06 03:42:11 +00:00
|
|
|
DeleteWebsiteDomain(domainId uint) error
|
2022-12-13 10:54:46 +00:00
|
|
|
GetNginxConfigByScope(req request.NginxScopeReq) (*response.WebsiteNginxConfig, error)
|
|
|
|
UpdateNginxConfigByScope(req request.NginxConfigUpdate) error
|
2023-04-04 10:54:04 +00:00
|
|
|
GetWebsiteNginxConfig(websiteId uint, configType string) (response.FileInfo, error)
|
2022-12-13 09:20:13 +00:00
|
|
|
GetWebsiteHTTPS(websiteId uint) (response.WebsiteHTTPS, error)
|
2022-12-28 08:07:43 +00:00
|
|
|
OpWebsiteHTTPS(ctx context.Context, req request.WebsiteHTTPSOp) (response.WebsiteHTTPS, error)
|
2022-12-13 09:20:13 +00:00
|
|
|
PreInstallCheck(req request.WebsiteInstallCheckReq) ([]response.WebsitePreInstallCheck, error)
|
|
|
|
GetWafConfig(req request.WebsiteWafReq) (response.WebsiteWafConfig, error)
|
2022-12-06 03:42:11 +00:00
|
|
|
UpdateWafConfig(req request.WebsiteWafUpdate) error
|
2022-12-30 09:39:17 +00:00
|
|
|
UpdateNginxConfigFile(req request.WebsiteNginxUpdate) error
|
|
|
|
OpWebsiteLog(req request.WebsiteLogReq) (*response.WebsiteLog, error)
|
2023-03-28 10:00:06 +00:00
|
|
|
ChangeDefaultServer(id uint) error
|
2023-04-05 16:09:58 +00:00
|
|
|
GetPHPConfig(id uint) (*response.PHPConfig, error)
|
|
|
|
UpdatePHPConfig(req request.WebsitePHPConfigUpdate) error
|
2023-04-12 06:48:30 +00:00
|
|
|
UpdatePHPConfigFile(req request.WebsitePHPFileUpdate) error
|
2023-04-14 08:01:06 +00:00
|
|
|
GetRewriteConfig(req request.NginxRewriteReq) (*response.NginxRewriteRes, error)
|
|
|
|
UpdateRewriteConfig(req request.NginxRewriteUpdate) error
|
2023-04-17 08:54:34 +00:00
|
|
|
UpdateSiteDir(req request.WebsiteUpdateDir) error
|
2023-04-18 10:46:58 +00:00
|
|
|
UpdateSitePermission(req request.WebsiteUpdateDirPermission) error
|
2023-04-23 10:15:38 +00:00
|
|
|
OperateProxy(req request.WebsiteProxyConfig) (err error)
|
|
|
|
GetProxies(id uint) (res []request.WebsiteProxyConfig, err error)
|
2023-04-24 09:48:22 +00:00
|
|
|
UpdateProxyFile(req request.NginxProxyUpdate) (err error)
|
2023-04-25 10:10:17 +00:00
|
|
|
GetAuthBasics(req request.NginxAuthReq) (res response.NginxAuthRes, err error)
|
|
|
|
UpdateAuthBasic(req request.NginxAuthUpdate) (err error)
|
2022-11-30 07:47:11 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 10:00:06 +00:00
|
|
|
func NewIWebsiteService() IWebsiteService {
|
2022-11-30 07:47:11 +00:00
|
|
|
return &WebsiteService{}
|
|
|
|
}
|
|
|
|
|
2022-12-13 09:20:13 +00:00
|
|
|
func (w WebsiteService) PageWebsite(req request.WebsiteSearch) (int64, []response.WebsiteDTO, error) {
|
2022-12-30 08:13:13 +00:00
|
|
|
var (
|
|
|
|
websiteDTOs []response.WebsiteDTO
|
|
|
|
opts []repo.DBOption
|
|
|
|
)
|
2023-05-08 07:11:39 +00:00
|
|
|
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
2022-12-30 08:13:13 +00:00
|
|
|
opts = append(opts, commonRepo.WithOrderBy("created_at desc"))
|
|
|
|
if req.Name != "" {
|
2023-03-02 06:56:06 +00:00
|
|
|
opts = append(opts, websiteRepo.WithDomainLike(req.Name))
|
2022-12-30 08:13:13 +00:00
|
|
|
}
|
|
|
|
if req.WebsiteGroupID != 0 {
|
|
|
|
opts = append(opts, websiteRepo.WithGroupID(req.WebsiteGroupID))
|
|
|
|
}
|
|
|
|
total, websites, err := websiteRepo.Page(req.Page, req.PageSize, opts...)
|
2022-11-02 07:19:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
for _, web := range websites {
|
2023-04-10 08:24:26 +00:00
|
|
|
var (
|
|
|
|
appName string
|
|
|
|
runtimeName string
|
|
|
|
)
|
|
|
|
switch web.Type {
|
|
|
|
case constant.Deployment:
|
2023-03-09 09:50:47 +00:00
|
|
|
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(web.AppInstallID))
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
appName = appInstall.Name
|
2023-04-10 08:24:26 +00:00
|
|
|
case constant.Runtime:
|
|
|
|
runtime, err := runtimeRepo.GetFirst(commonRepo.WithByID(web.RuntimeID))
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
runtimeName = runtime.Name
|
2023-03-09 09:50:47 +00:00
|
|
|
}
|
2023-05-06 04:05:33 +00:00
|
|
|
sitePath := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "www", "sites", web.Alias)
|
2022-12-13 09:20:13 +00:00
|
|
|
websiteDTOs = append(websiteDTOs, response.WebsiteDTO{
|
2023-04-10 08:24:26 +00:00
|
|
|
Website: web,
|
|
|
|
AppName: appName,
|
|
|
|
RuntimeName: runtimeName,
|
2023-05-06 04:05:33 +00:00
|
|
|
SitePath: sitePath,
|
2022-11-02 07:19:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return total, websiteDTOs, nil
|
|
|
|
}
|
2022-10-28 09:04:57 +00:00
|
|
|
|
2023-01-03 08:56:36 +00:00
|
|
|
func (w WebsiteService) GetWebsites() ([]response.WebsiteDTO, error) {
|
|
|
|
var websiteDTOs []response.WebsiteDTO
|
|
|
|
websites, err := websiteRepo.List()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, web := range websites {
|
|
|
|
websiteDTOs = append(websiteDTOs, response.WebsiteDTO{
|
|
|
|
Website: web,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return websiteDTOs, nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 06:16:29 +00:00
|
|
|
func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error) {
|
2022-12-04 08:10:48 +00:00
|
|
|
if exist, _ := websiteRepo.GetBy(websiteRepo.WithDomain(create.PrimaryDomain)); len(exist) > 0 {
|
2023-03-08 06:43:38 +00:00
|
|
|
return buserr.New(constant.ErrDomainIsExist)
|
2022-12-04 08:10:48 +00:00
|
|
|
}
|
|
|
|
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 09:04:57 +00:00
|
|
|
defaultDate, _ := time.Parse(constant.DateLayout, constant.DefaultDate)
|
2022-12-13 09:20:13 +00:00
|
|
|
website := &model.Website{
|
2022-10-28 09:04:57 +00:00
|
|
|
PrimaryDomain: create.PrimaryDomain,
|
|
|
|
Type: create.Type,
|
|
|
|
Alias: create.Alias,
|
|
|
|
Remark: create.Remark,
|
|
|
|
Status: constant.WebRunning,
|
|
|
|
ExpireDate: defaultDate,
|
2022-12-13 09:20:13 +00:00
|
|
|
WebsiteGroupID: create.WebsiteGroupID,
|
2022-11-21 03:27:56 +00:00
|
|
|
Protocol: constant.ProtocolHTTP,
|
2022-12-12 07:36:45 +00:00
|
|
|
Proxy: create.Proxy,
|
2023-04-17 08:54:34 +00:00
|
|
|
SiteDir: "/",
|
2023-01-03 10:37:57 +00:00
|
|
|
AccessLog: true,
|
|
|
|
ErrorLog: true,
|
2022-10-28 09:04:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:47:23 +00:00
|
|
|
var (
|
|
|
|
appInstall *model.AppInstall
|
|
|
|
runtime *model.Runtime
|
|
|
|
)
|
2023-04-04 10:54:04 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if website.AppInstallID > 0 {
|
|
|
|
req := request.AppInstalledOperate{
|
|
|
|
InstallId: website.AppInstallID,
|
|
|
|
Operate: constant.Delete,
|
|
|
|
ForceDelete: true,
|
|
|
|
}
|
2023-04-12 06:22:29 +00:00
|
|
|
if err := NewIAppInstalledService().Operate(req); err != nil {
|
2023-04-04 10:54:04 +00:00
|
|
|
global.LOG.Errorf(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2023-04-12 13:52:30 +00:00
|
|
|
var proxy string
|
2023-04-04 10:54:04 +00:00
|
|
|
|
2022-12-12 07:36:45 +00:00
|
|
|
switch create.Type {
|
|
|
|
case constant.Deployment:
|
2022-12-13 09:20:13 +00:00
|
|
|
if create.AppType == constant.NewApp {
|
2023-04-11 06:38:27 +00:00
|
|
|
var (
|
|
|
|
req request.AppInstallCreate
|
|
|
|
install *model.AppInstall
|
|
|
|
)
|
2022-12-21 07:54:34 +00:00
|
|
|
req.Name = create.AppInstall.Name
|
|
|
|
req.AppDetailId = create.AppInstall.AppDetailId
|
|
|
|
req.Params = create.AppInstall.Params
|
2023-05-17 13:48:09 +00:00
|
|
|
req.AppContainerConfig = create.AppInstall.AppContainerConfig
|
2023-04-11 06:38:27 +00:00
|
|
|
tx, installCtx := getTxAndContext()
|
|
|
|
install, err = NewIAppService().Install(installCtx, req)
|
2022-12-22 07:44:38 +00:00
|
|
|
if err != nil {
|
2023-04-11 06:38:27 +00:00
|
|
|
tx.Rollback()
|
2022-12-22 07:44:38 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-04-11 06:38:27 +00:00
|
|
|
tx.Commit()
|
2022-12-22 07:44:38 +00:00
|
|
|
appInstall = install
|
2023-04-12 13:52:30 +00:00
|
|
|
website.AppInstallID = install.ID
|
|
|
|
website.Proxy = fmt.Sprintf("127.0.0.1:%d", appInstall.HttpPort)
|
2022-12-22 07:44:38 +00:00
|
|
|
} else {
|
2023-04-11 06:38:27 +00:00
|
|
|
var install model.AppInstall
|
|
|
|
install, err = appInstallRepo.GetFirst(commonRepo.WithByID(create.AppInstallID))
|
2022-12-22 07:44:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-11-21 08:28:51 +00:00
|
|
|
}
|
2022-12-22 07:44:38 +00:00
|
|
|
appInstall = &install
|
2023-02-27 09:22:14 +00:00
|
|
|
website.AppInstallID = appInstall.ID
|
2023-04-12 13:52:30 +00:00
|
|
|
website.Proxy = fmt.Sprintf("127.0.0.1:%d", appInstall.HttpPort)
|
2022-11-21 08:28:51 +00:00
|
|
|
}
|
2023-04-03 09:47:23 +00:00
|
|
|
case constant.Runtime:
|
|
|
|
runtime, err = runtimeRepo.GetFirst(commonRepo.WithByID(create.RuntimeID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-10 09:32:22 +00:00
|
|
|
website.RuntimeID = runtime.ID
|
2023-04-03 09:47:23 +00:00
|
|
|
if runtime.Resource == constant.ResourceAppstore {
|
2023-04-11 06:38:27 +00:00
|
|
|
var (
|
|
|
|
req request.AppInstallCreate
|
|
|
|
nginxInstall model.AppInstall
|
|
|
|
install *model.AppInstall
|
|
|
|
)
|
2023-04-10 06:44:23 +00:00
|
|
|
reg, _ := regexp.Compile(`[^a-z0-9_-]+`)
|
|
|
|
req.Name = reg.ReplaceAllString(strings.ToLower(create.PrimaryDomain), "")
|
2023-04-03 09:47:23 +00:00
|
|
|
req.AppDetailId = create.AppInstall.AppDetailId
|
|
|
|
req.Params = create.AppInstall.Params
|
|
|
|
req.Params["IMAGE_NAME"] = runtime.Image
|
2023-05-17 13:48:09 +00:00
|
|
|
req.AppContainerConfig = create.AppInstall.AppContainerConfig
|
2023-04-11 06:38:27 +00:00
|
|
|
nginxInstall, err = getAppInstallByKey(constant.AppOpenresty)
|
2023-04-03 09:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Params["PANEL_WEBSITE_DIR"] = path.Join(nginxInstall.GetPath(), "/www")
|
2023-04-11 06:38:27 +00:00
|
|
|
tx, installCtx := getTxAndContext()
|
|
|
|
install, err = NewIAppService().Install(installCtx, req)
|
2023-04-03 09:47:23 +00:00
|
|
|
if err != nil {
|
2023-04-11 06:38:27 +00:00
|
|
|
tx.Rollback()
|
2023-04-03 09:47:23 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-04-11 06:38:27 +00:00
|
|
|
tx.Commit()
|
2023-04-03 09:47:23 +00:00
|
|
|
website.AppInstallID = install.ID
|
|
|
|
appInstall = install
|
2023-04-12 13:52:30 +00:00
|
|
|
website.Proxy = fmt.Sprintf("127.0.0.1:%d", appInstall.HttpPort)
|
|
|
|
} else {
|
|
|
|
website.ProxyType = create.ProxyType
|
|
|
|
if website.ProxyType == constant.RuntimeProxyUnix {
|
|
|
|
proxy = fmt.Sprintf("unix:%s", path.Join("/www/sites", website.Alias, "php-pool", "php-fpm.sock"))
|
|
|
|
}
|
|
|
|
if website.ProxyType == constant.RuntimeProxyTcp {
|
|
|
|
proxy = fmt.Sprintf("127.0.0.1:%d", create.Port)
|
|
|
|
}
|
|
|
|
website.Proxy = proxy
|
2023-04-03 09:47:23 +00:00
|
|
|
}
|
2022-11-02 07:19:14 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 09:20:13 +00:00
|
|
|
var domains []model.WebsiteDomain
|
2023-04-12 13:52:30 +00:00
|
|
|
domains = append(domains, model.WebsiteDomain{Domain: website.PrimaryDomain, Port: 80})
|
2022-11-03 09:06:48 +00:00
|
|
|
otherDomainArray := strings.Split(create.OtherDomains, "\n")
|
|
|
|
for _, domain := range otherDomainArray {
|
2022-11-19 09:16:02 +00:00
|
|
|
if domain == "" {
|
|
|
|
continue
|
|
|
|
}
|
2023-04-12 13:52:30 +00:00
|
|
|
domainModel, err := getDomain(domain)
|
2022-10-28 09:04:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-13 09:20:13 +00:00
|
|
|
if reflect.DeepEqual(domainModel, model.WebsiteDomain{}) {
|
2022-10-28 09:04:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
domains = append(domains, domainModel)
|
|
|
|
}
|
2023-04-12 14:46:30 +00:00
|
|
|
if err = configDefaultNginx(website, domains, appInstall, runtime); err != nil {
|
2023-04-12 13:52:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
tx, ctx := helper.GetTxAndContext()
|
|
|
|
defer tx.Rollback()
|
|
|
|
if err = websiteRepo.Create(ctx, website); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := range domains {
|
|
|
|
domains[i].WebsiteID = website.ID
|
2022-10-28 09:04:57 +00:00
|
|
|
}
|
2023-04-12 13:52:30 +00:00
|
|
|
if err = websiteDomainRepo.BatchCreate(ctx, domains); err != nil {
|
2023-04-12 06:16:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
return nil
|
2022-10-28 09:04:57 +00:00
|
|
|
}
|
2022-11-02 07:19:14 +00:00
|
|
|
|
2022-12-26 08:09:21 +00:00
|
|
|
func (w WebsiteService) OpWebsite(req request.WebsiteOp) error {
|
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := opWebsite(&website, req.Operate); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return websiteRepo.Save(context.Background(), &website)
|
|
|
|
}
|
|
|
|
|
2022-11-30 07:47:11 +00:00
|
|
|
func (w WebsiteService) GetWebsiteOptions() ([]string, error) {
|
|
|
|
webs, err := websiteRepo.GetBy()
|
2022-11-29 09:39:10 +00:00
|
|
|
if err != nil {
|
2022-11-30 07:47:11 +00:00
|
|
|
return nil, err
|
2022-11-29 09:39:10 +00:00
|
|
|
}
|
2022-11-30 07:47:11 +00:00
|
|
|
var datas []string
|
|
|
|
for _, web := range webs {
|
|
|
|
datas = append(datas, web.PrimaryDomain)
|
2022-11-29 09:39:10 +00:00
|
|
|
}
|
2022-11-30 07:47:11 +00:00
|
|
|
return datas, nil
|
|
|
|
}
|
|
|
|
|
2022-12-13 09:20:13 +00:00
|
|
|
func (w WebsiteService) UpdateWebsite(req request.WebsiteUpdate) error {
|
2022-11-08 09:21:13 +00:00
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
website.PrimaryDomain = req.PrimaryDomain
|
2022-12-13 09:20:13 +00:00
|
|
|
website.WebsiteGroupID = req.WebsiteGroupID
|
2022-11-08 09:21:13 +00:00
|
|
|
website.Remark = req.Remark
|
2022-12-30 02:31:54 +00:00
|
|
|
if req.ExpireDate != "" {
|
|
|
|
expireDate, err := time.Parse(constant.DateLayout, req.ExpireDate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
website.ExpireDate = expireDate
|
|
|
|
}
|
2022-11-08 09:21:13 +00:00
|
|
|
|
|
|
|
return websiteRepo.Save(context.TODO(), &website)
|
|
|
|
}
|
|
|
|
|
2022-12-13 09:20:13 +00:00
|
|
|
func (w WebsiteService) GetWebsite(id uint) (response.WebsiteDTO, error) {
|
|
|
|
var res response.WebsiteDTO
|
2022-11-08 09:21:13 +00:00
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(id))
|
|
|
|
if err != nil {
|
2022-11-16 02:31:35 +00:00
|
|
|
return res, err
|
2022-11-08 09:21:13 +00:00
|
|
|
}
|
2022-12-13 09:20:13 +00:00
|
|
|
res.Website = website
|
2022-12-26 03:42:37 +00:00
|
|
|
|
2023-02-22 09:13:08 +00:00
|
|
|
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
2022-12-26 03:42:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2023-02-22 09:13:08 +00:00
|
|
|
sitePath := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "www", "sites", website.Alias)
|
2022-12-26 03:42:37 +00:00
|
|
|
res.ErrorLogPath = path.Join(sitePath, "log", "error.log")
|
|
|
|
res.AccessLogPath = path.Join(sitePath, "log", "access.log")
|
2022-12-26 10:32:13 +00:00
|
|
|
res.SitePath = sitePath
|
2022-11-16 02:31:35 +00:00
|
|
|
return res, nil
|
2022-11-08 09:21:13 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 06:22:29 +00:00
|
|
|
func (w WebsiteService) DeleteWebsite(req request.WebsiteDelete) error {
|
2022-11-02 07:19:14 +00:00
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-12 07:36:45 +00:00
|
|
|
if err := delNginxConfig(website, req.ForceDelete); err != nil {
|
2022-11-02 07:19:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-10 09:32:22 +00:00
|
|
|
if checkIsLinkApp(website) && req.DeleteApp {
|
2022-11-02 07:19:14 +00: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) {
|
2023-04-12 06:22:29 +00:00
|
|
|
if err := deleteAppInstall(appInstall, true, req.ForceDelete, true); err != nil && !req.ForceDelete {
|
2022-11-02 07:19:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-13 03:00:33 +00:00
|
|
|
|
|
|
|
tx, ctx := helper.GetTxAndContext()
|
|
|
|
defer tx.Rollback()
|
|
|
|
_ = backupRepo.DeleteRecord(ctx, commonRepo.WithByType("website"), commonRepo.WithByName(website.Alias))
|
|
|
|
if err := websiteRepo.DeleteBy(ctx, commonRepo.WithByID(req.ID)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := websiteDomainRepo.DeleteBy(ctx, websiteDomainRepo.WithWebsiteId(req.ID)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-12 06:22:29 +00:00
|
|
|
tx.Commit()
|
2023-03-02 05:54:07 +00:00
|
|
|
|
2022-12-20 14:00:05 +00:00
|
|
|
if req.DeleteBackup {
|
2023-04-12 06:22:29 +00:00
|
|
|
localDir, _ := loadLocalDir()
|
2023-03-02 05:54:07 +00:00
|
|
|
backupDir := fmt.Sprintf("%s/website/%s", localDir, website.Alias)
|
|
|
|
if _, err := os.Stat(backupDir); err == nil {
|
|
|
|
_ = os.RemoveAll(backupDir)
|
2022-12-20 14:00:05 +00:00
|
|
|
}
|
2023-03-02 05:54:07 +00:00
|
|
|
global.LOG.Infof("delete website %s backups successful", website.Alias)
|
2022-12-20 14:00:05 +00:00
|
|
|
}
|
2023-04-12 06:22:29 +00:00
|
|
|
uploadDir := fmt.Sprintf("%s/1panel/uploads/website/%s", global.CONF.System.BaseDir, website.Alias)
|
|
|
|
if _, err := os.Stat(uploadDir); err == nil {
|
|
|
|
_ = os.RemoveAll(uploadDir)
|
2022-11-02 07:19:14 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-03 09:06:48 +00:00
|
|
|
|
2022-12-13 09:20:13 +00:00
|
|
|
func (w WebsiteService) CreateWebsiteDomain(create request.WebsiteDomainCreate) (model.WebsiteDomain, error) {
|
2023-05-06 07:27:34 +00:00
|
|
|
var (
|
|
|
|
domainModel model.WebsiteDomain
|
|
|
|
ports []int
|
|
|
|
domains []string
|
|
|
|
)
|
|
|
|
if create.Port != 80 {
|
|
|
|
if common.ScanPort(create.Port) {
|
|
|
|
return domainModel, buserr.WithDetail(constant.ErrPortInUsed, create.Port, nil)
|
|
|
|
}
|
|
|
|
}
|
2022-12-13 09:20:13 +00:00
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(create.WebsiteID))
|
2022-11-03 10:02:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return domainModel, err
|
|
|
|
}
|
2022-12-13 09:20:13 +00:00
|
|
|
if oldDomains, _ := websiteDomainRepo.GetBy(websiteDomainRepo.WithWebsiteId(create.WebsiteID), websiteDomainRepo.WithPort(create.Port)); len(oldDomains) == 0 {
|
2022-11-03 10:02:07 +00: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 09:20:13 +00:00
|
|
|
domainModel = model.WebsiteDomain{
|
2022-11-03 10:02:07 +00:00
|
|
|
Domain: create.Domain,
|
|
|
|
Port: create.Port,
|
2022-12-13 09:20:13 +00:00
|
|
|
WebsiteID: create.WebsiteID,
|
2022-11-03 10:02:07 +00:00
|
|
|
}
|
2023-05-06 07:27:34 +00:00
|
|
|
if create.Port != 80 {
|
|
|
|
go func() {
|
|
|
|
_ = OperateFirewallPort(nil, []int{create.Port})
|
|
|
|
}()
|
|
|
|
}
|
2022-11-03 10:02:07 +00:00
|
|
|
return domainModel, websiteDomainRepo.Create(context.TODO(), &domainModel)
|
2022-11-03 09:06:48 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 09:20:13 +00:00
|
|
|
func (w WebsiteService) GetWebsiteDomain(websiteId uint) ([]model.WebsiteDomain, error) {
|
|
|
|
return websiteDomainRepo.GetBy(websiteDomainRepo.WithWebsiteId(websiteId))
|
2022-11-03 09:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w WebsiteService) DeleteWebsiteDomain(domainId uint) error {
|
|
|
|
webSiteDomain, err := websiteDomainRepo.GetFirst(commonRepo.WithByID(domainId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-13 09:20:13 +00:00
|
|
|
if websiteDomains, _ := websiteDomainRepo.GetBy(websiteDomainRepo.WithWebsiteId(webSiteDomain.WebsiteID)); len(websiteDomains) == 1 {
|
2022-11-03 09:06:48 +00:00
|
|
|
return fmt.Errorf("can not delete last domain")
|
|
|
|
}
|
2022-12-13 09:20:13 +00:00
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(webSiteDomain.WebsiteID))
|
2022-11-03 09:06:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var ports []int
|
2022-12-13 09:20:13 +00:00
|
|
|
if oldDomains, _ := websiteDomainRepo.GetBy(websiteDomainRepo.WithWebsiteId(webSiteDomain.WebsiteID), websiteDomainRepo.WithPort(webSiteDomain.Port)); len(oldDomains) == 1 {
|
2022-11-03 09:06:48 +00:00
|
|
|
ports = append(ports, webSiteDomain.Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
var domains []string
|
2022-12-13 09:20:13 +00:00
|
|
|
if oldDomains, _ := websiteDomainRepo.GetBy(websiteDomainRepo.WithWebsiteId(webSiteDomain.WebsiteID), websiteDomainRepo.WithDomain(webSiteDomain.Domain)); len(oldDomains) == 1 {
|
2022-11-03 09:06:48 +00:00
|
|
|
domains = append(domains, webSiteDomain.Domain)
|
|
|
|
}
|
|
|
|
if len(ports) > 0 || len(domains) > 0 {
|
2023-05-04 06:28:38 +00:00
|
|
|
stringBinds := make([]string, len(ports))
|
|
|
|
for i := 0; i < len(ports); i++ {
|
|
|
|
stringBinds[i] = strconv.Itoa(ports[i])
|
|
|
|
}
|
|
|
|
if err := deleteListenAndServerName(website, stringBinds, domains); err != nil {
|
2022-11-03 09:06:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return websiteDomainRepo.DeleteBy(context.TODO(), commonRepo.WithByID(domainId))
|
|
|
|
}
|
2022-11-07 08:19:05 +00:00
|
|
|
|
2022-12-13 10:54:46 +00:00
|
|
|
func (w WebsiteService) GetNginxConfigByScope(req request.NginxScopeReq) (*response.WebsiteNginxConfig, error) {
|
2022-11-07 08:19:05 +00:00
|
|
|
keys, ok := dto.ScopeKeyMap[req.Scope]
|
|
|
|
if !ok || len(keys) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-12-13 09:20:13 +00:00
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
|
2022-11-07 08:19:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-13 09:20:13 +00:00
|
|
|
var config response.WebsiteNginxConfig
|
2022-11-30 16:41:50 +00: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 08:19:05 +00:00
|
|
|
|
2022-11-30 16:41:50 +00:00
|
|
|
return &config, nil
|
2022-11-07 08:19:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 10:54:46 +00:00
|
|
|
func (w WebsiteService) UpdateNginxConfigByScope(req request.NginxConfigUpdate) error {
|
2022-11-07 08:19:05 +00:00
|
|
|
keys, ok := dto.ScopeKeyMap[req.Scope]
|
|
|
|
if !ok || len(keys) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-13 09:20:13 +00:00
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
|
2022-11-07 08:19:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-13 10:54:46 +00:00
|
|
|
if req.Operate == constant.ConfigDel {
|
2023-01-11 07:15:48 +00:00
|
|
|
var nginxParams []dto.NginxParam
|
|
|
|
for _, key := range keys {
|
|
|
|
nginxParams = append(nginxParams, dto.NginxParam{
|
|
|
|
Name: key,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return deleteNginxConfig(constant.NginxScopeServer, nginxParams, &website)
|
2022-11-08 07:42:31 +00:00
|
|
|
}
|
2022-11-30 16:41:50 +00:00
|
|
|
params := getNginxParams(req.Params, keys)
|
2022-12-13 10:54:46 +00:00
|
|
|
if req.Operate == constant.ConfigNew {
|
2022-11-30 16:41:50 +00:00
|
|
|
if _, ok := dto.StaticFileKeyMap[req.Scope]; ok {
|
|
|
|
params = getNginxParamsFromStaticFile(req.Scope, params)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return updateNginxConfig(constant.NginxScopeServer, params, &website)
|
2022-11-07 08:19:05 +00:00
|
|
|
}
|
2022-11-19 09:16:02 +00:00
|
|
|
|
2023-04-04 10:54:04 +00:00
|
|
|
func (w WebsiteService) GetWebsiteNginxConfig(websiteId uint, configType string) (response.FileInfo, error) {
|
2022-11-19 09:16:02 +00:00
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(websiteId))
|
|
|
|
if err != nil {
|
2022-12-14 11:39:32 +00:00
|
|
|
return response.FileInfo{}, err
|
2022-11-19 09:16:02 +00:00
|
|
|
}
|
2023-04-04 10:54:04 +00:00
|
|
|
configPath := ""
|
|
|
|
switch configType {
|
|
|
|
case constant.AppOpenresty:
|
|
|
|
nginxApp, err := appRepo.GetFirst(appRepo.WithKey(constant.AppOpenresty))
|
|
|
|
if err != nil {
|
|
|
|
return response.FileInfo{}, err
|
|
|
|
}
|
|
|
|
nginxInstall, err := appInstallRepo.GetFirst(appInstallRepo.WithAppId(nginxApp.ID))
|
|
|
|
if err != nil {
|
|
|
|
return response.FileInfo{}, err
|
|
|
|
}
|
|
|
|
configPath = path.Join(nginxInstall.GetPath(), "conf", "conf.d", website.Alias+".conf")
|
|
|
|
case constant.ConfigFPM:
|
|
|
|
runtimeInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID))
|
|
|
|
if err != nil {
|
|
|
|
return response.FileInfo{}, err
|
|
|
|
}
|
|
|
|
configPath = path.Join(runtimeInstall.GetPath(), "conf", "php-fpm.conf")
|
|
|
|
case constant.ConfigPHP:
|
|
|
|
runtimeInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID))
|
|
|
|
if err != nil {
|
|
|
|
return response.FileInfo{}, err
|
|
|
|
}
|
|
|
|
configPath = path.Join(runtimeInstall.GetPath(), "conf", "php.ini")
|
2022-11-19 09:16:02 +00:00
|
|
|
}
|
|
|
|
info, err := files.NewFileInfo(files.FileOption{
|
|
|
|
Path: configPath,
|
|
|
|
Expand: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-12-14 11:39:32 +00:00
|
|
|
return response.FileInfo{}, err
|
2022-11-19 09:16:02 +00:00
|
|
|
}
|
2022-12-14 11:39:32 +00:00
|
|
|
return response.FileInfo{FileInfo: *info}, nil
|
2022-11-19 09:16:02 +00:00
|
|
|
}
|
2022-11-20 10:32:56 +00:00
|
|
|
|
2022-12-13 09:20:13 +00:00
|
|
|
func (w WebsiteService) GetWebsiteHTTPS(websiteId uint) (response.WebsiteHTTPS, error) {
|
2022-11-20 10:32:56 +00:00
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(websiteId))
|
|
|
|
if err != nil {
|
2022-12-13 09:20:13 +00:00
|
|
|
return response.WebsiteHTTPS{}, err
|
2022-11-20 10:32:56 +00:00
|
|
|
}
|
2022-12-13 09:20:13 +00:00
|
|
|
var res response.WebsiteHTTPS
|
|
|
|
if website.WebsiteSSLID == 0 {
|
2022-11-20 10:32:56 +00:00
|
|
|
res.Enable = false
|
|
|
|
return res, nil
|
|
|
|
}
|
2022-12-13 09:20:13 +00:00
|
|
|
websiteSSL, err := websiteSSLRepo.GetFirst(commonRepo.WithByID(website.WebsiteSSLID))
|
2022-11-20 10:32:56 +00:00
|
|
|
if err != nil {
|
2022-12-13 09:20:13 +00:00
|
|
|
return response.WebsiteHTTPS{}, err
|
2022-11-20 10:32:56 +00:00
|
|
|
}
|
|
|
|
res.SSL = websiteSSL
|
|
|
|
res.Enable = true
|
2022-12-28 08:07:43 +00:00
|
|
|
if website.HttpConfig != "" {
|
|
|
|
res.HttpConfig = website.HttpConfig
|
|
|
|
} else {
|
|
|
|
res.HttpConfig = constant.HTTPToHTTPS
|
|
|
|
}
|
2022-12-28 10:59:05 +00:00
|
|
|
params, err := getNginxParamsByKeys(constant.NginxScopeServer, []string{"ssl_protocols", "ssl_ciphers"}, &website)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
for _, p := range params {
|
|
|
|
if p.Name == "ssl_protocols" {
|
|
|
|
res.SSLProtocol = p.Params
|
|
|
|
}
|
|
|
|
if p.Name == "ssl_ciphers" {
|
|
|
|
res.Algorithm = p.Params[0]
|
|
|
|
}
|
|
|
|
}
|
2022-11-20 10:32:56 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2022-12-28 08:07:43 +00:00
|
|
|
func (w WebsiteService) OpWebsiteHTTPS(ctx context.Context, req request.WebsiteHTTPSOp) (response.WebsiteHTTPS, error) {
|
2022-11-20 10:32:56 +00:00
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
|
|
|
|
if err != nil {
|
2022-12-13 09:20:13 +00:00
|
|
|
return response.WebsiteHTTPS{}, err
|
2022-11-20 10:32:56 +00:00
|
|
|
}
|
2022-11-24 09:50:47 +00:00
|
|
|
var (
|
2022-12-13 09:20:13 +00:00
|
|
|
res response.WebsiteHTTPS
|
|
|
|
websiteSSL model.WebsiteSSL
|
2022-11-24 09:50:47 +00:00
|
|
|
)
|
2022-11-20 10:32:56 +00:00
|
|
|
res.Enable = req.Enable
|
2022-12-28 10:59:05 +00:00
|
|
|
res.SSLProtocol = req.SSLProtocol
|
|
|
|
res.Algorithm = req.Algorithm
|
2022-12-28 09:21:15 +00:00
|
|
|
if !req.Enable {
|
2022-11-21 03:27:56 +00:00
|
|
|
website.Protocol = constant.ProtocolHTTP
|
2022-12-13 09:20:13 +00:00
|
|
|
website.WebsiteSSLID = 0
|
2023-05-04 06:28:38 +00:00
|
|
|
if err := deleteListenAndServerName(website, []string{"443", "[::]:443"}, []string{}); err != nil {
|
2022-12-13 09:20:13 +00:00
|
|
|
return response.WebsiteHTTPS{}, err
|
2022-11-28 07:11:39 +00:00
|
|
|
}
|
2023-01-11 07:15:48 +00:00
|
|
|
nginxParams := getNginxParamsFromStaticFile(dto.SSL, nil)
|
2023-05-04 06:28:38 +00:00
|
|
|
nginxParams = append(nginxParams,
|
|
|
|
dto.NginxParam{
|
|
|
|
Name: "if",
|
|
|
|
Params: []string{"($scheme", "=", "http)"},
|
|
|
|
},
|
|
|
|
dto.NginxParam{
|
|
|
|
Name: "ssl_certificate",
|
|
|
|
},
|
|
|
|
dto.NginxParam{
|
|
|
|
Name: "ssl_certificate_key",
|
|
|
|
},
|
|
|
|
dto.NginxParam{
|
|
|
|
Name: "ssl_protocols",
|
|
|
|
},
|
|
|
|
dto.NginxParam{
|
|
|
|
Name: "ssl_ciphers",
|
|
|
|
},
|
|
|
|
)
|
2023-01-11 07:15:48 +00:00
|
|
|
if err := deleteNginxConfig(constant.NginxScopeServer, nginxParams, &website); err != nil {
|
2022-12-13 09:20:13 +00:00
|
|
|
return response.WebsiteHTTPS{}, err
|
2022-11-24 09:50:47 +00:00
|
|
|
}
|
2022-12-28 09:21:15 +00:00
|
|
|
if err := websiteRepo.Save(ctx, &website); err != nil {
|
|
|
|
return response.WebsiteHTTPS{}, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Type == constant.SSLExisted {
|
|
|
|
websiteSSL, err = websiteSSLRepo.GetFirst(commonRepo.WithByID(req.WebsiteSSLID))
|
|
|
|
if err != nil {
|
|
|
|
return response.WebsiteHTTPS{}, err
|
|
|
|
}
|
|
|
|
website.WebsiteSSLID = websiteSSL.ID
|
|
|
|
res.SSL = websiteSSL
|
|
|
|
}
|
|
|
|
if req.Type == constant.SSLManual {
|
|
|
|
certBlock, _ := pem.Decode([]byte(req.Certificate))
|
|
|
|
cert, err := x509.ParseCertificate(certBlock.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return response.WebsiteHTTPS{}, err
|
|
|
|
}
|
|
|
|
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, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
websiteSSL.Provider = constant.Manual
|
|
|
|
websiteSSL.PrivateKey = req.PrivateKey
|
|
|
|
websiteSSL.Pem = req.Certificate
|
|
|
|
res.SSL = websiteSSL
|
|
|
|
}
|
|
|
|
website.Protocol = constant.ProtocolHTTPS
|
2022-12-28 10:59:05 +00:00
|
|
|
if err := applySSL(website, websiteSSL, req); err != nil {
|
2022-12-28 09:21:15 +00:00
|
|
|
return response.WebsiteHTTPS{}, err
|
2022-11-20 10:32:56 +00:00
|
|
|
}
|
2022-12-28 09:21:15 +00:00
|
|
|
website.HttpConfig = req.HttpConfig
|
2022-11-20 10:32:56 +00:00
|
|
|
|
2022-11-24 09:50:47 +00:00
|
|
|
if websiteSSL.ID == 0 {
|
|
|
|
if err := websiteSSLRepo.Create(ctx, &websiteSSL); err != nil {
|
2022-12-13 09:20:13 +00:00
|
|
|
return response.WebsiteHTTPS{}, err
|
2022-11-24 09:50:47 +00:00
|
|
|
}
|
2022-12-13 09:20:13 +00:00
|
|
|
website.WebsiteSSLID = websiteSSL.ID
|
2022-11-24 09:50:47 +00:00
|
|
|
}
|
|
|
|
if err := websiteRepo.Save(ctx, &website); err != nil {
|
2022-12-13 09:20:13 +00:00
|
|
|
return response.WebsiteHTTPS{}, err
|
2022-11-24 09:50:47 +00:00
|
|
|
}
|
2022-11-20 10:32:56 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
2022-11-30 07:47:11 +00:00
|
|
|
|
2022-12-13 09:20:13 +00:00
|
|
|
func (w WebsiteService) PreInstallCheck(req request.WebsiteInstallCheckReq) ([]response.WebsitePreInstallCheck, error) {
|
2022-12-04 09:45:54 +00:00
|
|
|
var (
|
2022-12-13 09:20:13 +00:00
|
|
|
res []response.WebsitePreInstallCheck
|
2022-12-04 09:45:54 +00:00
|
|
|
checkIds []uint
|
|
|
|
showErr = false
|
|
|
|
)
|
|
|
|
|
2023-02-22 09:13:08 +00:00
|
|
|
app, err := appRepo.GetFirst(appRepo.WithKey(constant.AppOpenresty))
|
2022-12-04 09:45:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
appInstall, _ := appInstallRepo.GetFirst(appInstallRepo.WithAppId(app.ID))
|
|
|
|
if reflect.DeepEqual(appInstall, model.AppInstall{}) {
|
2022-12-13 09:20:13 +00:00
|
|
|
res = append(res, response.WebsitePreInstallCheck{
|
2022-12-04 09:45:54 +00:00
|
|
|
Name: appInstall.Name,
|
|
|
|
AppName: app.Name,
|
2023-02-07 08:29:54 +00:00
|
|
|
Status: buserr.WithDetail(constant.ErrNotInstall, app.Name, nil).Error(),
|
2022-12-04 09:45:54 +00:00
|
|
|
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 {
|
2023-02-07 08:29:54 +00:00
|
|
|
installList, _ := appInstallRepo.ListBy(commonRepo.WithIdsIn(checkIds))
|
2022-12-04 09:45:54 +00:00
|
|
|
for _, install := range installList {
|
2022-12-13 09:20:13 +00:00
|
|
|
res = append(res, response.WebsitePreInstallCheck{
|
2022-12-04 09:45:54 +00: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 03:42:11 +00:00
|
|
|
|
2022-12-13 09:20:13 +00:00
|
|
|
func (w WebsiteService) GetWafConfig(req request.WebsiteWafReq) (response.WebsiteWafConfig, error) {
|
|
|
|
var res response.WebsiteWafConfig
|
2022-12-06 03:42:11 +00:00
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
|
|
|
|
if err != nil {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2022-12-06 07:55:42 +00: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 03:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-06 07:55:42 +00:00
|
|
|
|
2022-12-06 03:42:11 +00:00
|
|
|
nginxFull, err := getNginxFull(&website)
|
|
|
|
if err != nil {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-02-20 08:26:14 +00:00
|
|
|
filePath := path.Join(nginxFull.SiteDir, "sites", website.Alias, "waf", "rules", req.Rule+".json")
|
2022-12-06 03:42:11 +00:00
|
|
|
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 {
|
2022-12-28 03:37:04 +00:00
|
|
|
return err
|
2022-12-06 03:42:11 +00:00
|
|
|
}
|
|
|
|
updateValue := "on"
|
|
|
|
if !req.Enable {
|
|
|
|
updateValue = "off"
|
|
|
|
}
|
|
|
|
return updateNginxConfig(constant.NginxScopeServer, []dto.NginxParam{
|
|
|
|
{Name: "set", Params: []string{req.Key, updateValue}},
|
|
|
|
}, &website)
|
|
|
|
}
|
2022-12-28 03:37:04 +00:00
|
|
|
|
|
|
|
func (w WebsiteService) UpdateNginxConfigFile(req request.WebsiteNginxUpdate) error {
|
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nginxFull, err := getNginxFull(&website)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
filePath := nginxFull.SiteConfig.FilePath
|
|
|
|
if err := files.NewFileOp().WriteFile(filePath, strings.NewReader(req.Content), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nginxCheckAndReload(nginxFull.SiteConfig.OldContent, filePath, nginxFull.Install.ContainerName)
|
|
|
|
}
|
2022-12-30 09:39:17 +00:00
|
|
|
|
|
|
|
func (w WebsiteService) OpWebsiteLog(req request.WebsiteLogReq) (*response.WebsiteLog, error) {
|
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nginx, err := getNginxFull(&website)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sitePath := path.Join(nginx.SiteDir, "sites", website.Alias)
|
|
|
|
res := &response.WebsiteLog{
|
|
|
|
Content: "",
|
|
|
|
}
|
|
|
|
switch req.Operate {
|
|
|
|
case constant.GetLog:
|
|
|
|
switch req.LogType {
|
|
|
|
case constant.AccessLog:
|
|
|
|
res.Enable = website.AccessLog
|
|
|
|
if !website.AccessLog {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
case constant.ErrorLog:
|
|
|
|
res.Enable = website.ErrorLog
|
|
|
|
if !website.ErrorLog {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
}
|
2023-05-12 02:54:23 +00:00
|
|
|
filePath := path.Join(sitePath, "log", req.LogType)
|
|
|
|
fileInfo, err := os.Stat(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-17 05:46:28 +00:00
|
|
|
if fileInfo.Size() > 20<<20 {
|
2023-05-12 14:30:02 +00:00
|
|
|
return nil, buserr.New(constant.ErrFileToLarge)
|
2023-05-12 02:54:23 +00:00
|
|
|
}
|
|
|
|
fileInfo.Size()
|
|
|
|
content, err := os.ReadFile(filePath)
|
2022-12-30 09:39:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.Content = string(content)
|
|
|
|
return res, nil
|
|
|
|
case constant.DisableLog:
|
|
|
|
key := "access_log"
|
|
|
|
switch req.LogType {
|
|
|
|
case constant.AccessLog:
|
|
|
|
website.AccessLog = false
|
|
|
|
case constant.ErrorLog:
|
|
|
|
key = "error_log"
|
|
|
|
website.ErrorLog = false
|
|
|
|
}
|
2023-01-11 07:15:48 +00:00
|
|
|
var nginxParams []dto.NginxParam
|
|
|
|
nginxParams = append(nginxParams, dto.NginxParam{
|
|
|
|
Name: key,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := deleteNginxConfig(constant.NginxScopeServer, nginxParams, &website); err != nil {
|
2022-12-30 09:39:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := websiteRepo.Save(context.Background(), &website); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case constant.EnableLog:
|
|
|
|
key := "access_log"
|
|
|
|
logPath := path.Join("/www", "sites", website.Alias, "log", req.LogType)
|
|
|
|
switch req.LogType {
|
|
|
|
case constant.AccessLog:
|
|
|
|
website.AccessLog = true
|
|
|
|
case constant.ErrorLog:
|
|
|
|
key = "error_log"
|
|
|
|
website.ErrorLog = true
|
|
|
|
}
|
|
|
|
if err := updateNginxConfig(constant.NginxScopeServer, []dto.NginxParam{{Name: key, Params: []string{logPath}}}, &website); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := websiteRepo.Save(context.Background(), &website); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-23 07:47:43 +00:00
|
|
|
case constant.DeleteLog:
|
|
|
|
logPath := path.Join(nginx.Install.GetPath(), "www", "sites", website.Alias, "log", req.LogType)
|
|
|
|
if err := files.NewFileOp().WriteFile(logPath, strings.NewReader(""), 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-30 09:39:17 +00:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
2023-01-03 08:56:36 +00:00
|
|
|
|
|
|
|
func (w WebsiteService) ChangeDefaultServer(id uint) error {
|
|
|
|
defaultWebsite, _ := websiteRepo.GetFirst(websiteRepo.WithDefaultServer())
|
|
|
|
if defaultWebsite.ID > 0 {
|
2023-05-12 15:08:28 +00:00
|
|
|
params, err := getNginxParamsByKeys(constant.NginxScopeServer, []string{"listen"}, &defaultWebsite)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var changeParams []dto.NginxParam
|
|
|
|
for _, param := range params {
|
|
|
|
paramLen := len(param.Params)
|
|
|
|
var newParam []string
|
|
|
|
if paramLen > 1 && param.Params[paramLen-1] == components.DefaultServer {
|
|
|
|
newParam = param.Params[:paramLen-1]
|
|
|
|
}
|
|
|
|
changeParams = append(changeParams, dto.NginxParam{
|
|
|
|
Name: param.Name,
|
|
|
|
Params: newParam,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := updateNginxConfig(constant.NginxScopeServer, changeParams, &defaultWebsite); err != nil {
|
2023-01-03 08:56:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defaultWebsite.DefaultServer = false
|
|
|
|
if err := websiteRepo.Save(context.Background(), &defaultWebsite); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-01-03 09:54:26 +00:00
|
|
|
if id > 0 {
|
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(id))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-12 15:08:28 +00:00
|
|
|
params, err := getNginxParamsByKeys(constant.NginxScopeServer, []string{"listen"}, &website)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var changeParams []dto.NginxParam
|
|
|
|
for _, param := range params {
|
|
|
|
paramLen := len(param.Params)
|
|
|
|
bind := param.Params[0]
|
|
|
|
var newParam []string
|
|
|
|
if bind == "80" || bind == "443" || bind == "[::]:80" || bind == "[::]:443" {
|
|
|
|
if param.Params[paramLen-1] == components.DefaultServer {
|
|
|
|
newParam = param.Params
|
|
|
|
} else {
|
|
|
|
newParam = append(param.Params, components.DefaultServer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
changeParams = append(changeParams, dto.NginxParam{
|
|
|
|
Name: param.Name,
|
|
|
|
Params: newParam,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := updateNginxConfig(constant.NginxScopeServer, changeParams, &website); err != nil {
|
2023-01-03 09:54:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
website.DefaultServer = true
|
|
|
|
return websiteRepo.Save(context.Background(), &website)
|
2023-01-03 08:56:36 +00:00
|
|
|
}
|
2023-01-03 09:54:26 +00:00
|
|
|
return nil
|
2023-01-03 08:56:36 +00:00
|
|
|
}
|
2023-04-05 16:09:58 +00:00
|
|
|
|
|
|
|
func (w WebsiteService) GetPHPConfig(id uint) (*response.PHPConfig, error) {
|
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
phpConfigPath := path.Join(appInstall.GetPath(), "conf", "php.ini")
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
if !fileOp.Stat(phpConfigPath) {
|
|
|
|
return nil, buserr.WithDetail(constant.ErrFileCanNotRead, "php.ini", nil)
|
|
|
|
}
|
|
|
|
params := make(map[string]string)
|
|
|
|
configFile, err := fileOp.OpenFile(phpConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer configFile.Close()
|
|
|
|
scanner := bufio.NewScanner(configFile)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := strings.TrimSpace(scanner.Text())
|
|
|
|
if strings.HasPrefix(line, ";") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
matches := regexp.MustCompile(`^\s*([a-z_]+)\s*=\s*(.*)$`).FindStringSubmatch(line)
|
|
|
|
if len(matches) == 3 {
|
|
|
|
params[matches[1]] = matches[2]
|
|
|
|
}
|
|
|
|
}
|
2023-05-24 08:31:18 +00:00
|
|
|
cfg, err := ini.Load(phpConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
phpConfig, err := cfg.GetSection("PHP")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
disableFunctionStr := phpConfig.Key("disable_functions").Value()
|
|
|
|
res := &response.PHPConfig{Params: params}
|
|
|
|
if disableFunctionStr != "" {
|
|
|
|
disableFunctions := strings.Split(disableFunctionStr, ",")
|
|
|
|
if len(disableFunctions) > 0 {
|
|
|
|
res.DisableFunctions = disableFunctions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
2023-04-05 16:09:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w WebsiteService) UpdatePHPConfig(req request.WebsitePHPConfigUpdate) (err error) {
|
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
phpConfigPath := path.Join(appInstall.GetPath(), "conf", "php.ini")
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
if !fileOp.Stat(phpConfigPath) {
|
|
|
|
return buserr.WithDetail(constant.ErrFileCanNotRead, "php.ini", nil)
|
|
|
|
}
|
|
|
|
configFile, err := fileOp.OpenFile(phpConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer configFile.Close()
|
|
|
|
|
|
|
|
contentBytes, err := fileOp.GetContent(phpConfigPath)
|
2023-05-24 08:31:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Scope == "params" {
|
|
|
|
content := string(contentBytes)
|
|
|
|
lines := strings.Split(content, "\n")
|
|
|
|
for i, line := range lines {
|
|
|
|
if strings.HasPrefix(line, ";") {
|
|
|
|
continue
|
2023-04-05 16:09:58 +00:00
|
|
|
}
|
2023-05-24 08:31:18 +00:00
|
|
|
for key, value := range req.Params {
|
|
|
|
pattern := "^" + regexp.QuoteMeta(key) + "\\s*=\\s*.*$"
|
|
|
|
if matched, _ := regexp.MatchString(pattern, line); matched {
|
|
|
|
lines[i] = key + " = " + value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updatedContent := strings.Join(lines, "\n")
|
|
|
|
if err := fileOp.WriteFile(phpConfigPath, strings.NewReader(updatedContent), 0755); err != nil {
|
|
|
|
return err
|
2023-04-05 16:09:58 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-24 08:31:18 +00:00
|
|
|
|
|
|
|
cfg, err := ini.Load(phpConfigPath)
|
|
|
|
if err != nil {
|
2023-04-05 16:09:58 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-05-24 08:31:18 +00:00
|
|
|
phpConfig, err := cfg.GetSection("PHP")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if req.Scope == "disable_functions" {
|
|
|
|
disable := phpConfig.Key("disable_functions")
|
|
|
|
disable.SetValue(strings.Join(req.DisableFunctions, ","))
|
|
|
|
if err = cfg.SaveTo(phpConfigPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if req.Scope == "uploadSize" {
|
|
|
|
postMaxSize := phpConfig.Key("post_max_size")
|
|
|
|
postMaxSize.SetValue("")
|
|
|
|
}
|
|
|
|
|
2023-04-05 16:09:58 +00:00
|
|
|
appInstallReq := request.AppInstalledOperate{
|
|
|
|
InstallId: appInstall.ID,
|
|
|
|
Operate: constant.Restart,
|
|
|
|
}
|
2023-04-12 06:22:29 +00:00
|
|
|
if err = NewIAppInstalledService().Operate(appInstallReq); err != nil {
|
2023-04-05 16:09:58 +00:00
|
|
|
_ = fileOp.WriteFile(phpConfigPath, strings.NewReader(string(contentBytes)), 0755)
|
|
|
|
return err
|
|
|
|
}
|
2023-05-24 08:31:18 +00:00
|
|
|
|
2023-04-05 16:09:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-04-12 06:48:30 +00:00
|
|
|
|
|
|
|
func (w WebsiteService) UpdatePHPConfigFile(req request.WebsitePHPFileUpdate) error {
|
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if website.Type != constant.Runtime {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
runtime, err := runtimeRepo.GetFirst(commonRepo.WithByID(website.RuntimeID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if runtime.Resource != constant.ResourceAppstore {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
runtimeInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
configPath := ""
|
|
|
|
if req.Type == constant.ConfigFPM {
|
|
|
|
configPath = path.Join(runtimeInstall.GetPath(), "conf", "php-fpm.conf")
|
|
|
|
} else {
|
|
|
|
configPath = path.Join(runtimeInstall.GetPath(), "conf", "php.ini")
|
|
|
|
}
|
|
|
|
if err := files.NewFileOp().WriteFile(configPath, strings.NewReader(req.Content), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-14 08:01:06 +00:00
|
|
|
|
|
|
|
func (w WebsiteService) UpdateRewriteConfig(req request.NginxRewriteUpdate) error {
|
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nginxFull, err := getNginxFull(&website)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-18 15:27:00 +00:00
|
|
|
includePath := fmt.Sprintf("/www/sites/%s/rewrite/%s.conf", website.Alias, website.PrimaryDomain)
|
2023-04-14 08:01:06 +00:00
|
|
|
absolutePath := path.Join(nginxFull.Install.GetPath(), includePath)
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
var oldRewriteContent []byte
|
|
|
|
if !fileOp.Stat(path.Dir(absolutePath)) {
|
|
|
|
if err := fileOp.CreateDir(path.Dir(absolutePath), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !fileOp.Stat(absolutePath) {
|
|
|
|
if err := fileOp.CreateFile(absolutePath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
oldRewriteContent, err = fileOp.GetContent(absolutePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := fileOp.WriteFile(absolutePath, strings.NewReader(req.Content), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := updateNginxConfig(constant.NginxScopeServer, []dto.NginxParam{{Name: "include", Params: []string{includePath}}}, &website); err != nil {
|
|
|
|
_ = fileOp.WriteFile(absolutePath, bytes.NewReader(oldRewriteContent), 0755)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
website.Rewrite = req.Name
|
|
|
|
return websiteRepo.Save(context.Background(), &website)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w WebsiteService) GetRewriteConfig(req request.NginxRewriteReq) (*response.NginxRewriteRes, error) {
|
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var contentByte []byte
|
|
|
|
if req.Name == "current" {
|
|
|
|
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-18 15:27:00 +00:00
|
|
|
rewriteConfPath := path.Join(nginxInstall.GetPath(), "www", "sites", website.Alias, "rewrite", fmt.Sprintf("%s.conf", website.PrimaryDomain))
|
2023-04-14 08:01:06 +00:00
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
if fileOp.Stat(rewriteConfPath) {
|
|
|
|
contentByte, err = fileOp.GetContent(rewriteConfPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rewriteFile := fmt.Sprintf("rewrite/%s.conf", strings.ToLower(req.Name))
|
|
|
|
contentByte, err = nginx_conf.Rewrites.ReadFile(rewriteFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &response.NginxRewriteRes{
|
|
|
|
Content: string(contentByte),
|
|
|
|
}, err
|
|
|
|
}
|
2023-04-17 08:54:34 +00:00
|
|
|
|
|
|
|
func (w WebsiteService) UpdateSiteDir(req request.WebsiteUpdateDir) error {
|
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
runDir := req.SiteDir
|
|
|
|
siteDir := path.Join("/www/sites", website.Alias, "index")
|
|
|
|
if req.SiteDir != "/" {
|
|
|
|
siteDir = fmt.Sprintf("%s/%s", siteDir, req.SiteDir)
|
|
|
|
}
|
|
|
|
if err := updateNginxConfig(constant.NginxScopeServer, []dto.NginxParam{{Name: "root", Params: []string{siteDir}}}, &website); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
website.SiteDir = runDir
|
|
|
|
return websiteRepo.Save(context.Background(), &website)
|
|
|
|
}
|
2023-04-18 10:46:58 +00:00
|
|
|
|
|
|
|
func (w WebsiteService) UpdateSitePermission(req request.WebsiteUpdateDirPermission) error {
|
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-18 15:27:00 +00:00
|
|
|
absoluteIndexPath := path.Join(nginxInstall.GetPath(), "www", "sites", website.Alias, "index")
|
2023-04-18 10:46:58 +00:00
|
|
|
if website.SiteDir != "/" {
|
|
|
|
absoluteIndexPath = path.Join(absoluteIndexPath, website.SiteDir)
|
|
|
|
}
|
|
|
|
chownCmd := fmt.Sprintf("chown -R %s:%s %s", req.User, req.Group, absoluteIndexPath)
|
2023-04-19 03:17:00 +00:00
|
|
|
if cmd.HasNoPasswordSudo() {
|
|
|
|
chownCmd = fmt.Sprintf("sudo %s", chownCmd)
|
|
|
|
}
|
|
|
|
if out, err := cmd.ExecWithTimeOut(chownCmd, 1*time.Second); err != nil {
|
|
|
|
if out != "" {
|
|
|
|
return errors.New(out)
|
|
|
|
}
|
2023-04-18 10:46:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
website.User = req.User
|
|
|
|
website.Group = req.Group
|
|
|
|
return websiteRepo.Save(context.Background(), &website)
|
|
|
|
}
|
2023-04-23 10:15:38 +00:00
|
|
|
|
|
|
|
func (w WebsiteService) OperateProxy(req request.WebsiteProxyConfig) (err error) {
|
|
|
|
var (
|
|
|
|
website model.Website
|
|
|
|
params []response.NginxParam
|
|
|
|
nginxInstall model.AppInstall
|
|
|
|
par *parser.Parser
|
|
|
|
oldContent []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
website, err = websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
params, err = getNginxParamsByKeys(constant.NginxScopeHttp, []string{"proxy_cache"}, &website)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nginxInstall, err = getAppInstallByKey(constant.AppOpenresty)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
if len(params) == 0 || len(params[0].Params) == 0 {
|
|
|
|
commonDir := path.Join(nginxInstall.GetPath(), "www", "common", "proxy")
|
|
|
|
proxyTempPath := path.Join(commonDir, "proxy_temp_dir")
|
|
|
|
if !fileOp.Stat(proxyTempPath) {
|
|
|
|
_ = fileOp.CreateDir(proxyTempPath, 0755)
|
|
|
|
}
|
|
|
|
proxyCacheDir := path.Join(commonDir, "proxy_temp_dir")
|
|
|
|
if !fileOp.Stat(proxyCacheDir) {
|
|
|
|
_ = fileOp.CreateDir(proxyCacheDir, 0755)
|
|
|
|
}
|
|
|
|
nginxParams := getNginxParamsFromStaticFile(dto.CACHE, nil)
|
|
|
|
if err = updateNginxConfig(constant.NginxScopeHttp, nginxParams, &website); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
includeDir := path.Join(nginxInstall.GetPath(), "www", "sites", website.Alias, "proxy")
|
|
|
|
if !fileOp.Stat(includeDir) {
|
|
|
|
_ = fileOp.CreateDir(includeDir, 0755)
|
|
|
|
}
|
|
|
|
fileName := fmt.Sprintf("%s.conf", req.Name)
|
|
|
|
includePath := path.Join(includeDir, fileName)
|
2023-05-05 06:14:56 +00:00
|
|
|
backName := fmt.Sprintf("%s.bak", req.Name)
|
|
|
|
backPath := path.Join(includeDir, backName)
|
2023-04-23 10:15:38 +00:00
|
|
|
|
2023-05-06 11:13:34 +00:00
|
|
|
if req.Operate == "create" && (fileOp.Stat(includePath) || fileOp.Stat(backPath)) {
|
|
|
|
err = buserr.New(constant.ErrNameIsExist)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-23 10:15:38 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
switch req.Operate {
|
|
|
|
case "create":
|
|
|
|
_ = fileOp.DeleteFile(includePath)
|
2023-04-24 03:29:48 +00:00
|
|
|
case "edit":
|
2023-04-23 10:15:38 +00:00
|
|
|
_ = fileOp.WriteFile(includePath, bytes.NewReader(oldContent), 0755)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var config *components.Config
|
|
|
|
|
|
|
|
switch req.Operate {
|
|
|
|
case "create":
|
|
|
|
config = parser.NewStringParser(string(nginx_conf.Proxy)).Parse()
|
2023-04-24 03:29:48 +00:00
|
|
|
case "edit":
|
2023-04-23 10:15:38 +00:00
|
|
|
par, err = parser.NewParser(includePath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
config = par.Parse()
|
|
|
|
oldContent, err = fileOp.GetContent(includePath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "delete":
|
|
|
|
_ = fileOp.DeleteFile(includePath)
|
2023-05-05 06:14:56 +00:00
|
|
|
_ = fileOp.DeleteFile(backPath)
|
2023-04-23 10:15:38 +00:00
|
|
|
return updateNginxConfig(constant.NginxScopeServer, nil, &website)
|
|
|
|
case "disable":
|
|
|
|
_ = fileOp.Rename(includePath, backPath)
|
|
|
|
return updateNginxConfig(constant.NginxScopeServer, nil, &website)
|
2023-04-24 03:29:48 +00:00
|
|
|
case "enable":
|
|
|
|
_ = fileOp.Rename(backPath, includePath)
|
|
|
|
return updateNginxConfig(constant.NginxScopeServer, nil, &website)
|
2023-04-23 10:15:38 +00:00
|
|
|
}
|
|
|
|
config.FilePath = includePath
|
|
|
|
directives := config.Directives
|
|
|
|
location, ok := directives[0].(*components.Location)
|
|
|
|
if !ok {
|
|
|
|
err = errors.New("error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
location.UpdateDirective("proxy_pass", []string{req.ProxyPass})
|
|
|
|
location.UpdateDirective("proxy_set_header", []string{"Host", req.ProxyHost})
|
|
|
|
location.ChangePath(req.Modifier, req.Match)
|
|
|
|
if req.Cache {
|
2023-04-24 03:29:48 +00:00
|
|
|
location.AddCache(req.CacheTime, req.CacheUnit)
|
2023-04-23 10:15:38 +00:00
|
|
|
} else {
|
|
|
|
location.RemoveCache()
|
|
|
|
}
|
2023-04-24 09:48:22 +00:00
|
|
|
if len(req.Replaces) > 0 {
|
|
|
|
location.AddSubFilter(req.Replaces)
|
|
|
|
} else {
|
|
|
|
location.RemoveSubFilter()
|
|
|
|
}
|
2023-04-23 10:15:38 +00:00
|
|
|
if err = nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
|
|
|
|
return buserr.WithErr(constant.ErrUpdateBuWebsite, err)
|
|
|
|
}
|
2023-04-24 03:29:48 +00:00
|
|
|
nginxInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias)
|
2023-04-23 10:15:38 +00:00
|
|
|
if err = updateNginxConfig(constant.NginxScopeServer, []dto.NginxParam{{Name: "include", Params: []string{nginxInclude}}}, &website); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w WebsiteService) GetProxies(id uint) (res []request.WebsiteProxyConfig, err error) {
|
|
|
|
var (
|
|
|
|
website model.Website
|
|
|
|
nginxInstall model.AppInstall
|
|
|
|
fileList response.FileInfo
|
|
|
|
)
|
|
|
|
website, err = websiteRepo.GetFirst(commonRepo.WithByID(id))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nginxInstall, err = getAppInstallByKey(constant.AppOpenresty)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
includeDir := path.Join(nginxInstall.GetPath(), "www", "sites", website.Alias, "proxy")
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
if !fileOp.Stat(includeDir) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fileList, err = NewIFileService().GetFileList(request.FileOption{FileOption: files.FileOption{Path: includeDir, Expand: true, Page: 1, PageSize: 100}})
|
|
|
|
if len(fileList.Items) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
content []byte
|
|
|
|
config *components.Config
|
|
|
|
)
|
|
|
|
for _, configFile := range fileList.Items {
|
2023-04-24 03:29:48 +00:00
|
|
|
proxyConfig := request.WebsiteProxyConfig{
|
|
|
|
ID: website.ID,
|
|
|
|
}
|
2023-04-23 10:15:38 +00:00
|
|
|
parts := strings.Split(configFile.Name, ".")
|
|
|
|
proxyConfig.Name = parts[0]
|
|
|
|
if parts[1] == "conf" {
|
|
|
|
proxyConfig.Enable = true
|
|
|
|
} else {
|
|
|
|
proxyConfig.Enable = false
|
|
|
|
}
|
|
|
|
proxyConfig.FilePath = configFile.Path
|
|
|
|
content, err = fileOp.GetContent(configFile.Path)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-04-24 09:48:22 +00:00
|
|
|
proxyConfig.Content = string(content)
|
2023-04-23 10:15:38 +00:00
|
|
|
config = parser.NewStringParser(string(content)).Parse()
|
|
|
|
directives := config.GetDirectives()
|
|
|
|
|
|
|
|
location, ok := directives[0].(*components.Location)
|
|
|
|
if !ok {
|
|
|
|
err = errors.New("error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
proxyConfig.ProxyPass = location.ProxyPass
|
|
|
|
proxyConfig.Cache = location.Cache
|
2023-04-24 03:29:48 +00:00
|
|
|
if location.CacheTime > 0 {
|
|
|
|
proxyConfig.CacheTime = location.CacheTime
|
|
|
|
proxyConfig.CacheUnit = location.CacheUint
|
|
|
|
}
|
2023-04-23 10:15:38 +00:00
|
|
|
proxyConfig.Match = location.Match
|
2023-04-24 03:29:48 +00:00
|
|
|
proxyConfig.Modifier = location.Modifier
|
|
|
|
proxyConfig.ProxyHost = location.Host
|
2023-04-24 09:48:22 +00:00
|
|
|
proxyConfig.Replaces = location.Replaces
|
2023-04-23 10:15:38 +00:00
|
|
|
res = append(res, proxyConfig)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2023-04-24 09:48:22 +00:00
|
|
|
|
|
|
|
func (w WebsiteService) UpdateProxyFile(req request.NginxProxyUpdate) (err error) {
|
|
|
|
var (
|
|
|
|
website model.Website
|
|
|
|
nginxFull dto.NginxFull
|
|
|
|
oldRewriteContent []byte
|
|
|
|
)
|
|
|
|
website, err = websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nginxFull, err = getNginxFull(&website)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
includePath := fmt.Sprintf("/www/sites/%s/proxy/%s.conf", website.Alias, req.Name)
|
|
|
|
absolutePath := path.Join(nginxFull.Install.GetPath(), includePath)
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
oldRewriteContent, err = fileOp.GetContent(absolutePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = fileOp.WriteFile(absolutePath, strings.NewReader(req.Content), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
_ = fileOp.WriteFile(absolutePath, bytes.NewReader(oldRewriteContent), 0755)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return updateNginxConfig(constant.NginxScopeServer, nil, &website)
|
|
|
|
}
|
2023-04-25 10:10:17 +00:00
|
|
|
|
|
|
|
func (w WebsiteService) UpdateAuthBasic(req request.NginxAuthUpdate) (err error) {
|
|
|
|
var (
|
|
|
|
website model.Website
|
|
|
|
nginxInstall model.AppInstall
|
|
|
|
params []dto.NginxParam
|
|
|
|
authContent []byte
|
|
|
|
authArray []string
|
|
|
|
)
|
|
|
|
website, err = websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nginxInstall, err = getAppInstallByKey(constant.AppOpenresty)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
authPath := fmt.Sprintf("/www/sites/%s/auth_basic/auth.pass", website.Alias)
|
|
|
|
absoluteAuthPath := path.Join(nginxInstall.GetPath(), authPath)
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
if !fileOp.Stat(path.Dir(absoluteAuthPath)) {
|
2023-04-26 08:06:12 +00:00
|
|
|
_ = fileOp.CreateDir(path.Dir(absoluteAuthPath), 0755)
|
2023-04-25 10:10:17 +00:00
|
|
|
}
|
|
|
|
if !fileOp.Stat(absoluteAuthPath) {
|
|
|
|
_ = fileOp.CreateFile(absoluteAuthPath)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
switch req.Operate {
|
|
|
|
case "create":
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
params = append(params, dto.NginxParam{Name: "auth_basic", Params: []string{`"Authentication"`}})
|
|
|
|
params = append(params, dto.NginxParam{Name: "auth_basic_user_file", Params: []string{authPath}})
|
|
|
|
authContent, err = fileOp.GetContent(absoluteAuthPath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
authArray = strings.Split(string(authContent), "\n")
|
|
|
|
switch req.Operate {
|
|
|
|
case "disable":
|
|
|
|
return deleteNginxConfig(constant.NginxScopeServer, params, &website)
|
|
|
|
case "enable":
|
|
|
|
return updateNginxConfig(constant.NginxScopeServer, params, &website)
|
|
|
|
case "create":
|
|
|
|
for _, line := range authArray {
|
|
|
|
authParams := strings.Split(line, ":")
|
|
|
|
username := authParams[0]
|
|
|
|
if username == req.Username {
|
|
|
|
err = buserr.New(constant.ErrUsernameIsExist)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var passwdHash []byte
|
|
|
|
passwdHash, err = bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
line := fmt.Sprintf("%s:%s\n", req.Username, passwdHash)
|
|
|
|
if req.Remark != "" {
|
|
|
|
line = fmt.Sprintf("%s:%s:%s\n", req.Username, passwdHash, req.Remark)
|
|
|
|
}
|
|
|
|
authArray = append(authArray, line)
|
|
|
|
case "edit":
|
|
|
|
userExist := false
|
|
|
|
for index, line := range authArray {
|
|
|
|
authParams := strings.Split(line, ":")
|
|
|
|
username := authParams[0]
|
|
|
|
if username == req.Username {
|
|
|
|
userExist = true
|
|
|
|
var passwdHash []byte
|
|
|
|
passwdHash, err = bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
userPasswd := fmt.Sprintf("%s:%s\n", req.Username, passwdHash)
|
|
|
|
if req.Remark != "" {
|
|
|
|
userPasswd = fmt.Sprintf("%s:%s:%s\n", req.Username, passwdHash, req.Remark)
|
|
|
|
}
|
|
|
|
authArray[index] = userPasswd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !userExist {
|
|
|
|
err = buserr.New(constant.ErrUsernameIsNotExist)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "delete":
|
|
|
|
deleteIndex := -1
|
|
|
|
for index, line := range authArray {
|
|
|
|
authParams := strings.Split(line, ":")
|
|
|
|
username := authParams[0]
|
|
|
|
if username == req.Username {
|
|
|
|
deleteIndex = index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if deleteIndex < 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
authArray = append(authArray[:deleteIndex], authArray[deleteIndex+1:]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
var passFile *os.File
|
|
|
|
passFile, err = os.Create(absoluteAuthPath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer passFile.Close()
|
|
|
|
writer := bufio.NewWriter(passFile)
|
|
|
|
for _, line := range authArray {
|
|
|
|
_, err = writer.WriteString(line + "\n")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = writer.Flush()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w WebsiteService) GetAuthBasics(req request.NginxAuthReq) (res response.NginxAuthRes, err error) {
|
|
|
|
var (
|
|
|
|
website model.Website
|
|
|
|
nginxInstall model.AppInstall
|
|
|
|
authContent []byte
|
|
|
|
nginxParams []response.NginxParam
|
|
|
|
)
|
|
|
|
website, err = websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nginxInstall, err = getAppInstallByKey(constant.AppOpenresty)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
authPath := fmt.Sprintf("/www/sites/%s/auth_basic/auth.pass", website.Alias)
|
|
|
|
absoluteAuthPath := path.Join(nginxInstall.GetPath(), authPath)
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
if !fileOp.Stat(absoluteAuthPath) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nginxParams, err = getNginxParamsByKeys(constant.NginxScopeServer, []string{"auth_basic"}, &website)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res.Enable = len(nginxParams[0].Params) > 0
|
|
|
|
authContent, err = fileOp.GetContent(absoluteAuthPath)
|
|
|
|
authArray := strings.Split(string(authContent), "\n")
|
|
|
|
for _, line := range authArray {
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
params := strings.Split(line, ":")
|
|
|
|
auth := dto.NginxAuth{
|
|
|
|
Username: params[0],
|
|
|
|
}
|
|
|
|
if len(params) == 3 {
|
|
|
|
auth.Remark = params[2]
|
|
|
|
}
|
|
|
|
res.Items = append(res.Items, auth)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|