mirror of
https://github.com/MengMengCode/CLICD.git
synced 2026-12-06 21:26:01 +08:00
FIX #72
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package lxc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
@@ -239,6 +240,47 @@ func TestUpdateSSHPortMappingKeepsIdentityAndSynchronizesSSHPort(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizePortMappingRejectsNonTransportProtocols(t *testing.T) {
|
||||
c := &config.Container{}
|
||||
for _, protocol := range []string{"all", "tcp+udp", "icmp"} {
|
||||
if _, err := normalizePortMapping(c, -1, config.PortMapping{
|
||||
HostPort: 30080, ContainerPort: 80, Protocol: protocol,
|
||||
}); err == nil || !strings.Contains(err.Error(), "protocol must be tcp or udp") {
|
||||
t.Fatalf("protocol %q was accepted: %v", protocol, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdatePortMappingRestoresStateWhenApplyFails(t *testing.T) {
|
||||
previousConfig := config.AppConfig
|
||||
t.Cleanup(func() { config.AppConfig = previousConfig })
|
||||
previousRun := runPortMappingCommand
|
||||
t.Cleanup(func() { runPortMappingCommand = previousRun })
|
||||
|
||||
config.AppConfig = &config.ClicdConfig{
|
||||
NATPortStart: 30000,
|
||||
NATPortEnd: 65535,
|
||||
Containers: []config.Container{{
|
||||
ID: 12, Name: "ct-test", Status: "running", IP: "192.0.2.12", SSHPort: 30022,
|
||||
PortMappings: []config.PortMapping{{HostPort: 30022, ContainerPort: 22, Protocol: "tcp", Description: "SSH"}},
|
||||
}},
|
||||
}
|
||||
runPortMappingCommand = func(string, ...string) ([]byte, error) {
|
||||
return nil, errors.New("iptables unavailable")
|
||||
}
|
||||
|
||||
_, err := NewManager().UpdatePortMapping(12, 0, config.PortMapping{
|
||||
HostPort: 31022, ContainerPort: 22, Protocol: "tcp", Description: "renamed",
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected apply failure")
|
||||
}
|
||||
container := config.FindContainer(12)
|
||||
if container == nil || len(container.PortMappings) != 1 || container.PortMappings[0].HostPort != 30022 || container.SSHPort != 30022 {
|
||||
t.Fatalf("failed update changed persisted state: %+v", container)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReserveCreateNATPortsProtectsConcurrentTasks(t *testing.T) {
|
||||
previous := config.AppConfig
|
||||
t.Cleanup(func() { config.AppConfig = previous })
|
||||
|
||||
@@ -423,7 +423,7 @@ func (m *Manager) AddPortMapping(id int, pm config.PortMapping) ([]config.PortMa
|
||||
return nil, err
|
||||
}
|
||||
c.PortMappings = candidate.PortMappings
|
||||
if err := persistAndReloadMappings(m, c); err != nil {
|
||||
if err := persistAndReloadMappings(m, c, &previous); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
clearChangedIPv4Mappings(&previous, c)
|
||||
@@ -458,7 +458,7 @@ func (m *Manager) UpdatePortMapping(id int, index int, pm config.PortMapping) ([
|
||||
return nil, err
|
||||
}
|
||||
c.PortMappings = candidate.PortMappings
|
||||
if err := persistAndReloadMappings(m, c); err != nil {
|
||||
if err := persistAndReloadMappings(m, c, &previous); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
clearChangedIPv4Mappings(&previous, c)
|
||||
@@ -483,7 +483,7 @@ func (m *Manager) DeletePortMapping(id int, index int) ([]config.PortMapping, er
|
||||
}
|
||||
previous := *c
|
||||
c.PortMappings = append(append([]config.PortMapping(nil), c.PortMappings[:index]...), c.PortMappings[index+1:]...)
|
||||
if err := persistAndReloadMappings(m, c); err != nil {
|
||||
if err := persistAndReloadMappings(m, c, &previous); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
clearChangedIPv4Mappings(&previous, c)
|
||||
@@ -491,15 +491,35 @@ func (m *Manager) DeletePortMapping(id int, index int) ([]config.PortMapping, er
|
||||
return c.PortMappings, nil
|
||||
}
|
||||
|
||||
func persistAndReloadMappings(m *Manager, c *config.Container) error {
|
||||
syncContainerSSHPort(c)
|
||||
config.SaveConfig()
|
||||
if c.Status == "running" && (c.IP != "" || c.IPv6NATAddress != "") {
|
||||
return m.ApplyPortMappings(c.ID)
|
||||
func persistAndReloadMappings(m *Manager, c, previous *config.Container) error {
|
||||
if c == nil || previous == nil {
|
||||
return fmt.Errorf("container mapping state is required")
|
||||
}
|
||||
syncContainerSSHPort(c)
|
||||
running := c.Status == "running" && (c.IP != "" || c.IPv6NATAddress != "")
|
||||
if running {
|
||||
if err := m.ApplyPortMappings(c.ID); err != nil {
|
||||
restorePortMappingState(c, previous)
|
||||
if rollbackErr := m.ApplyPortMappings(c.ID); rollbackErr != nil {
|
||||
return errors.Join(err, fmt.Errorf("restore previous port mappings: %w", rollbackErr))
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
// Preserve the historical best-effort save behavior. A failed kernel apply is
|
||||
// handled above before this point, so invalid mappings cannot be persisted.
|
||||
_ = config.SaveConfig()
|
||||
return nil
|
||||
}
|
||||
|
||||
func restorePortMappingState(c, previous *config.Container) {
|
||||
if c == nil || previous == nil {
|
||||
return
|
||||
}
|
||||
c.PortMappings = append([]config.PortMapping(nil), previous.PortMappings...)
|
||||
c.SSHPort = previous.SSHPort
|
||||
}
|
||||
|
||||
func syncContainerSSHPort(c *config.Container) {
|
||||
if c == nil {
|
||||
return
|
||||
@@ -578,6 +598,9 @@ func normalizePortMapping(c *config.Container, skipIndex int, pm config.PortMapp
|
||||
pm.Protocol = "tcp"
|
||||
}
|
||||
pm.Protocol = strings.ToLower(strings.TrimSpace(pm.Protocol))
|
||||
if pm.Protocol != "tcp" && pm.Protocol != "udp" {
|
||||
return pm, fmt.Errorf("protocol must be tcp or udp")
|
||||
}
|
||||
pm.HostIP = strings.TrimSpace(pm.HostIP)
|
||||
if pm.HostIP != "" {
|
||||
addr, err := netip.ParseAddr(pm.HostIP)
|
||||
|
||||
@@ -737,7 +737,7 @@ export default function ContainerDetail() {
|
||||
host_port: String(pm.host_port),
|
||||
host_ip: pm.host_ip || '',
|
||||
container_port: String(pm.container_port),
|
||||
protocol: pm.protocol || 'all',
|
||||
protocol: pm.protocol || 'tcp',
|
||||
})
|
||||
setShowNat(true)
|
||||
setShowMappingEditor(true)
|
||||
@@ -770,7 +770,7 @@ export default function ContainerDetail() {
|
||||
if (isSubUser && draft.index !== null && container?.port_mappings?.[draft.index]) {
|
||||
const orig = container.port_mappings[draft.index]
|
||||
hostPortVal = orig.host_port
|
||||
protocolVal = orig.protocol || 'all'
|
||||
protocolVal = orig.protocol || 'tcp'
|
||||
}
|
||||
|
||||
const payload: PortMapping = {
|
||||
@@ -2537,11 +2537,8 @@ function MappingEditor({
|
||||
<Input value={draft.protocol.toUpperCase()} disabled className={`${disabledInputClass} h-auto`} />
|
||||
) : (
|
||||
<NativeSelect wrapperClassName="w-full" value={draft.protocol} onChange={(e) => updateDraft({ protocol: e.target.value })} className={`${inputClass} h-auto pr-9`}>
|
||||
<option value="all">全部 (ALL)</option>
|
||||
<option value="tcp">TCP</option>
|
||||
<option value="udp">UDP</option>
|
||||
<option value="tcp+udp">TCP+UDP</option>
|
||||
<option value="icmp">ICMP</option>
|
||||
</NativeSelect>
|
||||
)}
|
||||
</Field>
|
||||
|
||||
Reference in New Issue
Block a user