using System;
using System.Linq;
using System.Threading;
using System.IO;

namespace Host
{
    public partial class Admin : System.Web.UI.Page
    {
        private static readonly object _Lock = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            var s = String.Empty;
            var tenant = Request.Params["k"];
            var p = Request.Params["p"];

            if (!String.IsNullOrEmpty(tenant) && p == "6ca793d9e460")
            {
                var lDir = AppDomain.CurrentDomain.BaseDirectory;
                var lPath = Path.Combine(lDir, "server.txt");

                if (File.Exists(lPath))
                {
                    Monitor.Enter(_Lock);
                    using (var uFS = File.Open(lPath, FileMode.Open))
                    {
                        var r = new StreamReader(uFS);
                        var text = r.ReadToEnd();
                        var tenants = text.Split(new[] { ';', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

                        var dict = tenants.ToDictionary(
                            a => a.Split('=')[0], b => b.Split('=')[1]);
                        if (dict.ContainsKey(tenant))
                            s = dict[tenant];
                    }
                    Monitor.Exit(_Lock);
                }
            }
            else
            {
	            s = Request.UserHostAddress;
            }

            Response.Write(s);
        }
    }
}