Skip to content

Commit

Permalink
feat(网站): 支持设置QUIC和OCSP
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jul 12, 2024
1 parent 6c6fb09 commit 1e081fc
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 261 deletions.
12 changes: 5 additions & 7 deletions app/console/commands/panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,8 @@ func (receiver *Panel) Handle(ctx console.Context) error {
Name: name,
Status: status,
Path: path,
Php: php,
Ssl: ssl,
PHP: php,
SSL: ssl,
})
if err != nil {
color.Red().Printfln(translate.Get("commands.panel.writeSite.fail"))
Expand Down Expand Up @@ -615,15 +615,13 @@ func (receiver *Panel) Handle(ctx console.Context) error {
return nil
}

_, err = website.Add(types.WebsiteAdd{
_, err = website.Add(requests.Add{
Name: name,
Status: true,
Domains: domains,
Ports: uintPorts,
Path: path,
Php: php,
Ssl: false,
Db: false,
PHP: php,
DB: false,
})
if err != nil {
color.Red().Printfln(err.Error())
Expand Down
38 changes: 11 additions & 27 deletions app/http/controllers/website_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/str"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/types"
)

type WebsiteController struct {
Expand Down Expand Up @@ -55,7 +54,7 @@ func (r *WebsiteController) List(ctx http.Context) http.Response {
facades.Log().Request(ctx.Request()).Tags("面板", "网站管理").With(map[string]any{
"error": err.Error(),
}).Info("获取网站列表失败")
return ErrorSystem(ctx)
return Error(ctx, http.StatusInternalServerError, err.Error())
}

return Success(ctx, http.Json{
Expand Down Expand Up @@ -85,27 +84,12 @@ func (r *WebsiteController) Add(ctx http.Context) http.Response {
addRequest.Path = r.setting.Get(models.SettingKeyWebsitePath) + "/" + addRequest.Name
}

website := types.WebsiteAdd{
Name: addRequest.Name,
Status: true,
Domains: addRequest.Domains,
Ports: addRequest.Ports,
Path: addRequest.Path,
Php: addRequest.Php,
Ssl: false,
Db: addRequest.Db,
DbType: addRequest.DbType,
DbName: addRequest.DbName,
DbUser: addRequest.DbUser,
DbPassword: addRequest.DbPassword,
}

_, err := r.website.Add(website)
_, err := r.website.Add(addRequest)
if err != nil {
facades.Log().Request(ctx.Request()).Tags("面板", "网站管理").With(map[string]any{
"error": err.Error(),
}).Info("添加网站失败")
return ErrorSystem(ctx)
return Error(ctx, http.StatusInternalServerError, err.Error())
}

return Success(ctx, nil)
Expand Down Expand Up @@ -181,14 +165,14 @@ func (r *WebsiteController) SaveDefaultConfig(ctx http.Context) http.Response {
facades.Log().Request(ctx.Request()).Tags("面板", "网站管理").With(map[string]any{
"error": err.Error(),
}).Info("保存默认首页配置失败")
return ErrorSystem(ctx)
return Error(ctx, http.StatusInternalServerError, err.Error())
}

if err := io.Write("/www/server/openresty/html/stop.html", stop, 0644); err != nil {
facades.Log().Request(ctx.Request()).Tags("面板", "网站管理").With(map[string]any{
"error": err.Error(),
}).Info("保存默认停止页配置失败")
return ErrorSystem(ctx)
return Error(ctx, http.StatusInternalServerError, err.Error())
}

return Success(ctx, nil)
Expand Down Expand Up @@ -217,7 +201,7 @@ func (r *WebsiteController) GetConfig(ctx http.Context) http.Response {
"id": idRequest.ID,
"error": err.Error(),
}).Info("获取网站配置失败")
return ErrorSystem(ctx)
return Error(ctx, http.StatusInternalServerError, err.Error())
}

return Success(ctx, config)
Expand Down Expand Up @@ -335,7 +319,7 @@ func (r *WebsiteController) BackupList(ctx http.Context) http.Response {
facades.Log().Request(ctx.Request()).Tags("面板", "网站管理").With(map[string]any{
"error": err.Error(),
}).Info("获取备份列表失败")
return ErrorSystem(ctx)
return Error(ctx, http.StatusInternalServerError, err.Error())
}

paged, total := Paginate(ctx, backups)
Expand Down Expand Up @@ -412,7 +396,7 @@ func (r *WebsiteController) UploadBackup(ctx http.Context) http.Response {
facades.Log().Request(ctx.Request()).Tags("面板", "网站管理").With(map[string]any{
"error": err.Error(),
}).Info("上传备份失败")
return ErrorSystem(ctx)
return Error(ctx, http.StatusInternalServerError, err.Error())
}

return Success(ctx, nil)
Expand Down Expand Up @@ -506,7 +490,7 @@ func (r *WebsiteController) ResetConfig(ctx http.Context) http.Response {
}

website.Status = true
website.Ssl = false
website.SSL = false
if err := facades.Orm().Query().Save(&website); err != nil {
facades.Log().Request(ctx.Request()).Tags("面板", "网站管理").With(map[string]any{
"id": idRequest.ID,
Expand Down Expand Up @@ -575,7 +559,7 @@ server
error_log /www/wwwlogs/%s.log;
}
`, website.Path, website.Php, website.Name, website.Name, website.Name, website.Name)
`, website.Path, website.PHP, website.Name, website.Name, website.Name, website.Name)
if err := io.Write("/www/server/vhost/"+website.Name+".conf", raw, 0644); err != nil {
return nil
}
Expand Down Expand Up @@ -650,7 +634,7 @@ func (r *WebsiteController) Status(ctx http.Context) http.Response {
}

if err = io.Write("/www/server/vhost/"+website.Name+".conf", raw, 0644); err != nil {
return ErrorSystem(ctx)
return Error(ctx, http.StatusInternalServerError, err.Error())
}
if err = systemctl.Reload("openresty"); err != nil {
_, err = shell.Execf("openresty -t")
Expand Down
12 changes: 6 additions & 6 deletions app/http/requests/website/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ type Add struct {
Domains []string `form:"domains" json:"domains"`
Ports []uint `form:"ports" json:"ports"`
Path string `form:"path" json:"path"`
Php string `form:"php" json:"php"`
Db bool `form:"db" json:"db"`
DbType string `form:"db_type" json:"db_type"`
DbName string `form:"db_name" json:"db_name"`
DbUser string `form:"db_user" json:"db_user"`
DbPassword string `form:"db_password" json:"db_password"`
PHP string `form:"php" json:"php"`
DB bool `form:"db" json:"db"`
DBType string `form:"db_type" json:"db_type"`
DBName string `form:"db_name" json:"db_name"`
DBUser string `form:"db_user" json:"db_user"`
DBPassword string `form:"db_password" json:"db_password"`
}

func (r *Add) Authorize(ctx http.Context) error {
Expand Down
20 changes: 12 additions & 8 deletions app/http/requests/website/save_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ type SaveConfig struct {
ID uint `form:"id" json:"id"`
Domains []string `form:"domains" json:"domains"`
Ports []uint `form:"ports" json:"ports"`
TLSPorts []uint `form:"tls_ports" json:"tls_ports"`
Hsts bool `form:"hsts" json:"hsts"`
Ssl bool `form:"ssl" json:"ssl"`
HttpRedirect bool `form:"http_redirect" json:"http_redirect"`
SSLPorts []uint `form:"ssl_ports" json:"ssl_ports"`
QUICPorts []uint `form:"quic_ports" json:"quic_ports"`
OCSP bool `form:"ocsp" json:"ocsp"`
HSTS bool `form:"hsts" json:"hsts"`
SSL bool `form:"ssl" json:"ssl"`
HTTPRedirect bool `form:"http_redirect" json:"http_redirect"`
OpenBasedir bool `form:"open_basedir" json:"open_basedir"`
Waf bool `form:"waf" json:"waf"`
WafCache string `form:"waf_cache" json:"waf_cache"`
Expand All @@ -23,9 +25,9 @@ type SaveConfig struct {
Root string `form:"root" json:"root"`
Raw string `form:"raw" json:"raw"`
Rewrite string `form:"rewrite" json:"rewrite"`
Php int `form:"php" json:"php"`
SslCertificate string `form:"ssl_certificate" json:"ssl_certificate"`
SslCertificateKey string `form:"ssl_certificate_key" json:"ssl_certificate_key"`
PHP int `form:"php" json:"php"`
SSLCertificate string `form:"ssl_certificate" json:"ssl_certificate"`
SSLCertificateKey string `form:"ssl_certificate_key" json:"ssl_certificate_key"`
}

func (r *SaveConfig) Authorize(ctx http.Context) error {
Expand All @@ -37,7 +39,9 @@ func (r *SaveConfig) Rules(ctx http.Context) map[string]string {
"id": "required|exists:websites,id",
"domains": "required|slice",
"ports": "required|slice",
"tls_ports": "required_if:ssl,true|slice|not_in:80",
"ssl_ports": "slice|not_in:80",
"quic_ports": "slice|not_in:80",
"ocsp": "bool",
"hsts": "bool",
"ssl": "bool",
"http_redirect": "bool",
Expand Down
4 changes: 2 additions & 2 deletions app/models/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ type Website struct {
Name string `gorm:"not null;unique" json:"name"`
Status bool `gorm:"not null;default:true" json:"status"`
Path string `gorm:"not null" json:"path"`
Php int `gorm:"not null" json:"php"`
Ssl bool `gorm:"not null" json:"ssl"`
PHP int `gorm:"not null" json:"php"`
SSL bool `gorm:"not null" json:"ssl"`
Remark string `gorm:"not null" json:"remark"`

Cert *Cert `gorm:"foreignKey:WebsiteID" json:"cert"`
Expand Down
3 changes: 3 additions & 0 deletions embed/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ import "embed"

//go:embed frontend/*
var PublicFS embed.FS

//go:embed website/*
var WebsiteFS embed.FS
55 changes: 55 additions & 0 deletions embed/website/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>404 Not Found</title>
<style>
body {
background-color: #f9f9f9;
margin: 0;
padding: 0;
}
.container {
max-width: 800px;
margin: 2em auto;
background-color: #ffffff;
padding: 20px;
border-radius: 12px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
h1 {
font-size: 2.5em;
margin-top: 0;
margin-bottom: 20px;
text-align: center;
color: #333;
border-bottom: 2px solid #ddd;
padding-bottom: 0.5em;
}
p {
color: #555;
line-height: 1.8;
text-align: center;
}
a {
text-decoration: none;
color: #007bff;
}
@media screen and (max-width: 768px) {
.container {
padding: 15px;
margin: 2em 15px;
}
h1 {
font-size: 1.8em;
}
}
</style>
</head>
<body>
<div class="container">
<h1>404 Not Found</h1>
<p><a target="_blank" href="https://panel.haozi.net">耗子面板</a> 强力驱动</p>
</div>
</body>
</html>
58 changes: 58 additions & 0 deletions embed/website/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>耗子面板</title>
<style>
body {
background-color: #f9f9f9;
margin: 0;
padding: 0;
}
.container {
max-width: 800px;
margin: 2em auto;
background-color: #ffffff;
padding: 20px;
border-radius: 12px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
h1 {
font-size: 2.5em;
margin-top: 0;
margin-bottom: 20px;
text-align: center;
color: #333;
border-bottom: 2px solid #ddd;
padding-bottom: 0.5em;
}
p {
color: #555;
line-height: 1.8;
text-align: center;
}
a {
text-decoration: none;
color: #007bff;
}
@media screen and (max-width: 768px) {
.container {
padding: 15px;
margin: 2em 15px;
}
h1 {
font-size: 1.8em;
}
}
</style>
</head>
<body>
<div class="container">
<h1>耗子面板</h1>
<p>这是耗子面板的网站默认页面!</p>
<p>当您看到此页面,说明您的网站已创建成功。</p>
<p><a target="_blank" href="https://panel.haozi.net">耗子面板</a> 强力驱动</p>
</div>
</body>
</html>
Loading

0 comments on commit 1e081fc

Please sign in to comment.