diff --git a/WebCallerID/pom.xml b/WebCallerID/pom.xml index b563558..1c9bc49 100755 --- a/WebCallerID/pom.xml +++ b/WebCallerID/pom.xml @@ -11,11 +11,9 @@ 1.2.17 - org.apache.commons - commons-lang3 - 3.0.1 - jar - compile + commons-codec + commons-codec + 20041127.091804 \ No newline at end of file diff --git a/WebCallerID/src/main/java/de/tilman/callerId/CallerIdClient.java b/WebCallerID/src/main/java/de/tilman/callerId/CallerIdClient.java index 1deb760..34b2217 100755 --- a/WebCallerID/src/main/java/de/tilman/callerId/CallerIdClient.java +++ b/WebCallerID/src/main/java/de/tilman/callerId/CallerIdClient.java @@ -1,233 +1,126 @@ package de.tilman.callerId; import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; -import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; -import java.util.Scanner; +import org.apache.commons.codec.binary.Base64; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.ConsoleAppender; import org.apache.log4j.Logger; import org.apache.log4j.PatternLayout; public class CallerIdClient { - + private final static Logger log = Logger.getLogger(CallerIdClient.class); - String password = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.encode("53795306".getBytes()); - String cookie; + String password; + String cookie = null; - public CallerIdClient() { + public CallerIdClient(String routerPassword) { BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d{ISO8601} - %m%n"))); - + this.password = new String(Base64.encodeBase64(routerPassword.getBytes())); + try { - login(); - getList(); -// plainJava(); - logout(); - } catch (Exception e1) { - e1.printStackTrace(); + postUrl("https://speedport.ip/index/login.cgi", "Username=" + URLEncoder.encode("admin", "UTF-8") + "&" + "Password=" + + password); + getUrl("https://speedport.ip/auth/hcti_status_telanrl.php?cookie=SessionID_R3," + cookie); + postUrl("https://speedport.ip/auth/logout.cgi?RequestFile=/pub/top_beenden.php&cookie=SessionID_R3," + cookie, ""); + } catch (Exception e) { + e.printStackTrace(); } } - private void login() throws Exception { - log.info("login()"); - String body = "Username=" + URLEncoder.encode("admin", "UTF-8") + "&" + "Password=" + password; - log.info(body); + private void getUrl(String urlString) throws Exception { + log.info("getUrl(" + urlString + ")"); - URL url2 = new URL("https://speedport.ip/index/login.cgi"); - HttpURLConnection connection = (HttpURLConnection) url2.openConnection(); + URL url = new URL(urlString); + URLConnection connection = url.openConnection(); + + showHeaders(connection); + + BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String line; + + while ((line = in.readLine()) != null) { + if (line.startsWith("var in_call_list")) { + // TODO get list + } +// log.info(" " + line); + } + in.close(); + + } + + private void postUrl(String urlString, String payload) throws Exception { + log.info("postUrl(" + urlString + ", " + payload + ")"); + + URL url = new URL(urlString); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); + connection.setInstanceFollowRedirects(false); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); - connection.setRequestProperty("Content-Length", String.valueOf(body.length())); + connection.setRequestProperty("Content-Length", String.valueOf(payload.length())); + if (cookie != null) { + connection.setRequestProperty("Cookie", "SessionID_R3=" + cookie); + } + // connection.setRequestProperty("Referer", "https://speedport.ip/auth/hcti_status_telanrl.php?cookie=SessionID_R3," + cookie); + OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); - writer.write(body); + if (payload.length() > 0) { + writer.write(payload); + } writer.flush(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); for (String line; (line = reader.readLine()) != null;) { if (line.startsWith("SessionID_R3")) { - System.out.println("Cookie: " + line); cookie = line.substring(13); } else { - System.out.println("Answer: " + line); + log.info(" Received unexpected response: " + line); } } - int status = connection.getResponseCode(); - System.out.println("Status: " + status); - writer.close(); reader.close(); + + showHeaders(connection); } - private void getList() throws Exception { - log.info("getList()"); - URL u = new URL("https://speedport.ip/auth/hcti_status_telanrl.php?cookie=SessionID_R3," + cookie); - URLConnection urlConn = u.openConnection(); - String cookieString = "SessionID_R3=" + cookie; - urlConn.setRequestProperty("Cookie", cookieString); - InputStream response = urlConn.getInputStream(); - - String r = new Scanner(response).useDelimiter("\\Z").next(); - log.info(r); - - response.close(); - } + private void showHeaders(URLConnection connection) { + for (int i = 0;; i++) { + String headerName = connection.getHeaderFieldKey(i); + String headerValue = connection.getHeaderField(i); - private void logout() throws Exception { - log.info("logout()"); - - getUrl("https://speedport.ip/pub/info_status_de.xml"); - - - URL url = new URL("https://speedport.ip/auth/logout.cgi?RequestFile=/pub/top_beenden.php&cookie=SessionID_R3," + cookie); - -// URLConnection urlConn = url.openConnection(); -// int statusA = urlConn.get - - - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - - connection.setRequestMethod("POST"); - connection.setInstanceFollowRedirects(false); - connection.setDoInput(true); - connection.setDoOutput(true); - connection.setUseCaches(false); - connection.setRequestProperty("Content-Length", "0"); - connection.setRequestProperty("Cookie", "SessionID_R3=" + cookie); - connection.setRequestProperty("Referer", "https://speedport.ip/auth/hcti_status_telanrl.php?cookie=SessionID_R3," + cookie); - - for (int i=0; ; i++) { - String headerName = connection.getHeaderFieldKey(i); - String headerValue = connection.getHeaderField(i); - - if (headerName == null && headerValue == null) { - // No more headers - break; - } - if (headerName == null) { - // The header value contains the server's HTTP version - log.info("Server HTTP version, Response code: " + headerValue); - } - else { - log.info("Header: " + headerName + ", Value: " + headerValue); - } - } - - BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); - - for (String line; (line = reader.readLine()) != null;) { - System.out.println("Answer: " + line); - } - - int status = connection.getResponseCode(); - log.info("POST Status: " + status); - - reader.close(); - - - - -// URL myUrl = new URL("https://speedport.ip/auth/logout.cgi?RequestFile=/pub/top_beenden.php&cookie=SessionID_R3," + cookie); -//// URL myUrl = new URL("https://speedport.ip/auth/logout.cgi?cookie=SessionID_R3," + cookie); -// String cookieString = "SessionID_R3=" + cookie; -// URLConnection urlConn = myUrl.openConnection(); -// urlConn.setRequestProperty("Cookie", cookieString); -// urlConn.setRequestProperty("Referer", "https://speedport.ip/auth/hcti_status_telanrl.php?cookie=SessionID_R3," + cookie); -// urlConn.connect(); -// showStatus(urlConn); -// - URL myUrl = new URL("https://speedport.ip/"); - URLConnection urlConn = myUrl.openConnection(); - urlConn.setRequestProperty("Cookie", "SessionID_R3=" + cookie); - urlConn.setRequestProperty("Referer", "https://speedport.ip/auth/hcti_status_telanrl.php?cookie=SessionID_R3," + cookie); - urlConn.connect(); - showStatus(urlConn); - - getUrl("https://speedport.ip/pub/js_top_25.js"); - - } - - private void getUrl(String urlString) throws Exception { - log.info("getUrl(" + urlString + ")"); - - URL url = new URL(urlString); - URLConnection conn = url.openConnection(); - for (int i = 0; ; i++) { - String headerName = conn.getHeaderFieldKey(i); - String headerValue = conn.getHeaderField(i); if (headerName == null && headerValue == null) { + // No more headers break; } if (headerName == null) { + // The header value contains the server's HTTP version log.info(" Server HTTP version, Response code: " + headerValue); - } - else { - log.info(" " + headerName + "=" + headerValue); - } - } - - -// URL url = new URL(urlString); -// URLConnection urlConn = url.openConnection(); -// String cookieString = "SessionID_R3=" + cookie; -// urlConn.setRequestProperty("Cookie", cookieString); -// InputStream response = urlConn.getInputStream(); -// String r = new Scanner(response).useDelimiter("\\Z").next(); -// log.info(r); -// response.close(); - } - - - private void showStatus(URLConnection connection) throws IOException { - HttpURLConnection httpConnection = (HttpURLConnection) connection; - log.info("HTTP Status " + httpConnection.getResponseCode() + " - " + httpConnection.getURL()); - } - - private void plainJava() throws Exception { - - URL url = new URL("https://speedport.ip/auth/logout.cgi?RequestFile=/pub/top_beenden.php&cookie=SessionID_R3," + cookie); - URLConnection conn = url.openConnection(); - - for (int i = 0;; i++) { - String name = conn.getHeaderFieldKey(i); - String value = conn.getHeaderField(i); - if (name == null && value == null) { - break; - } - if (name == null) { - System.out.println("Server HTTP version, Response code:"); - System.out.println(value); - System.out.print("\n"); } else { - System.out.println(name + "=" + value); + log.info(" Header: " + headerName + ", Value: " + headerValue); } } - - // Startseite - URL u = new URL("https://speedport.ip/"); - String r = new Scanner(u.openStream()).useDelimiter("\\Z").next(); - System.out.println(r); - } /** * @param args */ public static void main(String[] args) { - new CallerIdClient(); + if (args.length != 1) + System.out.println("usage: CallerIdClient "); + else + new CallerIdClient(args[0]); } }