Console.Write("Public IP of the server? ");
var server_ip = Console.ReadLine();

var bytes = File.ReadAllBytes("Telegram.exe");
// patch the public key:
var prodKey = System.Text.Encoding.Latin1.GetBytes("MIIBCgKCAQEA6LszBcC1LGzyr992NzE0ieY+BSaOW622Aa9Bd4ZHLl+TuFQ4lo4g\n5nKaMBwK/BIb9xUfg0Q29/2mgIR6Zr9krM7HjuIcCzFvDtr+L0GQjae9H0pRB2OO\n62cECs5HKhT5DZ98K33vmWiLowc621dQuwKWSQKjWf50XYFw42h21P2KXUGyp2y/\n+aEyZ+uVgLLQbRA1dEjSDZ2iGRy12Mk5gpYc397aYp438fsJoHIgJ2lgMv5h7WY9\nt6N/byY9Nw9p21Og3AoXSL2q/2IJ1WRUhebgAdGVMlV1fkuOQoEzR7EdpqtQD9Cs\n5+bfo3Nhmcyvk5ftB0WkJ9z6bNZ7yxrP8wIDAQAB");
var new_key = System.Text.Encoding.Latin1.GetBytes("MIIBCgKCAQEAmu0N3BJAPxhRclG3NFkuC4qBkZiUVJF3bF2NPYegO7BKNGxGjvsM\nUJYofrhSLwsK+2fbyKpHaZg9Dc7IolmOdNhXDje+u8axlkv/onxj6hnMiBQTgkM7\nxB7Pkk5n/d7/3umFbepHSCGRUbxbg5m72OpWNdS6Lid+Om3XoMIsyWL7fzBXzann\nJdfA01SHqewHOT5q8LKgvhDapmv4YE/H3zZpSwdJrBAlT0oiDpLWPIk3Oig+bktb\npkTJER6gy4/yVs/Ko1zPS8xqs0bBrIxXTP0xwSfGr2g9jmKxR9uZ4aXBVRktOc3+\nkxxAWMjold3FNny4ZyDQtiES/fAQnhO+xwIDAQAB");
var start = bytes.AsSpan().IndexOf(prodKey);
if (start < 0) throw new Exception("Cannot find the prod key in Telegram.exe");
Array.Copy(new_key, 0, bytes, start, new_key.Length);

// patch server IPs to localhost:
var tg_ips = new[] { "149.154.175.50\0", "149.154.167.51\0", "95.161.76.100\0", "149.154.175.100\0", "149.154.167.91\0", "149.154.171.5\0", "149.154.175.10\0", "149.154.167.40\0", "149.154.175.117\0" };
var new_ip = System.Text.Encoding.Latin1.GetBytes(server_ip + "\0");
for (int i = 0; i < tg_ips.Length; i++)
{
	string ip = tg_ips[i];
	start = bytes.AsSpan().IndexOf(System.Text.Encoding.Latin1.GetBytes(ip));
	if (start >= 0)
		Array.Copy(new_ip, 0, bytes, start, new_ip.Length);
}

File.WriteAllBytes("Telegram_WTS.exe", bytes);

// disable Telegram.exe updates:
Directory.CreateDirectory("externalupdater.d");
File.WriteAllText("externalupdater.d/path.txt", Environment.CurrentDirectory.Replace('\\', '/') + "/Telegram_WTS.exe");
