ss-abramchuk / OpenVPNAdapter

Objective-C wrapper for OpenVPN library. Compatible with iOS and macOS.
GNU Affero General Public License v3.0
473 stars 209 forks source link

NESMVPNSession disconnected #200

Open miac-dev opened 3 years ago

miac-dev commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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:

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 commented 3 years ago

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

miac-dev commented 3 years ago

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 commented 3 years ago

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

miac-dev commented 3 years ago

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 commented 3 years ago

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

miac-dev commented 3 years ago

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

m1a7 commented 3 years ago

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

miac-dev commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

I'm using the same version of xcode

m1a7 commented 3 years ago

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

zubairahmad2 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago
- (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;
}