using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
class Program
{
const int port = 3131;
static Dictionary<string, string> validKeys = new Dictionary<string, string>();
static string keys = "keys.txt";
static bool run = true;
static void Main(string[] args)
{
Thread keyReloadThread = new Thread(ReloadKeys);
keyReloadThread.Start();
TcpListener server = null;
try
{
server = new TcpListener(IPAddress.Any, port);
server.Start();
Console.WriteLine("SERVER OPEN!");
while (run)
{
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.GetStream();
byte[] buffer = new byte[256];
int bytesRead = stream.Read(buffer, 0, buffer.Length);
string receivedData = Encoding.ASCII.GetString(buffer, 0, bytesRead);
if (receivedData.Contains(":"))
{
string[] parts = receivedData.Split(':');
string receivedKey = parts[0];
string receivedHwid = parts[1];
if (string.IsNullOrWhiteSpace(receivedKey))
{
client.Close();
continue;
}
if (validKeys.ContainsKey(receivedKey))
{
if (validKeys[receivedKey] == receivedHwid)
{
byte[] response = Encoding.ASCII.GetBytes("AUTH_SUCCESS");
stream.Write(response, 0, response.Length);
Console.WriteLine("Başarılı");
}
else
{
byte[] response = Encoding.ASCII.GetBytes("AUTH_FAILED");
stream.Write(response, 0, response.Length);
Console.WriteLine("Başarısız");
}
}
else
{
hwidup(receivedKey, receivedHwid);
byte[] response = Encoding.ASCII.GetBytes("AUTH_SUCCESS_WITH_HWID_UPDATE");
stream.Write(response, 0, response.Length);
Console.WriteLine("Başarılı");
}
}
else
{
byte[] response = Encoding.ASCII.GetBytes("INVALID_FORMAT");
stream.Write(response, 0, response.Length);
Console.WriteLine("Başarısız");
}
client.Close();
}
}
catch (Exception e)
{
}
finally
{
server?.Stop();
run = false;
}
}
static void ReloadKeys()
{
while (run)
{
try
{
Dictionary<string, string> newKeys = new Dictionary<string, string>();
foreach (var line in File.ReadAllLines(keys))
{
if (line.Contains(":"))
{
var parts = line.Split(':');
newKeys[parts[0]] = parts[1]; // key:hwid
}
}
validKeys = newKeys;
}
catch (Exception e)
{
}
Thread.Sleep(2000);
}
}
static void hwidup(string key, string hwid)
{
try
{
List<string> lines = new List<string>(File.ReadAllLines(keys));
for (int i = 0; i < lines.Count; i++)
{
if (lines[i].Trim() == key)
{
lines[i] = $"{key}:{hwid}";
break;
}
}
File.WriteAllLines(keys, lines.ToArray());
}
catch (Exception e)
{
}
}
}