Skip to content

Commit

Permalink
Use optional modifier for optional literals in protobuf messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Oct 12, 2023
1 parent 7f8b7e9 commit 7c67c11
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 155 deletions.
42 changes: 19 additions & 23 deletions mullvad-management-interface/proto/management_interface.proto
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ message ErrorState {

// LOCKED
uint32 lock_pid = 2;
string lock_name = 3;
optional string lock_name = 3;
}

Cause cause = 1;
Expand Down Expand Up @@ -240,17 +240,17 @@ message ProxyEndpoint {
}

message GeoIpLocation {
string ipv4 = 1;
string ipv6 = 2;
optional string ipv4 = 1;
optional string ipv6 = 2;
string country = 3;
string city = 4;
optional string city = 4;
double latitude = 5;
double longitude = 6;
bool mullvad_exit_ip = 7;
string hostname = 8;
string bridge_hostname = 9;
string entry_hostname = 10;
string obfuscator_hostname = 11;
optional string hostname = 8;
optional string bridge_hostname = 9;
optional string entry_hostname = 10;
optional string obfuscator_hostname = 11;
}

message TunnelMetadata { string tunnel_interface = 1; }
Expand Down Expand Up @@ -316,7 +316,7 @@ message BridgeState {
State state = 1;
}

message Udp2TcpObfuscationSettings { uint32 port = 1; }
message Udp2TcpObfuscationSettings { optional uint32 port = 1; }

message ObfuscationSettings {
enum SelectedObfuscation {
Expand Down Expand Up @@ -410,12 +410,10 @@ message RelaySettings {
}
}

message TunnelTypeConstraint { TunnelType tunnel_type = 1; }

message NormalRelaySettings {
LocationConstraint location = 1;
repeated string providers = 2;
TunnelTypeConstraint tunnel_type = 3;
optional TunnelType tunnel_type = 3;
WireguardConstraints wireguard_constraints = 4;
OpenvpnConstraints openvpn_constraints = 5;
Ownership ownership = 6;
Expand All @@ -433,11 +431,11 @@ message NormalRelaySettingsUpdate {

message ProviderUpdate { repeated string providers = 1; }

message TunnelTypeUpdate { TunnelTypeConstraint tunnel_type = 2; }
message TunnelTypeUpdate { optional TunnelType tunnel_type = 2; }

message TransportPort {
TransportProtocol protocol = 1;
uint32 port = 2;
optional uint32 port = 2;
}

message OpenvpnConstraints { TransportPort port = 1; }
Expand All @@ -449,11 +447,9 @@ enum IpVersion {
V6 = 1;
}

message IpVersionConstraint { IpVersion protocol = 1; }

message WireguardConstraints {
uint32 port = 1;
IpVersionConstraint ip_version = 2;
optional uint32 port = 1;
optional IpVersion ip_version = 2;
bool use_multihop = 3;
LocationConstraint entry_location = 4;
}
Expand Down Expand Up @@ -484,7 +480,7 @@ message ConnectionConfig {
TunnelConfig tunnel = 1;
PeerConfig peer = 2;
string ipv4_gateway = 3;
string ipv6_gateway = 4;
optional string ipv6_gateway = 4;
}

oneof config {
Expand All @@ -503,9 +499,9 @@ message QuantumResistantState {
}

message TunnelOptions {
message OpenvpnOptions { uint32 mssfix = 1; }
message OpenvpnOptions { optional uint32 mssfix = 1; }
message WireguardOptions {
uint32 mtu = 1;
optional uint32 mtu = 1;
google.protobuf.Duration rotation_interval = 2;
QuantumResistantState quantum_resistant = 4;
}
Expand Down Expand Up @@ -555,7 +551,7 @@ message AppVersionInfo {
bool supported = 1;
string latest_stable = 2;
string latest_beta = 3;
string suggested_upgrade = 4;
optional string suggested_upgrade = 4;
}

message RelayListCountry {
Expand All @@ -581,7 +577,7 @@ message Relay {

string hostname = 1;
string ipv4_addr_in = 2;
string ipv6_addr_in = 3;
optional string ipv6_addr_in = 3;
bool include_in_country = 4;
bool active = 5;
bool owned = 6;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::types::{
conversions::{bytes_to_privkey, bytes_to_pubkey, option_from_proto_string},
conversions::{bytes_to_privkey, bytes_to_pubkey},
proto, FromProtobufTypeError,
};
use talpid_types::net::wireguard;
Expand Down Expand Up @@ -51,7 +51,8 @@ impl TryFrom<proto::ConnectionConfig> for mullvad_types::ConnectionConfig {
let ipv4_gateway = config.ipv4_gateway.parse().map_err(|_err| {
FromProtobufTypeError::InvalidArgument("invalid IPv4 gateway")
})?;
let ipv6_gateway = option_from_proto_string(config.ipv6_gateway)
let ipv6_gateway = config
.ipv6_gateway
.map(|addr| {
addr.parse().map_err(|_err| {
FromProtobufTypeError::InvalidArgument("invalid IPv6 gateway")
Expand Down Expand Up @@ -144,8 +145,7 @@ impl From<mullvad_types::ConnectionConfig> for proto::ConnectionConfig {
ipv6_gateway: config
.ipv6_gateway
.as_ref()
.map(|address| address.to_string())
.unwrap_or_default(),
.map(|address| address.to_string()),
})
}
}),
Expand Down
35 changes: 17 additions & 18 deletions mullvad-management-interface/src/types/conversions/location.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
use crate::types::{
conversions::{arg_from_str, option_from_proto_string},
proto, FromProtobufTypeError,
};
use crate::types::{conversions::arg_from_str, proto, FromProtobufTypeError};

impl From<mullvad_types::location::GeoIpLocation> for proto::GeoIpLocation {
fn from(geoip: mullvad_types::location::GeoIpLocation) -> proto::GeoIpLocation {
proto::GeoIpLocation {
ipv4: geoip.ipv4.map(|ip| ip.to_string()).unwrap_or_default(),
ipv6: geoip.ipv6.map(|ip| ip.to_string()).unwrap_or_default(),
ipv4: geoip.ipv4.map(|ip| ip.to_string()),
ipv6: geoip.ipv6.map(|ip| ip.to_string()),
country: geoip.country,
city: geoip.city.unwrap_or_default(),
city: geoip.city,
latitude: geoip.latitude,
longitude: geoip.longitude,
mullvad_exit_ip: geoip.mullvad_exit_ip,
hostname: geoip.hostname.unwrap_or_default(),
bridge_hostname: geoip.bridge_hostname.unwrap_or_default(),
entry_hostname: geoip.entry_hostname.unwrap_or_default(),
obfuscator_hostname: geoip.obfuscator_hostname.unwrap_or_default(),
hostname: geoip.hostname,
bridge_hostname: geoip.bridge_hostname,
entry_hostname: geoip.entry_hostname,
obfuscator_hostname: geoip.obfuscator_hostname,
}
}
}
Expand All @@ -26,21 +23,23 @@ impl TryFrom<proto::GeoIpLocation> for mullvad_types::location::GeoIpLocation {

fn try_from(geoip: proto::GeoIpLocation) -> Result<Self, Self::Error> {
Ok(mullvad_types::location::GeoIpLocation {
ipv4: option_from_proto_string(geoip.ipv4)
ipv4: geoip
.ipv4
.map(|addr| arg_from_str(&addr, "invalid IPv4 address"))
.transpose()?,
ipv6: option_from_proto_string(geoip.ipv6)
ipv6: geoip
.ipv6
.map(|addr| arg_from_str(&addr, "invalid IPv6 address"))
.transpose()?,
country: geoip.country,
city: option_from_proto_string(geoip.city),
city: geoip.city,
latitude: geoip.latitude,
longitude: geoip.longitude,
mullvad_exit_ip: geoip.mullvad_exit_ip,
hostname: option_from_proto_string(geoip.hostname),
bridge_hostname: option_from_proto_string(geoip.bridge_hostname),
entry_hostname: option_from_proto_string(geoip.entry_hostname),
obfuscator_hostname: option_from_proto_string(geoip.obfuscator_hostname),
hostname: geoip.hostname,
bridge_hostname: geoip.bridge_hostname,
entry_hostname: geoip.entry_hostname,
obfuscator_hostname: geoip.obfuscator_hostname,
})
}
}
19 changes: 4 additions & 15 deletions mullvad-management-interface/src/types/conversions/net.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::types::{conversions::arg_from_str, proto, FromProtobufTypeError};
use mullvad_types::relay_constraints::Constraint;
use std::net::SocketAddr;

impl From<talpid_types::net::TunnelEndpoint> for proto::TunnelEndpoint {
Expand Down Expand Up @@ -155,21 +154,11 @@ impl From<proto::TransportProtocol> for talpid_types::net::TransportProtocol {
}
}

impl TryFrom<proto::TunnelTypeConstraint> for Constraint<talpid_types::net::TunnelType> {
type Error = FromProtobufTypeError;

fn try_from(
tunnel_type: proto::TunnelTypeConstraint,
) -> Result<Constraint<talpid_types::net::TunnelType>, Self::Error> {
let tunnel_type = try_tunnel_type_from_i32(tunnel_type.tunnel_type)?;
Ok(Constraint::Only(tunnel_type))
}
}

impl From<proto::IpVersion> for proto::IpVersionConstraint {
impl From<proto::IpVersion> for talpid_types::net::IpVersion {
fn from(version: proto::IpVersion) -> Self {
Self {
protocol: i32::from(version),
match version {
proto::IpVersion::V4 => talpid_types::net::IpVersion::V4,
proto::IpVersion::V6 => talpid_types::net::IpVersion::V6,
}
}
}
Expand Down
Loading

0 comments on commit 7c67c11

Please sign in to comment.