//  
//   Rebex Sample Code License
// 
//   Copyright 2026, REBEX CR s.r.o.
//   All rights reserved.
//   https://www.rebex.net/
// 
//   Permission to use, copy, modify, and/or distribute this software for any
//   purpose with or without fee is hereby granted.
// 
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//   OTHER DEALINGS IN THE SOFTWARE.
// 

using System;

namespace Rebex.Samples
{
    /// <summary>
    /// This sample demonstrates proxy server, which forwards data between two endpoints.
    /// Incoming connection uses TLS layer to secure the data.
    /// Outgoing connection does not use any encryption.
    /// This can be used to secure legacy services with lack of TLS support,
    /// to be accessed securely from the Internet using modern clients with TLS support.
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            // set the trial license key
            Rebex.Licensing.Key = Rebex.Samples.TrialKey.Key;


            // - initialize TLS Proxy configuration
            // - parse input arguments
            // - display help on errors
            var config = new TlsProxyConfiguration();
            try
            {
                config.Parse(args);
            }
            catch (TlsProxyConfiguration.ParseException ex)
            {
                ShowHelp(ex.Message);
                return;
            }
            catch (Exception ex)
            {
                ShowHelp(ex.ToString());
                return;
            }

            // - initialize TLS Proxy object with specified configuration
            // - start TLS Proxy
            // - wait for ENTER to quit the proxy
            using (var proxy = new TlsProxy(config))
            {
                Console.WriteLine("Starting Proxy...");

                proxy.Start();

                Console.WriteLine("Proxy started.");
                Console.WriteLine("Press ENTER to stop the Proxy.");
                Console.ReadLine();
            }
        }

        /// <summary>
        /// Displays help.
        /// </summary>
        private static void ShowHelp(string error)
        {
            string applicationName = AppDomain.CurrentDomain.FriendlyName;
            Console.WriteLine("=====================================================================");
            Console.WriteLine(" {0} ", applicationName);
            Console.WriteLine("=====================================================================");
            Console.WriteLine();
            Console.WriteLine("Provides TLS proxy.");
            Console.WriteLine();
            Console.WriteLine("The program is a sample for Rebex .NET components.");
            Console.WriteLine("For more info, see http://www.rebex.net/");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Syntax: {0} IP_address:port host:port certificate_path [certificate_password]", applicationName);
            Console.WriteLine();
            Console.WriteLine(@"Example: {0} 0.0.0.0:443 192.168.0.1:80 c:\data\cert.pfx password", applicationName);
            Console.WriteLine(@"Example: {0} 0.0.0.0:443 httpbin.org:80 c:\data\cert.pfx password", applicationName);
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("ERROR: {0}", error);
            Console.WriteLine();
        }
    }
}
