Browse Source

feat: 修改反向代理网站创建逻辑 (#793)

pull/795/head
zhengkunwang223 2 years ago committed by GitHub
parent
commit
8a32d8032f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      backend/app/service/website.go
  2. 46
      backend/app/service/website_utils.go
  3. 29
      frontend/src/lang/modules/en.ts
  4. 31
      frontend/src/lang/modules/zh.ts
  5. 19
      frontend/src/views/website/website/config/basic/proxy/create/index.vue

2
backend/app/service/website.go

@ -1351,7 +1351,7 @@ func (w WebsiteService) UpdateAuthBasic(req request.NginxAuthUpdate) (err error)
absoluteAuthPath := path.Join(nginxInstall.GetPath(), authPath)
fileOp := files.NewFileOp()
if !fileOp.Stat(path.Dir(absoluteAuthPath)) {
_ = fileOp.CreateDir(path.Dir(absoluteAuthPath), 755)
_ = fileOp.CreateDir(path.Dir(absoluteAuthPath), 0755)
}
if !fileOp.Stat(absoluteAuthPath) {
_ = fileOp.CreateFile(absoluteAuthPath)

46
backend/app/service/website_utils.go

@ -2,7 +2,9 @@ package service
import (
"fmt"
"github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/nginx/components"
"path"
"strconv"
"strings"
@ -84,6 +86,40 @@ func createIndexFile(website *model.Website, runtime *model.Runtime) error {
return nil
}
func createProxyFile(website *model.Website, runtime *model.Runtime) error {
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
if err != nil {
return err
}
proxyFolder := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "www", "sites", website.Alias, "proxy")
filePath := path.Join(proxyFolder, "root.conf")
fileOp := files.NewFileOp()
if !fileOp.Stat(proxyFolder) {
if err := fileOp.CreateDir(proxyFolder, 0755); err != nil {
return err
}
}
if !fileOp.Stat(filePath) {
if err := fileOp.CreateFile(filePath); err != nil {
return err
}
}
config := parser.NewStringParser(string(nginx_conf.Proxy)).Parse()
config.FilePath = filePath
directives := config.Directives
location, ok := directives[0].(*components.Location)
if !ok {
return errors.New("error")
}
location.ChangePath("^~", "/")
location.UpdateDirective("proxy_pass", []string{website.Proxy})
location.UpdateDirective("proxy_set_header", []string{"Host", "$host"})
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
return buserr.WithErr(constant.ErrUpdateBuWebsite, err)
}
return nil
}
func createWebsiteFolder(nginxInstall model.AppInstall, website *model.Website, runtime *model.Runtime) error {
nginxFolder := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name)
siteFolder := path.Join(nginxFolder, "www", "sites", website.Alias)
@ -123,6 +159,11 @@ func createWebsiteFolder(nginxInstall model.AppInstall, website *model.Website,
return err
}
}
if website.Type == constant.Proxy {
if err := createProxyFile(website, runtime); err != nil {
return err
}
}
}
return fileOp.CopyDir(path.Join(nginxFolder, "www", "common", "waf", "rules"), path.Join(siteFolder, "waf"))
}
@ -167,9 +208,9 @@ func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain, a
server.UpdateRootProxy([]string{proxy})
case constant.Static:
server.UpdateRoot(rootIndex)
//server.UpdateRootLocation()
case constant.Proxy:
server.UpdateRootProxy([]string{website.Proxy})
nginxInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias)
server.UpdateDirective("include", []string{nginxInclude})
case constant.Runtime:
if runtime.Resource == constant.ResourceLocal {
switch runtime.Type {
@ -192,7 +233,6 @@ func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain, a
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
return err
}
if err := opNginx(nginxInstall.ContainerName, constant.NginxCheck); err != nil {
_ = deleteWebsiteFolder(nginxInstall, website)
return err

29
frontend/src/lang/modules/en.ts

@ -1213,23 +1213,18 @@ const message = {
startProxy: 'Start Reverse proxy',
stopProxy: 'Stop the Reverse proxy',
proxyFile: 'Source',
proxyHelper1:
'Proxy directory: when accessing this directory, the content of the target URL will be returned and displayed',
proxyPassHelper:
'Target URL: You can fill in the site you need to proxy, the target URL must be a URL that can be accessed normally, otherwise an error will be returned',
proxyHostHelper:
'Send domain name: Add the domain name to the request header and pass it to the proxy server. The default is the target URL domain name. If it is not set properly, the proxy may not work properly',
replacementHelper:
'Content replacement: you can add up to 3 replacement content, if you do not need to replace, please leave blank',
modifier: 'Path Match',
modifierHelper:
'Path matching: Example: = exact match, ~ regular match, ^~ match the beginning of the path, etc',
replace: 'Content Replacement',
addReplace: 'Add content to replace',
replaced: 'The replaced text cannot be empty',
replaceText: 'Replacement text, can be empty',
replacedErr: 'The replaced text cannot be empty',
replacedErr2: 'The replaced text cannot be repeated',
proxyHelper1: 'When accessing this directory, the content of the target URL will be returned and displayed',
proxyPassHelper: 'The proxy site must be a URL that can be accessed normally',
proxyHostHelper: 'Add the domain name to the request header and pass it to the proxy server',
replacementHelper: 'Up to 5 replacements can be added, please leave blank if no replacement is required',
modifier: 'Matching rules',
modifierHelper: 'Example: = exact match, ~ regular match, ^~ match the beginning of the path, etc',
replace: 'Text Replacement',
addReplace: 'Add text replacement',
replaced: 'Search string (cannot be empty)',
replaceText: 'Replace with string',
replacedErr: 'The search string cannot be empty',
replacedErr2: 'The search string cannot be repeated',
basicAuth: 'Password Access',
editBasicAuthHelper:
'The password is asymmetrically encrypted and cannot be echoed. Editing needs to reset the password',

31
frontend/src/lang/modules/zh.ts

@ -1203,32 +1203,31 @@ const message = {
userGroup: '运行用户/',
user: '用户',
uGroup: '用户组',
proxyPath: '代理目录',
proxyPass: '目标URL',
proxyPath: '前端请求路径',
proxyPass: '后端代理地址',
cache: '缓存',
status: '状态',
createProxy: '创建反向代理',
editProxy: '编辑反向代理',
cacheTime: '缓存时间',
enableCache: '开启缓存',
proxyHost: '发送域名',
proxyHost: '后端域名',
disabled: '已停止',
startProxy: '开启反向代理',
stopProxy: '关闭反向代理',
proxyFile: '源文',
proxyHelper1: '代理目录访问这个目录时将会把目标URL的内容返回并显示',
proxyPassHelper: '目标URL可以填写你需要代理的站点目标URL必须为可正常访问的URL否则将返回错误',
proxyHostHelper:
'发送域名将域名添加到请求头传递到代理服务器默认为目标URL域名若设置不当可能导致代理无法正常运行',
replacementHelper: '内容替换最多可以添加3条替换内容,如果不需要替换请留空',
modifier: '路径匹配',
modifierHelper: '路径匹配= 精确匹配~ 正则匹配^~ 匹配路径开头 ',
replace: '内容替换',
addReplace: '添加内容替换',
replaced: '被替换的文本,不能为空',
replaceText: '替换的文本可为空',
replacedErr: '被替换的文本不能为空',
replacedErr2: '被替换的文本不能重复',
proxyHelper1: '访问这个目录时将会把目标URL的内容返回并显示',
proxyPassHelper: '代理的站点必须为可正常访问的URL',
proxyHostHelper: '将域名添加到请求头传递到代理服务器',
replacementHelper: '最多可以添加5条替换内容,如果不需要替换请留空',
modifier: '匹配规则',
modifierHelper: '= 精确匹配~ 正则匹配^~ 匹配路径开头 ',
replace: '文本替换',
addReplace: '添加文本替换',
replaced: '搜索字符串不能为空',
replaceText: '替换为字符串',
replacedErr: '搜索字符串不能为空',
replacedErr2: '搜索字符串不能重复',
basicAuth: '密码访问',
editBasicAuthHelper: '密码为非对称加密无法回显编辑需要重新设置密码',
createPassword: '生成密码',

19
frontend/src/views/website/website/config/basic/proxy/create/index.vue

@ -11,6 +11,9 @@
</el-form-item>
<el-form-item :label="$t('website.modifier')" prop="modifier">
<el-input v-model.trim="proxy.modifier"></el-input>
<div>
<span class="input-help">{{ $t('website.modifierHelper') }}</span>
</div>
</el-form-item>
<el-form-item :label="$t('website.proxyPath')" prop="match">
<el-input v-model.trim="proxy.match"></el-input>
@ -36,11 +39,17 @@
<el-col :span="12">
<el-form-item :label="$t('website.proxyPass')" prop="proxyPass">
<el-input v-model.trim="proxy.proxyPass"></el-input>
<div>
<span class="input-help">{{ $t('website.proxyPassHelper') }}</span>
</div>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item :label="$t('website.proxyHost')" prop="proxyHost">
<el-input v-model.trim="proxy.proxyHost"></el-input>
<div>
<span class="input-help">{{ $t('website.proxyHostHelper') }}</span>
</div>
</el-form-item>
</el-col>
</el-row>
@ -68,16 +77,14 @@
</div>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="addReplaces" :disabled="replaces.length >= 3">
<el-button type="primary" @click="addReplaces" :disabled="replaces.length >= 5">
{{ $t('website.addReplace') }}
</el-button>
<div>
<span class="input-help">{{ $t('website.replacementHelper') }}</span>
</div>
</el-form-item>
</el-form>
<el-alert :title="$t('website.modifierHelper')" type="info" :closable="false" />
<el-alert :title="$t('website.proxyHelper1')" type="info" :closable="false" />
<el-alert :title="$t('website.proxyPassHelper')" type="info" :closable="false" />
<el-alert :title="$t('website.proxyHostHelper')" type="info" :closable="false" />
<el-alert :title="$t('website.replacementHelper')" type="info" :closable="false" />
</el-col>
</el-row>
<template #footer>

Loading…
Cancel
Save