fix: 代理表单字段保存时自动去除前后空格
前后端同时处理,防止因意外空格导致代理连接失败
This commit is contained in:
@@ -2,6 +2,7 @@ package admin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"sub2api/internal/pkg/response"
|
"sub2api/internal/pkg/response"
|
||||||
"sub2api/internal/service"
|
"sub2api/internal/service"
|
||||||
@@ -112,12 +113,12 @@ func (h *ProxyHandler) Create(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
proxy, err := h.adminService.CreateProxy(c.Request.Context(), &service.CreateProxyInput{
|
proxy, err := h.adminService.CreateProxy(c.Request.Context(), &service.CreateProxyInput{
|
||||||
Name: req.Name,
|
Name: strings.TrimSpace(req.Name),
|
||||||
Protocol: req.Protocol,
|
Protocol: strings.TrimSpace(req.Protocol),
|
||||||
Host: req.Host,
|
Host: strings.TrimSpace(req.Host),
|
||||||
Port: req.Port,
|
Port: req.Port,
|
||||||
Username: req.Username,
|
Username: strings.TrimSpace(req.Username),
|
||||||
Password: req.Password,
|
Password: strings.TrimSpace(req.Password),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.BadRequest(c, "Failed to create proxy: "+err.Error())
|
response.BadRequest(c, "Failed to create proxy: "+err.Error())
|
||||||
@@ -143,13 +144,13 @@ func (h *ProxyHandler) Update(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
proxy, err := h.adminService.UpdateProxy(c.Request.Context(), proxyID, &service.UpdateProxyInput{
|
proxy, err := h.adminService.UpdateProxy(c.Request.Context(), proxyID, &service.UpdateProxyInput{
|
||||||
Name: req.Name,
|
Name: strings.TrimSpace(req.Name),
|
||||||
Protocol: req.Protocol,
|
Protocol: strings.TrimSpace(req.Protocol),
|
||||||
Host: req.Host,
|
Host: strings.TrimSpace(req.Host),
|
||||||
Port: req.Port,
|
Port: req.Port,
|
||||||
Username: req.Username,
|
Username: strings.TrimSpace(req.Username),
|
||||||
Password: req.Password,
|
Password: strings.TrimSpace(req.Password),
|
||||||
Status: req.Status,
|
Status: strings.TrimSpace(req.Status),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.InternalError(c, "Failed to update proxy: "+err.Error())
|
response.InternalError(c, "Failed to update proxy: "+err.Error())
|
||||||
@@ -263,8 +264,14 @@ func (h *ProxyHandler) BatchCreate(c *gin.Context) {
|
|||||||
skipped := 0
|
skipped := 0
|
||||||
|
|
||||||
for _, item := range req.Proxies {
|
for _, item := range req.Proxies {
|
||||||
|
// Trim all string fields
|
||||||
|
host := strings.TrimSpace(item.Host)
|
||||||
|
protocol := strings.TrimSpace(item.Protocol)
|
||||||
|
username := strings.TrimSpace(item.Username)
|
||||||
|
password := strings.TrimSpace(item.Password)
|
||||||
|
|
||||||
// Check for duplicates (same host, port, username, password)
|
// Check for duplicates (same host, port, username, password)
|
||||||
exists, err := h.adminService.CheckProxyExists(c.Request.Context(), item.Host, item.Port, item.Username, item.Password)
|
exists, err := h.adminService.CheckProxyExists(c.Request.Context(), host, item.Port, username, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.InternalError(c, "Failed to check proxy existence: "+err.Error())
|
response.InternalError(c, "Failed to check proxy existence: "+err.Error())
|
||||||
return
|
return
|
||||||
@@ -278,11 +285,11 @@ func (h *ProxyHandler) BatchCreate(c *gin.Context) {
|
|||||||
// Create proxy with default name
|
// Create proxy with default name
|
||||||
_, err = h.adminService.CreateProxy(c.Request.Context(), &service.CreateProxyInput{
|
_, err = h.adminService.CreateProxy(c.Request.Context(), &service.CreateProxyInput{
|
||||||
Name: "default",
|
Name: "default",
|
||||||
Protocol: item.Protocol,
|
Protocol: protocol,
|
||||||
Host: item.Host,
|
Host: host,
|
||||||
Port: item.Port,
|
Port: item.Port,
|
||||||
Username: item.Username,
|
Username: username,
|
||||||
Password: item.Password,
|
Password: password,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If creation fails due to duplicate, count as skipped
|
// If creation fails due to duplicate, count as skipped
|
||||||
|
|||||||
@@ -647,10 +647,10 @@ const parseProxyUrl = (line: string): {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
protocol: protocol.toLowerCase() as ProxyProtocol,
|
protocol: protocol.toLowerCase() as ProxyProtocol,
|
||||||
host,
|
host: host.trim(),
|
||||||
port: portNum,
|
port: portNum,
|
||||||
username: username || '',
|
username: username?.trim() || '',
|
||||||
password: password || ''
|
password: password?.trim() || ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -714,9 +714,12 @@ const handleCreateProxy = async () => {
|
|||||||
submitting.value = true
|
submitting.value = true
|
||||||
try {
|
try {
|
||||||
await adminAPI.proxies.create({
|
await adminAPI.proxies.create({
|
||||||
...createForm,
|
name: createForm.name.trim(),
|
||||||
username: createForm.username || null,
|
protocol: createForm.protocol,
|
||||||
password: createForm.password || null
|
host: createForm.host.trim(),
|
||||||
|
port: createForm.port,
|
||||||
|
username: createForm.username.trim() || null,
|
||||||
|
password: createForm.password.trim() || null
|
||||||
})
|
})
|
||||||
appStore.showSuccess(t('admin.proxies.proxyCreated'))
|
appStore.showSuccess(t('admin.proxies.proxyCreated'))
|
||||||
closeCreateModal()
|
closeCreateModal()
|
||||||
@@ -752,17 +755,18 @@ const handleUpdateProxy = async () => {
|
|||||||
submitting.value = true
|
submitting.value = true
|
||||||
try {
|
try {
|
||||||
const updateData: any = {
|
const updateData: any = {
|
||||||
name: editForm.name,
|
name: editForm.name.trim(),
|
||||||
protocol: editForm.protocol,
|
protocol: editForm.protocol,
|
||||||
host: editForm.host,
|
host: editForm.host.trim(),
|
||||||
port: editForm.port,
|
port: editForm.port,
|
||||||
username: editForm.username || null,
|
username: editForm.username.trim() || null,
|
||||||
status: editForm.status
|
status: editForm.status
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only include password if it was changed
|
// Only include password if it was changed
|
||||||
if (editForm.password) {
|
const trimmedPassword = editForm.password.trim()
|
||||||
updateData.password = editForm.password
|
if (trimmedPassword) {
|
||||||
|
updateData.password = trimmedPassword
|
||||||
}
|
}
|
||||||
|
|
||||||
await adminAPI.proxies.update(editingProxy.value.id, updateData)
|
await adminAPI.proxies.update(editingProxy.value.id, updateData)
|
||||||
|
|||||||
Reference in New Issue
Block a user