Skip to content

Commit

Permalink
Merge pull request #48 from Psiphon-Inc/enda/ios-refactor-params-mapping
Browse files Browse the repository at this point in the history
[iOS] Handle toggleInProxy params dictionary as valid struct or reject
  • Loading branch information
tmgrask authored Nov 12, 2024
2 parents ad6c72d + 88ff2f6 commit af5eb15
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
7 changes: 0 additions & 7 deletions ios/ConduitModule/ConduitManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ extension Logging.Logger {
static let psiphonTunnel = Logger(label: "PsiphonTunnel")
}

struct ConduitParams: Equatable {
let maxClients: Int
let limitUpstream: Int
let limitDownstream: Int
let privateKey: String?
}

struct ActivitySeries: Equatable {
let msBucketPeriod: UInt64
let numBuckets: Int
Expand Down
5 changes: 1 addition & 4 deletions ios/ConduitModule/ConduitModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@

@interface RCT_EXTERN_MODULE(ConduitModule, RCTEventEmitter)

RCT_EXTERN_METHOD(toggleInProxy:(NSInteger)maxClients
limitUpstream:(NSInteger)limitUpstream
limitDownstream:(NSInteger)limitDownstream
privateKey:(NSString *_Nullable)privateKey
RCT_EXTERN_METHOD(toggleInProxy:(NSDictionary *)params
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)

Expand Down
65 changes: 41 additions & 24 deletions ios/ConduitModule/ConduitModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,29 @@ extension ConduitEvent: ReactNativeEvent {

}

/// User provided in proxy configurations received through the React Native bridge.
struct ConduitParams: Equatable {
let maxClients: Int
let limitUpstream: Int
let limitDownstream: Int
let privateKey: String?

init?(params: NSDictionary) {
guard
let maxClients = params["maxClients"] as? Int,
let limitUpstream = params["limitUpstreamBytesPerSecond"] as? Int,
let limitDownstream = params["limitDownstreamBytesPerSecond"] as? Int,
let privateKey = params["privateKey"] as? String?
else {
return nil
}

self.maxClients = maxClients
self.limitUpstream = limitUpstream
self.limitDownstream = limitDownstream
self.privateKey = privateKey
}
}

// MARK: - ConduitModule

Expand Down Expand Up @@ -239,22 +262,23 @@ final class ConduitModule: RCTEventEmitter {
// Exported native methods
extension ConduitModule {

@objc(toggleInProxy:limitUpstream:limitDownstream:privateKey:withResolver:withRejecter:)
@objc(toggleInProxy:withResolver:withRejecter:)
func toggleInProxy(
_ maxClients: Int, limitUpstream: Int, limitDownstream: Int, privateKey: String?,
_ params: NSDictionary,
resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock
) {

guard let conduitParams = ConduitParams(params: params) else {
Logger.conduitModule.warning("NSDictionary to ConduitParams conversion failed.")
reject("error", "params NSDictionary could not be loaded into ConduitParams.", nil)
return
}

Task {
switch await self.conduitManager.conduitStatus {
case .stopped:
let params = ConduitParams(
maxClients: maxClients,
limitUpstream: limitUpstream,
limitDownstream: limitDownstream,
privateKey: privateKey
)
do {
try await self.conduitManager.startConduit(params)
try await self.conduitManager.startConduit(conduitParams)
} catch {
sendEvent(.proxyError(.inProxyStartFailed))
reject("error", "Proxy start failed.", error)
Expand All @@ -275,14 +299,13 @@ extension ConduitModule {
_ params: NSDictionary,
resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock
) {
guard let maxClients = params["maxClients"] as? Int,
let limitUpstream = params["limitUpstreamBytesPerSecond"] as? Int,
let limitDownstream = params["limitDownstreamBytesPerSecond"] as? Int,
let privateKey = params["inProxyPrivateKey"] as? String? else {
reject("error", "Did not receive four valid key value pairs from params.", nil)
return
}


guard let conduitParams = ConduitParams(params: params) else {
Logger.conduitModule.warning("NSDictionary to ConduitParams conversion failed.")
reject("error", "params NSDictionary could not be loaded into ConduitParams.", nil)
return
}

Task {
switch await self.conduitManager.conduitStatus {
case .stopping, .stopped:
Expand All @@ -291,14 +314,8 @@ extension ConduitModule {

case .started, .starting:

let params = ConduitParams(
maxClients: maxClients,
limitUpstream: limitUpstream,
limitDownstream: limitDownstream,
privateKey: privateKey
)
do {
try await self.conduitManager.startConduit(params)
try await self.conduitManager.startConduit(conduitParams)
resolve(nil)
} catch {
sendEvent(.proxyError(.inProxyRestartFailed))
Expand Down

0 comments on commit af5eb15

Please sign in to comment.