Skip to content

Commit

Permalink
replace ErrorCode with error description
Browse files Browse the repository at this point in the history
  • Loading branch information
jyyi1 committed Aug 14, 2024
1 parent 4c9b68e commit bdb353c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class OutlineVpn: NSObject {
// MARK: Interface

/** Starts a VPN tunnel as specified in the OutlineTunnel object. */
public func start(_ tunnelId: String, name: String?, transportConfig: [String: Any]) async -> ErrorCode {
public func start(_ tunnelId: String, name: String?, transportConfig: [String: Any]) async -> String? {
if let manager = await getTunnelManager(), isActiveSession(manager.connection) {
DDLogDebug("Stoppping active session before starting new one")
await stopSession(manager)
Expand All @@ -61,7 +61,7 @@ public class OutlineVpn: NSObject {
manager = try await setupVpn(withId: tunnelId, named: name ?? "Outline Server", withTransport: transportConfig)
} catch {
DDLogError("Failed to setup VPN: \(error.localizedDescription))")
return ErrorCode.vpnPermissionNotGranted;
return "VPN permission is not granted";
}
let session = manager.connection as! NETunnelProviderSession

Expand Down Expand Up @@ -98,7 +98,7 @@ public class OutlineVpn: NSObject {
DDLogDebug("NETunnelProviderSession.startTunnel() returned")
} catch let error as NSError {
DDLogError("Failed to start VPN: \(error.localizedDescription)")
return ErrorCode.vpnStartFailure
return error.localizedDescription
}

// Wait for it to be done.
Expand All @@ -108,14 +108,13 @@ public class OutlineVpn: NSObject {
case .connected:
break
case .disconnected:
return ErrorCode.vpnStartFailure
return "Failed to start VPN"
case .invalid:
return ErrorCode.systemMisconfigured
return "VPN profile has been modified"
default:
// This shouldn't happen.
return ErrorCode.systemMisconfigured
return "Unknown connection status"
}
//

// Set an on-demand rule to connect to any available network to implement auto-connect on boot
do { try await manager.loadFromPreferences() }
Expand All @@ -129,22 +128,22 @@ public class OutlineVpn: NSObject {
catch {
DDLogWarn("OutlineVpn.start: Failed to save on-demand preference change: \(error.localizedDescription)")
}
return ErrorCode.noError
return nil
}

/** Starts the last successful VPN tunnel. */
@objc public func startLastSuccessfulTunnel(_ completion: @escaping (Callback)) {
Task {
guard let manager = await getTunnelManager() else {
DDLogDebug("Tunnel manager not setup")
completion(ErrorCode.illegalServerConfiguration)
completion("Failed to get TunnelManager")
return
}
do {
try manager.connection.startVPNTunnel()
completion(ErrorCode.noError)
completion(nil)
} catch {
completion(ErrorCode.vpnStartFailure)
completion("Failed to start VPN")
}
}
}
Expand Down Expand Up @@ -227,7 +226,7 @@ public class OutlineVpn: NSObject {
}
guard let manager = session.manager as? NETunnelProviderManager else {
// For some reason we get spurious notifications with connecting and disconnecting states
DDLogDebug("Bad manager in OutlineVpn.vpnStatusChanged session=\(String(describing:session)) status=\(String(describing: session.status)) manager=\(session.manager)")
DDLogDebug("Bad manager in OutlineVpn.vpnStatusChanged session=\(String(describing:session))")
return
}
guard let protoConfig = manager.protocolConfiguration as? NETunnelProviderProtocol,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ @interface PacketTunnelProvider ()<Tun2socksTunWriter>
@property (nonatomic) NSString *hostNetworkAddress; // IP address of the host in the active network.
@property id<Tun2socksTunnel> tunnel;

// result callbacks for 'start' and 'stop', accepts a nullable error string
@property (nonatomic, copy) void (^startCompletion)(NSString * _Nullable);
@property (nonatomic, copy) void (^stopCompletion)(NSString * _Nullable);
// mimics fetchLastDisconnectErrorWithCompletionHandler on older systems
@property (nullable) NSError *lastDisconnectError;

@property (nonatomic) DDFileLogger *fileLogger;
@property (nonatomic, nullable) OutlineTunnel *transportConfig;
Expand Down Expand Up @@ -62,10 +61,16 @@ - (id)init {
}

- (void)startTunnelWithOptions:(NSDictionary *)options
completionHandler:(void (^)(NSError *))startDone {
completionHandler:(void (^)(NSError *))completion {
DDLogInfo(@"Starting tunnel");
DDLogDebug(@"Options are %@", options);

// mimics fetchLastDisconnectErrorWithCompletionHandler on older systems
void (^startDone)(NSError *) = ^(NSError *err) {
self.lastDisconnectError = err;
completion(err);
};

// MARK: Process Config.
if (self.protocolConfiguration == nil) {
DDLogError(@"Failed to retrieve NETunnelProviderProtocol.");
Expand Down Expand Up @@ -121,31 +126,34 @@ - (void)startTunnelWithOptions:(NSDictionary *)options
NSError *err = nil;
ShadowsocksClient* client = [self newClientWithError:&err];
if (err != nil) {
return startDone(err);
return startDone([NSError errorWithDomain:NEVPNErrorDomain
code:NEVPNErrorConfigurationReadWriteFailed
userInfo:nil]);
}

long connStatus;
ShadowsocksCheckConnectivity(client, &connStatus, &err);
if (err != nil) {
return completionHandler(err);
return startDone(err);
}
supportUDP = ((connStatus & ShadowsocksUDPConnected) == ShadowsocksUDPConnected);
}

[self startRouting:[OutlineTunnel getTunnelNetworkSettings]
completion:^(NSError *_Nullable error) {
if (error != nil) {
return startDone(error);
}
BOOL isUdpSupported = isOnDemand ? self.isUdpSupported : supportUDP;
if (![self startTun2Socks:isUdpSupported]) {
return startDone([NSError errorWithDomain:NEVPNErrorDomain
code:NEVPNErrorConnectionFailed
userInfo:nil]);
}
[self listenForNetworkChanges];
startDone(nil);
}];
completion:^(NSError *_Nullable error) {
if (error != nil) {
return startDone(error);
}
BOOL isUdpSupported = isOnDemand ? self.isUdpSupported : supportUDP;
[self startTun2SocksWithUDPSupported:isUdpSupported error:&error];
if (error != nil) {
return startDone([NSError errorWithDomain:NEVPNErrorDomain
code:NEVPNErrorConnectionFailed
userInfo:nil]);
}
[self listenForNetworkChanges];
startDone(nil);
}];
}

- (void)stopTunnelWithReason:(NEProviderStopReason)reason
Expand Down Expand Up @@ -181,7 +189,7 @@ - (void)startRouting:(NEPacketTunnelNetworkSettings *)settings
__weak PacketTunnelProvider *weakSelf = self;
[self setTunnelNetworkSettings:settings completionHandler:^(NSError * _Nullable error) {
if (error != nil) {
DDLogError(@"Failed to start routing: %@", error.localizedDescription);
DDLogError(@"Failed to start routing: %@", error);
} else {
DDLogInfo(@"Routing started");
// Passing nil settings clears the tunnel network configuration. Indicate to the system that
Expand Down

0 comments on commit bdb353c

Please sign in to comment.