Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relay selector should use overridden ip addresses for relays #5724

Conversation

rablador
Copy link
Contributor

@rablador rablador commented Jan 24, 2024

The relay selector should use the overridden IP address for relays which have overrides. The correct IP address should be shown in the connection details in the UI.


This change is Reviewable

@rablador rablador added the iOS Issues related to iOS label Jan 24, 2024
@rablador rablador force-pushed the relay-selector-should-use-overridden-ip-addresses-for-relays-ios-452 branch 2 times, most recently from 35ab784 to b4b2a59 Compare January 24, 2024 21:54
@rablador rablador marked this pull request as ready for review January 24, 2024 21:54
Copy link

linear bot commented Jan 24, 2024

@rablador rablador marked this pull request as draft January 24, 2024 21:55
@rablador rablador force-pushed the relay-selector-should-use-overridden-ip-addresses-for-relays-ios-452 branch 5 times, most recently from e4487fe to e8f2878 Compare January 31, 2024 12:36
@rablador rablador changed the base branch from main to allow-users-to-import-settings-by-pasting-json-blobs-ios-451 January 31, 2024 12:41
@rablador rablador changed the base branch from allow-users-to-import-settings-by-pasting-json-blobs-ios-451 to main January 31, 2024 12:42
@rablador rablador force-pushed the relay-selector-should-use-overridden-ip-addresses-for-relays-ios-452 branch 2 times, most recently from ede210b to aeca222 Compare February 1, 2024 11:49
@rablador rablador changed the base branch from main to allow-users-to-import-settings-by-pasting-json-blobs-ios-451 February 1, 2024 11:49
@rablador rablador force-pushed the relay-selector-should-use-overridden-ip-addresses-for-relays-ios-452 branch from aeca222 to 71a16bd Compare February 1, 2024 11:51
@rablador rablador force-pushed the allow-users-to-import-settings-by-pasting-json-blobs-ios-451 branch 2 times, most recently from 69f0518 to 79012fb Compare February 2, 2024 21:04
Copy link
Contributor

@buggmagnet buggmagnet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 10 of 11 files at r1, all commit messages.
Reviewable status: 10 of 11 files reviewed, 3 unresolved discussions (waiting on @rablador)


ios/MullvadREST/ApiHandlers/ServerRelaysResponse.swift line 38 at r1 (raw file):

        public let includeInCountry: Bool

        public func copyWith(ipv4AddrIn: IPv4Address?) -> Self {

nit
Technically we're not doing a copy of self so I'd rather we call this either override (which functions well thematically speaking IMHO)

Alternatively, I wonder if we could make sure of the consuming operator to make sure we don't reuse this instance of the relay ?


ios/MullvadREST/Relay/RelayCache.swift line 70 at r1 (raw file):

    }

    private func apply(

It would be cleaner to have a wrapper class that consumes the RelayCache rather than patching it in.

That way, we don't change its implementation, and at runtime, we can do the equivalent of that.
On top of that we don't need to modify existing unit tests, just add tests for the new class and it's all good.

struct CachedRelays {}

protocol RelayCacheProtocol {
    func read() throws -> CachedRelays
    func write(record: CachedRelays) throws
}

struct IPOverrideWrapper: RelayCacheProtocol {
    let relayCache: RelayCacheProtocol

    func read() throws -> CachedRelays {
        let relays = try relayCache.read()
        // Override the IPs here
        return relays
    }
    
    func write(record: CachedRelays) throws {
        try relayCache.write(record: record)
    }
}

struct RelayCache: RelayCacheProtocol {
    func read() throws -> CachedRelays { CachedRelays() }
    func write(record: CachedRelays) throws { }
}


func relayCache(shouldOverrideIP: Bool) -> RelayCacheProtocol {
    let relayCache = RelayCache()
    if shouldOverrideIP {
        return IPOverrideWrapper(relayCache: relayCache)
    }
    return relayCache
}

ios/MullvadREST/Relay/RelayCache.swift line 100 at r1 (raw file):

    private func apply<T: AnyRelay>(overrides: [IPOverride], to relay: T) -> T {
        if let override = overrides.first(where: { host in

nit
I was thinking : we could rewrite it like this

        overrides.first(where: { $0.hostname == relay.hostname })
            .flatMap { relay.copyWith(ipv4AddrIn: $0.ipv4Address, ipv6AddrIn: $0.ipv6Address) }
            ?? relay

But now I'm wondering about readability 🤔

@rablador rablador force-pushed the relay-selector-should-use-overridden-ip-addresses-for-relays-ios-452 branch from 71a16bd to de722eb Compare February 6, 2024 09:25
Copy link
Contributor Author

@rablador rablador left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 3 of 30 files reviewed, 3 unresolved discussions (waiting on @buggmagnet)


ios/MullvadREST/ApiHandlers/ServerRelaysResponse.swift line 38 at r1 (raw file):

Previously, buggmagnet wrote…

nit
Technically we're not doing a copy of self so I'd rather we call this either override (which functions well thematically speaking IMHO)

Alternatively, I wonder if we could make sure of the consuming operator to make sure we don't reuse this instance of the relay ?

You mean consuming at the call site?


ios/MullvadREST/Relay/RelayCache.swift line 70 at r1 (raw file):

Previously, buggmagnet wrote…

It would be cleaner to have a wrapper class that consumes the RelayCache rather than patching it in.

That way, we don't change its implementation, and at runtime, we can do the equivalent of that.
On top of that we don't need to modify existing unit tests, just add tests for the new class and it's all good.

struct CachedRelays {}

protocol RelayCacheProtocol {
    func read() throws -> CachedRelays
    func write(record: CachedRelays) throws
}

struct IPOverrideWrapper: RelayCacheProtocol {
    let relayCache: RelayCacheProtocol

    func read() throws -> CachedRelays {
        let relays = try relayCache.read()
        // Override the IPs here
        return relays
    }
    
    func write(record: CachedRelays) throws {
        try relayCache.write(record: record)
    }
}

struct RelayCache: RelayCacheProtocol {
    func read() throws -> CachedRelays { CachedRelays() }
    func write(record: CachedRelays) throws { }
}


func relayCache(shouldOverrideIP: Bool) -> RelayCacheProtocol {
    let relayCache = RelayCache()
    if shouldOverrideIP {
        return IPOverrideWrapper(relayCache: relayCache)
    }
    return relayCache
}

Done.


ios/MullvadREST/Relay/RelayCache.swift line 100 at r1 (raw file):

Previously, buggmagnet wrote…

nit
I was thinking : we could rewrite it like this

        overrides.first(where: { $0.hostname == relay.hostname })
            .flatMap { relay.copyWith(ipv4AddrIn: $0.ipv4Address, ipv6AddrIn: $0.ipv6Address) }
            ?? relay

But now I'm wondering about readability 🤔

I did something similar based on your idea.

@rablador rablador force-pushed the relay-selector-should-use-overridden-ip-addresses-for-relays-ios-452 branch 4 times, most recently from 6f36b36 to bbe107e Compare February 6, 2024 16:17
@buggmagnet buggmagnet force-pushed the allow-users-to-import-settings-by-pasting-json-blobs-ios-451 branch from 79012fb to 9b86bcb Compare February 7, 2024 08:17
Base automatically changed from allow-users-to-import-settings-by-pasting-json-blobs-ios-451 to main February 7, 2024 08:19
@rablador rablador force-pushed the relay-selector-should-use-overridden-ip-addresses-for-relays-ios-452 branch from bbe107e to 35c5250 Compare February 7, 2024 08:29
@rablador rablador marked this pull request as ready for review February 7, 2024 08:30
@rablador rablador force-pushed the relay-selector-should-use-overridden-ip-addresses-for-relays-ios-452 branch from 35c5250 to 33098a3 Compare February 7, 2024 08:37
Copy link
Contributor

@buggmagnet buggmagnet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 14 of 27 files at r2, 23 of 23 files at r3, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @rablador)


ios/MullvadSettings/IPOverrideRepository.swift line 59 at r3 (raw file):

    public func deleteAll() {
        do {
            try readWriteLock.withLock {

nit
I wonder if it wouldn't have been easier using a dispatch queue instead 🤔


ios/MullvadVPN/Coordinators/Settings/IPOverride/IPOverrideViewController.swift line 244 at r3 (raw file):

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        if let url = urls.first {
            url.securelyScoped { [weak self] scopedUrl in

nit
We can avoid the optional binding and make it a one liner with a map operation

url.securelyScoped { scopedURL in scopedURL.map { interactor.import(url: $0) } }

@rablador rablador force-pushed the relay-selector-should-use-overridden-ip-addresses-for-relays-ios-452 branch from 33098a3 to 758e217 Compare February 7, 2024 10:08
Copy link
Contributor Author

@rablador rablador left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @buggmagnet)


ios/MullvadVPN/Coordinators/Settings/IPOverride/IPOverrideViewController.swift line 244 at r3 (raw file):

Previously, buggmagnet wrote…

nit
We can avoid the optional binding and make it a one liner with a map operation

url.securelyScoped { scopedURL in scopedURL.map { interactor.import(url: $0) } }

A little too compressed I think. I did something in-between.

Copy link
Contributor

@buggmagnet buggmagnet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r4, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved

@buggmagnet buggmagnet merged commit 76da84c into main Feb 7, 2024
5 checks passed
@buggmagnet buggmagnet deleted the relay-selector-should-use-overridden-ip-addresses-for-relays-ios-452 branch February 7, 2024 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
iOS Issues related to iOS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants