Compare commits

...

8 Commits

12 changed files with 235 additions and 66 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.NetworkInformation;
using System.Reflection;
@ -24,7 +25,8 @@ public partial class MainWindow : Window
private Button restartToggleBtn;
private bool ConnectedToVPN;
private bool IsHidden;
private bool RestartMode = false;
private bool RestartMode;
private List<NotificationThreadMap> CurrentNotifications;
public static readonly string MelonIconImg = "MiniMelonVPNIcon.png";
public static readonly string MelonOnlineImg = "MiniMelonVPNOnline.png";
@ -33,6 +35,8 @@ public partial class MainWindow : Window
Build();
CurrentNotifications = new List<NotificationThreadMap>();
Title = "Melon VPN";
UpdateIcon(MelonIconImg);
SetSizeRequest(300, 300);
@ -280,25 +284,26 @@ public partial class MainWindow : Window
{
string msg = (current ? "Connected to" : "Disconnected from") + " the VPN";
string icon = "/usr/lib/melon-vpn/MiniMelonVPN" + (current ? "Online" : "Icon") + ".png";
Notification pop = new Notification("Melon VPN", msg, icon);
pop.Show();
CloseNotificationAfterWait(pop, 1000);
Notification notification = new Notification("Melon VPN", msg, icon);
notification.Show();
CloseNotificationAfterWait(notification, 1000);
}
}
void CloseNotificationAfterWait(Notification pop, int timeout)
void CloseNotificationAfterWait(Notification notification, int timeout)
{
// Close the notification after a wait
// so it doesn't hang around
Thread wait = new Thread(() =>
Thread closeTimer = new Thread(() =>
{
Thread.Sleep(timeout);
pop.Close();
notification.Close();
})
{
IsBackground = true
};
wait.Start();
CurrentNotifications.Add(new NotificationThreadMap(notification, closeTimer));
closeTimer.Start();
}
void SendToTray()
@ -309,18 +314,7 @@ public partial class MainWindow : Window
Iconify();
SkipPagerHint = true;
SkipTaskbarHint = true;
// Changing the visible property needs to be
// delayed for the window to start minimizing
Thread thread = new Thread(() =>
{
Thread.Sleep(100);
Visible = false;
})
{
IsBackground = true
};
thread.Start();
UpdateTrayMenu();
}
@ -357,6 +351,9 @@ public partial class MainWindow : Window
wrapper.Join();
}
// Destroy of all temporary notifications and the close timers
foreach(NotificationThreadMap map in CurrentNotifications) map.Dispose();
// Destroy the tray icon first
if (trayIcon != null) trayIcon.Dispose();
Application.Quit();

View File

@ -26,15 +26,6 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<CustomCommands>
<CustomCommands>
<Command>
<type>AfterBuild</type>
<command>rm appindicator3-sharp.dll</command>
<workingdir>${TargetDir}</workingdir>
</Command>
</CustomCommands>
</CustomCommands>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
@ -88,6 +79,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PopupMenu.cs" />
<Compile Include="NotificationThreadMap.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MelonVPNCore\MelonVPNCore.csproj">
@ -111,6 +103,14 @@
<None Include="MelonVPNDesktopIcon.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="net-libs\libnotify.net.dll" />
<None Include="net-libs\appindicator3-sharp.dll" />
<None Include="net-libs\Newtonsoft.Json.dll" />
<None Include="net-libs\libappindicator3sharpglue-12.10.0.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>libappindicator3sharpglue-12.10.0.so</Link>
</None>
<None Include="net-libs\Newtonsoft.Json.xml" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View File

@ -0,0 +1,24 @@
using System;
using System.Threading;
using Notify;
namespace MelonVPNClient
{
public class NotificationThreadMap : IDisposable
{
private readonly Notification notification;
private readonly Thread thread;
public NotificationThreadMap(Notification notification, Thread thread)
{
this.notification = notification;
this.thread = thread;
}
public void Dispose()
{
thread.Abort();
notification.Close();
}
}
}

View File

@ -4,6 +4,7 @@
<images-root-path>..</images-root-path>
</configuration>
<import>
<widget-library name="glade-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
<widget-library name="../net-libs/appindicator3-sharp.dll" />
<widget-library name="../bin/Debug/MelonVPNClient.exe" internal="true" />
</import>

View File

@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
@ -9,15 +10,24 @@ namespace MelonVPNCore
{
public static class DaemonSocketServer
{
private static Process currentVpnProcess = null;
private static bool shouldBeRunning = false;
private static bool shouldRestart = false;
private static bool isRestarting = false;
private static int startingTime = 3000;
private static int restartDelay = 250;
private static Process currentVpnProcess;
private static bool shouldBeRunning;
private static bool isRestarting;
private const int startingTime = 3000;
private const int restartDelay = 250;
private static DaemonConfig config;
public const string daemonConfigPath = "/etc/melon-vpn/daemon.cfg";
private static void SaveConfig()
{
File.WriteAllText(daemonConfigPath, config.ToJson());
}
public static void StartServer()
{
if (File.Exists(daemonConfigPath)) config = DaemonConfig.FromJson(File.ReadAllText(daemonConfigPath));
else config = new DaemonConfig();
IPHostEntry host = Dns.GetHostEntry("localhost");
IPAddress ipAddress = host.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 22035);
@ -52,18 +62,22 @@ namespace MelonVPNCore
}
else if (data == Messages.RestartOnMsg)
{
shouldRestart = true;
Console.WriteLine("Updated shouldRestart to true");
config.ShouldRestart = true;
SaveConfig();
Client.SendDataMessage(DataMessage.RestartOn, true);
}
else if (data == Messages.RestartOffMsg)
{
shouldRestart = false;
Console.WriteLine("Updated shouldRestart to false");
config.ShouldRestart = false;
SaveConfig();
Client.SendDataMessage(DataMessage.RestartOff, true);
}
else if (data == Messages.StatusMsg)
{
Console.WriteLine("Status requested");
if (isProcessOnline(currentVpnProcess) || shouldBeRunning)
if (IsProcessOnline(currentVpnProcess) || shouldBeRunning)
{
if (isRestarting)
{
@ -84,11 +98,11 @@ namespace MelonVPNCore
Client.SendDataMessage(DataMessage.Offline, true);
Client.SendCustomMessage(Messages.ClientListEmptyMsg, true);
}
Client.SendDataMessage((shouldRestart) ? DataMessage.RestartOn : DataMessage.RestartOff);
Client.SendDataMessage(config.ShouldRestart ? DataMessage.RestartOn : DataMessage.RestartOff);
}
else if (data == Messages.StartMsg)
{
if (! isProcessOnline(currentVpnProcess))
if (! IsProcessOnline(currentVpnProcess))
{
shouldBeRunning = true;
Console.WriteLine("Starting VPN");
@ -125,7 +139,7 @@ namespace MelonVPNCore
{
shouldBeRunning = false;
bool haderr = false;
if (isProcessOnline(currentVpnProcess))
if (IsProcessOnline(currentVpnProcess))
{
Console.WriteLine("Stopping VPN");
try
@ -169,23 +183,10 @@ namespace MelonVPNCore
}
}
public static bool isProcessOnline(Process p)
public static bool IsProcessOnline(Process p)
{
if (p == null)
{
return false;
}
else
{
if (p.HasExited)
{
return false;
}
else
{
return true;
}
}
if (p == null) return false;
return !p.HasExited;
}
static void CurrentVpnProcess_Exited(object sender, EventArgs e)
@ -196,7 +197,7 @@ namespace MelonVPNCore
Thread.Sleep(restartDelay);
bool imonline = false;
isRestarting = true;
while (shouldRestart && shouldBeRunning)
while (config.ShouldRestart && shouldBeRunning)
{
Console.WriteLine("Sending restarting reply");
Client.SendDataMessage(DataMessage.Restarting, true);

80
MelonVPNCore/Json.cs Normal file
View File

@ -0,0 +1,80 @@
using System;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace MelonVPNCore
{
// I hate overhead but this code is just nice xD
// Base json class with type property FromJson and ToJson methods
public class JsonBase
{
[JsonProperty("type")]
public string Type { get; set; }
// Convert from json string to WSJson class
public static JsonBase FromJson(string json) => JsonConvert.DeserializeObject<JsonBase>(json, Converter.Settings);
// Convert any class into a json string
public string ToJson() => JsonConvert.SerializeObject(this, Converter.Settings);
// Converter with settings
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter{DateTimeStyles=DateTimeStyles.AssumeUniversal }
}
};
}
}
// Base class for custom json with a type argument
public class JsonBase<T> : JsonBase
{
// Convert from json string to T class
public static new T FromJson(string json) => JsonConvert.DeserializeObject<T>(json, Converter.Settings);
// Used to convert json property into type long
internal class ParseStringConverter : JsonConverter
{
public override bool CanConvert(Type objectType) { return objectType == typeof(long) || objectType == typeof(long); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null) return null;
var value = serializer.Deserialize<string>(reader);
if (long.TryParse(value, out long l)) return l;
throw new Exception("Cannot unmarshal type long");
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null)
{
serializer.Serialize(writer, null);
return;
}
var v = (long)value;
serializer.Serialize(writer, v.ToString());
return;
}
public static readonly ParseStringConverter Singleton = new ParseStringConverter();
}
}
// Class for the daemon config
public class DaemonConfig : JsonBase<DaemonConfig>
{
public DaemonConfig() => Type = "DaemonConfig";
[JsonProperty("restart")]
public bool ShouldRestart { get; set; }
}
}

View File

@ -35,6 +35,7 @@
<Compile Include="GUISocketServer.cs" />
<Compile Include="ClientListParser.cs" />
<Compile Include="ConnectedClient.cs" />
<Compile Include="Json.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System" />

57
install
View File

@ -1,8 +1,61 @@
#!/bin/bash
# idiomatic parameter and option handling in bash
forceVpnInstall="no"
forceAppIndicator="no"
badoption=""
unknownargument=""
while test $# -gt 0
do
case "$1" in
--reinstall-vpn) forceVpnInstall="yes"
;;
--appindicator) forceAppIndicator="yes"
;;
--help) showHelp="yes"
;;
--*) badoption="$1"
;;
*) unknownargument="$1"
;;
esac
if [ -n "$badoption" ]; then
echo "Unknown option: $badoption"
echo "Run './install --help' for more information"
exit 1
elif [ -n "$unknownargument" ]; then
echo "Unknown argument: $unknownargument"
echo "Run './install --help' for more information"
exit 1
fi
shift
done
firstTime="no"
firstTimePath=".git/melon-vpn-install"
if [ ! -f "$firstTimePath" ]; then
showHelp="yes"
firstTime="yes"
fi
if [ "$showHelp" == "yes" ]; then
echo "MelonVPN installer"
echo "=================="
echo "Use './install' for a normal build"
echo "--help = display these help options"
echo "--reinstall-vpn = force reinstall simple-vpn"
echo "--appindicator = force copy appindicator dll into install directory"
if [ "$firstTime" == "yes" ]; then
echo -e "\nThis message was displayed as it is your first time using this installer"
echo "Repeat the command to install MelonVPN"
touch "$firstTimePath"
fi
exit 0
fi
echo "[info] Preparing to setup dependencies"
./install-dependencies $1
./install-dependencies $forceVpnInstall
echo "[info] Preparing to build projects from source"
./build
echo "[info] Preparing to install components"
./install-components
./install-components $forceAppIndicator
echo "[info] Install process finished"

View File

@ -1,7 +1,14 @@
#!/bin/bash
# arguments
forceAppIndicator="$1"
# new lib folder
sudo mkdir -p /usr/lib/melon-vpn/
# stop and disable daemon first
sudo systemctl stop melonvpndaemon
sudo systemctl disable melonvpndaemon
# copying
sudo cp MelonVPNCore/bin/Release/MelonVPNCore.dll /usr/lib/melon-vpn/
sudo cp MelonVPNCore/bin/Release/Newtonsoft.Json.dll /usr/lib/melon-vpn/
@ -36,13 +43,17 @@ fi
sudo chown root:root /etc/melon-vpn/client.cfg
# copy more files
if [ "$forceAppIndicator" == "yes" ]; then
sudo cp MelonVPNClient/bin/Release/appindicator3-sharp.dll /usr/lib/melon-vpn/
echo "[warning] appindicator3-sharp.dll has been copied as a component, if appindicator was installed through software run ./remove-appindicator3-component"
fi
sudo cp MelonVPNClient/bin/Release/libnotify.net.dll /usr/lib/melon-vpn/
sudo cp MelonVPNClient/bin/Release/MelonVPNClient.exe /usr/lib/melon-vpn/
sudo cp MelonVPNClient/bin/Release/MelonVPNClient.exe.config /usr/lib/melon-vpn/
sudo cp MelonVPNClient/bin/Release/MiniMelonVPNIcon.png /usr/lib/melon-vpn/
sudo cp MelonVPNClient/bin/Release/MiniMelonVPNOnline.png /usr/lib/melon-vpn/
sudo cp MelonVPNClient/bin/Release/MelonVPNDesktopIcon.png /usr/lib/melon-vpn/
sudo cp MelonVPNClient/bin/Release/libappindicator3sharpglue-12.10.0.so /usr/lib/melon-vpn/
# copy app indicator icons
mkdir -p ~/.local/share/icons/hicolor/128x128/apps/
@ -58,8 +69,6 @@ sudo chmod +x /usr/bin/melonvpnclient
echo "[info] Restarting daemon"
# cuz science
sudo systemctl stop melonvpndaemon
sudo systemctl disable melonvpndaemon
sudo systemctl daemon-reload
sudo systemctl enable melonvpndaemon
sudo systemctl start melonvpndaemon

View File

@ -1,4 +1,7 @@
#!/bin/bash
# arguments
forceVpnInstall="$1"
# Check if golang exists by the output of "go version"
if [ "$(go version | head -c 13)" == "go version go" ]; then
echo "Assuming go is installed"
@ -11,7 +14,7 @@ if [ "$(go version | head -c 13)" == "go version go" ]; then
shouldbuild="ye"
fi
# Look for an argument to force simple vpn re-install
if [ "$1" = "force" ]; then
if [ "$forceVpnInstall" = "yes" ]; then
shouldbuild="ye"
echo "Install enforced!"
fi