/// <summary>
        /// The Handler function triggered by the AWS APIGateway.
        /// </summary>
        /// <param name="gwEvent">Data passed from the APIGateway to the handler. The Handler only checks for one QueryParameter withe the name "d" and then tries to decode its value.</param>
        /// <returns>The response that will beconverted to a HTTP response by the API Gateway</returns>
        /// <example>Parameter d structure: content of the parameter is aes encrypted and base62 encoded. Format: "[optional salt];[validuntil ISO 8601];[target url urlencoded];[target ip];[salt]*"</example>
        public async Task <APIGatewayHttpApiV2ProxyResponse> Handler(APIGatewayHttpApiV2ProxyRequest gwEvent)
        {
            var key    = Environment.GetEnvironmentVariable("ENCRYPTION_KEY");
            var dataok = gwEvent.QueryStringParameters.TryGetValue("d", out var data);

            if (!dataok)
            {
                return(new APIGatewayHttpApiV2ProxyResponse()
                {
                    StatusCode = 400, Body = "Missing Data"
                });
            }

            try
            {
                var prd  = ProxyRequestDataDecoder.CreateFromEncodedAndEncrypted(data, key);
                var body = $"ValidUntil: {prd.ValidUntil:O}  -  SourceIP: {prd.IP}  -  Url: {prd.Url}";
                if (prd.ValidUntil < DateTime.Now)
                {
                    LambdaLogger.Log($"Token outdated : {body}");
                    return(new APIGatewayHttpApiV2ProxyResponse()
                    {
                        StatusCode = 400, Body = "Token not valid anymore."
                    });
                }

                if (!prd.Debug)
                {
                    var cli = new HttpClient();
                    LambdaLogger.Log($"Triggering Request: {body}");
                    var result = await cli.SendAsync(CreateRequest(prd));

                    LambdaLogger.Log($"Request result: {result.StatusCode}");
                    return(new APIGatewayHttpApiV2ProxyResponse()
                    {
                        StatusCode = 200, Body = "MyFunc"
                    });
                }
                else
                {
                    LambdaLogger.Log($"Debug Request: {body}");
                    return(new APIGatewayHttpApiV2ProxyResponse()
                    {
                        StatusCode = 200, Body = body
                    });
                }
            }
            catch (Exception ex)
            {
                LambdaLogger.Log($"Exception while processing: {ex.Message}");
                LambdaLogger.Log(ex.StackTrace);
                return(new APIGatewayHttpApiV2ProxyResponse()
                {
                    StatusCode = 400, Body = ex.Message
                });
            }
        }
Exemple #2
0
        private static void DecodeToken()
        {
            DisplayHeader();
            Console.WriteLine($"Key:         {_key}");
            Console.WriteLine();
            DisplaySubheader("Decode Token");
            var token = EasyConsole.Input.ReadString("Encoded & Encrypted token:");

            try
            {
                _tokenData = ProxyRequestDataDecoder.CreateFromEncodedAndEncrypted(token, _key);
                _token     = token;
                Console.WriteLine();
                DisplayToken();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Decoding token failed: {ex.Message}");
            }
            ContinueWithEnter();
        }
 public void TestNullInput()
 {
     Assert.Throws <ParsingException>(() => ProxyRequestDataDecoder.CreateFrom(null));
 }
        public void TestInputWithCorrectUrl(string arg, string expected)
        {
            var result = ProxyRequestDataDecoder.CreateFrom("2020-05-31T15:52:15.100Z;" + arg + ";192.168.2.1");

            Assert.AreEqual(expected, result.Url);
        }
 public void TestInputWithInvalidIp(string arg)
 {
     Assert.Throws <ParsingException>(() => ProxyRequestDataDecoder.CreateFrom("2020-05-31T15:52:15.100Z;http%3a%2f%2fwww.heise.de%2fq%3fd%3dhello+world;" + arg));
 }
 public void TestInputWithInvalidUrl(string arg)
 {
     Assert.Throws <ParsingException>(() => ProxyRequestDataDecoder.CreateFrom("2020-05-31T15:52:15.100Z;" + arg + ";192.168.2.1"));
 }
 public void TestInputWithInvalidTimestamp(string arg)
 {
     Assert.Throws <ParsingException>(() => ProxyRequestDataDecoder.CreateFrom(arg + ";http%3a%2f%2fwww.heise.de%2fq%3fd%3dhello+world;192.168.2.1"));
 }
 public void TestInputWithInvalidChunkCount(string arg)
 {
     Assert.Throws <ParsingException>(() => ProxyRequestDataDecoder.CreateFrom(arg));
 }