Git Product home page Git Product logo

wireguard_flutter's Introduction

wireguard_flutter

A flutter plugin to setup and control VPN connection via Wireguard tunnel.

Contributing to wireguard_flutter

Thank you for your interest in contributing to wireguard_flutter! We appreciate your help in making this project better.

Before you start contributing, please take a moment to read the following guidelines.

How to Contribute

  1. Fork the repository to your GitHub account.
  2. Clone the forked repository to your local machine.
  3. Create a new branch for your contribution:
    git checkout -b feature/your-feature-name
  4. Make your changes and ensure that the code follows the project's coding standards.
  5. Commit your changes with a descriptive commit message:
    git commit -m "Add your descriptive message here"
  6. Push your changes to your forked repository:
    git push origin feature/your-feature-name
  7. Open a pull request in the original repository and provide a detailed description of your changes.

Usage

To use this plugin, add wireguard_flutter or visit Flutter Tutorial.

flutter pub add wireguard_flutter

Initialize

Initialize a wireguard instance with a valid name using initialize:

final wireguard = WireGuardFlutter.instance;

// initialize the interface
await wireguard.initialize(interfaceName: 'wg0');

and declare the .conf data:

const String conf = '''[Interface]
PrivateKey = 0IZmHsxiNQ54TsUs0EQ71JNsa5f70zVf1LmDvON1CXc=
Address = 10.8.0.4/32
DNS = 1.1.1.1


[Peer]
PublicKey = 6uZg6T0J1bHuEmdqPx8OmxQ2ebBJ8TnVpnCdV8jHliQ=
PresharedKey = As6JiXcYcqwjSHxSOrmQT13uGVlBG90uXZWmtaezZVs=
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 0
Endpoint = 38.180.13.85:51820''';

For more info on the configuration data, see the documentation with examples.

Connect

After initializing, connect using startVpn:

await wireguard.startVpn(
  serverAddress: address, // the server address (e.g 'demo.wireguard.com:51820')
  wgQuickConfig: conf, // the quick-config file
  providerBundleIdentifier: 'com.example', // your app identifier
);

Disconnect

After connecting, disconnect using stopVpn:

await wireguard.stopVpn();

Stage

Listen to stage change using vpnStageSnapshot:

wireguard.vpnStageSnapshot.listen((event) {
  debugPrint("status changed $event");
});

Or get the current stage using getStage:

final stage = await wireguard.stage();

The available stages are:

Code Description
connecting The interface is connecting
connected The interface is connected
disconnecting The interface is disconnecting
disconnected The interface is disconnected
waitingConnection Waiting for a user interaction
authenticating Authenticating with the server
reconnect Reconnecting the the interface
noConnection Any connection has not been made
preparing Preparing to connect
denied The connection has been denied by the system, usually by refused permissions
exiting Exiting the interface

Supported Platforms

Android iOS macOS Windows Linux
Version SDK 21+ 15.0+ 12+ 7+ Any

Windows

On Windows, the app must be run as administrator to be able to create and manipulate the tunnel. To debug the app, run flutter run from an elevated command prompt. To run the app normally, the system will request your app to be run as administrator. No code changes or external dependencies are required.

Linux

Install dependencies

The required dependencies need to be installed: wireguard and wireguard-tools.

On Ubuntu/Debian, use the following command to install the dependencies:

sudo apt install wireguard wireguard-tools openresolv

For other Linux distros, see this.

Note

If openresolv is not installed in the system, configuration files with a DNS provided may not connect. See this issue for more information.

Initializing

When wireguard.initialize is called, the application will request your user password ([sudo] password for <user>:). This is necessary because wireguard must run as a root to be able to create AND manipulate the tunnels. This is true for either debug and release modes or a distributed executable.

Caution

Do not run the app in root mode (e.g sudo ./executable, sudo flutter run), otherwise the connection will not be established.

FAQ & Troubleshooting

Linux error resolvconf: command not found

On Linux, you may receive the error resolvconf: command not found. This is because wireguard tried to adjust the nameserver. Make sure to install openresolv or not provide the "DNS" field.


"WireGuard" is a registered trademark of Jason A. Donenfeld.

Fork from mysteriumnetwork tunnel.

Many Thanks for Bruno D'Luka for help me.

wireguard_flutter's People

Contributors

bdlukaa avatar caqil avatar adarshaketh avatar imgbotapp avatar

Stargazers

Nikhil M Jain avatar bellukchips avatar  avatar  avatar A. Noval Adip avatar Atiq Samtia avatar denuitt avatar W avatar Scott Tiger avatar Wachu985 avatar Shujaat Ali Khan avatar pythonwood avatar  avatar H3mnz avatar Alex avatar  avatar Andrey Kapitonov avatar  avatar Alex Ibraimov avatar  avatar

Watchers

 avatar

wireguard_flutter's Issues

Vpn not connecting in iOS

Whenever i click on connect its shows connecting then shows disconnected it not connecting can you help me or give me any demo of config or address to make me easy to test it
Screenshot 2024-04-14 at 10 39 39 PM

iOS: "AppName" must be updated by the developer before "VpnName" can be connected

I built an app for iOS, and the build and launch process goes smoothly. However, the VPN does not connect because in the iPhone settings, next to the VPN profile, it says 'Update Required' and 'VPN: "AppName" must be updated by the developer before "VpnName" can be connected.' What exactly needs to be updated and how can I fix this problem?

6e850544-6d37-4ac1-ab09-1552f725460f

Can't connect with ios

Hello and thank you for this package.

I am having trouble with iOS connection to the VPN. Android work perfect. I followed your example, updated the bundle, and checked the VPN configuration. I also reviewed the info.plist and entitlements.

However, it still does not work. In the output log, I see it connecting and immediately disconnecting. It seems to me that the code in PacketTunnelProvider.swift, in the network extension, is not being called at all.

MacOS support?

Does this work on MacOS?

I have been trying to get the project in the example directory working for some time and I have had no luck. I have renamed and double checked the bundle identifiers throughout the project and entitlements and they seem to be fine. I changed the path for the external build tool to my own downloaded copy of wireguard-apple sources vs the hardcoded path. and even tried recreating the WGExtension as a system extension vs an app extension after reading that could be an issue. It is still not working.

I can build the app and initialize "successfully" but I am just getting logs like this every time I try and connect where it does nothing and there is no error output. I am able to connect to the wireguard server using the macOS client without issue.

flutter: status changed VpnStage.disconnected
2024-03-14 14:48:34.798 example_flutter[49429:4784548] Starting the tunnel
flutter: status changed VpnStage.connecting
flutter: status changed VpnStage.connecting
flutter: status changed VpnStage.disconnected
flutter: status changed VpnStage.disconnected

I added an override to the init of the PacketTunnelProvider to log when it is initialized and it never prints so I am thinking that is my issue but I'm not sure how to deal with that.

I am very new to macOS/iOS development so if I am missing some basic setup steps that are not mentioned in the readme or wireguard-apple repo, please let me know

Android VpnStageSnapshot listen is not working

Hello Developers

On my Android device wireGuardFlutter.vpnStageSnapshot.listen is not working fine it's changing VPpnStage Connected after Disconnected and also this code is not working always showing VpnStage.disconnected

       wireGuardFlutter.stage().then((po) {
           debugPrint("stage $po");
        }); 

Please Help Me

Flet support

Great package, can you add flet support to this? Think would be great add-on for python. People could VPN into various places, run AI models, etc.

https://flet.dev/blog/

Feb 14 post:

void ensureInitialized();
Widget createControl(CreateControlArgs args);

All you'd have to do is implement and export these with examples in that blog for audio and video GitHub.

no connection

I/flutter (12703): initialize success my_wg_vpn
V/AudioManager(12703): querySoundEffectsEnabled...
I/System.out(12703): VPN is not active
E/NVPN (12703): Connect - Can't connect to tunnel: com.wireguard.config.BadConfigException
E/NVPN (12703): com.wireguard.config.BadConfigException
E/NVPN (12703): at com.wireguard.config.Interface.parse(Interface.java:100)
E/NVPN (12703): at com.wireguard.config.Config$Builder.parseInterface(Config.java:210)
E/NVPN (12703): at com.wireguard.config.Config.parse(Config.java:111)
E/NVPN (12703): at com.wireguard.config.Config.parse(Config.java:52)
E/NVPN (12703): at billion.group.wireguard_flutter.WireguardFlutterPlugin$connect$1.invokeSuspend(WireguardFlutterPlugin.kt:240)
E/NVPN (12703): at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
E/NVPN (12703): at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
E/NVPN (12703): at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115)
E/NVPN (12703): at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103)
E/NVPN (12703): at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
E/NVPN (12703): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
E/NVPN (12703): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
E/NVPN (12703): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
I/flutter (12703): status changed VpnStage.preparing
I/flutter (12703): failed to start PlatformException(null, null, null, null)
I/flutter (12703): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:651:7)
I/flutter (12703): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
I/flutter (12703):
I/flutter (12703): #2 _MyAppState.startVpn (package:coolapp/main.dart:56:7)
I/flutter (12703):
I/flutter (12703):
I/flutter (12703): getting stage
V/AudioManager(12703): querySoundEffectsEnabled...
I/flutter (12703): stage: VpnStage.preparing
W/InputMethodManager(12703): startInputReason = 8
W/HwRemoteInputMethodManager(12703): isCasting false because IHwDistributedWindowManager is invalid.
E/RtgSchedManager(12703): endActivityTransaction: margin state not match
E/RtgSchedManager(12703): endActivityTransaction: margin state not match
E/RtgSchedManager(12703): endActivityTransaction: margin state not match
W/libEGL (12703): EGLNativeWindowType 0x7163b0a010 disconnect failed
W/libEGL (12703): EGLNativeWindowType 0x7225b4d610 disconnect failed
I/ActivityThread(12703): Handle window ActivityRecord{ef74758 token=android.os.BinderProxy@95d5b5e {com.example.coolapp/com.example.coolapp.MainActivity}} visibility: false
E/RtgSchedManager(12703): endActivityTransaction: margin state not match
D/DecorView(12703): showOrHideHighlightView: hasFocus=false; winMode=1; isMrgNull=true
I/RmeSchedManager(12703): init Rme, version is: v1.0
D/ZrHung.AppEyeUiProbe(12703): not watching, wait.

stucks on preparing, it says bad config but works on my linux computer with same config

Windows vpn is not connecting

I try to debug in windows and its successfully debug then i press connect button its throw this error please help me.

Starting service with command line: D:\Codecaneone Projects\wireguard\build\windows\x64\runner\Debug\wireguard_svc.exe -service -config-file="C:\Users\cheta\AppData\Local\Temp\wg_12FA.tmp.conf"
flutter: status is changed VpnStage.preparing
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Missing extension byte (at offset 1)
#0      _Utf8Decoder.convertSingle (dart:convert-patch/convert_patch.dart:1748:7)
#1      Utf8Decoder.convert (dart:convert/utf.dart:356:42)
#2      StandardMessageCodec.readValueOfType (package:flutter/src/services/message_codecs.dart:502:29)
#3      StandardMessageCodec.readValue (package:flutter/src/services/message_codecs.dart:477:12)
#4      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:646:44)
#5      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:322:18)

Cant connect VPN in IOS

I am using the example code you provided but still can't run it on IOS, I get an error on the profile saying "Update Required".
I have a developer account and I created new bundle IDs for the app. The original WireGuardKit IOS package runs fine with the same bundle ID but this one doesnt.
IMG_9785

VPN status incorrect and disconnect not working in certain scenarios

On Android, when you connect to the VPN and then close the app, if the VPN disconnects in the background (imagine you have a periodic function), the VPN does not disconnect. However, when you reopen the app, the VPN status in the Android settings shows as connected, but the app shows the VPN as disconnected. You cannot do anything to disconnect it. Please fix this. Thank you.

add wstunnel to your flutter app

there is a open source project in here
https://github.com/erebe/wstunnel
if you can add Wstunnel to you project it will be the perfect thing because in some countries Wireguard is ban
in Wstunnel your client endpoint will be 127.0.0.1:port and it will send TLS data through Websocket tunnel and it will be untraceable
please add this to your project i beg youuu

My app keep crashing when about to launch

E/AndroidRuntime( 8674): FATAL EXCEPTION: DefaultDispatcher-worker-1
E/AndroidRuntime( 8674): Process: com.radiantmoney.app, PID: 8674
E/AndroidRuntime( 8674): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
E/AndroidRuntime( 8674): at android.net.VpnService.prepare(VpnService.java:206)
E/AndroidRuntime( 8674): at billion.group.wireguard_flutter.WireguardFlutterPlugin.checkPermission(WireguardFlutterPlugin.kt:275)
E/AndroidRuntime( 8674): at billion.group.wireguard_flutter.WireguardFlutterPlugin.access$checkPermission(WireguardFlutterPlugin.kt:39)
E/AndroidRuntime( 8674): at billion.group.wireguard_flutter.WireguardFlutterPlugin$setupTunnel$1.invokeSuspend(WireguardFlutterPlugin.kt:269)
E/AndroidRuntime( 8674): at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
E/AndroidRuntime( 8674): at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
E/AndroidRuntime( 8674): at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115)
E/AndroidRuntime( 8674): at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103)
E/AndroidRuntime( 8674): at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
E/AndroidRuntime( 8674): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
E/AndroidRuntime( 8674): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
E/AndroidRuntime( 8674): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
E/AndroidRuntime( 8674): Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@96049dd, Dispatchers.IO]
I/ExceptionHandle( 8674): at android.net.VpnService.prepare(VpnService.java:206)
I/ExceptionHandle( 8674): at billion.group.wireguard_flutter.WireguardFlutterPlugin.checkPermission(WireguardFlutterPlugin.kt:275)
I/ExceptionHandle( 8674): at billion.group.wireguard_flutter.WireguardFlutterPlugin.access$checkPermission(WireguardFlutterPlugin.kt:39)
I/ExceptionHandle( 8674): at billion.group.wireguard_flutter.WireguardFlutterPlugin$setupTunnel$1.invokeSuspend(WireguardFlutterPlugin.kt:269)
I/ExceptionHandle( 8674): at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
I/ExceptionHandle( 8674): at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
I/ExceptionHandle( 8674): at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115)
I/ExceptionHandle( 8674): at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103)
I/ExceptionHandle( 8674): at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
I/ExceptionHandle( 8674): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
I/ExceptionHandle( 8674): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
I/ExceptionHandle( 8674): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
I/Process ( 8674): Sending signal. PID: 8674 SIG: 9
Lost connection to device.

feature request: status

Is it possible to get interface status like, last handshake, data transfer etc, just like how wg-quick shows?

Get rx and tx

how to get rx and tx in in this package i need to see data send and recive

Context.getPackageName()' on a null object reference - Possibility to run from WorkManager?

Hi

Thanks for a great package :)

Would it be possible to create a variable which could be set so that the vpn could be triggered from the background.
This is useful for "snoozing" the vpn for a user defined time.
Right now it seems that the package is using Context to get package name which is not accessible from an isolate.

Heres the error log:

E/MainActivity( 7910): com.wireguard.android.util.RootShell$RootShellException
E/MainActivity( 7910): 	at com.wireguard.android.util.RootShell.start(RootShell.java:135)
E/MainActivity( 7910): 	at app.wachu.wireguard_vpn.WireguardVpnPlugin.createBackend(WireguardVpnPlugin.kt:145)
E/MainActivity( 7910): 	at app.wachu.wireguard_vpn.WireguardVpnPlugin.access$createBackend(WireguardVpnPlugin.kt:39)
E/MainActivity( 7910): 	at app.wachu.wireguard_vpn.WireguardVpnPlugin$onAttachedToEngine$1.invokeSuspend(WireguardVpnPlugin.kt:104)
E/MainActivity( 7910): 	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
E/MainActivity( 7910): 	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
E/MainActivity( 7910): 	at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115)
E/MainActivity( 7910): 	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103)
E/MainActivity( 7910): 	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
E/MainActivity( 7910): 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
E/MainActivity( 7910): 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
E/MainActivity( 7910): 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
E/MainActivity( 7910): Entre 1
E/MainActivity( 7910): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
E/MainActivity( 7910): 	at android.net.VpnService.prepare(VpnService.java:208)
E/MainActivity( 7910): 	at app.wachu.wireguard_vpn.WireguardVpnPlugin.checkPermission(WireguardVpnPlugin.kt:131)
E/MainActivity( 7910): 	at app.wachu.wireguard_vpn.WireguardVpnPlugin.access$checkPermission(WireguardVpnPlugin.kt:39)
E/MainActivity( 7910): 	at app.wachu.wireguard_vpn.WireguardVpnPlugin$onAttachedToEngine$1.invokeSuspend(WireguardVpnPlugin.kt:107)
E/MainActivity( 7910): 	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
E/MainActivity( 7910): 	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
E/MainActivity( 7910): 	at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115)
E/MainActivity( 7910): 	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103)
E/MainActivity( 7910): 	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
E/MainActivity( 7910): 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
E/MainActivity( 7910): 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
E/MainActivity( 7910): 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.