Skip to content

Commit

Permalink
Use optional modifier for optional primitives 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 1081354
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 292 deletions.
93 changes: 44 additions & 49 deletions gui/src/main/daemon-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@ export class DaemonRpc {

if (settingsUpdate.tunnelProtocol) {
const tunnelTypeUpdate = new grpcTypes.TunnelTypeUpdate();
tunnelTypeUpdate.setTunnelType(
convertToTunnelTypeConstraint(settingsUpdate.tunnelProtocol),
);
if (settingsUpdate.tunnelProtocol !== 'any') {
tunnelTypeUpdate.setTunnelType(convertToTunnelType(settingsUpdate.tunnelProtocol.only));
}
normalUpdate.setTunnelType(tunnelTypeUpdate);
}

Expand Down Expand Up @@ -416,11 +416,9 @@ export class DaemonRpc {

if (obfuscationSettings.udp2tcpSettings) {
const grpcUdp2tcpSettings = new grpcTypes.Udp2TcpObfuscationSettings();
grpcUdp2tcpSettings.setPort(
obfuscationSettings.udp2tcpSettings.port === 'any'
? 0
: obfuscationSettings.udp2tcpSettings.port.only,
);
if (obfuscationSettings.udp2tcpSettings.port !== 'any') {
grpcUdp2tcpSettings.setPort(obfuscationSettings.udp2tcpSettings.port.only);
}
grpcObfuscationSettings.setUdp2tcp(grpcUdp2tcpSettings);
}

Expand Down Expand Up @@ -995,7 +993,7 @@ function convertFromBlockingError(
return { type: FirewallPolicyErrorType.generic };
case grpcTypes.ErrorState.FirewallPolicyError.ErrorType.LOCKED: {
const pid = error.lockPid;
const name = error.lockName;
const name = error.lockName!;
return { type: FirewallPolicyErrorType.locked, pid, name };
}
}
Expand Down Expand Up @@ -1151,7 +1149,10 @@ function convertFromRelaySettings(
const normal = relaySettings.getNormal()!;
const locationConstraint = convertFromLocationConstraint(normal.getLocation());
const location = locationConstraint ? { only: locationConstraint } : 'any';
const tunnelProtocol = convertFromTunnelTypeConstraint(normal.getTunnelType()!);
// `getTunnelType()` is not falsy if type is 'any'
const tunnelProtocol = convertFromTunnelTypeConstraint(
normal.hasTunnelType() ? normal.getTunnelType() : undefined,
);
const providers = normal.getProvidersList();
const ownership = convertFromOwnership(normal.getOwnership());
const openvpnConstraints = convertFromOpenVpnConstraints(normal.getOpenvpnConstraints()!);
Expand Down Expand Up @@ -1276,11 +1277,13 @@ function convertFromLocationConstraint(
return { customList: location.getCustomList() };
} else {
const innerLocation = location.getLocation()?.toObject();
return innerLocation && convertFromRelayLocation(innerLocation);
return innerLocation && convertFromGeographicConstraint(innerLocation);
}
}

function convertFromRelayLocation(location: grpcTypes.RelayLocation.AsObject): RelayLocation {
function convertFromGeographicConstraint(
location: grpcTypes.GeographicLocationConstraint.AsObject,
): RelayLocation {
if (location.hostname) {
return location;
} else if (location.city) {
Expand Down Expand Up @@ -1356,10 +1359,9 @@ function convertFromObfuscationSettings(

return {
selectedObfuscation: selectedObfuscationType,
udp2tcpSettings:
obfuscationSettings?.udp2tcp && obfuscationSettings.udp2tcp.port !== 0
? { port: { only: obfuscationSettings.udp2tcp.port } }
: { port: 'any' },
udp2tcpSettings: obfuscationSettings?.udp2tcp
? { port: convertFromConstraint(obfuscationSettings.udp2tcp.port) }
: { port: 'any' },
};
}

Expand Down Expand Up @@ -1458,14 +1460,16 @@ function convertFromWireguardConstraints(
result.port = { only: port };
}

const ipVersion = constraints.getIpVersion()?.getProtocol();
switch (ipVersion) {
case grpcTypes.IpVersion.V4:
result.ipVersion = { only: 'ipv4' };
break;
case grpcTypes.IpVersion.V6:
result.ipVersion = { only: 'ipv6' };
break;
// `getIpVersion()` is not falsy if type is 'any'
if (constraints.hasIpVersion()) {
switch (constraints.getIpVersion()) {
case grpcTypes.IpVersion.V4:
result.ipVersion = { only: 'ipv4' };
break;
case grpcTypes.IpVersion.V6:
result.ipVersion = { only: 'ipv6' };
break;
}
}

const entryLocation = constraints.getEntryLocation();
Expand All @@ -1478,9 +1482,9 @@ function convertFromWireguardConstraints(
}

function convertFromTunnelTypeConstraint(
constraint: grpcTypes.TunnelTypeConstraint | undefined,
constraint: grpcTypes.TunnelType | undefined,
): Constraint<TunnelProtocol> {
switch (constraint?.getTunnelType()) {
switch (constraint) {
case grpcTypes.TunnelType.WIREGUARD: {
return { only: 'wireguard' };
}
Expand Down Expand Up @@ -1518,15 +1522,17 @@ function convertToLocation(
if (constraint && 'customList' in constraint && constraint.customList) {
locationConstraint.setCustomList(constraint.customList);
} else {
const location = constraint && convertToRelayLocation(constraint);
const location = constraint && convertToGeographicConstraint(constraint);
locationConstraint.setLocation(location);
}

return locationConstraint;
}

function convertToRelayLocation(location: RelayLocation): grpcTypes.RelayLocation {
const relayLocation = new grpcTypes.RelayLocation();
function convertToGeographicConstraint(
location: RelayLocation,
): grpcTypes.GeographicLocationConstraint {
const relayLocation = new grpcTypes.GeographicLocationConstraint();
if ('hostname' in location) {
relayLocation.setCountry(location.country);
relayLocation.setCity(location.city);
Expand All @@ -1541,22 +1547,13 @@ function convertToRelayLocation(location: RelayLocation): grpcTypes.RelayLocatio
return relayLocation;
}

function convertToTunnelTypeConstraint(
constraint: Constraint<TunnelType>,
): grpcTypes.TunnelTypeConstraint | undefined {
const grpcConstraint = new grpcTypes.TunnelTypeConstraint();

if (constraint !== undefined && constraint !== 'any' && 'only' in constraint) {
switch (constraint.only) {
case 'wireguard':
grpcConstraint.setTunnelType(grpcTypes.TunnelType.WIREGUARD);
return grpcConstraint;
case 'openvpn':
grpcConstraint.setTunnelType(grpcTypes.TunnelType.OPENVPN);
return grpcConstraint;
}
function convertToTunnelType(tunnelProtocol: TunnelProtocol): grpcTypes.TunnelType {
switch (tunnelProtocol) {
case 'wireguard':
return grpcTypes.TunnelType.WIREGUARD;
case 'openvpn':
return grpcTypes.TunnelType.OPENVPN;
}
return undefined;
}

function convertToOpenVpnConstraints(
Expand Down Expand Up @@ -1595,9 +1592,7 @@ function convertToWireguardConstraints(
if (ipVersion) {
const ipVersionProtocol =
ipVersion === 'ipv4' ? grpcTypes.IpVersion.V4 : grpcTypes.IpVersion.V6;
const ipVersionConstraints = new grpcTypes.IpVersionConstraint();
ipVersionConstraints.setProtocol(ipVersionProtocol);
wireguardConstraints.setIpVersion(ipVersionConstraints);
wireguardConstraints.setIpVersion(ipVersionProtocol);
}

if (constraint.useMultihop) {
Expand Down Expand Up @@ -1687,7 +1682,7 @@ function convertFromCustomLists(customLists: Array<grpcTypes.CustomList>): Custo
locations: list
.getLocationsList()
.map((location) =>
convertFromRelayLocation(location.toObject()),
convertFromGeographicConstraint(location.toObject()),
) as Array<RelayLocationGeographical>,
}));
}
Expand All @@ -1697,7 +1692,7 @@ function convertToCustomList(customList: ICustomList): grpcTypes.CustomList {
grpcCustomList.setId(customList.id);
grpcCustomList.setName(customList.name);

const locations = customList.locations.map(convertToRelayLocation);
const locations = customList.locations.map(convertToGeographicConstraint);
grpcCustomList.setLocationsList(locations);

return grpcCustomList;
Expand Down
52 changes: 24 additions & 28 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 @@ -297,14 +297,14 @@ message BridgeSettings {
message LocationConstraint {
oneof type {
string custom_list = 1;
RelayLocation location = 2;
GeographicLocationConstraint location = 2;
}
}

message RelayLocation {
message GeographicLocationConstraint {
string country = 1;
string city = 2;
string hostname = 3;
optional string city = 2;
optional string hostname = 3;
}

message BridgeState {
Expand All @@ -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 All @@ -331,7 +331,7 @@ message ObfuscationSettings {
message CustomList {
string id = 1;
string name = 2;
repeated RelayLocation locations = 3;
repeated GeographicLocationConstraint locations = 3;
}

message CustomListSettings { repeated CustomList custom_lists = 1; }
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
Expand Up @@ -33,7 +33,7 @@ impl From<mullvad_types::custom_list::CustomList> for proto::CustomList {
let locations = custom_list
.locations
.into_iter()
.map(proto::RelayLocation::from)
.map(proto::GeographicLocationConstraint::from)
.collect();
Self {
id: custom_list.id.to_string(),
Expand All @@ -60,39 +60,3 @@ impl TryFrom<proto::CustomList> for mullvad_types::custom_list::CustomList {
})
}
}

impl TryFrom<proto::RelayLocation> for GeographicLocationConstraint {
type Error = FromProtobufTypeError;

fn try_from(relay_location: proto::RelayLocation) -> Result<Self, Self::Error> {
match (
relay_location.country.as_ref(),
relay_location.city.as_ref(),
relay_location.hostname.as_ref(),
) {
("", ..) => Err(FromProtobufTypeError::InvalidArgument(
"Invalid geographic relay location",
)),
(_country, "", "") => Ok(GeographicLocationConstraint::Country(
relay_location.country,
)),
(_country, _city, "") => Ok(GeographicLocationConstraint::City(
relay_location.country,
relay_location.city,
)),
(_country, city, _hostname) => {
if city.is_empty() {
Err(FromProtobufTypeError::InvalidArgument(
"Relay location must contain a city if hostname is included",
))
} else {
Ok(GeographicLocationConstraint::Hostname(
relay_location.country,
relay_location.city,
relay_location.hostname,
))
}
}
}
}
}
Loading

0 comments on commit 1081354

Please sign in to comment.