Skip to content

Commit

Permalink
PublishScripts
Browse files Browse the repository at this point in the history
  • Loading branch information
trudyhood committed Dec 23, 2020
1 parent 8341f4b commit e16b505
Show file tree
Hide file tree
Showing 15 changed files with 367 additions and 25 deletions.
5 changes: 4 additions & 1 deletion Pub/Common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ $solutionDir = Split-Path -parent $PSScriptRoot;
$msbuild = Join-Path ${Env:ProgramFiles(x86)} "Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe"
$credentials = (Get-Content "$solutionDir\..\.user\credentials.json" | Out-String | ConvertFrom-Json);
$nugetApiKey = $credentials.NugetApiKey;
$packagesRootDir = "$PSScriptRoot/bin";
$packagesDir = "$PSScriptRoot/bin";
$packagesClientDir="$packagesDir/Client";
$packagesServerDir="$packagesDir/Server";
$env:GITHUB_TOKEN = $credentials.GithubToken;

# version
$versionFile = Join-Path $PSScriptRoot "version.json"
Expand Down
11 changes: 6 additions & 5 deletions Pub/PublishApps.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ $ftp = $ftp -eq "1";
$noclean = $true;

# publish client
Remove-Item "$packagesDir/client" -ErrorAction Ignore -Recurse;
# & "$solutionDir/VpnHood.Client.App.Win/_publish.ps1";
# & "$solutionDir/VpnHood.Client.App.Android/_publish.ps1";
# & "$solutionDir/VpnHood.Client.App.Win.Setup/_publish.ps1";
Remove-Item "$packagesDir/*" -ErrorAction Ignore;
Remove-Item $packagesClientDir -ErrorAction Ignore -Recurse;
& "$solutionDir/VpnHood.Client.App.Win/_publish.ps1";
& "$solutionDir/VpnHood.Client.App.Android/_publish.ps1";
& "$solutionDir/VpnHood.Client.App.Win.Setup/_publish.ps1";

# publish server
if ($server)
{
Remove-Item "$packagesDir/server" -ErrorAction Ignore -Recurse;
Remove-Item $packagesServerDir -ErrorAction Ignore -Recurse;
& "$solutionDir/VpnHood.Server.App.Net/_publish.ps1" -ftp:$ftp;
}

Expand Down
3 changes: 2 additions & 1 deletion Pub/PublishServerApps.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ param([switch]$bump, [switch]$dis, [switch]$ftp)
$noclean = $true;

# publish server
Remove-Item "$packagesDir/server" -ErrorAction Ignore -Recurse;
Remove-Item "$packagesDir/*" -ErrorAction Ignore;
Remove-Item $packagesServerDir -ErrorAction Ignore -Recurse;
& "$solutionDir/VpnHood.Server.App.Net/_publish.ps1" -ftp:$ftp;

# upload
Expand Down
23 changes: 8 additions & 15 deletions Pub/PublishToGitHub.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $changeLog = $text -replace "# Upcoming", "# v$versionParam";
$releaseNote = $text -replace "# Upcoming", "$tag`n";
$releaseNote = $releaseNote.SubString(0, $releaseNote.IndexOf("`n# "));
# $releaseNote += "To see a list of all changes visit: [Changelog](https://github.com/vpnhood/VpnHood/blob/main/CHANGELOG.md)";
$releaseNote | Out-File -FilePath "$packagesRootDir/ReleaseNote.txt" -Encoding utf8 -Force;
$releaseNote | Out-File -FilePath "$packagesDir/ReleaseNote.txt" -Encoding utf8 -Force;

# commit and push git
$gitDir = "$solutionDir/.git";
Expand All @@ -21,20 +21,13 @@ git --git-dir=$gitDir --work-tree=$solutionDir pull;
git --git-dir=$gitDir --work-tree=$solutionDir push;

# publish using github CLI: https://github.com/github/hub
hub --git-dir=$gitDir --work-tree=$solutionDir release create `
-a $packagesRootDir/Server/VpnHoodServer.json `
-a $packagesRootDir/ReleaseNote.txt `
-F $packagesRootDir/ReleaseNote.txt `
--prerelease "$tag";
exit
hub --git-dir=$gitDir --work-tree=$solutionDir release create `
-a $releaseDir/VpnHoodClient-Android.apk `
-a $releaseDir/VpnHoodClient-win.exe `
-a $releaseDir/VpnHoodClient-win.json `
-a $releaseDir/VpnHoodClient-win.zip `
-a $releaseDir/VpnHoodServer.json `
-a $releaseDir/VpnHoodServer.zip `
hub --git-dir=$gitDir --work-tree=$solutionDir release create `
-a $packagesClientDir/VpnHoodClient-Android.apk `
-a $packagesClientDir/VpnHoodClient-win.exe `
-a $packagesClientDir/VpnHoodClient-win.json `
-a $packagesClientDir/VpnHoodClient-win.zip `
-a $packagesServerDir/VpnHoodServer.json `
-a $packagesServerDir/VpnHoodServer.zip `
-a $releaseDir/ReleaseNote.txt `
-m "title" `
-F $releaseDir/ReleaseNote.txt `
--prerelease "$tag";
Binary file modified Pub/Version.json
Binary file not shown.
2 changes: 1 addition & 1 deletion VpnHood.Client.App.Android/Properties/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="119" android:versionName="Beta" package="com.vpnhood.client.android" android:installLocation="auto">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="133" android:versionName="Beta" package="com.vpnhood.client.android" android:installLocation="auto">
<uses-sdk android:minSdkVersion="22" android:targetSdkVersion="30" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand Down
2 changes: 1 addition & 1 deletion VpnHood.Client.App.Android/_publish.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

$projectDir = $PSScriptRoot
$projectFile = (Get-ChildItem -path $projectDir -file -Filter "*.csproj").FullName;
$packagesDir = "$packagesRootDir/Client/VpnHoodClient-Android.apk";
$packageFile = "$packagesClientDir/VpnHoodClient-Android.apk";

# android
$keystore = Join-Path "$solutionDir\..\.user\" $credentials.Android.KeyStoreFile
Expand Down
2 changes: 1 addition & 1 deletion VpnHood.Client.App.Win/_publish.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $packageName = "VpnHoodClient-win";
. "$PSScriptRoot\..\Pub\PublishApp.ps1" `
$PSScriptRoot `
-withVbsLauncher `
-packagesDir "$packagesRootDir/Client" `
-packagesDir $packagesClientDir `
-packageName "$packageName" `
-updateUrl "https://github.com/vpnhood/VpnHood/releases/latest/download/$packageName.json" `
-packageDownloadUrl "https://github.com/vpnhood/VpnHood/releases/latest/download/$packageName.zip" `
9 changes: 9 additions & 0 deletions VpnHood.Server.App.Net/AppData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace VpnHood.Server.App
{
class AppData
{
public Guid ServerId {get;set;}
}
}
13 changes: 13 additions & 0 deletions VpnHood.Server.App.Net/AppSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace VpnHood.Server.App
{
class AppSettings
{
public Uri RestBaseUrl { get; set; }
public string RestAuthHeader { get; set; }
public ushort Port { get; set; } = 443;
public bool IsAnonymousTrackerEnabled { get; set; } = true;
public string SslCertificatesPassword { get; set; }
}
}
251 changes: 251 additions & 0 deletions VpnHood.Server.App.Net/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text.Json;
using System.Threading;
using VpnHood.Common;
using VpnHood.Common.Trackers;
using VpnHood.Logging;
using VpnHood.Server.AccessServers;

namespace VpnHood.Server.App
{
class Program
{
public static AppSettings AppSettings { get; set; } = new AppSettings();
public static AppData AppData { get; set; } = new AppData();
public static bool IsFileAccessServer => AppSettings.RestBaseUrl == null;
private static FileAccessServer _fileAccessServer;
private static RestAccessServer _restAccessServer;
private static AppUpdater _appUpdater;
private static VpnHoodServer _vpnHoodServer;
private static GoogleAnalyticsTracker _googleAnalytics;
private static AssemblyName AssemblyName => typeof(Program).Assembly.GetName();
private static IAccessServer AccessServer => (IAccessServer)_fileAccessServer ?? _restAccessServer;

public static string AppFolderPath { get; private set; }

static void Main(string[] args)
{
// Report current Version
// Replace dot in version to prevent anonymouizer treat it as ip.
VhLogger.Current.LogInformation($"AccessServer. Version: {AssemblyName.Version.ToString().Replace('.', ',')}, Time: {DateTime.Now}");
AppFolderPath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
if (File.Exists(Path.Combine(Path.GetDirectoryName(AppFolderPath), "publish.json")))
AppFolderPath = Path.GetDirectoryName(AppFolderPath);

// check update
_appUpdater = new AppUpdater(AppFolderPath);
_appUpdater.Updated += (sender, e) => _vpnHoodServer?.Dispose();
_appUpdater.Start();
if (_appUpdater.IsUpdated)
{
_appUpdater.LaunchUpdated();
return;
}

//Init AppData
LoadAppData();

// load AppSettings
var appSettingsFilePath = Path.Combine(AppFolderPath, "appsettings.json");
if (File.Exists(appSettingsFilePath))
AppSettings = JsonSerializer.Deserialize<AppSettings>(File.ReadAllText(appSettingsFilePath));

// track run
_googleAnalytics.IsEnabled = AppSettings.IsAnonymousTrackerEnabled;
_googleAnalytics.TrackEvent("Usage", "ServerRun");

// init AccessServer
if (AppSettings.IsAnonymousTrackerEnabled)
InitAccessServer();

// replace "/?"
for (var i = 0; i < args.Length; i++)
if (args[i] == "/?") args[i] = "-?";

// set default
if (args.Length == 0) args = new string[] { "run" };

var cmdApp = new CommandLineApplication()
{
AllowArgumentSeparator = true,
Name = AssemblyName.Name,
FullName = "VpnHood server",
MakeSuggestionsInErrorMessage = true,
};

cmdApp.HelpOption(true);
cmdApp.VersionOption("-n|--version", AssemblyName.Version.ToString());

cmdApp.Command("run", RunServer);

// show file access server options
if (IsFileAccessServer)
{
cmdApp.Command("print", PrintToken);
cmdApp.Command("gen", GenerateToken);
}

try
{
cmdApp.Execute(args);
}
catch (ArgumentException ex)
{
VhLogger.Current.LogError(ex.Message);
}
}

private static void LoadAppData()
{
// try to load
var appDataFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "VpnHood.Server", "AppData.json");
try
{
var json = File.ReadAllText(appDataFilePath);
AppData = JsonSerializer.Deserialize<AppData>(json);
}
catch { }

if (AppData == null)
AppData = new AppData();

// set serverId if not exists
if (AppData.ServerId == Guid.Empty)
{
AppData.ServerId = Guid.NewGuid();
var json = JsonSerializer.Serialize(AppData);
Directory.CreateDirectory(Path.GetDirectoryName(appDataFilePath));
File.WriteAllText(appDataFilePath, json);
}

_googleAnalytics = new GoogleAnalyticsTracker(trackId: "UA-183010362-1", anonyClientId: AppData.ServerId.ToString());
}

private static void InitAccessServer()
{
if (AppSettings.RestBaseUrl != null)
{
_restAccessServer = new RestAccessServer(AppSettings.RestBaseUrl, AppSettings.RestAuthHeader);
var authHeader = string.IsNullOrEmpty(AppSettings.RestAuthHeader) ? "<Notset>" : "*****";
VhLogger.Current.LogInformation($"Using ResetAccessServer!, BaseUri: {_restAccessServer.BaseUri}, AuthHeader: {authHeader}");
}
else
{
var accessServerFolder = Path.Combine(AppFolderPath, "access");
_fileAccessServer = new FileAccessServer(accessServerFolder, AppSettings.SslCertificatesPassword);
VhLogger.Current.LogInformation($"Using FileAccessServer!, AccessFolder: {accessServerFolder}");
}
}

private static void GenerateToken(CommandLineApplication cmdApp)
{
var localIpAddress = Util.GetLocalIpAddress();
cmdApp.Description = "Generate a token";
var nameOption = cmdApp.Option("-name", $"TokenName. Default: <NoName>", CommandOptionType.SingleValue);
var endPointOption = cmdApp.Option("-ep", $"ServerEndPoint. Default: {localIpAddress}:443", CommandOptionType.SingleValue);
var maxClientOption = cmdApp.Option("-maxClient", $"MaximumClient. Default: 2", CommandOptionType.SingleValue);

cmdApp.OnExecute(() =>
{
var accessServer = _fileAccessServer;
var serverEndPoint = endPointOption.HasValue() ? IPEndPoint.Parse(endPointOption.Value()) : IPEndPoint.Parse($"{localIpAddress}:{AppSettings.Port}");
if (serverEndPoint.Port == 0) serverEndPoint.Port = AppSettings.Port; //set defult port

var accessItem = accessServer.CreateAccessItem(
maxClientCount: maxClientOption.HasValue() ? int.Parse(maxClientOption.Value()) : 2,
serverEndPoint: serverEndPoint);

Console.WriteLine($"The following token has been generated: ");
PrintToken(accessItem.Token.TokenId);
Console.WriteLine($"Store Token Count: {accessServer.GetAllTokenIds().Length}");
return 0;
});
}

private static void PrintToken(CommandLineApplication cmdApp)
{
cmdApp.Description = "Print a token";

var tokenIdArg = cmdApp.Argument("tokenId", "tokenId (Guid) or tokenSupportId (id) to print");
cmdApp.OnExecute(() =>
{
if (!Guid.TryParse(tokenIdArg.Value, out Guid tokenId))
{
var supportId = int.Parse(tokenIdArg.Value);
try
{
tokenId = _fileAccessServer.TokenIdFromSupportId(supportId);
}
catch (KeyNotFoundException)
{
throw new KeyNotFoundException($"supportId does not exist! supportId: {supportId}");
}
}

PrintToken(tokenId);
return 0;
});
}

private static void PrintToken(Guid tokenId)
{
var accessItem = _fileAccessServer.AccessItem_Read(tokenId).Result;
if (accessItem == null) throw new KeyNotFoundException($"Token does not exist! tokenId: {tokenId}");

var access = AccessServer.GetAccess(new ClientIdentity() { TokenId = tokenId }).Result;
if (access == null) throw new KeyNotFoundException($"Token does not exist! tokenId: {tokenId}");

Console.WriteLine($"Token:");
Console.WriteLine(JsonSerializer.Serialize(accessItem.Token, new JsonSerializerOptions() { WriteIndented = true }));
Console.WriteLine($"---");
Console.WriteLine(accessItem.Token.ToAccessKey());
Console.WriteLine($"---");
Console.WriteLine();
Console.WriteLine($"Access:");
Console.WriteLine(JsonSerializer.Serialize(access, new JsonSerializerOptions() { WriteIndented = true }));
}

private static void RunServer(CommandLineApplication cmdApp)
{
cmdApp.Description = "Run the server (default command)";
var portOption = cmdApp.Option("-p|--port", "listening port. default is 443 (https)", CommandOptionType.SingleValue);
cmdApp.OnExecute(() =>
{
var portNumber = portOption.HasValue() ? int.Parse(portOption.Value()) : 443;

// check FileAccessServer
if (_fileAccessServer != null && _fileAccessServer.GetAllTokenIds().Length == 0)
{
Console.ForegroundColor = ConsoleColor.Yellow;
VhLogger.Current.LogInformation("There is no token in the store! Use the following command to create:");
VhLogger.Current.LogInformation("server gen -?");
Console.ResetColor();
return 0;
}

// run server
_vpnHoodServer = new VpnHoodServer(AccessServer, new ServerOptions()
{
TcpHostEndPoint = new IPEndPoint(IPAddress.Any, portNumber),
Tracker = _googleAnalytics
});

_vpnHoodServer.Start().Wait();
while (_vpnHoodServer.State != ServerState.Disposed)
Thread.Sleep(1000);

// launch new version
if (_appUpdater.IsUpdated)
_appUpdater.LaunchUpdated();
return 0;
});
}
}

}
7 changes: 7 additions & 0 deletions VpnHood.Server.App.Net/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"profiles": {
"VpnHood.Server.App.Net": {
"commandName": "Project"
}
}
}
Loading

0 comments on commit e16b505

Please sign in to comment.