Skip to content
This repository has been archived by the owner on Mar 19, 2022. It is now read-only.

NESMVPNSession disconnected #200

Open
miac-dev opened this issue Feb 24, 2021 · 17 comments
Open

NESMVPNSession disconnected #200

miac-dev opened this issue Feb 24, 2021 · 17 comments

Comments

@miac-dev
Copy link

Hi,
I have a problem with my OpenVPN connection on my app with iOS 14.4.
I perform my VPN configuration from an oven file, with a NETunnelProviderManager protocol, but when I perform the startVPNTunnel, it starts connecting and immediately disconnects. The error I see in the logs is the following:

NESMVPNSession[Primary Tunnel:OpenVPN Client: -----(null)]: status changed to disconnected, last stop reason Plugin was disabled

This happens to me when running my app on a physical iPad.

Regards

@ss-abramchuk
Copy link
Owner

Hi @miac-dev,

It is hard to say what's wrong without more details. Try checking project configuration, identifiers, entitlements, provision profiles, etc.

@miac-dev
Copy link
Author

miac-dev commented Feb 26, 2021

Hi,
this is my connection code. I have followed all the steps in the README but it still does not connect me to the VPN. It saves the VPN configuration, tries to connect and disconnects.
These are the errors I get:

  • Tearing down XPC connection due to setup error: Error Domain=NEAgentErrorDomain Code=2 "(null)"
  • NESMVPNSession[Primary Tunnel:OpenVPN Client: -----(null)]: status changed to disconnected, last stop reason Plugin was disabled

Attached code
ConnectVPN.txt

This is my .entitlement and my .plist
Entitlement & plist.txt

Can see this link
https://developer.apple.com/forums/thread/674686

@m1a7
Copy link

m1a7 commented Mar 2, 2021

Hi @miac-dev, did you resolved this problem ?

@miac-dev
Copy link
Author

miac-dev commented Mar 2, 2021

Yes, I created a new network target in my project and added all my configuration and connection to my VPN in this target. I also added as dependencies SystemConfiguration.framework and UIKit.framework, plus NetworkExtension.framework and the OpenVPNAdapter library.

@m1a7
Copy link

m1a7 commented Mar 2, 2021

@miac-dev, wow, can you give a code snippet where you perform NETunnelProviderManager initialization and vpn connection?
I would really appreciate it:)

@miac-dev
Copy link
Author

miac-dev commented Mar 2, 2021

This is my NETunnelProviderManager initialization and vpn connection. This parameter tunnelProtocol.providerBundleIdentifier = "com.app.ios.OpenVPN"
is very important, "com.app.ios.OpenVPN" is my new Network target with the PacketTunnelProvider.

[ConnectVPN.txt] (https://github.com/ss-abramchuk/OpenVPNAdapter/files/6067954/ConnectVPN.txt)

@m1a7
Copy link

m1a7 commented Mar 2, 2021

Many thanks !
I still have another question, what code did you use in the PacketTunnelProvider?

@miac-dev
Copy link
Author

miac-dev commented Mar 2, 2021

The same that appears in the Readme.md of this project

@m1a7
Copy link

m1a7 commented Mar 2, 2021

Yeah, I understood, thank you very much, today I will try your solution)

@miac-dev
Copy link
Author

miac-dev commented Mar 2, 2021

And, you have to add this parameters:

<key>NSExtension</key>
<dict>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.networkextension.packet-tunnel</string>
    <key>NSExtensionPrincipalClass</key>
    <string>$(PRODUCT_MODULE_NAME).PacketTunnelProvider</string>
</dict>

in info.plist in your new Network target.

@m1a7
Copy link

m1a7 commented Mar 2, 2021

I tried it again, but it still didn't work.
What version of xcode do you have ?

P. S.
I'm using Version 12.4

@miac-dev
Copy link
Author

miac-dev commented Mar 2, 2021

I'm using the same version of xcode

@m1a7
Copy link

m1a7 commented Mar 2, 2021

Oh, I managed to do it in a swift project ! @miac-dev, thank you for your help !

@zubairahmad2
Copy link

Yes, I created a new network target in my project and added all my configuration and connection to my VPN in this target. I also added as dependencies SystemConfiguration.framework and UIKit.framework, plus NetworkExtension.framework and the OpenVPNAdapter library.

hi @miac-dev can share me any code related to configuration. I have a complete server setup ready with .ovpn file but I am stuck there. I have follow of your code step by step but my connection is not establish successfully. please help me I am very thankful to you.

@zubairahmad2
Copy link

Oh, I managed to do it in a swift project ! @miac-dev, thank you for your help !

@m1a7 have you resolve the problem . if then please help me to configure out my problem is in configuration

@m1a7
Copy link

m1a7 commented Jun 4, 2021

Hi @zubairahmad2, my problem was the following:
From the server, I received this json:

{
"IP address" : "111.111.111.111 2222"
}
And in the IPAddress field (I don't remember what it's called), I only inserted '111.111.111.111', and so I had a problem.
Be sure to pass all the values along with a space -> "111.111.111.111 2222".

@m1a7
Copy link

m1a7 commented Jun 4, 2021

- (void) initManagerWithConfiguration:(VPNConfiguration*)config
                           completion:(nullable void(^)(NSError* _Nullable error))completion
{
    __weak typeof(self) weak = self;
    [NETunnelProviderManager loadAllFromPreferencesWithCompletionHandler:^(NSArray<NETunnelProviderManager*>* _Nullable managers, NSError* _Nullable error) {
        if (error) { if (completion)completion(error); return; }

         if (managers.count > 1) {
             [weak uninstallAllConfigurationFromManagers:^(NSError * _Nullable error) {
                 
                 weak.providerManager = managers.firstObject ? managers.firstObject : [NETunnelProviderManager new];
                [weak.providerManager loadFromPreferencesWithCompletionHandler:^(NSError * _Nullable error) {
                    if (error) { if (completion)completion(error); return; }
                    
                    NETunnelProviderProtocol *tunnelProtocol   = [weak providerProtocolFromConfiguration:config];
                    weak.providerManager.protocolConfiguration = tunnelProtocol;
                    weak.providerManager.localizedDescription  = @"MySuperVPN";
                    weak.providerManager.enabled         = YES;
                    weak.providerManager.onDemandEnabled = NO;
                    [weak.providerManager saveToPreferencesWithCompletionHandler:^(NSError *error) {
                        NSLog(@"%@", (error) ? @"Saved with error" : @"Save successfully");
                       if (completion) completion(error);
                    }];
                }];
             }];
         }else{
             weak.providerManager = managers.firstObject ? managers.firstObject : [NETunnelProviderManager new];
            [weak.providerManager loadFromPreferencesWithCompletionHandler:^(NSError * _Nullable error) {
                if (error) { if (completion)completion(error); return; }

                NETunnelProviderProtocol *tunnelProtocol   = [weak providerProtocolFromConfiguration:config];
                weak.providerManager.protocolConfiguration = tunnelProtocol;
                weak.providerManager.localizedDescription  = @"MySuperVPN";
                weak.providerManager.enabled         = YES;
                weak.providerManager.onDemandEnabled = NO;
                
                [weak.providerManager saveToPreferencesWithCompletionHandler:^(NSError *error) {
                    NSLog(@"%@", (error) ? @"Saved with error" : @"Save successfully");
                   if (completion) completion(error);
                }];
            }];
         }
    }];
}

- (NETunnelProviderProtocol*) providerProtocolFromConfiguration:(VPNConfiguration*)configuration
{
    NETunnelProviderProtocol *tunel = [[NETunnelProviderProtocol alloc] init];    
    // 1) 'serverAddress' выглядит так:  11.111.11.111 22222
    // Be sure to capture the numbers after the space !
    tunel.serverAddress             = configuration.remoteIP;
    tunel.providerBundleIdentifier  = @"mySite.com.MYAPPName.iOSPacketTunnel";
    
    tunel.providerConfiguration     = @{ @"ovpn" : configuration.configData };
    tunel.disconnectOnSleep         = NO;
    return tunel;
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants