搜索插件、代码仓与 APP 分类

插件详情v2.0.0

openvpn_flutter

一个允许您使用 Flutter 连接 OpenVPN 服务的插件

AndroidiOS
近 30 天下载
1250
喜欢数
134
Pub 分数
160

安装插件

flutter pub add openvpn_flutter

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

openvpn_flutter 包使 Flutter 应用程序能够在 Android 和 iOS 平台上连接 OpenVPN 服务。它提供了一个简单的接口来管理 VPN 连接,允许用户以编程方式建立安全的网络隧道。该插件集成了原生 OpenVPN 库,确保与标准 OpenVPN 配置和 .ovpn 文件兼容。该插件专为希望在其应用中添加安全连接功能(如私有网络访问或增强数据保护)的开发者设计。

适合场景

  • 安全的应用程序到服务器通信
  • 企业应用中的私有网络访问
  • 增强的用户数据隐私
  • 绕过地区内容限制
  • 连接到企业VPN

主要特点

  • 支持 Android 和 iOS 平台
  • 与标准 .ovpn 配置文件集成
  • 程序化连接管理
  • 安全隧道建立
  • 轻量且易于集成

适合对象

  • 企业移动应用
  • 需要安全网络访问的应用程序
  • 开发注重隐私的工具的开发者
  • 需要远程服务器连接的应用程序
  • 使用 OpenVPN 基础设施的项目

使用建议

  • 需要原生 OpenVPN 库集成
  • 在 Android 和 iOS 上可能需要额外的权限
  • 配置文件处理必须安全
  • 仅限 OpenVPN 协议
  • 可能需要特定于平台的设置

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Connect to the OpenVPN service using Flutter. Contributions through issues and pull requests are highly appreciated!

Android Setup

1. Permission Handler
Java

Include the following code in the onActivityResult method of MainActivity.java (if you are using Java):

OpenVPNFlutterPlugin.connectWhileGranted(requestCode == 24 && resultCode == RESULT_OK);

The complete method should look like this:

...
import id.laskarmedia.openvpn_flutter.OpenVPNFlutterPlugin;
...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    OpenVPNFlutterPlugin.connectWhileGranted(requestCode == 24 && resultCode == RESULT_OK);
    super.onActivityResult(requestCode, resultCode, data);
}
Kotlin

Include the following code in the onActivityResult method of MainActivity.kt (if you are using Kotlin):

OpenVPNFlutterPlugin.connectWhileGranted(requestCode == 24 && resultCode == RESULT_OK);

The complete method should look like this:

...
import id.laskarmedia.openvpn_flutter.OpenVPNFlutterPlugin
...
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    OpenVPNFlutterPlugin.connectWhileGranted(requestCode == 24 && resultCode == RESULT_OK)
    super.onActivityResult(requestCode, resultCode, data)
}
2. App Bundle Build Not Connecting

If you encounter issues with the app not connecting using the latest Flutter SDK, apply the following quick fix:

Ensure that you include the following attribute within the <application> tag in your AndroidManifest.xml file:

<application
    ...
    android:extractNativeLibs="true"
    ...>
</application>

iOS Setup

1. Add Capabilities

Add the App Groups and Network Extensions capabilities to the Runner's target. Refer to the image below for detailed instructions:

2. Add New Target

Click the + button on the bottom left, choose NETWORK EXTENSION, and follow the instructions in the image below:

Add the same capabilities to the VPNExtension as you did for the Runner's target:

3. Copy and Paste

Add the following lines to your Podfile (ios/Podfile):

target 'VPNExtension' do
  use_frameworks!
  pod 'OpenVPNAdapter', :git => 'https://github.com/ss-abramchuk/OpenVPNAdapter.git', :tag => '0.8.0'
end

Open VPNExtension > PacketTunnelProvider.swift and copy-paste the script from PacketTunnelProvider.swift.

Note

You must use iOS devices instead of the simulator to connect.

Recipe

Initialize

Before starting, initialize the OpenVPN plugin:

late OpenVPN openvpn;

@override
void initState() {
    openvpn = OpenVPN(onVpnStatusChanged: _onVpnStatusChanged, onVpnStageChanged: _onVpnStageChanged);
    openvpn.initialize(
        groupIdentifier: "GROUP_IDENTIFIER", ///Example 'group.com.laskarmedia.vpn'
        providerBundleIdentifier: "NETWORK_EXTENSION_IDENTIFIER", ///Example 'id.laskarmedia.openvpnFlutterExample.VPNExtension'
        localizedDescription: "LOCALIZED_DESCRIPTION" ///Example 'Laskarmedia VPN'
    );
}

void _onVpnStatusChanged(VPNStatus? vpnStatus){
    setState((){
        this.status = vpnStatus;
    });
}

void _onVpnStageChanged(VPNStage? stage){
    setState((){
        this.stage = stage;
    });
}
Connect to VPN
void connect() {
  openvpn.connect(
    config,
    name,
    username: username,
    password: password,
    bypassPackages: [],
    // In iOS connection can get stuck in "connecting" if this flag is "false". 
    // Solution is to switch it to "true".
    certIsRequired: false,
  );
}
Disconnect
void disconnect(){
    openvpn.disconnect();
}

Publishing to Play Store and App Store

Android
  1. You can use app bundles to publish the app.
  2. Add the following to your files in the android folder (special thanks to https://github.com/nizwar/openvpn_flutter/issues/10). Otherwise, the connection may not be established in some cases and will silently report "disconnected" when trying to connect. This is likely related to some symbols being stripped by Google Play.
gradle.properties > android.bundle.enableUncompressedNativeLibs=false
AndroidManifest > android:extractNativeLibs="true" in the application tag

Add the following inside the android tag in app/build.gradle:

android {
    ...
    //from here ======
    lintOptions {
        disable 'InvalidPackage'
        checkReleaseBuilds false
    }

    packagingOptions {
        jniLibs {
            useLegacyPackaging = true
        }
    }

    bundle {
        language {
            enableSplit = false
        }
        density {
            enableSplit = false
        }
        abi {
            enableSplit = false
        }
    }
    //to here
    ...
}
Notifications

As the plugin shows notifications for connection status and connection details, you must request permission using third-party packages.

Example using permission_handler:

///Put it anywhere you wish, like once you initialize the VPN or pre-connect to the server
Permission.notification.isGranted.then((_) {
  if (!_) Permission.notification.request();
});
iOS
  1. View Apple Guidelines relating to VPN.
  2. This plugin DOES use encryption, but it uses exempt encryptions.

Licenses

Support

If you appreciate my work, don't forget to give a thumbs up or support me with a cup of coffee.

所属分类

源码仓库

使用此插件的项目

暂无相关数据。