vpnhood / VpnHood

Undetectable Fast Portable VPN
GNU Lesser General Public License v2.1
992 stars 149 forks source link

Guide for android.permission #474

Closed SalimiHabib closed 8 months ago

SalimiHabib commented 8 months ago

i cannot find

<uses-permission android:name="android.permission.BIND_VPN_SERVICE" />

in sample android manifest in official repo and also cannot find how permission will request from user , do i can use VpnHoodApp , or it must write by my self .

it seems repo is different from published version (may be security consideration ) , am i right ? normally VpnHoodApp.Instance.Connect() must ask for permission when calling for CreatePacketCapture()

// create packet capture
var packetCapture = await Device.CreatePacketCapture();
if (packetCapture.IsMtuSupported)
    packetCapture.Mtu = TunnelDefaults.MtuWithoutFragmentation;

but it not happening and i get exception Exception("Please open the app and grant VPN permission to proceed.");

thank you

SalimiHabib commented 8 months ago

it seems vpnhood not ready for maui i will go with android

trudyhood commented 8 months ago

It is. give me time i will create the MAUI scaffold for you. You just need to implement its UI. No need for implementing permissions or so on.

SalimiHabib commented 8 months ago

i will be thankful

just want you to know : we test VpnHoodClientand VpnHoodAppseparately and in both class we ended to permission error .

we try to solve vpn permission by having a service as below , but even i get Grant permission , we get permission denied error on VpnHoodClient and VpnHoodApp ( after using connect method ).

android manifest android:permission="android.permission.BIND_VPN_SERVICE"

vpnpermission.cs

public class VpnServicePermission : Permissions.BasePlatformPermission
{
    public override (string androidPermission, bool isRuntime)[] RequiredPermissions => new List<(string androidPermission, bool isRuntime)>
    {
        (Android.Manifest.Permission.BindVpnService, false)
    }.ToArray();
}

 public class PermissionService : IPermissionService
{
    Context? context;
    public PermissionService()
    {
        context = Android.App.Application.Context;
    }
    public bool IsPermissionGranted(string permission)
    {
        var result = context.CheckSelfPermission(permission);
        return result == Permission.Granted;
    }
    public async Task  CheckAndRequestVpnServicePermission()
    {
        try
        {
            var permission = Manifest.Permission.BindVpnService;
            var status = ContextCompat.CheckSelfPermission(context, permission);
            if (status == Permission.Granted)
            {
                return  ;
            }
            else
            {  // Request permission
                var currentActivity = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity;
                ActivityCompat.RequestPermissions(currentActivity, new string[] { permission }, 1);

            }
            return;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
#if DEBUG
            throw;
#endif

#if !DEBUG
         return false;
#endif

        }
    }
}

part of connect method

 var packetCapture = await Device.CreatePacketCapture();

 _vpnHoodClient = new VpnHoodClient(packetCapture, clientId.Value, accessKeyToken, new ClientOptions());

 _vpnHoodClient.StateChanged += (_, _) => UpdateUi();

 await _vpnHoodClient.Connect();_

thank you

trudyhood commented 8 months ago

Please give me a couple of days. I am working on it.

trudyhood commented 8 months ago

Please check the samples here: https://github.com/vpnhood/VpnHood.Client.Samples

I aimed to keep it as straightforward as possible. If you require additional functionalities in the VpnHood modules, feel free to request them from us. Let VpnHood manage permissions and other complexities for you. We welcome any ideas you may have and are here to support you in running your business.