package de.gurkengewuerz.postfix_rest_send; import fi.iki.elonen.NanoHTTPD; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Map; /** * Created by gurkengewuerz.de on 12.07.2017. */ public class Main extends NanoHTTPD { public Main() throws IOException { super(8081); start(NanoHTTPD.SOCKET_READ_TIMEOUT, false); } // http://www.simplejavamail.org/#/debugging public static void main(String[] args) { // Mailer m = new Mailer(new ServerConfig("localhost", 25)); // m.setDebug(true); // // Email email = new Email(); // email.setFromAddress("admin@gurkengewuerz.de", "admin@gurkengewuerz.de"); // email.setReplyToAddress("support@gurkengewuerz.de", "support@gurkengewuerz.de"); // email.addRecipient("developer@the-town.net", "developer@the-town.net", Message.RecipientType.TO); // email.setSubject("Email Test"); // email.setText("Dies ist ein Email Test von Java.\nHoffentlich bald mit Rest API"); // email.setTextHTML("We should meet up!"); // // m.sendMail(email); // HtmlToPlainText formatter = new HtmlToPlainText(); // formatter.getPlainText() try { System.out.println("Started"); new Main(); } catch (IOException e) { e.printStackTrace(); } } @Override public Response serve(IHTTPSession session) { Map parms = session.getParms(); JSONObject json = new JSONObject("{'error':'not found'}"); Response.Status status = Response.Status.NOT_FOUND; if (session.getUri().startsWith("/authorize")) { if (session.getUri().startsWith("/authorize/add")) { String username = parms.get("username"); String password = parms.get("password"); if (username != null && password != null) { System.out.println(checkPassword("/var/www/html/tools/functions.inc.php", "md5", "EMailFam.Schuetrumpf@123", "460b7d128333a4be972d4c7bdb0ee142")); status = Response.Status.OK; json = new JSONObject("{'token':''}"); } else { json = new JSONObject("{'error':'username/password is null'}"); } } else if (session.getUri().startsWith("/authorize/revoke")) { } } else if (session.getUri().startsWith("/validate/address")) { } else if (session.getUri().startsWith("/send")) { } return newFixedLengthResponse(status, "application/json", json.toString()); } public boolean checkPassword(String path, String encryption, String plain, String dbPW) { String crypted = execPHP("-r '$CONF = array(); $CONF[\"encrypt\"] = \"" + encryption + "\"; include \"" + path + "\"; echo(pacrypt(\"" + plain + "\", \"" + dbPW + "\")).\"\\n\";'"); System.out.println(crypted); return crypted.equals(dbPW); } public String execPHP(String args) { try { System.out.println(args); String line; StringBuilder output = new StringBuilder(); Process p = Runtime.getRuntime().exec("php " + args); BufferedReader input = new BufferedReader (new InputStreamReader(p.getInputStream())); while ((line = input.readLine()) != null) { output.append(line); System.out.println(line); } input.close(); return output.toString(); } catch (Exception err) { err.printStackTrace(); } return ""; } }