@@ -18,47 +18,42 @@ public protocol ShadowsocksLoaderProtocol {
18
18
public class ShadowsocksLoader : ShadowsocksLoaderProtocol {
19
19
let cache : ShadowsocksConfigurationCacheProtocol
20
20
let relaySelector : ShadowsocksRelaySelectorProtocol
21
- let constraintsUpdater : RelayConstraintsUpdater
22
- let multihopUpdater : MultihopUpdater
23
- private var multihopState : MultihopState = . off
24
- private var observer : MultihopObserverBlock !
21
+ let settingsUpdater : SettingsUpdater
22
+
23
+ private var observer : SettingsObserverBlock !
24
+ private var tunnelSettings = LatestTunnelSettings ( )
25
+ private let settingsStrategy = TunnelSettingsStrategy ( )
25
26
26
27
deinit {
27
- self . multihopUpdater . removeObserver ( observer)
28
+ self . settingsUpdater . removeObserver ( observer)
28
29
}
29
30
30
- private var relayConstraints = RelayConstraints ( )
31
-
32
31
public init (
33
32
cache: ShadowsocksConfigurationCacheProtocol ,
34
33
relaySelector: ShadowsocksRelaySelectorProtocol ,
35
- constraintsUpdater: RelayConstraintsUpdater ,
36
- multihopUpdater: MultihopUpdater
34
+ settingsUpdater: SettingsUpdater
37
35
) {
38
36
self . cache = cache
39
37
self . relaySelector = relaySelector
40
- self . constraintsUpdater = constraintsUpdater
41
- self . multihopUpdater = multihopUpdater
38
+ self . settingsUpdater = settingsUpdater
42
39
self . addObservers ( )
43
40
}
44
41
45
42
private func addObservers( ) {
46
- // The constraints gets updated a lot when observing the tunnel, clear the cache if the constraints have changed.
47
- constraintsUpdater. onNewConstraints = { [ weak self] newConstraints in
48
- if self ? . relayConstraints != newConstraints {
49
- self ? . relayConstraints = newConstraints
50
- try ? self ? . clear ( )
51
- }
52
- }
53
-
54
- // The multihop state gets updated a lot when observing the tunnel, clear the cache if the multihop settings have changed.
55
- self . observer = MultihopObserverBlock ( didUpdateMultihop: { [ weak self] _, newMultihopState in
56
- if self ? . multihopState != newMultihopState {
57
- self ? . multihopState = newMultihopState
58
- try ? self ? . clear ( )
59
- }
60
- } )
61
- multihopUpdater. addObserver ( self . observer)
43
+ observer =
44
+ SettingsObserverBlock (
45
+ didUpdateSettings: { [ weak self] latestTunnelSettings in
46
+ guard let self else { return }
47
+ if settingsStrategy. shouldReconnectToNewRelay (
48
+ oldSettings: tunnelSettings,
49
+ newSettings: latestTunnelSettings
50
+ ) {
51
+ try ? clear ( )
52
+ }
53
+ tunnelSettings = latestTunnelSettings
54
+ }
55
+ )
56
+ settingsUpdater. addObserver ( self . observer)
62
57
}
63
58
64
59
public func clear( ) throws {
@@ -81,7 +76,7 @@ public class ShadowsocksLoader: ShadowsocksLoaderProtocol {
81
76
/// Returns a randomly selected shadowsocks configuration.
82
77
private func create( ) throws -> ShadowsocksConfiguration {
83
78
let bridgeConfiguration = try relaySelector. getBridges ( )
84
- let closestRelay = try relaySelector. selectRelay ( with: relayConstraints , multihopState : multihopState )
79
+ let closestRelay = try relaySelector. selectRelay ( with: tunnelSettings )
85
80
86
81
guard let bridgeAddress = closestRelay? . ipv4AddrIn,
87
82
let bridgeConfiguration else { throw POSIXError ( . ENOENT) }
0 commit comments