117 lines
3.6 KiB
C#
117 lines
3.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net.NetworkInformation;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Data;
|
|
using System.Windows.Documents;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Navigation;
|
|
using System.Windows.Shapes;
|
|
|
|
namespace KurtzPelRegionPicker
|
|
{
|
|
/// <summary>
|
|
/// Logique d'interaction pour MainWindow.xaml
|
|
/// </summary>
|
|
public partial class MainWindow : Window
|
|
{
|
|
public const string HOSTS_PATH = @"C:\Windows\System32\drivers\etc\hosts";
|
|
public const string COMMENT = "# KurtzPelRegionPicker";
|
|
// language=regex
|
|
public const string IP_REGEX = @"\[(?<ip>.+)\]";
|
|
|
|
private List<Region> _regions = new List<Region>()
|
|
{
|
|
new Region("Europe", "gs.live.europe.kurtzpel.playkog.com"),
|
|
new Region("America", "gs.live.northamerica.kurtzpel.playkog.com"),
|
|
new Region("Test", "gs.qa1.japan.kurtzpel.playkog.com")
|
|
};
|
|
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
|
|
ComboBox_Source_Region.ItemsSource = _regions.Where(x => x.Name != "Test");
|
|
ComboBox_Fake_Region.ItemsSource = _regions;
|
|
}
|
|
|
|
private void Button_Apply_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (ComboBox_Source_Region.SelectedItem is Region source && ComboBox_Fake_Region.SelectedItem is Region destination)
|
|
{
|
|
ApplyRegion(source, destination);
|
|
|
|
MessageBox.Show("Region applied", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
Environment.Exit(0);
|
|
}
|
|
}
|
|
|
|
private void ApplyRegion(Region source, Region destination)
|
|
{
|
|
string[] content = File.ReadAllLines(HOSTS_PATH);
|
|
string[] newContent;
|
|
|
|
if (content.Contains(COMMENT))
|
|
{
|
|
// Purge
|
|
int index = content.IndexOf(COMMENT);
|
|
|
|
var list = content.ToList();
|
|
|
|
list.RemoveAt(index);
|
|
list.RemoveAt(index);
|
|
|
|
content = list.ToArray();
|
|
File.WriteAllLines(HOSTS_PATH, content);
|
|
}
|
|
|
|
string line = $"{GetRealIP(destination.DNS)} {source.DNS}";
|
|
|
|
newContent = new string[content.Length + 2];
|
|
content.CopyTo(newContent, 0);
|
|
|
|
newContent[newContent.Length - 2] = COMMENT;
|
|
newContent[newContent.Length - 1] = line;
|
|
|
|
File.WriteAllLines(HOSTS_PATH, newContent);
|
|
}
|
|
|
|
private string GetRealIP(string dns)
|
|
{
|
|
var process = new Process()
|
|
{
|
|
StartInfo = new ProcessStartInfo("ping.exe", $"-n 1 -w 10 {dns}")
|
|
{
|
|
CreateNoWindow = true,
|
|
RedirectStandardOutput = true,
|
|
UseShellExecute = false,
|
|
WindowStyle = ProcessWindowStyle.Hidden
|
|
}
|
|
};
|
|
|
|
process.Start();
|
|
process.WaitForExit();
|
|
|
|
string output = process.StandardOutput.ReadToEnd();
|
|
string ip = Regex.Match(output, IP_REGEX).Groups["ip"].Value;
|
|
|
|
if (ip == "")
|
|
{
|
|
MessageBox.Show($"Can't resolve hostname: {dns}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
Environment.Exit(1);
|
|
}
|
|
|
|
return ip;
|
|
}
|
|
}
|
|
}
|