2

I am using Network Extension framework for configure and connect VPN server programmatically. I can start and stop VPN. I have written following code to configure VPN in viewDidLoad.

NEVPNManager *manager = [NEVPNManager sharedManager];
[manager loadFromPreferencesWithCompletionHandler:^(NSError *error) {

        if(manager.protocol == nil)
        {
            NSString *filePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"VPNCert" ofType:@"p12"];
            NSData *certData = [NSData dataWithContentsOfFile:filePath];
            NSString *certPassword = @"password";

            NSString *vpnUsername = @"username";
            NSString *vpnPassword = @"password";
            NSString *vpnUrl = @"VPN Server IP";

            // This saves my credentials to the keychain and returns a persistent keychain reference
            NSData *passRef = [self addVPNCredentialsToKeychain:vpnUsername withPassword:vpnPassword];

            NEVPNProtocolIPSec *p = [[NEVPNProtocolIPSec alloc] init];
            p.authenticationMethod = NEVPNIKEAuthenticationMethodCertificate;
            p.serverAddress = vpnUrl;
            p.username = vpnUsername;
            p.passwordReference = passRef;
            p.identityData = certData;
            p.identityDataPassword = certPassword;
            p.disconnectOnSleep = NO;
            p.useExtendedAuthentication = YES;

            manager.protocol = p;
            manager.enabled = YES;
            [manager setOnDemandEnabled:NO];
            [manager setLocalizedDescription:@"VPN Network"];
            [manager saveToPreferencesWithCompletionHandler:^(NSError *error) {

                if(error)
                {
                    NSLog(@"Load error: %@", error);
                }
            }];
        }
    }];

I also wrote code for start VPN when button is pressed.

- (IBAction)buttonPressed:(id)sender {

    NEVPNManager *manager = [NEVPNManager sharedManager];
    [manager loadFromPreferencesWithCompletionHandler:^(NSError *error) {

                if (!error)
                {
                    NSError *startError = [[NSError alloc] init];
                    [[NEVPNManager sharedManager].connection startVPNTunnelAndReturnError:&startError];
                }
            }];
}

Cases working for me.

  1. If i connect VPN in mobile data and switched to wifi this case VPN is not disconnecting.
  2. Also working if i connect VPN in mobile data and switched to wifi and then back to mobile data VPN is not disconnecting.

Problem is

If i connect VPN in wifi and then switch to mobile data, in this case VPN is stopping. I want VPN must stop only when user will stop the VPN.

Is any steps are missing while configuring VPN because of that VPN is stopping?

Thanks in Advance.

Rayan Dsouza
  • 71
  • 1
  • 5

1 Answers1

0

You would need to add rules to keep the connection persisted. Also enable onDemand [manager setOnDemandEnabled:YES];

Swift

    let connectRule = NEOnDemandRuleConnect()
    connectRule.interfaceTypeMatch = .any

    let disconnectRule = NEOnDemandRuleDisconnect()
    disconnectRule.probeURL = URL(string:VPNCredentialsModel.instance.vpnProbeURL()!)
Siddharthan Asokan
  • 4,321
  • 11
  • 44
  • 80