Beispiel #1
0
        public void Connexion()
        {
            if (_connexionFile == null)
            {
                throw new PBException("DebriderDebridLink connexion file is null");
            }
            //if (_serverTimeFile == null)
            //    throw new PBException("DebriderDebridLink server time file is null");

            pb.Trace.WriteLine("{0:dd-MM-yyyy HH:mm:ss} - new connexion to debrid-link.fr", DateTime.Now);

            if (__trace)
            {
                pb.Trace.WriteLine("DebriderDebridLink.Connexion() :");
            }

            string url = __url + string.Format("/token/{0}/new", _publicKey);

            HttpRequestParameters requestParameters = new HttpRequestParameters {
                Encoding = Encoding.UTF8
            };
            DateTime dt = DateTime.Now;

            Http.Http http = HttpManager.CurrentHttpManager.Load(new HttpRequest {
                Url = url
            }, requestParameters);
            BsonDocument result = BsonSerializer.Deserialize <BsonDocument>(http.ResultText);

            if (__trace)
            {
                pb.Trace.WriteLine("  result                      :");
                pb.Trace.WriteLine(result.zToJson());
            }
            DebridLinkConnexion connexion = new DebridLinkConnexion();

            //DebridLinkServerTime serverTime = new DebridLinkServerTime();
            connexion.ConnexionTime = dt;
            //string token = doc.zGet("value.token").zAsString();
            connexion.Token = result.zGet("value.token").zAsString();
            string validTokenUrl = result.zGet("value.validTokenUrl").zAsString();

            //string key = doc.zGet("value.key").zAsString();
            connexion.Key = result.zGet("value.key").zAsString();
            int ts = result.zGet("ts").zAsInt();

            connexion.ClientTime        = dt;
            connexion.ServerTime        = zdate.UnixTimeStampToDateTime(ts);
            connexion.ServerTimeGap     = connexion.ServerTime - dt;
            connexion.ConnexionLifetime = _connexionLifetime;
            connexion.EndConnexionTime  = connexion.ConnexionTime + GetConnexionTimespan(connexion.ConnexionLifetime) - TimeSpan.FromMinutes(5);
            if (__trace)
            {
                pb.Trace.WriteLine("  request time                : \"{0:dd/MM/yyyy HH:mm:ss}\"", dt);
                pb.Trace.WriteLine("  result                      : \"{0}\"", result.zGet("result").zAsString());
                pb.Trace.WriteLine("  token                       : \"{0}\"", connexion.Token);
                pb.Trace.WriteLine("  validTokenUrl               : \"{0}\"", validTokenUrl);
                pb.Trace.WriteLine("  key                         : \"{0}\"", connexion.Key);
                pb.Trace.WriteLine("  server time                 : {0} - {1:dd/MM/yyyy HH:mm:ss}", ts, connexion.ServerTime);
                pb.Trace.WriteLine("  server time gap             : {0}", connexion.ServerTimeGap);
            }

            // validTokenUrl : "https://secure.debrid-link.fr/user/2_2d481d8991e4db60f43d24d9d387b75699db7a0157182967/login"
            http = HttpManager.CurrentHttpManager.Load(new HttpRequest {
                Url = validTokenUrl
            }, requestParameters);

            // <script>if (window!=window.top) { top.location.href='https://secure.debrid-link.fr/login'; }</script>
            // <form action='' method='POST' class='form-horizontal'>
            // <input type='text' class='form-control' name='user'>
            // <input type='password' class='form-control' name='password'>
            // <select name='sessidTime' class='form-control'>
            // <option value='all'  selected='selected'> Toujours</option>
            // ...
            // </select>
            // <input type='hidden' value='10_a3a206c4398f195283a4843d44f017f3211275e443747173' name='token'>
            // <button type='submit' name='authorizedToken' value='1' class='btn btn-dl'>Envoyer</button>
            // <input type='submit' style='display:none'>

            XXElement xeSource = http.zGetXDocument().zXXElement();

            // le script n'est plus là dans la page html 24/03/2015
            // script : if (window!=window.top) { top.location.href='https://secure.debrid-link.fr/login'; }
            //string script = xeSource.XPathValue("//head//script//text()");
            //if (script == null)
            //{
            //    //Trace.WriteLine("//head//script not found");
            //    //return;
            //    throw new PBException("DebriderDebridLink.Connect() : //head//script not found");
            //}
            //if (__trace)
            //    pb.Trace.WriteLine("  script                      : \"{0}\"", script);
            //Regex rg = new Regex("top\\.location\\.href=[\"'](.*)[\"']", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
            //Match match = rg.Match(script);
            //if (!match.Success)
            //{
            //    //Trace.WriteLine("top.location.href='...' not found in script");
            //    //return;
            //    throw new PBException("DebriderDebridLink.Connect() : top.location.href='...' not found in script");
            //}
            //url = match.Groups[1].Value;

            url = "https://secure.debrid-link.fr/login";
            if (__trace)
            {
                pb.Trace.WriteLine("  login url                   : \"{0}\"", url);
            }

            XXElement xeForm = xeSource.XPathElement("//form");
            string    action = xeForm.AttribValue("action");

            if (__trace)
            {
                pb.Trace.WriteLine("  form action                 : \"{0}\"", action);
            }
            if (action != null && action != "")
            {
                url = action;
            }
            HttpRequestMethod method = HttpTools.GetHttpRequestMethod(xeForm.AttribValue("method"));

            if (__trace)
            {
                pb.Trace.WriteLine("  form method                 : {0}", method);
            }

            StringBuilder content = new StringBuilder();
            bool          first = true;
            string        name, value;

            foreach (XXElement xe in xeForm.DescendantFormItems())
            {
                name = xe.AttribValue("name");
                if (name == null)
                {
                    continue;
                }
                if (name == "user")
                {
                    value = _login;
                }
                else if (name == "password")
                {
                    value = _password;
                }
                else if (name == "sessidTime")
                {
                    value = GetConnexionLifetime(_connexionLifetime);
                }
                else
                {
                    value = xe.AttribValue("value");
                }
                if (!first)
                {
                    content.Append('&');
                }
                content.AppendFormat("{0}={1}", name, value);
                if (__trace)
                {
                    if (name != "password")
                    {
                        pb.Trace.WriteLine("  {0}={1}", name, value);
                    }
                    else
                    {
                        pb.Trace.WriteLine("  {0}=xxx", name);
                    }
                }
                first = false;
            }

            // "user=la_beuze&password=xxxxxx&sessidTime=all&token=10_56b51ee12ad5dabcac620230cda436cab94bd37154742765&authorizedToken=1"
            //if (__trace)
            //    pb.Trace.WriteLine("content : \"{0}\"", content.ToString());

            http = HttpManager.CurrentHttpManager.Load(new HttpRequest {
                Url = url, Method = method, Content = content.ToString()
            }, requestParameters);

            // <div class='panel-body'>
            // <div class='alert alert-success'>
            // La session a bien été activée. Vous pouvez utiliser l'application API Test
            // </div>
            // </div>
            xeSource = http.zGetXDocument().zXXElement();
            //string loginMessage = xeSource.ExplicitXPathValue("//div[@class='panel-body']//text()");
            string loginMessage = xeSource.ExplicitXPathValue("//div[@class='alert alert-success']//text()");

            if (__trace)
            {
                pb.Trace.WriteLine("  login message               : \"{0}\"", loginMessage);
            }
            //if (loginMessage == null || !loginMessage.Trim().StartsWith("La session a bien été activée", StringComparison.InvariantCultureIgnoreCase))
            if (loginMessage == null || !loginMessage.Trim().StartsWith("Vous avez été connecté avec succès", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new PBException("DebriderDebridLink.Connect() : wrong login message \"{0}\"", loginMessage);
            }


            connexion.zSave(_connexionFile);
            _connexion = connexion;
            //serverTime.zSave(_serverTimeFile);
            //_serverTime = serverTime;
        }
Beispiel #2
0
        private void _Login(string url)
        {
            // https://debrid-link.fr/user/2_21c744ba958f13fac08ee5c8855f72ab9a3b3e3224789126/login
            Http.Http http = HttpManager.CurrentHttpManager.Load(new HttpRequest {
                Url = url
            }, _requestParameters);
            XXElement xeSource = HttpManager.CurrentHttpManager.GetXDocument(http).zXXElement();

            string    loginUrl = __loginUrl;
            XXElement xeForm   = xeSource.XPathElement("//form");

            string action = xeForm.AttribValue("action");

            if (action != null && action != "")
            {
                loginUrl = action;
            }

            string            method     = xeForm.AttribValue("method");
            HttpRequestMethod httpMethod = HttpRequestMethod.Get;

            if (method != null && method != "")
            {
                httpMethod = HttpTools.GetHttpRequestMethod(method);
            }

            StringBuilder content = new StringBuilder();
            bool          first = true;
            string        name, value;

            foreach (XXElement xe in xeForm.DescendantFormItems())
            {
                name = xe.AttribValue("name");
                if (name == null)
                {
                    continue;
                }
                if (name == "user")
                {
                    value = _login;
                }
                else if (name == "password")
                {
                    value = _password;
                }
                //else if (name == "sessidTime")
                //    value = GetConnexionLifetime(_connexionLifetime);
                else
                {
                    value = xe.AttribValue("value");
                }
                if (!first)
                {
                    content.Append('&');
                }
                content.AppendFormat("{0}={1}", name, value);
                if (__trace)
                {
                    if (name != "password")
                    {
                        pb.Trace.WriteLine("  {0}={1}", name, value);
                    }
                    else
                    {
                        pb.Trace.WriteLine("  {0}=xxx", name);
                    }
                }
                first = false;
            }

            if (__trace)
            {
                pb.Trace.WriteLine("  form login url              : \"{0}\"", loginUrl);
                pb.Trace.WriteLine("  form action                 : \"{0}\"", action);
                pb.Trace.WriteLine("  form method                 : {0}", httpMethod);
                //pb.Trace.WriteLine("  form values                 : {0}", content.ToString());
            }

            http = HttpManager.CurrentHttpManager.Load(new HttpRequest {
                Url = loginUrl, Method = httpMethod, Content = content.ToString()
            }, _requestParameters);

            xeSource = http.zGetXDocument().zXXElement();
            //<div class="alert alert-success">
            XXElement xeLogin = xeSource.XPathElement("//div[@class='alert alert-success']");

            if (xeLogin.XElement == null)
            {
                throw new PBException("can't login to debrid-link.fr");
            }
        }
Beispiel #3
0
        public static void Test_DebridLink_01()
        {
            //HttpRun.Load("https://api.debrid-link.fr/rest/token/1R6858wC6lO15X8i/new");
            string urlBase = "https://api.debrid-link.fr/rest/";
            //string login = RunSource.CurrentRunSource.Config.GetConfig("LocalConfig").GetExplicit("DownloadAutomateManager/DebridLink/Login");
            string login    = XmlConfig.CurrentConfig.GetConfig("LocalConfig").GetExplicit("DownloadAutomateManager/DebridLink/Login");
            string password = XmlConfig.CurrentConfig.GetConfig("LocalConfig").GetExplicit("DownloadAutomateManager/DebridLink/Password");
            //string publickey = "1R6858wC6lO15X8i";
            string publickey = XmlConfig.CurrentConfig.GetConfig("LocalConfig").GetExplicit("DownloadAutomateManager/DebridLink/PublicKey");
            //string sessidTime = "all";
            string sessidTime = XmlConfig.CurrentConfig.GetConfig("LocalConfig").GetExplicit("DownloadAutomateManager/DebridLink/SessidTime");
            string url        = urlBase + string.Format("token/{0}/new", publickey);
            HttpRequestParameters requestParameters = new HttpRequestParameters {
                Encoding = Encoding.UTF8
            };
            Http http = HttpManager.CurrentHttpManager.Load(new HttpRequest {
                Url = url
            }, requestParameters);
            DateTime dt = DateTime.Now;

            http.ResultText.zTraceJson();
            BsonDocument doc           = BsonSerializer.Deserialize <BsonDocument>(http.ResultText);
            string       token         = doc.zGet("value.token").zAsString();
            string       validTokenUrl = doc.zGet("value.validTokenUrl").zAsString();
            string       key           = doc.zGet("value.key").zAsString();
            int          ts            = doc.zGet("ts").zAsInt();

            Trace.WriteLine("request time   : \"{0:dd/MM/yyyy HH:mm:ss}\"", dt);
            Trace.WriteLine("result         : \"{0}\"", doc.zGet("result").zAsString());
            Trace.WriteLine("token          : \"{0}\"", token);
            Trace.WriteLine("validTokenUrl  : \"{0}\"", validTokenUrl);
            Trace.WriteLine("key            : \"{0}\"", key);
            Trace.WriteLine("ts             : \"{0}\"", ts);
            Trace.WriteLine("ts             : \"{0:dd/MM/yyyy HH:mm:ss}\"", zdate.UnixTimeStampToDateTime(ts));
            Trace.WriteLine("ts             : \"{0}\"", zdate.UnixTimeStampToDateTime(ts) - dt);

            // validTokenUrl : "https://secure.debrid-link.fr/user/2_2d481d8991e4db60f43d24d9d387b75699db7a0157182967/login"
            http = HttpManager.CurrentHttpManager.Load(new HttpRequest {
                Url = validTokenUrl
            }, requestParameters);

            // <script>if (window!=window.top) { top.location.href='https://secure.debrid-link.fr/login'; }</script>
            // <form action='' method='POST' class='form-horizontal'>
            // <input type='text' class='form-control' name='user'>
            // <input type='password' class='form-control' name='password'>
            // <input type='hidden' value='10_a3a206c4398f195283a4843d44f017f3211275e443747173' name='token'>
            // <input type='submit' style='display:none'>
            // <button type='submit' name='authorizedToken' value='1' class='btn btn-dl'>Envoyer</button>

            XXElement xeSource = http.zGetXDocument().zXXElement();

            // script : if (window!=window.top) { top.location.href='https://secure.debrid-link.fr/login'; }
            string script = xeSource.XPathValue("//head//script//text()");

            if (script == null)
            {
                Trace.WriteLine("//head//script not found");
                return;
            }
            Trace.WriteLine("script : \"{0}\"", script);
            Regex rg    = new Regex("top\\.location\\.href=[\"'](.*)[\"']", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
            Match match = rg.Match(script);

            if (!match.Success)
            {
                Trace.WriteLine("top.location.href='...' not found in script");
                return;
            }
            url = match.Groups[1].Value;
            Trace.WriteLine("login url : \"{0}\"", url);

            XXElement xeForm = xeSource.XPathElement("//form");
            string    action = xeForm.AttribValue("action");

            Trace.WriteLine("form action : \"{0}\"", action);
            if (action != null && action != "")
            {
                url = action;
            }
            HttpRequestMethod method = Http.GetHttpRequestMethod(xeForm.AttribValue("method"));

            Trace.WriteLine("form method : {0}", method);

            //XmlConfig localConfig = new XmlConfig(RunSource.CurrentRunSource.Config.GetExplicit("LocalConfig"));
            //string login = localConfig.GetExplicit("DownloadAutomateManager/DebridLink/Login");
            //string password = localConfig.GetExplicit("DownloadAutomateManager/DebridLink/Password");
            StringBuilder content = new StringBuilder();
            bool          first = true;
            string        name, value;

            //foreach (XXElement xe in xeForm.XPathElements(".//input"))
            foreach (XXElement xe in xeForm.DescendantFormItems())
            {
                name = xe.AttribValue("name");
                if (name == null)
                {
                    continue;
                }
                if (name == "user")
                {
                    value = login;
                }
                else if (name == "password")
                {
                    value = password;
                }
                else if (name == "sessidTime")
                {
                    value = sessidTime;
                }
                else
                {
                    value = xe.AttribValue("value");
                }
                if (!first)
                {
                    content.Append('&');
                }
                content.AppendFormat("{0}={1}", name, value);
                Trace.WriteLine("{0}={1}", name, value);
                first = false;
            }
            //XXElement xeButton = xeForm.XPathElement(".//button");
            //name = xeButton.AttribValue("name");
            //value = xeButton.AttribValue("value");
            //if (name != null && value != null)
            //{
            //    content.AppendFormat("&{0}={1}", name, value);
            //    Trace.WriteLine("{0}={1}", name, value);
            //}

            // "user=la_beuze&password=xxxxxx&sessidTime=all&token=10_56b51ee12ad5dabcac620230cda436cab94bd37154742765&authorizedToken=1"
            //  user=la_beuze&password=pbeuz0&sessidTime=all&token=10_3205776c76bb0479b1d57e9bf834b38ae2c5d10669848384&authorizedToken=1
            Trace.WriteLine("content : \"{0}\"", content.ToString());
            http = HttpManager.CurrentHttpManager.Load(new HttpRequest {
                Url = url, Method = method, Content = content.ToString()
            }, requestParameters);

            // <div class='panel-body'>
            // <div class='alert alert-success'>
            // La session a bien été activée. Vous pouvez utiliser l'application API Test
            // </div>
            // </div>
            xeSource = http.zGetXDocument().zXXElement();
            string loginMessage = xeSource.XPathValue("//div[@class='panel-body']//text()").Trim();

            Trace.WriteLine("login message : \"{0}\"", loginMessage);
        }