OneConnect VPN is a private virtual network that has unique features and has high security. Any Developers can Create their Dream VPN App Using Our SDK and Resource, it support android and iOS
Installation
Install OneConnect library by putting this code in Pubsec.yaml
VpnServer class contains the server id, name, flag, ovpn configuration, username, password and server type (free or pro)
List<VpnServer> vpnServerList = [];
vpnServerList.addAll(await AppConstants.openVPN.fetchOneConnect(OneConnect.free)); //Free
vpnServerList.addAll(await AppConstants.openVPN.fetchOneConnect(OneConnect.pro)); //Pro
//Logging first server from vpnServerList for visualization
debugPrint("${vpnServerList[0].id}"); //Server id
debugPrint("${vpnServerList[0].serverName}"); //Server name as show in OneConnect account
debugPrint("${vpnServerList[0].flagUrl}"); //Country flag image url of server
debugPrint("${vpnServerList[0].ovpnConfiguration}"); //Configuration exclusive to OpenVPN
debugPrint("${vpnServerList[0].vpnUserName}"); // handle by SDK itself
debugPrint("${vpnServerList[0].vpnPassword}"); // handle by SDK itself
debugPrint("${vpnServerList[0].isFree}"); //Equals to 1 of server is free
Connecting to VPN
Declare variables
Select a server from the server list you have fetched earlier then save that to 'vpnConfig'
VPNStage? vpnStage;
VpnStatus? vpnStatus;
VpnServer? vpnConfig; //Initialize variable later using a server from vpnServerList
//OpenVPN engine
late OpenVPN engine;
//Check if VPN is connected
bool get isConnected => vpnStage == VPNStage.connected;
For the sake of demonstration, we will use the first server (position 0) in vpnServerList and save that to 'vpnConfig'. Modify the code based on how to select servers in your project
void connect() async {
vpnConfig = vpnServerList[0];
const bool certificateVerify = true; //Turn it on if you use certificate
String? config;
try {
config = await OpenVPN.filteredConfig(vpnConfig?.ovpnConfiguration);
} catch (e) {
config = vpnConfig?.ovpnConfiguration;
}
if (config == null) return;
engine.connect(
config,
vpnConfig!.serverName,
certIsRequired: certificateVerify,
username: vpnConfig!.vpnUserName,
password: vpnConfig!.vpnPassword,
);
}
Disconnect VPN
engine.disconnect();
Looking for ready project, below is example project file