diff options
| author | David Lönnhager <david.l@mullvad.net> | 2023-09-23 22:03:09 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2023-10-13 10:30:25 +0200 |
| commit | 4d6174765b5fef9c3d1b1f08328df79683ce8ba8 (patch) | |
| tree | f4b514c4b0b4db4dc4a65a96e7955be355fd5c28 /gui/src | |
| parent | 1a40a208b1ab582b5e02de8693af41f9af96d36f (diff) | |
| download | mullvadvpn-4d6174765b5fef9c3d1b1f08328df79683ce8ba8.tar.xz mullvadvpn-4d6174765b5fef9c3d1b1f08328df79683ce8ba8.zip | |
Use optional modifier for optional primitives in protobuf messages
Diffstat (limited to 'gui/src')
| -rw-r--r-- | gui/src/main/daemon-rpc.ts | 93 |
1 files changed, 44 insertions, 49 deletions
diff --git a/gui/src/main/daemon-rpc.ts b/gui/src/main/daemon-rpc.ts index 608bf82241..288bbfd7fc 100644 --- a/gui/src/main/daemon-rpc.ts +++ b/gui/src/main/daemon-rpc.ts @@ -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); } @@ -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); } @@ -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 }; } } @@ -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()!); @@ -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) { @@ -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' }, }; } @@ -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(); @@ -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' }; } @@ -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); @@ -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( @@ -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) { @@ -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>, })); } @@ -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; |
