|
| 1 | +package net.zomis.duga.chat; |
| 2 | + |
| 3 | +import com.gistlabs.mechanize.document.html.HtmlDocument; |
| 4 | +import com.gistlabs.mechanize.document.html.form.Form; |
| 5 | +import com.gistlabs.mechanize.document.html.form.SubmitButton; |
| 6 | +import com.gistlabs.mechanize.impl.MechanizeAgent; |
| 7 | +import org.apache.http.HttpRequest; |
| 8 | +import org.apache.http.HttpResponse; |
| 9 | +import org.apache.http.ProtocolException; |
| 10 | +import org.apache.http.client.RedirectStrategy; |
| 11 | +import org.apache.http.client.methods.HttpGet; |
| 12 | +import org.apache.http.client.methods.HttpUriRequest; |
| 13 | +import org.apache.http.protocol.HttpContext; |
| 14 | + |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.List; |
| 17 | +import java.util.logging.Logger; |
| 18 | + |
| 19 | +public class StackExchangeLogin implements LoginFunction { |
| 20 | + private static final Logger logger = Logger.getLogger(StackExchangeLogin.class.getName()); |
| 21 | + |
| 22 | + @Override |
| 23 | + public MechanizeAgent constructAgent(BotConfiguration configuration) { |
| 24 | + MechanizeAgent agent = new MechanizeAgent(); |
| 25 | + agent.getClient().setRedirectStrategy(new RedirectStrategy() { |
| 26 | + @Override |
| 27 | + public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { |
| 28 | + return response.getStatusLine().getStatusCode() == 302; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { |
| 33 | + System.out.println(Arrays.toString(response.getAllHeaders())); |
| 34 | + String host = request.getFirstHeader("Host").getValue(); |
| 35 | + String location = response.getFirstHeader("Location").getValue(); |
| 36 | + String protocol = host.equals("openid.stackexchange.com") ? "https" : "http"; |
| 37 | + if (location.startsWith("http://") || location.startsWith("https://")) { |
| 38 | + logger.info("Redirecting to " + location); |
| 39 | + return new HttpGet(location); |
| 40 | + } else { |
| 41 | + logger.info("Redirecting to " + protocol + "://" + host + location); |
| 42 | + return new HttpGet(protocol + "://" + host + location); |
| 43 | + } |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + agent.getClient().addRequestInterceptor((request, context) -> { |
| 48 | + logger.info("Request to " + request.getRequestLine().getUri()); |
| 49 | + if (request.getRequestLine().getUri().equals("/login/global-fallback")) { |
| 50 | + request.addHeader("Referer", configuration.getRootUrl() + "/users/chat-login"); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + return agent; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public String retrieveFKey(MechanizeAgent agent, BotConfiguration configuration) { |
| 59 | + loginOpenId(agent, configuration); |
| 60 | + loginRoot(agent, configuration); |
| 61 | + loginChat(agent, configuration); |
| 62 | + return retrieveFKeyReal(agent, configuration); |
| 63 | + } |
| 64 | + |
| 65 | + private void loginOpenId(MechanizeAgent agent, BotConfiguration configuration) { |
| 66 | + HtmlDocument openIdLoginPage = agent.get("https://openid.stackexchange.com/account/login"); |
| 67 | + System.out.println(openIdLoginPage); |
| 68 | + System.out.println(openIdLoginPage.getRoot()); |
| 69 | + Form loginForm = openIdLoginPage.forms().getAll().get(0); |
| 70 | + loginForm.get("email").setValue(configuration.getBotEmail()); |
| 71 | + loginForm.get("password").setValue(configuration.getBotPassword()); |
| 72 | + List<SubmitButton> submitButtons = loginForm.findAll("input[type=submit]", SubmitButton.class); |
| 73 | + HtmlDocument response = loginForm.submit(submitButtons.get(0)); |
| 74 | + logger.info(response.getTitle()); |
| 75 | + logger.info("OpenID login attempted."); |
| 76 | + } |
| 77 | + |
| 78 | + private void loginRoot(MechanizeAgent agent, BotConfiguration configuration) { |
| 79 | + HtmlDocument rootLoginPage = agent.get(configuration.getRootUrl() + "/users/login"); |
| 80 | + Form loginForm = rootLoginPage.forms().getAll().get(rootLoginPage.forms().getAll().size() - 1); |
| 81 | + loginForm.get("openid_identifier").setValue("https://openid.stackexchange.com/"); |
| 82 | + List<SubmitButton> submitButtons = loginForm.findAll("input[type=submit]", SubmitButton.class); |
| 83 | + HtmlDocument response = loginForm.submit(submitButtons.get(submitButtons.size() - 1)); |
| 84 | + logger.info(response.getTitle()); |
| 85 | + logger.info("Root login attempted."); |
| 86 | + } |
| 87 | + |
| 88 | + private void loginChat(MechanizeAgent agent, BotConfiguration configuration) { |
| 89 | + HtmlDocument chatLoginPage = agent.get(configuration.getRootUrl() + "/users/chat-login"); |
| 90 | + Form loginForm = chatLoginPage.forms().getAll().get(chatLoginPage.forms().getAll().size() - 1); |
| 91 | + List<SubmitButton> submitButtons = loginForm.findAll("input[type=submit]", SubmitButton.class); |
| 92 | + HtmlDocument response = loginForm.submit(submitButtons.get(submitButtons.size() - 1)); |
| 93 | + logger.info(response.getTitle()); |
| 94 | + logger.info("Chat login attempted."); |
| 95 | + } |
| 96 | + |
| 97 | + private String retrieveFKeyReal(MechanizeAgent agent, BotConfiguration configuration) { |
| 98 | + HtmlDocument joinFavoritesPage = agent.get(configuration.getChatUrl() + "/chats/join/favorite"); |
| 99 | + Form joinForm = joinFavoritesPage.forms().getAll().get(joinFavoritesPage.forms().getAll().size() - 1); |
| 100 | + return joinForm.get("fkey").getValue(); |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments