diff --git a/backend/internal/lxc/lxc_test.go b/backend/internal/lxc/lxc_test.go
index b2e2e72..79efba7 100644
--- a/backend/internal/lxc/lxc_test.go
+++ b/backend/internal/lxc/lxc_test.go
@@ -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 })
diff --git a/backend/internal/lxc/portmap.go b/backend/internal/lxc/portmap.go
index 7d4ae83..5f81c9a 100644
--- a/backend/internal/lxc/portmap.go
+++ b/backend/internal/lxc/portmap.go
@@ -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)
diff --git a/frontend/src/pages/ContainerDetail.tsx b/frontend/src/pages/ContainerDetail.tsx
index 2249617..8fce935 100644
--- a/frontend/src/pages/ContainerDetail.tsx
+++ b/frontend/src/pages/ContainerDetail.tsx
@@ -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({
) : (
updateDraft({ protocol: e.target.value })} className={`${inputClass} h-auto pr-9`}>
-
-
-
)}