From 684f20a5dc9c4e0783d1b6467d795c49cb0c65a9 Mon Sep 17 00:00:00 2001
From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com>
Date: Thu, 24 Aug 2023 18:20:15 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20Openresty=20=E6=94=AF=E6=8C=81=E9=9D=9E?=
=?UTF-8?q?=2080=20443=20=E7=AB=AF=E5=8F=A3=E5=AE=89=E8=A3=85=20(#2059)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
backend/app/dto/response/app.go | 2 ++
backend/app/service/app.go | 8 +++--
backend/app/service/app_install.go | 2 ++
backend/app/service/app_utils.go | 15 +++++++--
backend/app/service/nginx.go | 11 ++++++-
backend/app/service/website.go | 34 ++++++++++++++------
backend/app/service/website_ssl.go | 5 ++-
backend/app/service/website_utils.go | 27 ++++++++++------
backend/i18n/lang/en.yaml | 1 +
backend/i18n/lang/zh-Hant.yaml | 1 +
backend/i18n/lang/zh.yaml | 1 +
backend/utils/cmd/cmd.go | 2 +-
frontend/src/api/interface/app.ts | 2 ++
frontend/src/components/app-status/index.vue | 17 ++++++++++
frontend/src/lang/modules/en.ts | 1 +
frontend/src/lang/modules/tw.ts | 1 +
frontend/src/lang/modules/zh.ts | 1 +
17 files changed, 103 insertions(+), 28 deletions(-)
diff --git a/backend/app/dto/response/app.go b/backend/app/dto/response/app.go
index cf51844f6..11fbd9a41 100644
--- a/backend/app/dto/response/app.go
+++ b/backend/app/dto/response/app.go
@@ -39,6 +39,8 @@ type AppInstalledCheck struct {
AppInstallID uint `json:"appInstallId"`
ContainerName string `json:"containerName"`
InstallPath string `json:"installPath"`
+ HttpPort int `json:"httpPort"`
+ HttpsPort int `json:"httpsPort"`
}
type AppDetailDTO struct {
diff --git a/backend/app/service/app.go b/backend/app/service/app.go
index 54bc33e61..e5c86571c 100644
--- a/backend/app/service/app.go
+++ b/backend/app/service/app.go
@@ -394,12 +394,14 @@ func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) (
return
}
go func() {
- if err = copyData(app, appDetail, appInstall, req); err != nil {
- if appInstall.Status == constant.Installing {
+ defer func() {
+ if err != nil {
appInstall.Status = constant.Error
appInstall.Message = err.Error()
+ _ = appInstallRepo.Save(context.Background(), appInstall)
}
- _ = appInstallRepo.Save(context.Background(), appInstall)
+ }()
+ if err = copyData(app, appDetail, appInstall, req); err != nil {
return
}
if err = runScript(appInstall, "init"); err != nil {
diff --git a/backend/app/service/app_install.go b/backend/app/service/app_install.go
index bd2cf5988..967edf1b7 100644
--- a/backend/app/service/app_install.go
+++ b/backend/app/service/app_install.go
@@ -156,6 +156,8 @@ func (a *AppInstallService) CheckExist(key string) (*response.AppInstalledCheck,
res.AppInstallID = appInstall.ID
res.IsExist = true
res.InstallPath = path.Join(constant.AppInstallDir, app.Key, appInstall.Name)
+ res.HttpPort = appInstall.HttpPort
+ res.HttpsPort = appInstall.HttpsPort
return res, nil
}
diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go
index 2094e602e..a1cdcbe5a 100644
--- a/backend/app/service/app_utils.go
+++ b/backend/app/service/app_utils.go
@@ -613,8 +613,9 @@ func runScript(appInstall *model.AppInstall, operate string) error {
out, err := cmd.ExecScript(scriptPath, workDir)
if err != nil {
if out != "" {
- global.LOG.Errorf("run script %s error %s", scriptPath, out)
- return errors.New(out)
+ errMsg := fmt.Sprintf("run script %s error %s", scriptPath, out)
+ global.LOG.Error(errMsg)
+ return errors.New(errMsg)
}
return err
}
@@ -907,6 +908,16 @@ func getAppInstallByKey(key string) (model.AppInstall, error) {
return appInstall, nil
}
+func getAppInstallPort(key string) (httpPort, httpsPort int, err error) {
+ install, err := getAppInstallByKey(key)
+ if err != nil {
+ return
+ }
+ httpPort = install.HttpPort
+ httpsPort = install.HttpsPort
+ return
+}
+
func updateToolApp(installed *model.AppInstall) {
tooKey, ok := dto.AppToolMap[installed.App.Key]
if !ok {
diff --git a/backend/app/service/nginx.go b/backend/app/service/nginx.go
index 32f7f0ede..c012619f2 100644
--- a/backend/app/service/nginx.go
+++ b/backend/app/service/nginx.go
@@ -1,6 +1,7 @@
package service
import (
+ "fmt"
"io"
"net/http"
"os"
@@ -61,7 +62,15 @@ func (n NginxService) UpdateConfigByScope(req request.NginxConfigUpdate) error {
}
func (n NginxService) GetStatus() (response.NginxStatus, error) {
- res, err := http.Get("http://127.0.0.1/nginx_status")
+ httpPort, _, err := getAppInstallPort(constant.AppOpenresty)
+ if err != nil {
+ return response.NginxStatus{}, err
+ }
+ url := "http://127.0.0.1/nginx_status"
+ if httpPort != 80 {
+ url = fmt.Sprintf("http://127.0.0.1:%v/nginx_status", httpPort)
+ }
+ res, err := http.Get(url)
if err != nil {
return response.NginxStatus{}, err
}
diff --git a/backend/app/service/website.go b/backend/app/service/website.go
index 31fc46e06..ee3fc49bf 100644
--- a/backend/app/service/website.go
+++ b/backend/app/service/website.go
@@ -22,7 +22,6 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
- "github.com/1Panel-dev/1Panel/backend/utils/common"
"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"
@@ -175,6 +174,11 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
if exist, _ := websiteDomainRepo.GetBy(websiteDomainRepo.WithDomain(create.PrimaryDomain)); len(exist) > 0 {
return buserr.New(constant.ErrDomainIsExist)
}
+ nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
+ if err != nil {
+ return err
+ }
+ defaultHttpPort := nginxInstall.HttpPort
defaultDate, _ := time.Parse(constant.DateLayout, constant.DefaultDate)
website := &model.Website{
@@ -291,13 +295,13 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
}
var domains []model.WebsiteDomain
- domains = append(domains, model.WebsiteDomain{Domain: website.PrimaryDomain, Port: 80})
+ domains = append(domains, model.WebsiteDomain{Domain: website.PrimaryDomain, Port: defaultHttpPort})
otherDomainArray := strings.Split(create.OtherDomains, "\n")
for _, domain := range otherDomainArray {
if domain == "" {
continue
}
- domainModel, err := getDomain(domain)
+ domainModel, err := getDomain(domain, defaultHttpPort)
if err != nil {
return err
}
@@ -445,11 +449,11 @@ func (w WebsiteService) CreateWebsiteDomain(create request.WebsiteDomainCreate)
ports []int
domains []string
)
- if create.Port != 80 {
- if common.ScanPort(create.Port) {
- return domainModel, buserr.WithDetail(constant.ErrPortInUsed, create.Port, nil)
- }
+ httpPort, _, err := getAppInstallPort(constant.AppOpenresty)
+ if err != nil {
+ return domainModel, err
}
+
website, err := websiteRepo.GetFirst(commonRepo.WithByID(create.WebsiteID))
if err != nil {
return domainModel, err
@@ -466,7 +470,7 @@ func (w WebsiteService) CreateWebsiteDomain(create request.WebsiteDomainCreate)
Port: create.Port,
WebsiteID: create.WebsiteID,
}
- if create.Port != 80 {
+ if create.Port != httpPort {
go func() {
_ = OperateFirewallPort(nil, []int{create.Port})
}()
@@ -652,7 +656,12 @@ func (w WebsiteService) OpWebsiteHTTPS(ctx context.Context, req request.WebsiteH
if !req.Enable {
website.Protocol = constant.ProtocolHTTP
website.WebsiteSSLID = 0
- if err := deleteListenAndServerName(website, []string{"443", "[::]:443"}, []string{}); err != nil {
+ _, httpsPort, err := getAppInstallPort(constant.AppOpenresty)
+ if err != nil {
+ return nil, err
+ }
+ httpsPortStr := strconv.Itoa(httpsPort)
+ if err := deleteListenAndServerName(website, []string{httpsPortStr, "[::]:" + httpsPortStr}, []string{}); err != nil {
return nil, err
}
nginxParams := getNginxParamsFromStaticFile(dto.SSL, nil)
@@ -1007,12 +1016,17 @@ func (w WebsiteService) ChangeDefaultServer(id uint) error {
if err != nil {
return err
}
+ httpPort, httpsPort, err := getAppInstallPort(constant.AppOpenresty)
+ 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 bind == strconv.Itoa(httpPort) || bind == strconv.Itoa(httpsPort) || bind == "[::]:"+strconv.Itoa(httpPort) || bind == "[::]:"+strconv.Itoa(httpsPort) {
if param.Params[paramLen-1] == components.DefaultServer {
newParam = param.Params
} else {
diff --git a/backend/app/service/website_ssl.go b/backend/app/service/website_ssl.go
index 0260896f3..947a7a221 100644
--- a/backend/app/service/website_ssl.go
+++ b/backend/app/service/website_ssl.go
@@ -115,7 +115,10 @@ func (w WebsiteSSLService) Create(create request.WebsiteSSLCreate) (request.Webs
if err != nil {
return request.WebsiteSSLCreate{}, err
}
- if err := client.UseHTTP(path.Join(constant.AppInstallDir, constant.AppOpenresty, appInstall.Name, "root")); err != nil {
+ if appInstall.HttpPort != 80 {
+ return request.WebsiteSSLCreate{}, buserr.WithDetail("ErrOpenrestyPort", appInstall.HttpPort, nil)
+ }
+ if err := client.UseHTTP(path.Join(appInstall.GetPath(), "root")); err != nil {
return res, err
}
websiteSSL.AutoRenew = create.AutoRenew
diff --git a/backend/app/service/website_utils.go b/backend/app/service/website_utils.go
index f68001492..2c1520b8e 100644
--- a/backend/app/service/website_utils.go
+++ b/backend/app/service/website_utils.go
@@ -23,12 +23,12 @@ import (
"gorm.io/gorm"
)
-func getDomain(domainStr string) (model.WebsiteDomain, error) {
+func getDomain(domainStr string, defaultPort int) (model.WebsiteDomain, error) {
domain := model.WebsiteDomain{}
domainArray := strings.Split(domainStr, ":")
if len(domainArray) == 1 {
domain.Domain = domainArray[0]
- domain.Port = 80
+ domain.Port = defaultPort
return domain, nil
}
if len(domainArray) > 1 {
@@ -186,6 +186,7 @@ func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain, a
return errors.New("nginx config is not valid")
}
server := servers[0]
+ server.DeleteListen("80")
var serverNames []string
for _, domain := range domains {
serverNames = append(serverNames, domain.Domain)
@@ -378,27 +379,33 @@ func applySSL(website model.Website, websiteSSL model.WebsiteSSL, req request.We
}
config := nginxFull.SiteConfig.Config
server := config.FindServers()[0]
- server.UpdateListen("443", website.DefaultServer, "ssl", "http2")
+
+ httpPort := strconv.Itoa(nginxFull.Install.HttpPort)
+ httpsPort := strconv.Itoa(nginxFull.Install.HttpsPort)
+ httpPortIPV6 := "[::]:" + httpPort
+ httpsPortIPV6 := "[::]:" + httpsPort
+
+ server.UpdateListen(httpsPort, website.DefaultServer, "ssl", "http2")
if website.IPV6 {
- server.UpdateListen("[::]:443", website.DefaultServer, "ssl", "http2")
+ server.UpdateListen(httpsPortIPV6, website.DefaultServer, "ssl", "http2")
}
switch req.HttpConfig {
case constant.HTTPSOnly:
- server.RemoveListenByBind("80")
- server.RemoveListenByBind("[::]:80")
+ server.RemoveListenByBind(httpPort)
+ server.RemoveListenByBind(httpPortIPV6)
server.RemoveDirective("if", []string{"($scheme"})
case constant.HTTPToHTTPS:
- server.UpdateListen("80", website.DefaultServer)
+ server.UpdateListen(httpPort, website.DefaultServer)
if website.IPV6 {
- server.UpdateListen("[::]:80", website.DefaultServer)
+ server.UpdateListen(httpPortIPV6, website.DefaultServer)
}
server.AddHTTP2HTTPS()
case constant.HTTPAlso:
- server.UpdateListen("80", website.DefaultServer)
+ server.UpdateListen(httpPort, website.DefaultServer)
server.RemoveDirective("if", []string{"($scheme"})
if website.IPV6 {
- server.UpdateListen("[::]:80", website.DefaultServer)
+ server.UpdateListen(httpPortIPV6, website.DefaultServer)
}
}
diff --git a/backend/i18n/lang/en.yaml b/backend/i18n/lang/en.yaml
index 9f7734c64..c52ae82a3 100644
--- a/backend/i18n/lang/en.yaml
+++ b/backend/i18n/lang/en.yaml
@@ -75,6 +75,7 @@ ErrSSLKeyNotFound: 'The private key file does not exist'
ErrSSLCertificateNotFound: 'The certificate file does not exist'
ErrSSLKeyFormat: 'Private key file verification error'
ErrSSLCertificateFormat: 'Certificate file format error, please use pem format'
+ErrOpenrestyPort: 'The default port of openresty is {{ .detail }}, HTTP mode cannot be used!'
#mysql
ErrUserIsExist: "The current user already exists. Please enter a new user"
diff --git a/backend/i18n/lang/zh-Hant.yaml b/backend/i18n/lang/zh-Hant.yaml
index 76741de93..b1e851eb3 100644
--- a/backend/i18n/lang/zh-Hant.yaml
+++ b/backend/i18n/lang/zh-Hant.yaml
@@ -75,6 +75,7 @@ ErrSSLKeyNotFound: '私鑰文件不存在'
ErrSSLCertificateNotFound: '證書文件不存在'
ErrSSLKeyFormat: '私鑰文件校驗錯誤'
ErrSSLCertificateFormat: '證書文件格式錯誤,請使用 pem 格式'
+ErrOpenrestyPort: 'openresty 默認端口為 {{ .detail }},無法使用 HTTP 模式!'
#mysql
ErrUserIsExist: "當前用戶已存在,請重新輸入"
diff --git a/backend/i18n/lang/zh.yaml b/backend/i18n/lang/zh.yaml
index 81b43f24a..741e15f74 100644
--- a/backend/i18n/lang/zh.yaml
+++ b/backend/i18n/lang/zh.yaml
@@ -75,6 +75,7 @@ ErrSSLKeyNotFound: '私钥文件不存在'
ErrSSLCertificateNotFound: '证书文件不存在'
ErrSSLKeyFormat: '私钥文件校验失败'
ErrSSLCertificateFormat: '证书文件格式错误,请使用 pem 格式'
+ErrOpenrestyPort: 'openresty 默认端口为 {{ .detail }},无法使用 HTTP 模式!'
#mysql
ErrUserIsExist: "当前用户已存在,请重新输入"
diff --git a/backend/utils/cmd/cmd.go b/backend/utils/cmd/cmd.go
index a3498b506..4c8214994 100644
--- a/backend/utils/cmd/cmd.go
+++ b/backend/utils/cmd/cmd.go
@@ -144,7 +144,7 @@ func ExecWithCheck(name string, a ...string) (string, error) {
func ExecScript(scriptPath, workDir string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
- cmd := exec.Command("/bin/sh", scriptPath)
+ cmd := exec.Command("bash", scriptPath)
cmd.Dir = workDir
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
diff --git a/frontend/src/api/interface/app.ts b/frontend/src/api/interface/app.ts
index f067a754e..426928803 100644
--- a/frontend/src/api/interface/app.ts
+++ b/frontend/src/api/interface/app.ts
@@ -133,6 +133,8 @@ export namespace App {
appInstallId: number;
containerName: string;
installPath: string;
+ httpPort: number;
+ httpsPort: number;
}
export interface DatabaseConnInfo {
diff --git a/frontend/src/components/app-status/index.vue b/frontend/src/components/app-status/index.vue
index 37ec08251..04c994d4d 100644
--- a/frontend/src/components/app-status/index.vue
+++ b/frontend/src/components/app-status/index.vue
@@ -41,6 +41,12 @@
{{ $t('commons.button.set') }}
+
+
+
+ {{ $t('website.openrestryHelper', [httpPort]) }}
+
+
@@ -95,6 +101,7 @@ let operateReq = reactive({
operate: '',
});
let refresh = ref(1);
+const httpPort = ref(0);
const em = defineEmits(['setting', 'isExist', 'before', 'update:loading', 'update:maskShow']);
const setting = () => {
@@ -111,6 +118,7 @@ const onCheck = async () => {
data.value = res.data;
em('isExist', res.data);
operateReq.installId = res.data.appInstallId;
+ httpPort.value = res.data.httpPort;
refresh.value++;
})
.catch(() => {
@@ -166,3 +174,12 @@ onMounted(() => {
onCheck();
});
+
+
diff --git a/frontend/src/lang/modules/en.ts b/frontend/src/lang/modules/en.ts
index 13bd5336c..04528c761 100644
--- a/frontend/src/lang/modules/en.ts
+++ b/frontend/src/lang/modules/en.ts
@@ -1496,6 +1496,7 @@ const message = {
changeVersion: 'Switch version',
retainConfig: 'Whether to keep php-fpm.conf and php.ini files',
runDirHelper2: 'Please ensure that the secondary running directory is under the index directory',
+ openrestryHelper: 'The default port of OpenResty is {0}, which affects website domain name access',
},
php: {
short_open_tag: 'Short tag support',
diff --git a/frontend/src/lang/modules/tw.ts b/frontend/src/lang/modules/tw.ts
index 7b413b985..296ecf23c 100644
--- a/frontend/src/lang/modules/tw.ts
+++ b/frontend/src/lang/modules/tw.ts
@@ -1426,6 +1426,7 @@ const message = {
changeVersion: '切換版本',
retainConfig: '是否保留 php-fpm.conf 和 php.ini 文件',
runDirHelper2: '請確保二級運行目錄位於 index 目錄下',
+ openrestryHelper: 'OpenResty默認端口為 {0},影響網站域名訪問',
},
php: {
short_open_tag: '短標簽支持',
diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts
index e79e241cb..25209e9b5 100644
--- a/frontend/src/lang/modules/zh.ts
+++ b/frontend/src/lang/modules/zh.ts
@@ -1426,6 +1426,7 @@ const message = {
changeVersion: '切换版本',
retainConfig: '是否保留 php-fpm.conf 和 php.ini 文件',
runDirHelper2: '请确保二级运行目录位于 index 目录下',
+ openrestryHelper: 'OpenResty默认端口为 {0},影响网站域名访问',
},
php: {
short_open_tag: '短标签支持',