refactor(config): unify runtime configuration management via ConfigRpc (#1397)

* refactor(config): unify runtime configuration management via ConfigRpc
* feat(tests): add config patch test and fix problem
This commit is contained in:
Mg Pig
2025-10-01 00:32:28 +08:00
committed by GitHub
parent 4d91582fd8
commit 020bf04ec4
16 changed files with 772 additions and 280 deletions
-35
View File
@@ -181,21 +181,8 @@ message ListMappedListenerRequest {}
message ListMappedListenerResponse { repeated MappedListener mappedlisteners = 1; }
enum MappedListenerManageAction {
MAPPED_LISTENER_ADD = 0;
MAPPED_LISTENER_REMOVE = 1;
}
message ManageMappedListenerRequest {
MappedListenerManageAction action = 1;
common.Url url = 2;
}
message ManageMappedListenerResponse {}
service MappedListenerManageRpc {
rpc ListMappedListener(ListMappedListenerRequest) returns (ListMappedListenerResponse);
rpc ManageMappedListener(ManageMappedListenerRequest) returns (ManageMappedListenerResponse);
}
message VpnPortalInfo {
@@ -261,17 +248,9 @@ message GetAclStatsResponse {
service AclManageRpc {
rpc GetAclStats(GetAclStatsRequest) returns (GetAclStatsResponse);
rpc SetWhitelist(SetWhitelistRequest) returns (SetWhitelistResponse);
rpc GetWhitelist(GetWhitelistRequest) returns (GetWhitelistResponse);
}
message SetWhitelistRequest {
repeated string tcp_ports = 1;
repeated string udp_ports = 2;
}
message SetWhitelistResponse {}
message GetWhitelistRequest {}
message GetWhitelistResponse {
@@ -279,18 +258,6 @@ message GetWhitelistResponse {
repeated string udp_ports = 2;
}
message AddPortForwardRequest {
common.PortForwardConfigPb cfg = 1;
}
message AddPortForwardResponse {}
message RemovePortForwardRequest {
common.PortForwardConfigPb cfg = 1;
}
message RemovePortForwardResponse {}
message ListPortForwardRequest {}
message ListPortForwardResponse {
@@ -298,8 +265,6 @@ message ListPortForwardResponse {
}
service PortForwardManageRpc {
rpc AddPortForward(AddPortForwardRequest) returns (AddPortForwardResponse);
rpc RemovePortForward(RemovePortForwardRequest) returns (RemovePortForwardResponse);
rpc ListPortForward(ListPortForwardRequest) returns (ListPortForwardResponse);
}
+10 -3
View File
@@ -49,10 +49,10 @@ message FlagsInConfig {
// enable relay foreign network kcp packets
bool enable_relay_foreign_network_kcp = 28;
// encryption algorithm to use, empty string means default (aes-gcm)
// encryption algorithm to use, empty string means default (aes-gcm)
string encryption_algorithm = 29;
// disable symmetric nat hole punching, treat symmetric as cone when enabled
bool disable_sym_hole_punching = 30;
}
@@ -144,6 +144,13 @@ message Ipv6Addr {
uint32 part4 = 4;
}
message IpAddr {
oneof ip {
Ipv4Addr ipv4 = 1;
Ipv6Addr ipv6 = 2;
};
}
message Ipv4Inet {
Ipv4Addr address = 1;
uint32 network_length = 2;
+47
View File
@@ -108,6 +108,43 @@ impl From<cidr::Ipv4Inet> for Ipv4Inet {
}
}
impl From<std::net::IpAddr> for IpAddr {
fn from(value: std::net::IpAddr) -> Self {
match value {
std::net::IpAddr::V4(v4) => IpAddr {
ip: Some(ip_addr::Ip::Ipv4(Ipv4Addr::from(v4))),
},
std::net::IpAddr::V6(v6) => IpAddr {
ip: Some(ip_addr::Ip::Ipv6(Ipv6Addr::from(v6))),
},
}
}
}
impl From<IpAddr> for std::net::IpAddr {
fn from(value: IpAddr) -> Self {
match value.ip {
Some(ip_addr::Ip::Ipv4(v4)) => std::net::IpAddr::V4(v4.into()),
Some(ip_addr::Ip::Ipv6(v6)) => std::net::IpAddr::V6(v6.into()),
None => panic!("IpAddr is None"),
}
}
}
impl Display for IpAddr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", std::net::IpAddr::from(*self))
}
}
impl FromStr for IpAddr {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(IpAddr::from(std::net::IpAddr::from_str(s)?))
}
}
impl From<Ipv4Inet> for cidr::Ipv4Inet {
fn from(value: Ipv4Inet) -> Self {
cidr::Ipv4Inet::new(
@@ -118,6 +155,16 @@ impl From<Ipv4Inet> for cidr::Ipv4Inet {
}
}
impl From<Ipv4Inet> for cidr::Ipv4Cidr {
fn from(value: Ipv4Inet) -> Self {
cidr::Ipv4Cidr::new(
value.address.unwrap_or_default().into(),
value.network_length as u8,
)
.unwrap()
}
}
impl fmt::Display for Ipv4Inet {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", cidr::Ipv4Inet::from(*self))
+65
View File
@@ -0,0 +1,65 @@
syntax = "proto3";
import "common.proto";
import "acl.proto";
package config;
enum ConfigPatchAction {
ADD = 0;
REMOVE = 1;
CLEAR = 2;
}
message InstanceConfigPatch {
optional string hostname = 1;
optional common.Ipv4Inet ipv4 = 2;
optional common.Ipv6Inet ipv6 = 3;
repeated PortForwardPatch port_forwards = 4;
optional AclPatch acl = 5;
repeated ProxyNetworkPatch proxy_networks = 6;
repeated RoutePatch routes = 7;
repeated ExitNodePatch exit_nodes = 8;
repeated UrlPatch mapped_listeners = 9;
}
message PortForwardPatch {
ConfigPatchAction action = 1;
common.PortForwardConfigPb cfg = 2;
}
message StringPatch {
ConfigPatchAction action = 1;
string value = 2;
}
message UrlPatch {
ConfigPatchAction action = 1;
common.Url url = 2;
}
message AclPatch {
optional acl.Acl acl = 1;
repeated StringPatch tcp_whitelist = 2;
repeated StringPatch udp_whitelist = 3;
}
message ProxyNetworkPatch {
ConfigPatchAction action = 1;
common.Ipv4Inet cidr = 2;
optional common.Ipv4Inet mapped_cidr = 3;
}
message RoutePatch {
ConfigPatchAction action = 1;
common.Ipv4Inet cidr = 2;
}
message ExitNodePatch {
ConfigPatchAction action = 1;
common.IpAddr node = 2;
}
message PatchConfigRequest { InstanceConfigPatch patch = 1; }
service ConfigRpc { rpc PatchConfig(PatchConfigRequest) returns (common.Void); }
+75
View File
@@ -0,0 +1,75 @@
include!(concat!(env!("OUT_DIR"), "/config.rs"));
pub struct Patchable<T> {
pub action: Option<ConfigPatchAction>,
pub value: Option<T>,
}
impl From<PortForwardPatch> for Patchable<crate::common::config::PortForwardConfig> {
fn from(patch: PortForwardPatch) -> Self {
Patchable {
action: ConfigPatchAction::try_from(patch.action).ok(),
value: patch.cfg.map(Into::into),
}
}
}
impl From<RoutePatch> for Patchable<cidr::Ipv4Cidr> {
fn from(value: RoutePatch) -> Self {
Patchable {
action: ConfigPatchAction::try_from(value.action).ok(),
value: value.cidr.map(Into::into),
}
}
}
impl From<ExitNodePatch> for Patchable<std::net::IpAddr> {
fn from(value: ExitNodePatch) -> Self {
Patchable {
action: ConfigPatchAction::try_from(value.action).ok(),
value: value.node.map(Into::into),
}
}
}
impl From<StringPatch> for Patchable<String> {
fn from(value: StringPatch) -> Self {
Patchable {
action: ConfigPatchAction::try_from(value.action).ok(),
value: Some(value.value),
}
}
}
impl From<UrlPatch> for Patchable<url::Url> {
fn from(value: UrlPatch) -> Self {
Patchable {
action: ConfigPatchAction::try_from(value.action).ok(),
value: value.url.map(Into::into),
}
}
}
pub fn patch_vec<T>(v: &mut Vec<T>, patches: Vec<Patchable<T>>)
where
T: PartialEq,
{
for patch in patches {
match patch.action {
Some(ConfigPatchAction::Add) => {
if let Some(value) = patch.value {
v.push(value);
}
}
Some(ConfigPatchAction::Remove) => {
if let Some(value) = patch.value {
v.retain(|x| x != &value);
}
}
Some(ConfigPatchAction::Clear) => {
v.clear();
}
None => {}
}
}
}
+1
View File
@@ -4,6 +4,7 @@ pub mod rpc_types;
pub mod acl;
pub mod cli;
pub mod common;
pub mod config;
pub mod error;
pub mod magic_dns;
pub mod peer_rpc;