Skip to content

Commit

Permalink
Add AppConfigs to all Apps
Browse files Browse the repository at this point in the history
  • Loading branch information
trudyhood committed Dec 11, 2024
1 parent 8629b79 commit 395e889
Show file tree
Hide file tree
Showing 22 changed files with 203 additions and 125 deletions.
2 changes: 1 addition & 1 deletion Tests/VpnHood.Test/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public static async Task<VpnHoodClient> CreateClient(Token token,
public static AppOptions CreateAppOptions()
{
var tracker = new TestTracker();
var appOptions = new AppOptions("com.vpnhood.client.test") {
var appOptions = new AppOptions("com.vpnhood.client.test", isDebugMode: true) {
StorageFolderPath = Path.Combine(WorkingPath, "AppData_" + Guid.CreateVersion7()),
SessionTimeout = TimeSpan.FromSeconds(2),
Ga4MeasurementId = null,
Expand Down
19 changes: 6 additions & 13 deletions VpnHood.Apps/VpnHood.Client.App.Android.Client.Google/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,20 @@ namespace VpnHood.Client.App.Droid.Client.Google;
public class App(IntPtr javaReference, JniHandleOwnership transfer)
: VpnHoodAndroidApp(javaReference, transfer)
{
public static int? SpaDefaultPort => IsDebugMode ? 9581 : 9580;
public static bool SpaListenToAllIps => IsDebugMode;

protected override AppOptions CreateAppOptions()
{
var appConfigs = AppConfigs.Load();

var resources = DefaultAppResource.Resources;
resources.Strings.AppName = IsDebugMode ? "VpnHOOD! CLIENT (DEBUG)" : "VpnHood! CLIENT";
resources.Strings.AppName = appConfigs.AppName;

return new AppOptions(PackageName!, IsDebugMode) {
return new AppOptions(PackageName!, AppConfigs.IsDebugMode) {
StorageFolderPath = AppOptions.BuildStorageFolderPath(appId: "VpnHood"), // for compatibility with old versions
Resource = resources,
AccessKeys = IsDebugMode ? [ClientOptions.SampleAccessKey] : [],
UpdateInfoUrl = new Uri("https://github.com/vpnhood/VpnHood/releases/latest/download/VpnHoodClient-android.json"),
AccessKeys = appConfigs.DefaultAccessKey != null ? [appConfigs.DefaultAccessKey] : [],
UpdateInfoUrl = appConfigs.UpdateInfoUrl,
IsAddAccessKeySupported = true,
UpdaterProvider = new GooglePlayAppUpdaterProvider()
};
}

#if DEBUG
public static bool IsDebugMode => true;
#else
public override bool IsDebugMode => false;
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using VpnHood.Client.App.Utils;

// ReSharper disable StringLiteralTypo
// ReSharper disable CommentTypo
namespace VpnHood.Client.App.Droid.Client.Google;

internal class AppConfigs : AppConfigsBase<AppConfigs>
{
public string AppName { get; init; } = IsDebugMode ? "VpnHOOD! CLIENT (DEBUG)" : "VpnHood! CLIENT";
public Uri? UpdateInfoUrl { get; init; } = new("https://github.com/vpnhood/VpnHood/releases/latest/download/VpnHoodClient-android.json");
public int? SpaDefaultPort { get; init; } = IsDebugMode ? 9581 : 9580;
public bool SpaListenToAllIps { get; init; } = IsDebugMode;

// SampleAccessKey is a test access key, you should replace it with your own access key.
// It is limited and can not be used in production.
public string? DefaultAccessKey { get; init; } = IsDebugMode ? ClientOptions.SampleAccessKey : null;

public static AppConfigs Load()
{
var appConfigs = new AppConfigs();
appConfigs.Merge("AppSettings");
appConfigs.Merge("AppSettings_Environment");
return appConfigs;
}

#if DEBUG
public static bool IsDebugMode => true;
#else
public static bool IsDebugMode => false;
#endif
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Android.Content;
using Android.Service.QuickSettings;
using VpnHood.Client.App.Droid.Client.Google.Properties;
using VpnHood.Client.App.Droid.Common.Activities;
using VpnHood.Client.App.Droid.Common.Constants;

Expand Down Expand Up @@ -31,8 +30,8 @@ public class MainActivity : AndroidAppMainActivity
protected override AndroidAppMainActivityHandler CreateMainActivityHandler()
{
return new AndroidAppWebViewMainActivityHandler(this, new AndroidMainActivityWebViewOptions {
SpaDefaultPort = App.SpaDefaultPort,
SpaListenToAllIps = App.SpaListenToAllIps,
SpaDefaultPort = AppConfigs.Instance.SpaDefaultPort,
SpaListenToAllIps = AppConfigs.Instance.SpaListenToAllIps,
AccessKeySchemes = [AccessKeyScheme1, AccessKeyScheme2],
AccessKeyMimes = [AccessKeyMime1, AccessKeyMime2, AccessKeyMime3]
});
Expand Down

This file was deleted.

24 changes: 7 additions & 17 deletions VpnHood.Apps/VpnHood.Client.App.Android.Client.Web/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,19 @@ namespace VpnHood.Client.App.Droid.Client.Web;
public class App(IntPtr javaReference, JniHandleOwnership transfer)
: VpnHoodAndroidApp(javaReference, transfer)
{
public static int? SpaDefaultPort => IsDebugMode ? 9581 : 9580;
public static bool SpaListenToAllIps => IsDebugMode;

protected override AppOptions CreateAppOptions()
{
var appConfigs = AppConfigs.Load();

var resources = DefaultAppResource.Resources;
resources.Strings.AppName = IsDebugMode ? "VpnHOOD! CLIENT (DEBUG)" : "VpnHood! CLIENT";
resources.Strings.AppName = appConfigs.AppName;

return new AppOptions(PackageName!, IsDebugMode) {
return new AppOptions(PackageName!, AppConfigs.IsDebugMode) {
StorageFolderPath = AppOptions.BuildStorageFolderPath(appId: "VpnHood"), // for compatibility with old VpnHood app versions to keep tokens
Resource = resources,
AccessKeys = IsDebugMode ? [ClientOptions.SampleAccessKey] : [],
UpdateInfoUrl = new Uri("https://github.com/vpnhood/VpnHood/releases/latest/download/VpnHoodClient-android-web.json"),
IsAddAccessKeySupported = true,
LogAnonymous = !IsDebugMode
AccessKeys = appConfigs.DefaultAccessKey != null ? [appConfigs.DefaultAccessKey] : [],
UpdateInfoUrl = appConfigs.UpdateInfoUrl,
IsAddAccessKeySupported = true
};
}


#if DEBUG
public static bool IsDebugMode => true;
#else
public static bool IsDebugMode => false;
#endif

}
31 changes: 31 additions & 0 deletions VpnHood.Apps/VpnHood.Client.App.Android.Client.Web/AppConfigs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using VpnHood.Client.App.Utils;

// ReSharper disable StringLiteralTypo
// ReSharper disable CommentTypo
namespace VpnHood.Client.App.Droid.Client.Web;

internal class AppConfigs : AppConfigsBase<AppConfigs>
{
public string AppName { get; init; } = IsDebugMode ? "VpnHOOD! CLIENT (DEBUG)" : "VpnHood! CLIENT";
public Uri? UpdateInfoUrl { get; init; } = new ("https://github.com/vpnhood/VpnHood/releases/latest/download/VpnHoodClient-android-web.json");
public int? SpaDefaultPort { get; init; } = IsDebugMode ? 9581 : 9580;
public bool SpaListenToAllIps { get; init; } = IsDebugMode;

// SampleAccessKey is a test access key, you should replace it with your own access key.
// It is limited and can not be used in production.
public string? DefaultAccessKey { get; init; } = IsDebugMode ? ClientOptions.SampleAccessKey : null;

public static AppConfigs Load()
{
var appConfigs = new AppConfigs();
appConfigs.Merge("AppSettings");
appConfigs.Merge("AppSettings_Environment");
return appConfigs;
}

#if DEBUG
public static bool IsDebugMode => true;
#else
public static bool IsDebugMode => false;
#endif
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Android.Content;
using Android.Service.QuickSettings;
using VpnHood.Client.App.Droid.Client.Web.Properties;
using VpnHood.Client.App.Droid.Common.Activities;
using VpnHood.Client.App.Droid.Common.Constants;

Expand Down Expand Up @@ -33,8 +32,8 @@ public class MainActivity : AndroidAppMainActivity
protected override AndroidAppMainActivityHandler CreateMainActivityHandler()
{
return new AndroidAppWebViewMainActivityHandler(this, new AndroidMainActivityWebViewOptions {
SpaDefaultPort = App.SpaDefaultPort,
SpaListenToAllIps = App.SpaListenToAllIps,
SpaDefaultPort = AppConfigs.Instance.SpaDefaultPort,
SpaListenToAllIps = AppConfigs.Instance.SpaListenToAllIps,
AccessKeySchemes = [AccessKeyScheme1, AccessKeyScheme2],
AccessKeyMimes = [AccessKeyMime1, AccessKeyMime2, AccessKeyMime3]
});
Expand Down
19 changes: 6 additions & 13 deletions VpnHood.Apps/VpnHood.Client.App.Android.Connect.Web/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,27 @@ namespace VpnHood.Client.App.Droid.Connect.Web;
public class App(IntPtr javaReference, JniHandleOwnership transfer)
: VpnHoodAndroidApp(javaReference, transfer)
{
public static int? SpaDefaultPort => IsDebugMode ? 9581 : 9580;
public static bool SpaListenToAllIps => IsDebugMode; // if true it will cause crash in network change

protected override AppOptions CreateAppOptions()
{
// load app settings and resources
var appConfigs = AppConfigs.Load();

var resources = DefaultAppResource.Resources;
resources.Strings.AppName = "VpnHood! CONNECT";
resources.Strings.AppName = appConfigs.AppName;
resources.Colors.NavigationBarColor = Color.FromArgb(21, 14, 61);
resources.Colors.WindowBackgroundColor = Color.FromArgb(21, 14, 61);
resources.Colors.ProgressBarColor = Color.FromArgb(231, 180, 129);

var appConfigs = AppConfigs.Load();
return new AppOptions(appId: PackageName!, IsDebugMode) {
return new AppOptions(appId: PackageName!, AppConfigs.IsDebugMode) {
DeviceId = AndroidUtil.GetDeviceId(this), //this will be hashed using AppId
AccessKeys = [appConfigs.DefaultAccessKey],
Resource = resources,
UiName = "VpnHoodConnect",
IsAddAccessKeySupported = false,
UpdateInfoUrl = new Uri("https://github.com/vpnhood/VpnHood.Client.App.Connect/releases/latest/download/VpnHoodConnect-android-web.json"),
AllowEndPointTracker = true,
UpdateInfoUrl = appConfigs.UpdateInfoUrl,
AllowEndPointTracker = appConfigs.AllowEndPointTracker,
Ga4MeasurementId = appConfigs.Ga4MeasurementId
};
}

#if DEBUG
public static bool IsDebugMode => true;
#else
public static bool IsDebugMode => false;
#endif
}
23 changes: 13 additions & 10 deletions VpnHood.Apps/VpnHood.Client.App.Android.Connect.Web/AppConfigs.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using VpnHood.Common.Utils;
using VpnHood.Client.App.Utils;

// ReSharper disable StringLiteralTypo
// ReSharper disable CommentTypo

namespace VpnHood.Client.App.Droid.Connect.Web;

internal class AppConfigs : Singleton<AppConfigs>
internal class AppConfigs : AppConfigsBase<AppConfigs>
{
public string AppName { get; init; } = IsDebugMode ? "VpnHOOD! CONNECT (DEBUG)" : "VpnHood! CONNECT";
public Uri? UpdateInfoUrl { get; init; } = new("https://github.com/vpnhood/VpnHood.Client.App.Connect/releases/latest/download/VpnHoodConnect-Android-web.json");
public int? SpaDefaultPort { get; init; } = IsDebugMode ? 9571 : 9570;
public bool SpaListenToAllIps { get; init; } = IsDebugMode;
public bool AllowEndPointTracker { get; init; } = true;
public string? Ga4MeasurementId { get; init; }
// SampleAccessKey is a test access key, you should replace it with your own access key.
// It is limited and can not be used in production.
public string DefaultAccessKey { get; init; } = ClientOptions.SampleAccessKey;
public string? Ga4MeasurementId { get; init; }

public static AppConfigs Load()
{
Expand All @@ -20,10 +24,9 @@ public static AppConfigs Load()
return appConfigs;
}

private void Merge(string configName)
{
var json = VhUtil.GetAssemblyMetadata(typeof(AppConfigs).Assembly, configName, "");
if (!string.IsNullOrEmpty(json))
JsonSerializerExt.PopulateObject(this, json);
}
#if DEBUG
public static bool IsDebugMode => true;
#else
public static bool IsDebugMode => false;
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class MainActivity : AndroidAppMainActivity
protected override AndroidAppMainActivityHandler CreateMainActivityHandler()
{
return new AndroidAppWebViewMainActivityHandler(this, new AndroidMainActivityWebViewOptions {
SpaDefaultPort = App.SpaDefaultPort,
SpaListenToAllIps = App.SpaListenToAllIps
SpaDefaultPort = AppConfigs.Instance.SpaDefaultPort,
SpaListenToAllIps = AppConfigs.Instance.SpaListenToAllIps
});
}
}
19 changes: 6 additions & 13 deletions VpnHood.Apps/VpnHood.Client.App.Android.Connect/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,28 @@ public class App(IntPtr javaReference, JniHandleOwnership transfer)
: VpnHoodAndroidApp(javaReference, transfer)
{
private FirebaseAnalytics? _analytics;
public static int? SpaDefaultPort => IsDebugMode ? 9581 : 9580;
public static bool SpaListenToAllIps => IsDebugMode;

protected override AppOptions CreateAppOptions()
{
var appConfigs = AppConfigs.Load();

// initialize Firebase services
try { _analytics = FirebaseAnalytics.GetInstance(this); } catch { /* ignored*/ }
try { FirebaseCrashlytics.Instance.SetCrashlyticsCollectionEnabled(Java.Lang.Boolean.True); } catch { /* ignored */ }

// load app settings and resources
var storageFolderPath = AppOptions.BuildStorageFolderPath(PackageName!);
var resources = DefaultAppResource.Resources;
resources.Strings.AppName = IsDebugMode ? "VpnHOOD! CONNECT (DEBUG)" : "VpnHood! CONNECT";
resources.Strings.AppName = appConfigs.AppName;
resources.Colors.NavigationBarColor = Color.FromArgb(21, 14, 61);
resources.Colors.WindowBackgroundColor = Color.FromArgb(21, 14, 61);
resources.Colors.ProgressBarColor = Color.FromArgb(231, 180, 129);

var appConfigs = AppConfigs.Load();
return new AppOptions(appId: PackageName!, IsDebugMode) {
return new AppOptions(appId: PackageName!, AppConfigs.IsDebugMode) {
StorageFolderPath = storageFolderPath,
AccessKeys = [appConfigs.DefaultAccessKey],
Resource = resources,
UpdateInfoUrl = new Uri("https://github.com/vpnhood/VpnHood/releases/latest/download/VpnHoodConnect-android.json"),
UpdateInfoUrl = appConfigs.UpdateInfoUrl,
UiName = "VpnHoodConnect",
IsAddAccessKeySupported = false,
UpdaterProvider = new GooglePlayAppUpdaterProvider(),
Expand Down Expand Up @@ -112,7 +111,7 @@ private static AppAdProviderItem[] CreateAppAdProviderItems(AppConfigs appConfig
try {
var authenticationExternalProvider = new GooglePlayAuthenticationProvider(appConfigs.GoogleSignInClientId);
var authenticationProvider = new StoreAuthenticationProvider(storageFolderPath, new Uri(appConfigs.StoreBaseUri),
appConfigs.StoreAppId, authenticationExternalProvider, ignoreSslVerification: IsDebugMode);
appConfigs.StoreAppId, authenticationExternalProvider, ignoreSslVerification: appConfigs.StoreIgnoreSslVerification);
var googlePlayBillingProvider = new GooglePlayBillingProvider(authenticationProvider);
var accountProvider = new StoreAccountProvider(authenticationProvider, googlePlayBillingProvider, appConfigs.StoreAppId);
return accountProvider;
Expand All @@ -122,10 +121,4 @@ private static AppAdProviderItem[] CreateAppAdProviderItems(AppConfigs appConfig
return null;
}
}

#if DEBUG
public static bool IsDebugMode => true;
#else
public static bool IsDebugMode => false;
#endif
}
26 changes: 15 additions & 11 deletions VpnHood.Apps/VpnHood.Client.App.Android.Connect/AppConfigs.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using VpnHood.Common.Utils;
using VpnHood.Client.App.Utils;

// ReSharper disable StringLiteralTypo
// ReSharper disable CommentTypo

namespace VpnHood.Client.App.Android.Client.Google;

internal class AppConfigs : Singleton<AppConfigs>
internal class AppConfigs : AppConfigsBase<AppConfigs>
{
public string AppName { get; init; } = IsDebugMode ? "VpnHOOD! CONNECT (DEBUG)" : "VpnHood! CONNECT";
public Uri? UpdateInfoUrl { get; init; } = new ("https://github.com/vpnhood/VpnHood.Client.App.Connect/releases/latest/download/VpnHoodConnect-Android.json");
public int? SpaDefaultPort { get; init; }= IsDebugMode ? 9571 : 9570;
public bool SpaListenToAllIps { get; init; } = IsDebugMode;

// This is a test access key, you should replace it with your own access key.
// It is limited and can not be used in production.
public string DefaultAccessKey { get; init; } = ClientOptions.SampleAccessKey;
Expand All @@ -21,6 +25,7 @@ internal class AppConfigs : Singleton<AppConfigs>
public Guid StoreAppId { get; init; } =
Guid.Parse("00000000-0000-0000-0000-000000000000"); //YOUR_VPNHOOD_STORE_APP_ID

public bool StoreIgnoreSslVerification { get; init; } = IsDebugMode;

// AdMob
// Default value is AdMob test AdUnit id, References: https://developers.google.com/admob/android/test-ads
Expand All @@ -38,8 +43,7 @@ internal class AppConfigs : Singleton<AppConfigs>
// Inmobi
public string InmobiAccountId { get; init; } = "000000000000000000000000"; //YOUR_INMMOBI_ACCOUNT_ID
public string InmobiPlacementId { get; init; } = "000000000000"; //YOUR_INMOBI_PLACEMENT_ID
public bool InmobiIsDebugMode { get; init; } = App.IsDebugMode;

public bool InmobiIsDebugMode { get; init; } = IsDebugMode;

public static AppConfigs Load()
{
Expand All @@ -49,10 +53,10 @@ public static AppConfigs Load()
return appConfigs;
}

private void Merge(string configName)
{
var json = VhUtil.GetAssemblyMetadata(typeof(AppConfigs).Assembly, configName, "");
if (!string.IsNullOrEmpty(json))
JsonSerializerExt.PopulateObject(this, json);
}
#if DEBUG
public static bool IsDebugMode => true;
#else
public static bool IsDebugMode => false;
#endif

}
Loading

0 comments on commit 395e889

Please sign in to comment.