From da26a320b41f7e39bac76e1f337a649b9044bd2f Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Sat, 9 May 2020 09:49:52 -0400 Subject: [PATCH] Basic TCP server --- authorizer/.gitignore | 37 ++++++++++++++++++++++ authorizer/Dockerfile | 7 +++++ authorizer/Program.cs | 23 ++++++++++++++ authorizer/Server.cs | 61 ++++++++++++++++++++++++++++++++++++ authorizer/authorizer.csproj | 8 +++++ 5 files changed, 136 insertions(+) create mode 100644 authorizer/.gitignore create mode 100644 authorizer/Dockerfile create mode 100644 authorizer/Program.cs create mode 100644 authorizer/Server.cs create mode 100644 authorizer/authorizer.csproj diff --git a/authorizer/.gitignore b/authorizer/.gitignore new file mode 100644 index 0000000..a437a65 --- /dev/null +++ b/authorizer/.gitignore @@ -0,0 +1,37 @@ +*.swp +*.*~ +project.lock.json +.DS_Store +*.pyc +nupkg/ + +# Visual Studio Code +.vscode + +# Rider +.idea + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +build/ +bld/ +[Bb]in/ +[Oo]bj/ +[Oo]ut/ +msbuild.log +msbuild.err +msbuild.wrn + +# Visual Studio 2015 +.vs/ diff --git a/authorizer/Dockerfile b/authorizer/Dockerfile new file mode 100644 index 0000000..f240eea --- /dev/null +++ b/authorizer/Dockerfile @@ -0,0 +1,7 @@ +FROM mcr.microsoft.com/dotnet/core/runtime:3.1 + +COPY bin/Release/netcoreapp3.1/publish/ App/ +WORKDIR /App +ENTRYPOINT ["dotnet", "authorizer.dll"] + +EXPOSE 7778/tcp \ No newline at end of file diff --git a/authorizer/Program.cs b/authorizer/Program.cs new file mode 100644 index 0000000..62f7484 --- /dev/null +++ b/authorizer/Program.cs @@ -0,0 +1,23 @@ +using System; + +namespace authorizer +{ + class Program + { + static AuthServer server; + static void Main(string[] args) + { + server = new AuthServer(); + + server.Start(); + + string input; + do + { + input = Console.ReadLine(); + } + while(input != "stop"); + server.Stop(); + } + } +} diff --git a/authorizer/Server.cs b/authorizer/Server.cs new file mode 100644 index 0000000..feecd77 --- /dev/null +++ b/authorizer/Server.cs @@ -0,0 +1,61 @@ +using System; +using System.Threading; +using System.Net; +using System.Net.Sockets; +class AuthServer +{ + private int port; + private IPAddress address; + private TcpListener server; + private Thread thread; + private bool running = false; + public AuthServer(string addr = "0.0.0.0", int p = 7778) + { + port = p; + address = IPAddress.Parse(addr); + + server = new TcpListener(address, port); + } + private void ServerLoop() + { + while(running) + { + Console.WriteLine("Waiting for a connection..."); + + TcpClient client = server.AcceptTcpClient(); + Console.WriteLine("Connected!"); + + Byte[] bytes = new byte[256]; + String data = null; + + NetworkStream stream = client.GetStream(); + + 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(); + } + } + + public void Start() + { + server.Start(); + running = true; + ThreadStart entrypoint = new ThreadStart(ServerLoop); + thread = new Thread(entrypoint); + thread.Start(); + } + public void Stop() + { + running = false; + } + public void ForceStop() + { + thread.Join(); + } +} \ No newline at end of file diff --git a/authorizer/authorizer.csproj b/authorizer/authorizer.csproj new file mode 100644 index 0000000..d453e9a --- /dev/null +++ b/authorizer/authorizer.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + +