Fix a few bugs in server selector, do not hardcode paths

This commit is contained in:
Mikhail Tyukin
2025-12-10 15:14:49 -05:00
parent 5f27e2ea44
commit 8b8d58854f
5 changed files with 323 additions and 433 deletions

View File

@@ -4,210 +4,174 @@ using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
namespace ServerSelector
namespace ServerSelector;
public class ServerSwitcher
{
public class ServerSwitcher
private const string HostsStartMarker = "# begin ServerSelector entries";
private const string HostsEndMarker = "# end ServerSelector entries";
private static PathUtil util = new();
public static bool IsUsingLocalServer()
{
private const string HostsStartMarker = "# begin ServerSelector entries";
private const string HostsEndMarker = "# end ServerSelector entries";
return File.ReadAllText(util.SystemHostsFile).Contains("global-lobby.nikke-kr.com");
}
public static bool IsUsingOfficalServer()
public static bool IsOffline()
{
return File.ReadAllText(util.SystemHostsFile).Contains("cloud.nikke-kr.com");
}
public static (bool, string?) SetBasePath(string basePath)
{
return util.SetBasePath(basePath);
}
public static async Task<string> CheckIntegrity()
{
if (!IsUsingLocalServer())
return "Official server";
if (File.Exists(util.LauncherCertificatePath))
{
string hostsFile = File.ReadAllText(OperatingSystem.IsWindows() ? "C:\\Windows\\System32\\drivers\\etc\\hosts" : "/etc/hosts");
return !hostsFile.Contains("global-lobby.nikke-kr.com");
string certList1 = await File.ReadAllTextAsync(util.LauncherCertificatePath);
if (!certList1.Contains("Good SSL Ca"))
return "SSL Cert Patch missing Launcher";
}
public static bool IsOffline()
if (File.Exists(util.GameCertificatePath))
{
string hostsFile = File.ReadAllText(OperatingSystem.IsWindows() ? "C:\\Windows\\System32\\drivers\\etc\\hosts" : "/etc/hosts");
return hostsFile.Contains("cloud.nikke-kr.com");
string certList2 = await File.ReadAllTextAsync(util.GameCertificatePath);
if (!certList2.Contains("Good SSL Ca"))
return "SSL Cert Patch missing Game";
}
public static async Task<string> CheckIntegrity(string gamePath, string launcherPath)
// TODO: Check sodium lib
// TODO: check hosts file
return "OK";
}
public static async Task RevertHostsFile(string hostsFilePath)
{
string txt = await File.ReadAllTextAsync(hostsFilePath);
// remove stuff
try
{
if (IsUsingOfficalServer())
return "Official server";
if (!Directory.Exists(gamePath))
int startIdx = txt.IndexOf(HostsStartMarker);
int endIdx;
if (startIdx == -1)
{
return "Game path does not exist";
startIdx = txt.IndexOf("cloud.nikke-kr.com");
}
if (!Directory.Exists(launcherPath))
string endIndexStr = HostsEndMarker;
if (!txt.Contains(endIndexStr))
{
return "Launcher path does not exist";
}
if (!File.Exists(Path.Combine(launcherPath, "nikke_launcher.exe")))
{
return "Launcher path is invalid. Make sure that the game executable exists in the launcher folder";
}
// TODO fix this mess
string launcherCertList = launcherPath + "/intl_service/intl_cacert.pem";
if (!File.Exists(launcherCertList))
launcherCertList = launcherPath + "/intl_service/cacert.pem"; // older INTL sdk versions
string gameCertList = gamePath + "/nikke_Data/Plugins/x86_64/intl_cacert.pem";
if (!File.Exists(gameCertList))
gameCertList = gamePath + "/nikke_Data/Plugins/x86_64/cacert.pem"; // older INTL sdk versions
if (File.Exists(launcherCertList))
{
string certList1 = await File.ReadAllTextAsync(launcherCertList);
if (!certList1.Contains("Good SSL Ca"))
return "SSL Cert Patch missing";
}
if (File.Exists(gameCertList))
{
string certList2 = await File.ReadAllTextAsync(gameCertList);
if (!certList2.Contains("Good SSL Ca"))
return "SSL Cert Patch missing";
}
// TODO: Check sodium lib
// TODO: check hosts file
return "OK";
}
public static async Task RevertHostsFile(string hostsFilePath)
{
string txt = await File.ReadAllTextAsync(hostsFilePath);
// remove stuff
try
{
int startIdx = txt.IndexOf(HostsStartMarker);
int endIdx;
if (startIdx == -1)
// old code, find new line character before start index
for (int i = startIdx - 1; i >= 0; i--)
{
startIdx = txt.IndexOf("cloud.nikke-kr.com");
}
string endIndexStr = HostsEndMarker;
if (!txt.Contains(endIndexStr))
{
// old code, find new line character before start index
for (int i = startIdx - 1; i >= 0; i--)
char c = txt[i];
if (c == '\n')
{
char c = txt[i];
if (c == '\n')
{
startIdx = i + 1;
break;
}
}
endIndexStr = "y.io";
endIdx = txt.IndexOf(endIndexStr) + endIndexStr.Length;
}
else
{
// add/subtract 2 to take into account newline
startIdx = txt.IndexOf(HostsStartMarker) - 2;
endIdx = txt.IndexOf(endIndexStr) + endIndexStr.Length;
}
txt = string.Concat(txt.AsSpan(0, startIdx), txt.AsSpan(endIdx));
await File.WriteAllTextAsync(hostsFilePath, txt);
}
catch
{
}
}
public static async Task<ServerSwitchResult> SaveCfg(bool useOffical, string gamePath, string? launcherPath, string ip, bool offlineMode)
{
string sodiumLib = AppDomain.CurrentDomain.BaseDirectory + "sodium.dll";
string gameSodium = gamePath + "/nikke_Data/Plugins/x86_64/sodium.dll";
string gameAssembly = gamePath + "/GameAssembly.dll";
string sodiumBackup = gameSodium + ".bak";
string hostsFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers/etc/hosts");
string CAcert = await File.ReadAllTextAsync(AppDomain.CurrentDomain.BaseDirectory + "myCA.pem");
string launcherCertList = launcherPath + "/intl_service/intl_cacert.pem";
if (!File.Exists(launcherCertList))
launcherCertList = launcherPath + "/intl_service/cacert.pem"; // older INTL sdk versions
string gameCertList = gamePath + "/nikke_Data/Plugins/x86_64/intl_cacert.pem";
if (!File.Exists(gameCertList))
gameCertList = gamePath + "/nikke_Data/Plugins/x86_64/cacert.pem"; // older INTL sdk versions
bool supported = true;
if (OperatingSystem.IsLinux())
{
// for wine
hostsFilePath = gamePath + "/../../../windows/system32/drivers/etc/hosts";
}
if (useOffical)
{
await RevertHostsFile(hostsFilePath);
if (OperatingSystem.IsLinux())
{
await RevertHostsFile("/etc/hosts");
}
try
{
// remove cert
if (OperatingSystem.IsWindows())
{
X509Store store = new(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Remove(new X509Certificate2(X509Certificate.CreateFromCertFile(AppDomain.CurrentDomain.BaseDirectory + "myCA.pfx")));
store.Close();
startIdx = i + 1;
break;
}
}
catch
{
// may not be installed
}
// restore sodium
if (!File.Exists(sodiumBackup))
{
throw new Exception("sodium backup does not exist. Repair the game in the launcher and switch to local server and back to official.");
}
File.Copy(sodiumBackup, gameSodium, true);
if (File.Exists(launcherCertList))
{
string certList = await File.ReadAllTextAsync(launcherCertList);
int goodSslIndex1 = certList.IndexOf("Good SSL Ca");
if (goodSslIndex1 != -1)
await File.WriteAllTextAsync(launcherCertList, certList[..goodSslIndex1]);
}
if (File.Exists(gameCertList))
{
string certList = await File.ReadAllTextAsync(gameCertList);
int newCertIndex = certList.IndexOf("Good SSL Ca");
if (newCertIndex != -1)
await File.WriteAllTextAsync(gameCertList, certList[..newCertIndex]);
}
endIndexStr = "y.io";
endIdx = txt.IndexOf(endIndexStr) + endIndexStr.Length;
}
else
{
// add to hosts file
string hosts = $@"{HostsStartMarker}
// add/subtract 2 to take into account newline
startIdx = txt.IndexOf(HostsStartMarker) - 2;
endIdx = txt.IndexOf(endIndexStr) + endIndexStr.Length;
}
txt = string.Concat(txt.AsSpan(0, startIdx), txt.AsSpan(endIdx));
await File.WriteAllTextAsync(hostsFilePath, txt);
}
catch
{
}
}
public static async Task<ServerSwitchResult> SaveCfg(bool useOffical, string ip, bool offlineMode)
{
string CAcert = await File.ReadAllTextAsync(AppDomain.CurrentDomain.BaseDirectory + "myCA.pem");
string sodiumLib = AppDomain.CurrentDomain.BaseDirectory + "sodium.dll";
bool supported = true;
if (useOffical)
{
await RevertHostsFile(util.SystemHostsFile);
if (OperatingSystem.IsLinux())
{
await RevertHostsFile(util.WineHostsFile);
}
try
{
// remove cert
if (OperatingSystem.IsWindows())
{
X509Store store = new(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Remove(new X509Certificate2(X509Certificate.CreateFromCertFile(AppDomain.CurrentDomain.BaseDirectory + "myCA.pfx")));
store.Close();
}
}
catch
{
// may not be installed
}
// restore sodium
if (!File.Exists(util.GameSodiumBackupPath))
{
throw new Exception("sodium backup does not exist. Repair the game in the launcher and switch to local server and back to official.");
}
File.Copy(util.GameSodiumBackupPath, util.GameSodiumPath, true);
if (util.LauncherCertificatePath != null && File.Exists(util.LauncherCertificatePath))
{
string certList = await File.ReadAllTextAsync(util.LauncherCertificatePath);
int goodSslIndex1 = certList.IndexOf("Good SSL Ca");
if (goodSslIndex1 != -1)
await File.WriteAllTextAsync(util.LauncherCertificatePath, certList[..goodSslIndex1]);
}
if (File.Exists(util.GameCertificatePath))
{
string certList = await File.ReadAllTextAsync(util.GameCertificatePath);
int newCertIndex = certList.IndexOf("Good SSL Ca");
if (newCertIndex != -1)
await File.WriteAllTextAsync(util.GameCertificatePath, certList[..newCertIndex]);
}
}
else
{
// add to hosts file
string hosts = $@"{HostsStartMarker}
{ip} global-lobby.nikke-kr.com
";
if (offlineMode)
{
hosts += $"{ip} cloud.nikke-kr.com" + Environment.NewLine;
}
if (offlineMode)
{
hosts += $"{ip} cloud.nikke-kr.com" + Environment.NewLine;
}
hosts += $@"{ip} jp-lobby.nikke-kr.com
hosts += $@"{ip} jp-lobby.nikke-kr.com
{ip} us-lobby.nikke-kr.com
{ip} kr-lobby.nikke-kr.com
{ip} sea-lobby.nikke-kr.com
@@ -224,94 +188,92 @@ namespace ServerSelector
255.255.221.21 sentry.io
{HostsEndMarker}";
await RevertHostsFile(hostsFilePath);
await RevertHostsFile(util.SystemHostsFile);
try
try
{
FileInfo fi = new(util.SystemHostsFile);
if (fi.IsReadOnly)
{
FileInfo fi = new(hostsFilePath);
if (fi.IsReadOnly)
{
// try to remove readonly flag
fi.IsReadOnly = false;
}
if (!(await File.ReadAllTextAsync(hostsFilePath)).Contains("global-lobby.nikke-kr.com"))
{
using StreamWriter w = File.AppendText(hostsFilePath);
w.WriteLine();
w.Write(hosts);
}
}
catch
{
throw new Exception("cannot modify C:\\Windows\\System32\\drivers\\etc\\hosts file to redirect to server, check your antivirus software");
// try to remove readonly flag
fi.IsReadOnly = false;
}
// Also change /etc/hosts if running on linux
if (OperatingSystem.IsLinux())
if (!(await File.ReadAllTextAsync(util.SystemHostsFile)).Contains("global-lobby.nikke-kr.com"))
{
hostsFilePath = "/etc/hosts";
await RevertHostsFile(hostsFilePath);
if (!(await File.ReadAllTextAsync(hostsFilePath)).Contains("global-lobby.nikke-kr.com"))
{
using StreamWriter w = File.AppendText(hostsFilePath);
w.WriteLine();
w.Write(hosts);
}
using StreamWriter w = File.AppendText(util.SystemHostsFile);
w.WriteLine();
w.Write(hosts);
}
}
catch
{
throw new Exception($"cannot modify \"{util.SystemHostsFile}\" file to redirect to server, check your antivirus software");
}
// trust CA. TODO is this needed?
try
// Also change hosts file in wineprefix if running on linux
if (OperatingSystem.IsLinux())
{
await RevertHostsFile(util.WineHostsFile);
if (!(await File.ReadAllTextAsync(util.WineHostsFile)).Contains("global-lobby.nikke-kr.com"))
{
if (OperatingSystem.IsWindows())
{
X509Store store = new(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(AppDomain.CurrentDomain.BaseDirectory + "myCA.pfx")));
store.Close();
}
using StreamWriter w = File.AppendText(util.WineHostsFile);
w.WriteLine();
w.Write(hosts);
}
catch { }
}
if (!File.Exists(gameSodium))
// trust CA. TODO is this needed?
try
{
if (OperatingSystem.IsWindows())
{
throw new Exception("expected sodium library to exist at path " + gameSodium);
X509Store store = new(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(AppDomain.CurrentDomain.BaseDirectory + "myCA.pfx")));
store.Close();
}
}
catch { }
// copy backup if sodium size is correct
byte[] sod = await File.ReadAllBytesAsync(gameSodium);
if (sod.Length <= 307200) // TODO this is awful
{
// orignal file size, copy backup
await File.WriteAllBytesAsync(sodiumBackup, sod);
}
if (!File.Exists(util.GameSodiumPath))
{
throw new Exception("expected sodium library to exist at path " + util.GameSodiumPath);
}
// write new sodium library
await File.WriteAllBytesAsync(gameSodium, await File.ReadAllBytesAsync(sodiumLib));
// copy backup if sodium size is correct
byte[] sod = await File.ReadAllBytesAsync(util.GameSodiumPath);
if (sod.Length <= 307200) // TODO this is awful
{
// orignal file size, copy backup
await File.WriteAllBytesAsync(util.GameSodiumBackupPath, sod);
}
// Add generated CA certificate to launcher/game curl certificate list
if (launcherPath != null)
{
await File.WriteAllTextAsync(launcherCertList,
await File.ReadAllTextAsync(launcherCertList)
+ "\nGood SSL Ca\n===============================\n"
+ CAcert);
}
// write new sodium library
await File.WriteAllBytesAsync(util.GameSodiumPath, await File.ReadAllBytesAsync(sodiumLib));
await File.WriteAllTextAsync(gameCertList,
await File.ReadAllTextAsync(gameCertList)
// Add generated CA certificate to launcher/game curl certificate list
if (util.LauncherCertificatePath != null)
{
await File.WriteAllTextAsync(util.LauncherCertificatePath,
await File.ReadAllTextAsync(util.LauncherCertificatePath)
+ "\nGood SSL Ca\n===============================\n"
+ CAcert);
}
return new ServerSwitchResult(true, null, supported);
await File.WriteAllTextAsync(util.GameCertificatePath,
await File.ReadAllTextAsync(util.GameCertificatePath)
+ "\nGood SSL Ca\n===============================\n"
+ CAcert);
}
}
public class ServerSwitchResult(bool ok, Exception? exception, bool isSupported)
{
public bool Ok { get; set; } = ok;
public Exception? Exception { get; set; } = exception;
public bool IsSupported { get; set; } = isSupported;
return new ServerSwitchResult(true, null, supported);
}
}
public class ServerSwitchResult(bool ok, Exception? exception, bool isSupported)
{
public bool Ok { get; set; } = ok;
public Exception? Exception { get; set; } = exception;
public bool IsSupported { get; set; } = isSupported;
}