improve server selector

This commit is contained in:
Mikhail
2024-08-24 17:20:09 -04:00
parent 7ddaef38df
commit d00ab6d185
12 changed files with 269 additions and 107 deletions

View File

@@ -10,23 +10,123 @@ namespace ServerSelector
private static int GameAssemblySodiumIntegrityFuncHint = 0x5877FFB;
private static byte[] GameAssemblySodiumIntegrityFuncOrg = [0xE8, 0xF0, 0x9D, 0x43, 0xFB];
private static byte[] GameAssemblySodiumIntegrityFuncPatch = [0xb0, 0x01, 0x90, 0x90, 0x90];
private const string HostsStartMarker = "# begin ServerSelector entries";
private const string HostsEndMarker = "# end ServerSelector entries";
public static bool IsUsingOfficalServer()
{
var hostsFile = File.ReadAllText("C:\\Windows\\System32\\drivers\\etc\\hosts");
return !hostsFile.Contains("cloud.nikke-kr.com");
return !hostsFile.Contains("global-lobby.nikke-kr.com");
}
public static string CheckIntegrity()
public static bool IsOffline()
{
var hostsFile = File.ReadAllText("C:\\Windows\\System32\\drivers\\etc\\hosts");
return hostsFile.Contains("cloud.nikke-kr.com");
}
public static async Task<string> CheckIntegrity(string gamePath, string launcherPath)
{
if (IsUsingOfficalServer())
return "Official server";
// TODO
if (!Directory.Exists(gamePath))
{
return "Game path does not exist";
}
if (!Directory.Exists(launcherPath))
{
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";
}
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");
var CAcert = await File.ReadAllTextAsync(AppDomain.CurrentDomain.BaseDirectory + "myCA.pem");
string launcherCertList = launcherPath + "/intl_service/cacert.pem";
string gameCertList = gamePath + "/nikke_Data/Plugins/x86_64/cacert.pem";
var certList1 = await File.ReadAllTextAsync(launcherCertList);
int goodSslIndex1 = certList1.IndexOf("Good SSL Ca");
if (goodSslIndex1 == -1)
return "Patch missing";
var certList2 = await File.ReadAllTextAsync(gameCertList);
int goodSslIndex2 = certList2.IndexOf("Good SSL Ca");
if (goodSslIndex2 == -1)
return "Patch missing";
// TODO: Check sodium lib
// TODO: Check if gameassembly was patched
return "OK";
}
public static async Task SaveCfg(bool useOffical, string gamePath, string launcherPath, string ip)
public static async Task RevertHostsFile()
{
string hostsFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers/etc/hosts");
var txt = await File.ReadAllTextAsync(hostsFilePath);
// remove stuff
try
{
int startIdx = txt.IndexOf(HostsStartMarker);
int endIdx;
if (startIdx == -1)
{
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--)
{
var 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 = txt.Substring(0, startIdx) + txt.Substring(endIdx);
await File.WriteAllTextAsync(hostsFilePath, txt);
}
catch
{
}
}
public static async Task 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";
@@ -42,40 +142,12 @@ namespace ServerSelector
// TODO: allow changing ip address
if (useOffical)
{
var txt = await File.ReadAllTextAsync(hostsFilePath);
// remove stuff
try
{
int startIdx = txt.IndexOf("cloud.nikke-kr.com");
// find new line character before start index
for (int i = startIdx - 1; i >= 0; i--)
{
var c = txt[i];
if (c == '\n')
{
startIdx = i + 1;
break;
}
}
int endIdx = txt.IndexOf("y.io") + 4;
txt = txt.Substring(0, startIdx) + txt.Substring(endIdx);
await File.WriteAllTextAsync(hostsFilePath, txt);
}
catch
{
}
await RevertHostsFile();
// remove cert
X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Remove(new X509Certificate2(X509Certificate2.CreateFromCertFile(AppDomain.CurrentDomain.BaseDirectory + "myCA.pfx")));
store.Remove(new X509Certificate2(X509Certificate.CreateFromCertFile(AppDomain.CurrentDomain.BaseDirectory + "myCA.pfx")));
store.Close();
// restore sodium
@@ -117,17 +189,29 @@ namespace ServerSelector
}
var certList1 = await File.ReadAllTextAsync(launcherCertList);
await File.WriteAllTextAsync(launcherCertList, certList1.Substring(0, certList1.IndexOf("Good SSL Ca")));
int goodSslIndex1 = certList1.IndexOf("Good SSL Ca");
if (goodSslIndex1 != -1)
await File.WriteAllTextAsync(launcherCertList, certList1.Substring(0, goodSslIndex1));
var certList2 = await File.ReadAllTextAsync(gameCertList);
await File.WriteAllTextAsync(gameCertList, certList2.Substring(0, certList2.IndexOf("Good SSL Ca")));
int goodSslIndex2 = certList2.IndexOf("Good SSL Ca");
if (goodSslIndex2 != -1)
await File.WriteAllTextAsync(gameCertList, certList2.Substring(0, goodSslIndex2));
}
else
{
// add to hosts file
string hosts = $@"{ip} cloud.nikke-kr.com
string hosts = $@"{HostsStartMarker}
{ip} global-lobby.nikke-kr.com
{ip} jp-lobby.nikke-kr.com
";
if (offlineMode)
{
hosts += $"{ip} cloud.nikke-kr.com" + Environment.NewLine;
}
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
@@ -141,13 +225,15 @@ namespace ServerSelector
255.255.221.21 na.fleetlogd.com
{ip} www.jupiterlauncher.com
{ip} data-aws-na.intlgame.com
255.255.221.21 sentry.io";
255.255.221.21 sentry.io
{HostsEndMarker}";
await RevertHostsFile();
if (!(await File.ReadAllTextAsync(hostsFilePath)).Contains("global-lobby.nikke-kr.com"))
{
using StreamWriter w = File.AppendText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers/etc/hosts"));
w.WriteLine();
w.WriteLine(hosts);
w.Write(hosts);
}