Skip to content

Commit

Permalink
feat: website controller
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jul 19, 2023
1 parent 46644c3 commit ec7138a
Show file tree
Hide file tree
Showing 8 changed files with 758 additions and 24 deletions.
583 changes: 576 additions & 7 deletions app/http/controllers/website_controller.go

Large diffs are not rendered by default.

73 changes: 71 additions & 2 deletions app/services/backup.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,76 @@
// Package services 备份服务
package services

import (
"errors"
"os"

"github.com/goravel/framework/support/carbon"

"panel/app/models"
"panel/packages/helper"
)

type Backup interface {
BackupWebSite(string) error
BackupDatabase(string, string) error
WebsiteList() ([]BackupFile, error)
WebSiteBackup(website models.Website) error
}

type BackupFile struct {
Name string `json:"name"`
Size string `json:"size"`
}

type BackupImpl struct {
setting Setting
}

func NewBackupImpl() *BackupImpl {
return &BackupImpl{
setting: NewSettingImpl(),
}
}

func (s *BackupImpl) WebsiteList() ([]BackupFile, error) {
path := s.setting.Get(models.SettingKeyBackupPath)
if len(path) == 0 {
return []BackupFile{}, nil
}

path += "/website"

files, err := os.ReadDir(path)
if err != nil {
return []BackupFile{}, err
}
var backupList []BackupFile
for _, file := range files {
info, err := file.Info()
if err != nil {
continue
}
backupList = append(backupList, BackupFile{
Name: file.Name(),
Size: helper.FormatBytes(float64(info.Size())),
})
}

return backupList, nil
}

func (s *BackupImpl) WebSiteBackup(website models.Website) error {
backupPath := s.setting.Get(models.SettingKeyBackupPath)
if len(backupPath) == 0 {
return errors.New("未正确配置备份路径")
}

backupPath += "/website"
if !helper.Exists(backupPath) {
helper.Mkdir(backupPath, 0644)
}

backupFile := backupPath + "/" + website.Name + carbon.Now().ToShortDateTimeString() + ".zip"
helper.ExecShell("cd " + website.Path + " && zip -r " + backupFile + " .")

return nil
}
104 changes: 100 additions & 4 deletions app/services/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package services
import (
"errors"
"fmt"
"regexp"
"strings"

"github.com/goravel/framework/facades"
Expand All @@ -14,7 +15,10 @@ import (
)

type Website interface {
List() ([]models.Website, error)
List(page int, limit int) (int64, []models.Website, error)
Add(website PanelWebsite) (models.Website, error)
Delete(id int) error
GetConfig(id int) (WebsiteSetting, error)
}

type PanelWebsite struct {
Expand All @@ -40,6 +44,7 @@ type WebsiteSetting struct {
Root string `json:"root"`
Path string `json:"path"`
Index string `json:"index"`
Php int `json:"php"`
OpenBasedir bool `json:"open_basedir"`
Ssl bool `json:"ssl"`
SslCertificate string `json:"ssl_certificate"`
Expand Down Expand Up @@ -192,7 +197,7 @@ server
#error_page 502 /502.html;
# 伪静态规则引入,修改后将导致面板设置的伪静态规则失效
include /www/server/vhost/openresty/rewrite/%s.conf;
include /www/server/vhost/rewrite/%s.conf;
# 面板默认禁止访问部分敏感目录,可自行修改
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn)
Expand Down Expand Up @@ -226,9 +231,9 @@ server
}

// Delete 删除网站
func (r *WebsiteImpl) Delete(name string) error {
func (r *WebsiteImpl) Delete(id int) error {
var website models.Website
if err := facades.Orm().Query().Where("name", name).First(&website); err != nil {
if err := facades.Orm().Query().Where("id", id).First(&website); err != nil {
return err
}

Expand All @@ -248,3 +253,94 @@ func (r *WebsiteImpl) Delete(name string) error {

return nil
}

// GetConfig 获取网站配置
func (r *WebsiteImpl) GetConfig(id int) (WebsiteSetting, error) {
var website models.Website
if err := facades.Orm().Query().Where("id", id).First(&website); err != nil {
return WebsiteSetting{}, err
}

config := helper.ReadFile("/www/server/panel/vhost/openresty/" + website.Name + ".conf")

var setting WebsiteSetting
setting.Name = website.Name
setting.Path = website.Path
setting.Ssl = website.Ssl
setting.Php = website.Php
setting.Raw = config

ports := helper.Cut(config, "# port标记位开始", "# port标记位结束")
matches := regexp.MustCompile(`listen\s+(.*);`).FindAllStringSubmatch(ports, -1)
for _, match := range matches {
if len(match) < 2 {
continue
}
setting.Ports = append(setting.Ports, match[1])
}
serverName := helper.Cut(config, "# server_name标记位开始", "# server_name标记位结束")
match := regexp.MustCompile(`server_name\s+(.*);`).FindStringSubmatch(serverName)
if len(match) > 1 {
setting.Domains = strings.Split(match[1], " ")
}
root := helper.Cut(config, "# root标记位开始", "# root标记位结束")
match = regexp.MustCompile(`root\s+(.*);`).FindStringSubmatch(root)
if len(match) > 1 {
setting.Root = match[1]
}
index := helper.Cut(config, "# index标记位开始", "# index标记位结束")
match = regexp.MustCompile(`index\s+(.*);`).FindStringSubmatch(index)
if len(match) > 1 {
setting.Index = match[1]
}

if helper.Exists(setting.Root + "/.user.ini") {
userIni := helper.ReadFile(setting.Path + "/.user.ini")
if strings.Contains(userIni, "open_basedir") {
setting.OpenBasedir = true
} else {
setting.OpenBasedir = false
}
} else {
setting.OpenBasedir = false
}

if setting.Ssl {
ssl := helper.Cut(config, "# ssl标记位开始", "# ssl标记位结束")
match = regexp.MustCompile(`ssl_certificate\s+(.*);`).FindStringSubmatch(ssl)
if len(match) > 1 {
setting.SslCertificate = helper.ReadFile(match[1])
}
match = regexp.MustCompile(`ssl_certificate_key\s+(.*);`).FindStringSubmatch(ssl)
if len(match) > 1 {
setting.SslCertificateKey = helper.ReadFile(match[1])
}
setting.HttpRedirect = strings.Contains(ssl, "# http重定向标记位")
setting.Hsts = strings.Contains(ssl, "# hsts标记位")
} else {
setting.SslCertificate = ""
setting.SslCertificateKey = ""
setting.HttpRedirect = false
setting.Hsts = false
}

waf := helper.Cut(config, "# waf标记位开始", "# waf标记位结束")
setting.Waf = strings.Contains(waf, "waf on;")
match = regexp.MustCompile(`waf_mode\s+(.+);`).FindStringSubmatch(waf)
if len(match) > 1 {
setting.WafMode = match[1]
}
match = regexp.MustCompile(`waf_cc_deny\s+(.+);`).FindStringSubmatch(waf)
if len(match) > 1 {
setting.WafCcDeny = match[1]
}
match = regexp.MustCompile(`waf_cache\s+(.+);`).FindStringSubmatch(waf)
if len(match) > 1 {
setting.WafCache = match[1]
}

setting.Rewrite = helper.ReadFile("/www/server/panel/vhost/openresty/rewrite/" + website.Name + ".conf")
setting.Log = helper.ExecShell("tail -n 100 /www/wwwlogs/" + website.Name + ".log")

return setting, nil
}
2 changes: 1 addition & 1 deletion packages/helper/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func FormatBytes(size float64) string {
}

// Cut 裁剪字符串
func Cut(begin, end, str string) string {
func Cut(str, begin, end string) string {
bIndex := strings.Index(str, begin)
eIndex := strings.Index(str, end)
if bIndex == -1 || eIndex == -1 || bIndex > eIndex {
Expand Down
2 changes: 1 addition & 1 deletion packages/helper/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ func (s *StringHelperTestSuite) TestFormatBytes() {
}

func (s *StringHelperTestSuite) TestCut() {
s.Equal("aoZ", Cut("H", "i", "HaoZi"))
s.Equal("aoZ", Cut("HaoZi", "H", "i"))
}
10 changes: 5 additions & 5 deletions scripts/openresty/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ rm -rf src
mkdir -p /www/wwwroot/default
mkdir -p /www/wwwlogs
mkdir -p /www/server/vhost
mkdir -p /www/server/vhost/openresty
mkdir -p /www/server/vhost/openresty/rewrite
mkdir -p /www/server/vhost/openresty/ssl
mkdir -p /www/server/vhost
mkdir -p /www/server/vhost/rewrite
mkdir -p /www/server/vhost/ssl

# 写入主配置文件
cat >${openrestyPath}/conf/nginx.conf <<EOF
Expand Down Expand Up @@ -264,7 +264,7 @@ http {
fastcgi_param SCRIPT_FILENAME \$fastcgi_script_name;
}
}
include /www/server/vhost/openresty/*.conf;
include /www/server/vhost/*.conf;
}
EOF
# 写入pathinfo配置文件
Expand Down Expand Up @@ -318,7 +318,7 @@ chown -R www:www /www/wwwroot
chmod -R 644 /www/server/vhost

# 写入无php配置文件
echo "" >${openrestyPath}/conf/enable-php-00.conf
echo "" >${openrestyPath}/conf/enable-php-0.conf
# 写入代理默认配置文件
cat >${openrestyPath}/conf/proxy.conf <<EOF
proxy_temp_path ${openrestyPath}/proxy_temp_dir;
Expand Down
6 changes: 3 additions & 3 deletions scripts/phpmyadmin/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ if [ -d "/www/server/php/82" ]; then
fi

# 写入 phpMyAdmin 配置文件
cat >/www/server/vhost/openresty/phpmyadmin.conf <<EOF
cat >/www/server/vhost/phpmyadmin.conf <<EOF
# 配置文件中的标记位请勿随意修改,改错将导致面板无法识别!
# 有自定义配置需求的,请将自定义的配置写在各标记位下方。
server
Expand Down Expand Up @@ -100,8 +100,8 @@ server
}
EOF
# 设置文件权限
chown -R root:root /www/server/vhost/openresty/phpmyadmin.conf
chmod -R 644 /www/server/vhost/openresty/phpmyadmin.conf
chown -R root:root /www/server/vhost/phpmyadmin.conf
chmod -R 644 /www/server/vhost/phpmyadmin.conf

# 放行端口
firewall-cmd --permanent --zone=public --add-port=888/tcp >/dev/null 2>&1
Expand Down
2 changes: 1 addition & 1 deletion scripts/phpmyadmin/uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ setupPath="/www"
phpmyadminPath="${setupPath}/wwwroot/phpmyadmin"


rm -rf /www/server/vhost/openresty/phpmyadmin.conf
rm -rf /www/server/vhost/phpmyadmin.conf
rm -rf ${phpmyadminPath}
panel deletePlugin phpmyadmin
systemctl reload openresty
Expand Down

0 comments on commit ec7138a

Please sign in to comment.