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

[iOS] Handle toggleInProxy params dictionary as valid struct or reject #48

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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