0

我正在尝试设置自定义的 vpn 隧道

- (void) openTunnel{

[vpnManager loadFromPreferencesWithCompletionHandler:^(NSError *error){

    if(error != nil){
        NSLog(@"%@", error);
    }else{

        NSError *startError = nil;

        [self->vpnManager.connection startVPNTunnelWithOptions:nil andReturnError:&startError];

        if(startError != nil){

            NSLog(@"viewcontroller: startError: %@", startError);
        }else{
            NSLog(@"Complete");
            [[NSNotificationCenter defaultCenter] addObserver:self
                        selector:@selector(receiveNotification:)
                    name:NEVPNStatusDidChangeNotification
                        object:nil];
        }
    }
}];}

startVPNTunnelWithOptions启动自定义隧道,如下所示

    - (void)startTunnelWithOptions:(NSDictionary *)options completionHandler:(void (^)(NSError *error))completion{
    NSLog(@"===>  ");
     NSError *error = nil;
    vpnAdapter = [[OpenAdapter alloc] init];
    vpnAdapter.delegate = self;
    // get config
    config = [[NSDictionary alloc] init];
    NETunnelProviderProtocol *protocol = (NETunnelProviderProtocol *)self.protocolConfiguration;
    config = protocol.providerConfiguration;

    host = config[@"server"];

    // Load config data
    username = config[@"username"];
    password = config[@"password"];

    option = config[@"option"]; 
    NSError *newError = [NSError errorWithDomain:VPNTunnelProviderErrorDomain
                                            code:TunnelProviderErrorCodeInvalidConfiguration
                                        userInfo:@{NSLocalizedDescriptionKey: @"Settings validation failed!"}];
    if(option != nil){
        [vpnAdapter connect:host user:username pass:password add:YES completionHandler:^(int success) {
            if(success == 0)
            {NSLog(@"packettunnel: newerror: %d", 0);
                completion(error);}
            else {
                NSLog(@"packettunnel: newerror: %@", newError);
                completion(newError);
            }
        }];
    }else{
        [vpnAdapter connect:host user:username pass:password add:NO completionHandler:^(int success) {
            if(success == 0){
{NSLog(@"packettunnel: newerror: %d", 0);
                 completion(error);}
            else
            {
                NSLog(@"packettunnel: newerror: %@", newError);
                completion(newError);
            }
        }];
    }

}

使用假凭据进行测试时(使用正确的凭据可以正常工作),它应该打印 startError,但事实并非如此。我可以确认startTunnelWithOptions它实际上从 NSLog返回newError ,但不知何故, startVPNTunnelWithOptions没有返回任何错误。

有人可以帮忙吗?

谢谢

4

0 回答 0