Skip to content

Commit

Permalink
fix(website):Fix Reverse Proxy Deletion Failure in Certain Scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengkunwang223 committed Jan 13, 2025
1 parent 4dee4e1 commit 3a5b922
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 4 deletions.
22 changes: 22 additions & 0 deletions backend/app/api/v1/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,28 @@ func (b *BaseApi) GetProxyConfig(c *gin.Context) {
helper.SuccessWithData(c, res)
}

// @Tags Website
// @Summary Delete proxy conf
// @Accept json
// @Param request body request.WebsiteProxyDel true "request"
// @Success 200
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /websites/proxies/del [post]
// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"websites","output_column":"primary_domain","output_value":"domain"}],"formatZH":"删除网站 [domain] 反向代理配置","formatEN":"Delete domain [domain] proxy config"}
func (b *BaseApi) DeleteProxyConfig(c *gin.Context) {
var req request.WebsiteProxyDel
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}
err := websiteService.DeleteProxy(req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithOutData(c)
}

// @Tags Website
// @Summary Update proxy conf
// @Accept json
Expand Down
5 changes: 5 additions & 0 deletions backend/app/dto/request/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ type WebsiteProxyConfig struct {
ProxySSLName string `json:"proxySSLName"`
}

type WebsiteProxyDel struct {
ID uint `json:"id" validate:"required"`
Name string `json:"name" validate:"required"`
}

type WebsiteProxyReq struct {
ID uint `json:"id" validate:"required"`
}
Expand Down
26 changes: 26 additions & 0 deletions backend/app/service/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,12 @@ type IWebsiteService interface {
LoadWebsiteDirConfig(req request.WebsiteCommonReq) (*response.WebsiteDirConfig, error)
UpdateSiteDir(req request.WebsiteUpdateDir) error
UpdateSitePermission(req request.WebsiteUpdateDirPermission) error

OperateProxy(req request.WebsiteProxyConfig) (err error)
GetProxies(id uint) (res []request.WebsiteProxyConfig, err error)
UpdateProxyFile(req request.NginxProxyUpdate) (err error)
DeleteProxy(req request.WebsiteProxyDel) (err error)

GetAuthBasics(req request.NginxAuthReq) (res response.NginxAuthRes, err error)
UpdateAuthBasic(req request.NginxAuthUpdate) (err error)
GetAntiLeech(id uint) (*response.NginxAntiLeechRes, error)
Expand Down Expand Up @@ -1553,6 +1556,29 @@ func (w WebsiteService) UpdateSitePermission(req request.WebsiteUpdateDirPermiss
return websiteRepo.Save(context.Background(), &website)
}

func (w WebsiteService) DeleteProxy(req request.WebsiteProxyDel) (err error) {
fileOp := files.NewFileOp()
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
if err != nil {
return
}
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
if 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)
backName := fmt.Sprintf("%s.bak", req.Name)
backPath := path.Join(includeDir, backName)
_ = fileOp.DeleteFile(includePath)
_ = fileOp.DeleteFile(backPath)
return updateNginxConfig(constant.NginxScopeServer, nil, &website)
}

func (w WebsiteService) OperateProxy(req request.WebsiteProxyConfig) (err error) {
var (
website model.Website
Expand Down
1 change: 1 addition & 0 deletions backend/router/ro_website.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (a *WebsiteRouter) InitRouter(Router *gin.RouterGroup) {
websiteRouter.POST("/proxies", baseApi.GetProxyConfig)
websiteRouter.POST("/proxies/update", baseApi.UpdateProxyConfig)
websiteRouter.POST("/proxies/file", baseApi.UpdateProxyConfigFile)
websiteRouter.POST("/proxies/del", baseApi.DeleteProxyConfig)

websiteRouter.POST("/auths", baseApi.GetAuthConfig)
websiteRouter.POST("/auths/update", baseApi.UpdateAuthConfig)
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/api/interface/website.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ export namespace Website {
id: number;
}

export interface ProxyDel {
id: number;
name: string;
}

export interface ProxyConfig {
id: number;
operate: string;
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/api/modules/website.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ export const UpdateProxyConfigFile = (req: Website.ProxyFileUpdate) => {
return http.post<any>(`/websites/proxies/file`, req);
};

export const DelProxy = (req: Website.ProxyDel) => {
return http.post<any>(`/websites/proxies/del`, req);
};

export const GetAuthConfig = (req: Website.AuthReq) => {
return http.post<Website.AuthConfig>(`/websites/auths`, req);
};
Expand Down
13 changes: 9 additions & 4 deletions frontend/src/views/website/website/config/basic/proxy/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

<script lang="ts" setup name="proxy">
import { Website } from '@/api/interface/website';
import { OperateProxyConfig, GetProxyConfig } from '@/api/modules/website';
import { OperateProxyConfig, GetProxyConfig, DelProxy } from '@/api/modules/website';
import { computed, onMounted, ref } from 'vue';
import Create from './create/index.vue';
import File from './file/index.vue';
Expand Down Expand Up @@ -108,6 +108,8 @@ const initData = (id: number): Website.ProxyConfig => ({
proxyPass: 'http://',
proxyHost: '$host',
replaces: {},
sni: false,
proxySSLName: '',
});
const openCreate = () => {
Expand All @@ -128,16 +130,19 @@ const openEditFile = (proxyConfig: Website.ProxyConfig) => {
};
const deleteProxy = async (proxyConfig: Website.ProxyConfig) => {
proxyConfig.operate = 'delete';
const del = {
id: proxyConfig.id,
name: proxyConfig.name,
};
opRef.value.acceptParams({
title: i18n.global.t('commons.msg.deleteTitle'),
names: [proxyConfig.name],
msg: i18n.global.t('commons.msg.operatorHelper', [
i18n.global.t('website.proxy'),
i18n.global.t('commons.button.delete'),
]),
api: OperateProxyConfig,
params: proxyConfig,
api: DelProxy,
params: del,
});
};
Expand Down

0 comments on commit 3a5b922

Please sign in to comment.