/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package neembuu.uploader.accounts;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.net.ssl.SSLContext;
import neembuu.uploader.translation.Translation;
import neembuu.uploader.exceptions.NUException;
import neembuu.uploader.exceptions.accounts.NUInvalidLoginException;
import neembuu.uploader.httpclient.NUHttpClient;
import neembuu.uploader.httpclient.httprequest.NUHttpPost;
import neembuu.uploader.interfaces.abstractimpl.AbstractAccount;
import neembuu.uploader.utils.NULogger;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import neembuu.uploader.utils.NUHttpClientUtils;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
/**
*
* @author Paralytic
*/
public class OpenLoadAccount extends AbstractAccount{
private final HttpClient httpclient = NUHttpClient.getHttpClient();
private HttpResponse httpResponse;
private NUHttpPost httpPost;
private CookieStore cookieStore;
private String responseString;
private Document doc;
private String csrfToken = "";
public OpenLoadAccount() {
KEY_USERNAME = "opnldusername";
KEY_PASSWORD = "opnldpassword";
HOSTNAME = "OpenLoad.co";
setupSsl();
}
@Override
public void disableLogin() {
resetLogin();
NULogger.getLogger().log(Level.INFO, "{0} account disabled", getHOSTNAME());
}
@Override
public void login() {
loginsuccessful = false;
try {
initialize();
NULogger.getLogger().info("Trying to log in to OpenLoad.co");
httpPost = new NUHttpPost("https://openload.co/login");
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("_csrf", csrfToken));
formparams.add(new BasicNameValuePair("LoginForm[rememberMe]", "1"));
formparams.add(new BasicNameValuePair("LoginForm[email]", getUsername()));
formparams.add(new BasicNameValuePair("LoginForm[password]", getPassword()));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
httpPost.setEntity(entity);
httpResponse = httpclient.execute(httpPost, httpContext);
NULogger.getLogger().info(httpResponse.getStatusLine().toString());
responseString = NUHttpClientUtils.getData("https://openload.co/upload", httpContext);
if (responseString.contains("logout")) {
EntityUtils.consume(httpResponse.getEntity());
loginsuccessful = true;
username = getUsername();
password = getPassword();
NULogger.getLogger().info("OpenLoad.co login successful!");
} else {
//Get error message
responseString = EntityUtils.toString(httpResponse.getEntity());
Document doc = Jsoup.parse(responseString);
String error = doc.select(".err").first().text();
if("Incorrect Login or Password".equals(error)){
throw new NUInvalidLoginException(getUsername(), HOSTNAME);
}
//Generic exception
throw new Exception("Login error: " + error);
}
} catch(NUException ex){
resetLogin();
ex.printError();
accountUIShow().setVisible(true);
} catch (Exception e) {
resetLogin();
NULogger.getLogger().log(Level.SEVERE, "{0}: {1}", new Object[]{getClass().getName(), e});
showWarningMessage( Translation.T().loginerror(), HOSTNAME);
accountUIShow().setVisible(true);
}
}
private void initialize() throws Exception {
httpContext = new BasicHttpContext();
cookieStore = new BasicCookieStore();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
NULogger.getLogger().info("Getting startup cookies & link from OpenLoad.co");
responseString = NUHttpClientUtils.getData("https://openload.co/login", httpContext);
doc = Jsoup.parse(responseString);
csrfToken = doc.select("form[id=login-form]").first().select("input[name=_csrf]").attr("value");
}
private void resetLogin(){
loginsuccessful = false;
username = "";
password = "";
}
private void setupSsl() {
SSLSocketFactory sf = null;
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, null);
} catch (NoSuchAlgorithmException e) {
NULogger.getLogger().log(Level.SEVERE, "OpenLoad.co -> SSL error", e);
} catch (KeyManagementException e) {
NULogger.getLogger().log(Level.SEVERE, "OpenLoad.co -> SSL error", e);
}
try {
sf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
} catch (Exception e) {
NULogger.getLogger().log(Level.SEVERE, "OpenLoad.co -> SSL error", e);
}
Scheme scheme = new Scheme("https", 443, sf);
httpclient.getConnectionManager().getSchemeRegistry().register(scheme);
}
}