Serverless scaling and work to authorizer
This commit is contained in:
		@ -6,19 +6,9 @@ namespace authorizer
 | 
				
			|||||||
    class Program
 | 
					    class Program
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        static AuthServer server;
 | 
					        static AuthServer server;
 | 
				
			||||||
        static Redis redis;
 | 
					 | 
				
			||||||
        static void Main(string[] args)
 | 
					        static void Main(string[] args)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            redis = new Redis(Environment.GetEnvironmentVariable("REDIS_HOSTNAME"));
 | 
					            server = new AuthServer();
 | 
				
			||||||
 | 
					 | 
				
			||||||
            redis.SetTest();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            Thread.Sleep(100);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            redis.GetTest();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            /* server = new AuthServer();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            server.Start();
 | 
					            server.Start();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -28,7 +18,7 @@ namespace authorizer
 | 
				
			|||||||
                input = Console.ReadLine();
 | 
					                input = Console.ReadLine();
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            while(input != "stop");
 | 
					            while(input != "stop");
 | 
				
			||||||
            server.Stop(); */
 | 
					            server.Stop();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -4,7 +4,7 @@ using StackExchange.Redis;
 | 
				
			|||||||
class Redis
 | 
					class Redis
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    private ConnectionMultiplexer muxer;
 | 
					    private ConnectionMultiplexer muxer;
 | 
				
			||||||
    private IDatabase conn;
 | 
					    public IDatabase conn;
 | 
				
			||||||
    private string hostname;
 | 
					    private string hostname;
 | 
				
			||||||
    private int port;
 | 
					    private int port;
 | 
				
			||||||
    public Redis(string host = "127.0.0.1", int p = 6379)
 | 
					    public Redis(string host = "127.0.0.1", int p = 6379)
 | 
				
			||||||
@ -25,18 +25,6 @@ class Redis
 | 
				
			|||||||
        Console.WriteLine("Connected to redis server!");
 | 
					        Console.WriteLine("Connected to redis server!");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void SetTest()
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        string test_val = "Potato";
 | 
					 | 
				
			||||||
        conn.StringSet("test_val", test_val);
 | 
					 | 
				
			||||||
        Console.WriteLine("Set value to: " + test_val);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public void GetTest()
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        Console.WriteLine("Value is: " + conn.StringGet("test_val"));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ~Redis()
 | 
					    ~Redis()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        muxer.Close();
 | 
					        muxer.Close();
 | 
				
			||||||
 | 
				
			|||||||
@ -1,44 +1,86 @@
 | 
				
			|||||||
using System;
 | 
					using System;
 | 
				
			||||||
using System.Threading;
 | 
					using System.Threading;
 | 
				
			||||||
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
 | 
					using System.IO;
 | 
				
			||||||
using System.Net;
 | 
					using System.Net;
 | 
				
			||||||
using System.Net.Sockets;
 | 
					using System.Net.Sockets;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					using Amazon.ECS;
 | 
				
			||||||
 | 
					using Amazon.ECS.Model;
 | 
				
			||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
class AuthServer
 | 
					class AuthServer
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    private int port;
 | 
					 | 
				
			||||||
    private IPAddress address;
 | 
					 | 
				
			||||||
    private TcpListener server;
 | 
					 | 
				
			||||||
    private Thread thread;
 | 
					 | 
				
			||||||
    private bool running = false;
 | 
					    private bool running = false;
 | 
				
			||||||
 | 
					    private int port;
 | 
				
			||||||
 | 
					    private Thread thread;
 | 
				
			||||||
 | 
					    private IPAddress address;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Core objects
 | 
				
			||||||
 | 
					    private TcpListener server;
 | 
				
			||||||
 | 
					    private Redis redis;
 | 
				
			||||||
 | 
					    private AmazonECSClient ecs;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public AuthServer(string addr = "0.0.0.0", int p = 7778)
 | 
					    public AuthServer(string addr = "0.0.0.0", int p = 7778)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        port = p;
 | 
					        port = p;
 | 
				
			||||||
        address = IPAddress.Parse(addr);
 | 
					        address = IPAddress.Parse(addr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        redis = new Redis(Environment.GetEnvironmentVariable("REDIS_HOSTNAME"));
 | 
				
			||||||
        server = new TcpListener(address, port);
 | 
					        server = new TcpListener(address, port);
 | 
				
			||||||
 | 
					        ecs = new AmazonECSClient();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    private void ServerLoop()
 | 
					    private void ServerLoop()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        while(running)
 | 
					        while(running)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            Console.WriteLine("Waiting for a connection...");
 | 
					            try
 | 
				
			||||||
 | 
					 | 
				
			||||||
            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);
 | 
					                //Wait for connection
 | 
				
			||||||
                Console.WriteLine("Recieved: {0}", data);
 | 
					                TcpClient client = server.AcceptTcpClient();
 | 
				
			||||||
            }
 | 
					                //Get remote address
 | 
				
			||||||
 | 
					                IPEndPoint endPoint = (IPEndPoint) client.Client.RemoteEndPoint;
 | 
				
			||||||
 | 
					                Console.WriteLine(endPoint.Address.ToString() + " connected!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            client.Close();
 | 
					                //Create streams
 | 
				
			||||||
 | 
					                NetworkStream stream = client.GetStream();
 | 
				
			||||||
 | 
					                StreamWriter writer = new StreamWriter(stream);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                Byte[] bytes = new byte[256];
 | 
				
			||||||
 | 
					                String data = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                writer.Write("Hey there bud!");
 | 
				
			||||||
 | 
					                writer.Flush();
 | 
				
			||||||
 | 
					                Console.WriteLine("HERE");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                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)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                Console.WriteLine("Fatal exception: " + e.ToString());
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -49,6 +91,7 @@ class AuthServer
 | 
				
			|||||||
        ThreadStart entrypoint = new ThreadStart(ServerLoop);
 | 
					        ThreadStart entrypoint = new ThreadStart(ServerLoop);
 | 
				
			||||||
        thread = new Thread(entrypoint);
 | 
					        thread = new Thread(entrypoint);
 | 
				
			||||||
        thread.Start();
 | 
					        thread.Start();
 | 
				
			||||||
 | 
					        Console.WriteLine("Waiting for a connection...");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    public void Stop()
 | 
					    public void Stop()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
@ -56,6 +99,7 @@ class AuthServer
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
    public void ForceStop()
 | 
					    public void ForceStop()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        thread.Join();
 | 
					        running = false;
 | 
				
			||||||
 | 
					        thread.Abort();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -6,6 +6,8 @@
 | 
				
			|||||||
  </PropertyGroup>
 | 
					  </PropertyGroup>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  <ItemGroup>
 | 
					  <ItemGroup>
 | 
				
			||||||
 | 
					    <PackageReference Include="AWSSDK.Core" Version="3.5.0-beta" />
 | 
				
			||||||
 | 
					    <PackageReference Include="AWSSDK.ECS" Version="3.5.0-beta" />
 | 
				
			||||||
    <PackageReference Include="StackExchange.Redis" Version="2.1.39" />
 | 
					    <PackageReference Include="StackExchange.Redis" Version="2.1.39" />
 | 
				
			||||||
  </ItemGroup>
 | 
					  </ItemGroup>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -24,6 +24,7 @@ config/icon="res://icon.png"
 | 
				
			|||||||
MusicManager="*res://nodes/MusicManager.tscn"
 | 
					MusicManager="*res://nodes/MusicManager.tscn"
 | 
				
			||||||
NetworkManager="*res://nodes/NetworkManager.tscn"
 | 
					NetworkManager="*res://nodes/NetworkManager.tscn"
 | 
				
			||||||
ImportantEntities="*res://scripts/singletons/ImportantEntities.gd"
 | 
					ImportantEntities="*res://scripts/singletons/ImportantEntities.gd"
 | 
				
			||||||
 | 
					Authorizer="*res://scripts/network/Authorizer.gd"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[input]
 | 
					[input]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										61
									
								
								client/scripts/network/Authorizer.gd
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								client/scripts/network/Authorizer.gd
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,61 @@
 | 
				
			|||||||
 | 
					extends Node
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					signal auth_connected
 | 
				
			||||||
 | 
					signal auth_disconnected
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var client : StreamPeerTCP = null
 | 
				
			||||||
 | 
					var server_hostname : String = "127.0.0.1"
 | 
				
			||||||
 | 
					var server_port = 7778
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func _ready():
 | 
				
			||||||
 | 
						client = StreamPeerTCP.new()
 | 
				
			||||||
 | 
						client.set_no_delay(true)
 | 
				
			||||||
 | 
						set_process(false)
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
					func auth_connect(host=server_hostname, port=server_port):
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						# Connect if not connected
 | 
				
			||||||
 | 
						if !client.is_connected_to_host():
 | 
				
			||||||
 | 
							server_hostname = host
 | 
				
			||||||
 | 
							server_port = port
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							# Connect Socket & Create Stream
 | 
				
			||||||
 | 
							client.connect_to_host(server_hostname, port)
 | 
				
			||||||
 | 
							
 | 
				
			||||||
 | 
							# Start listening
 | 
				
			||||||
 | 
							set_process(true)
 | 
				
			||||||
 | 
							
 | 
				
			||||||
 | 
							# Validate intial connection
 | 
				
			||||||
 | 
							if client.is_connected_to_host():
 | 
				
			||||||
 | 
								client.put_string("Hey there daddy!")
 | 
				
			||||||
 | 
								emit_signal("auth_connected")
 | 
				
			||||||
 | 
								return true
 | 
				
			||||||
 | 
							else:
 | 
				
			||||||
 | 
								# Timeout implemented in `process` loop
 | 
				
			||||||
 | 
								print("Waiting for host connection...")
 | 
				
			||||||
 | 
								return false
 | 
				
			||||||
 | 
						else:
 | 
				
			||||||
 | 
							print("Client is already connected to server!")
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func auth_disconnect():
 | 
				
			||||||
 | 
						client.disconnect_from_host()
 | 
				
			||||||
 | 
						set_process(false) # Disable listening loop
 | 
				
			||||||
 | 
						print_debug("Disconnected from host.")
 | 
				
			||||||
 | 
						emit_signal("auth_disconnected")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var count = 0
 | 
				
			||||||
 | 
					func _process(delta):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if client.get_available_bytes() > 0:
 | 
				
			||||||
 | 
							print(client.get_available_bytes())
 | 
				
			||||||
 | 
							
 | 
				
			||||||
 | 
							print(client.get_string(client.get_available_bytes()))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						# Await for client connection
 | 
				
			||||||
 | 
						if client.get_status()==1:
 | 
				
			||||||
 | 
							count= count+delta
 | 
				
			||||||
 | 
						if count>1: # if it took more than 1s to connect, error
 | 
				
			||||||
 | 
							print_debug("Failed connect, disconnecting...")
 | 
				
			||||||
 | 
							auth_disconnect() #interrupts connection to nothing
 | 
				
			||||||
@ -1,17 +1,31 @@
 | 
				
			|||||||
extends Node
 | 
					extends Node
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var auth
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func _ready():
 | 
					func _ready():
 | 
				
			||||||
	$"/root/MusicManager".play_music("wizards")
 | 
						$"/root/MusicManager".play_music("wizards")
 | 
				
			||||||
	$Button.connect("button_down", self, "_on_button_press")
 | 
						$Button.connect("button_down", self, "_on_button_press")
 | 
				
			||||||
	$"/root/NetworkManager".connect("error_occured", self, "_on_error")
 | 
						$"/root/NetworkManager".connect("error_occured", self, "_on_error")
 | 
				
			||||||
	$"/root/NetworkManager".connect("logged_in", self, "_on_login")
 | 
						$"/root/NetworkManager".connect("logged_in", self, "_on_login")
 | 
				
			||||||
 | 
						auth = $"/root/Authorizer"
 | 
				
			||||||
 | 
						auth.connect("auth_connected", self, "_on_auth_connection")
 | 
				
			||||||
 | 
						auth.connect("auth_disconnected", self, "_on_auth_disconnection")
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func _on_error():
 | 
					func _on_error():
 | 
				
			||||||
	$ErrorDialog/ErrorLabel.text = $"/root/NetworkManager".error_info
 | 
						$ErrorDialog/ErrorLabel.text = $"/root/NetworkManager".error_info
 | 
				
			||||||
	$ErrorDialog.popup_centered()
 | 
						$ErrorDialog.popup_centered()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func _on_button_press():
 | 
					func _on_button_press():
 | 
				
			||||||
 | 
						auth.auth_connect()
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
					func _on_auth_connection():
 | 
				
			||||||
 | 
						$Button.disabled = true
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
					func _on_auth_disconnection():
 | 
				
			||||||
 | 
						$Button.disabled = false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func _on_button_press_OLD():
 | 
				
			||||||
	if($"/root/NetworkManager".connected):
 | 
						if($"/root/NetworkManager".connected):
 | 
				
			||||||
		$"/root/NetworkManager".disconnect_from_server()
 | 
							$"/root/NetworkManager".disconnect_from_server()
 | 
				
			||||||
	else:
 | 
						else:
 | 
				
			||||||
 | 
				
			|||||||
@ -101,7 +101,7 @@ Resources:
 | 
				
			|||||||
      TemplateURL: !Sub 'https://s3.${AWS::Region}.amazonaws.com/sumu-stacks/dt/${release}/cloudformation/dt/cloudwatch.yaml'
 | 
					      TemplateURL: !Sub 'https://s3.${AWS::Region}.amazonaws.com/sumu-stacks/dt/${release}/cloudformation/dt/cloudwatch.yaml'
 | 
				
			||||||
      Parameters:
 | 
					      Parameters:
 | 
				
			||||||
        environment: !Ref environment
 | 
					        environment: !Ref environment
 | 
				
			||||||
        Cluster: !GetAtt EcsCluster.Outputs.Cluster
 | 
					        Cluster: !GetAtt EcsCluster.Outputs.ClusterArn
 | 
				
			||||||
        LambdaArn: !GetAtt LambdaFunctions.Outputs.TaskListManager
 | 
					        LambdaArn: !GetAtt LambdaFunctions.Outputs.TaskListManager
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  #---------
 | 
					  #---------
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,15 @@
 | 
				
			|||||||
import json
 | 
					import redis
 | 
				
			||||||
 | 
					import json, os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def lambda_handler(event, context):
 | 
					def lambda_handler(event, context):
 | 
				
			||||||
    print(json.dumps(event))
 | 
					    r = redis.Redis(host=os.environ['REDIS_HOST'], port=6379, db=0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if event["detail"]["group"] == "service:" + os.environ["ECS_SERVICE"]:
 | 
				
			||||||
 | 
					        desired = event["detail"]["desiredStatus"]
 | 
				
			||||||
 | 
					        last = event["detail"]["lastStatus"]
 | 
				
			||||||
 | 
					        if desired == "RUNNING" and desired == last:
 | 
				
			||||||
 | 
					            print("Added task: " + event["detail"]["taskArn"])
 | 
				
			||||||
 | 
					            r.lpush("tasks", event["detail"]["taskArn"])
 | 
				
			||||||
 | 
					        elif desired == "STOPPED" or last == "STOPPED":
 | 
				
			||||||
 | 
					            r.lrem("tasks", event["detail"]["taskArn"], 1)
 | 
				
			||||||
 | 
					            print("Removed task: " + event["detail"]["taskArn"], 1)
 | 
				
			||||||
@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					redis
 | 
				
			||||||
		Reference in New Issue
	
	Block a user