private DebridLinkConnexion CreateDebridLinkConnexion(DebridLinkFrRequestResult tokenResult) { DebridLinkConnexion connexion = new DebridLinkConnexion(); connexion.ConnexionTime = tokenResult.RequestTime; connexion.Token = tokenResult.Result.zGet("value.token").zAsString(); connexion.Key = tokenResult.Result.zGet("value.key").zAsString(); int ts = tokenResult.Result.zGet("ts").zAsInt(); connexion.ClientTime = tokenResult.RequestTime; connexion.ServerTime = zdate.UnixTimeStampToDateTime(ts); connexion.ServerTimeGap = connexion.ServerTime - tokenResult.RequestTime; connexion.ConnexionLifetime = _connexionLifetime; connexion.EndConnexionTime = connexion.ConnexionTime + GetConnexionTimespan(connexion.ConnexionLifetime) - TimeSpan.FromMinutes(5); return connexion; }
public bool IsConnected() { if (_connexion == null) { if (_connexionFile == null) throw new PBException("DebriderDebridLink connexion file is null"); if (!zFile.Exists(_connexionFile)) return false; _connexion = zmongo.ReadFileAs<DebridLinkConnexion>(_connexionFile); } if (_connexion.EndConnexionTime != null && DateTime.Now >= _connexion.EndConnexionTime) { // la connection n'est plus valable, il faut se reconnecter return false; } return true; }
public void Connexion() { 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() :"); } DebridLinkFrRequestResult tokenResult = GetNewToken(); DebridLinkConnexion connexion = CreateDebridLinkConnexion(tokenResult); string validTokenUrl = tokenResult.Result.zGet("value.validTokenUrl").zAsString(); if (__trace) { pb.Trace.WriteLine(" request time : \"{0:dd/MM/yyyy HH:mm:ss}\"", tokenResult.RequestTime); pb.Trace.WriteLine(" result : \"{0}\"", tokenResult.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}", tokenResult.Result.zGet("ts").zAsInt(), connexion.ServerTime); pb.Trace.WriteLine(" server time gap : {0}", connexion.ServerTimeGap); } _Login(validTokenUrl); connexion.zSave(_connexionFile); _connexion = connexion; if (_traceData != null) _traceData.Trace(new BsonDocument { { "OpeType", "Debrider.DebridLinkFr" }, { "Ope", "Login" }, { "Param", new BsonDocument { } }, { "Result", connexion.ToBsonDocument() } }); }
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 = 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 = Http.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; }