2020-05-09 09:49:52 -04:00
|
|
|
using System;
|
|
|
|
using System.Threading;
|
2020-05-21 17:32:27 -04:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.IO;
|
2020-05-09 09:49:52 -04:00
|
|
|
using System.Net;
|
|
|
|
using System.Net.Sockets;
|
2020-05-21 17:32:27 -04:00
|
|
|
|
|
|
|
using Amazon.ECS;
|
|
|
|
using Amazon.ECS.Model;
|
|
|
|
using System.Collections.Generic;
|
2020-05-09 09:49:52 -04:00
|
|
|
class AuthServer
|
|
|
|
{
|
2020-05-21 17:32:27 -04:00
|
|
|
private bool running = false;
|
2020-05-09 09:49:52 -04:00
|
|
|
private int port;
|
2020-05-21 17:32:27 -04:00
|
|
|
private Thread thread;
|
2020-05-09 09:49:52 -04:00
|
|
|
private IPAddress address;
|
2020-05-21 17:32:27 -04:00
|
|
|
|
|
|
|
// Core objects
|
2020-05-09 09:49:52 -04:00
|
|
|
private TcpListener server;
|
2020-05-21 17:32:27 -04:00
|
|
|
private Redis redis;
|
|
|
|
private AmazonECSClient ecs;
|
|
|
|
|
|
|
|
|
2020-05-09 09:49:52 -04:00
|
|
|
public AuthServer(string addr = "0.0.0.0", int p = 7778)
|
|
|
|
{
|
|
|
|
port = p;
|
|
|
|
address = IPAddress.Parse(addr);
|
|
|
|
|
2020-05-21 17:32:27 -04:00
|
|
|
redis = new Redis(Environment.GetEnvironmentVariable("REDIS_HOSTNAME"));
|
2020-05-09 09:49:52 -04:00
|
|
|
server = new TcpListener(address, port);
|
2020-05-21 17:32:27 -04:00
|
|
|
ecs = new AmazonECSClient();
|
2020-05-09 09:49:52 -04:00
|
|
|
}
|
|
|
|
private void ServerLoop()
|
|
|
|
{
|
|
|
|
while(running)
|
|
|
|
{
|
2020-05-21 17:32:27 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
//Wait for connection
|
|
|
|
TcpClient client = server.AcceptTcpClient();
|
|
|
|
//Get remote address
|
|
|
|
IPEndPoint endPoint = (IPEndPoint) client.Client.RemoteEndPoint;
|
|
|
|
Console.WriteLine(endPoint.Address.ToString() + " connected!");
|
2020-05-09 09:49:52 -04:00
|
|
|
|
2020-05-21 17:32:27 -04:00
|
|
|
//Create streams
|
|
|
|
NetworkStream stream = client.GetStream();
|
|
|
|
StreamWriter writer = new StreamWriter(stream);
|
2020-05-09 09:49:52 -04:00
|
|
|
|
2020-05-21 17:32:27 -04:00
|
|
|
Byte[] bytes = new byte[256];
|
|
|
|
String data = null;
|
2020-05-09 09:49:52 -04:00
|
|
|
|
|
|
|
|
2020-05-21 17:32:27 -04:00
|
|
|
writer.Write("Hey there bud!");
|
|
|
|
writer.Flush();
|
|
|
|
Console.WriteLine("HERE");
|
2020-05-09 09:49:52 -04:00
|
|
|
|
2020-05-21 17:32:27 -04:00
|
|
|
byte[] sendBytes = System.Text.Encoding.ASCII.GetBytes("GET / HTTP/1.1");
|
|
|
|
stream.Write(sendBytes, 0, sendBytes.Length);
|
|
|
|
client.Client.Send(sendBytes);
|
|
|
|
|
|
|
|
|
|
|
|
//BinaryWriter writer = new BinaryWriter(stream);
|
|
|
|
//writer.Write("TEST");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
while((i = stream.Read(bytes, 0, bytes.Length)) != 0)
|
|
|
|
{
|
|
|
|
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
|
|
|
|
Console.WriteLine("Recieved: {0}", data);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
client.Close();
|
|
|
|
Console.WriteLine("Client disconnected");
|
|
|
|
}
|
|
|
|
catch(Exception e)
|
2020-05-09 09:49:52 -04:00
|
|
|
{
|
2020-05-21 17:32:27 -04:00
|
|
|
Console.WriteLine("Fatal exception: " + e.ToString());
|
2020-05-09 09:49:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
{
|
|
|
|
server.Start();
|
|
|
|
running = true;
|
|
|
|
ThreadStart entrypoint = new ThreadStart(ServerLoop);
|
|
|
|
thread = new Thread(entrypoint);
|
|
|
|
thread.Start();
|
2020-05-21 17:32:27 -04:00
|
|
|
Console.WriteLine("Waiting for a connection...");
|
2020-05-09 09:49:52 -04:00
|
|
|
}
|
|
|
|
public void Stop()
|
|
|
|
{
|
|
|
|
running = false;
|
|
|
|
}
|
|
|
|
public void ForceStop()
|
|
|
|
{
|
2020-05-21 17:32:27 -04:00
|
|
|
running = false;
|
|
|
|
thread.Abort();
|
2020-05-09 09:49:52 -04:00
|
|
|
}
|
|
|
|
}
|