package de.gurkengewuerz.termbin; import org.json.JSONArray; import org.json.JSONObject; import org.json.JSONTokener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.nio.file.AccessDeniedException; import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; /** * Created by gurkengewuerz.de on 02.07.2017. */ public class Config extends JSONObject { private File file; private List ban = new ArrayList<>(); private List whitelist = new ArrayList<>(); private JSONObject uploadServerConf; private JSONObject dataServerConf; private JSONObject apiServerConf; public Config(File file) throws IOException { this.file = file; if (!file.exists()) { file.createNewFile(); } if (!file.isFile()) { throw new FileNotFoundException(file.getAbsolutePath() + " not found"); } if (!file.canRead() || !file.canWrite()) { throw new AccessDeniedException(file.getAbsolutePath() + " is not accessable"); } this.put("accesslog", "access"); this.put("database", "db.sqlite3"); this.put("domain", "http://localhost/"); this.put("uploadlifetime", 24 * 7); // 1 week hours this.put("maxkb", 1024 * 2); // 2 mb JSONObject uploadserver = new JSONObject(); uploadserver.put("port", 8888); uploadserver.put("timeout", 5); uploadserver.put("bind", "0.0.0.0"); this.put("uploadserver", uploadserver); JSONObject dataserver = new JSONObject(); dataserver.put("port", 7070); this.put("dataserver", dataserver); JSONObject apiserver = new JSONObject(); apiserver.put("port", 9090); this.put("apiserver", apiserver); JSONArray ban_list = new JSONArray(); ban_list.put("125.38.39.40"); ban_list.put("27.46.74.43"); ban_list.put("210.35.171.4"); this.put("ban_list", ban_list); JSONArray white_list = new JSONArray(); white_list.put("127.0.0.1"); white_list.put("192.168.1.36"); this.put("white_list", white_list); } public void save() { try { FileWriter fw = new FileWriter(file.getAbsolutePath()); fw.write(this.toString(4)); fw.close(); loadRestData(); } catch (IOException e) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e); } } public void load() { try { String content = new String(Files.readAllBytes(file.toPath()), "UTF-8"); if (content.isEmpty()) { save(); return; } JSONTokener jt = new JSONTokener(content); if (jt.nextClean() != 123) { throw jt.syntaxError("A JSONObject text must begin with '{'"); } else { while (jt.more()) { char c = jt.nextClean(); switch (c) { case '\u0000': throw jt.syntaxError("A JSONObject text must end with '}'"); case '}': return; default: jt.back(); String key = jt.nextValue().toString(); c = jt.nextClean(); if (c != 58) { throw jt.syntaxError("Expected a ':' after a key"); } this.remove(key); this.putOnce(key, jt.nextValue()); switch (jt.nextClean()) { case ',': case ';': if (jt.nextClean() == 125) { return; } jt.back(); break; case '}': save(); return; default: throw jt.syntaxError("Expected a ',' or '}'"); } } } } } catch (IOException e) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e); } } private void loadRestData() { JSONArray json_whitelist = getJSONArray("white_list"); for (int i = 0; i < json_whitelist.length(); i++) { whitelist.add(json_whitelist.getString(i)); } JSONArray json_ban = getJSONArray("ban_list"); for (int i = 0; i < json_ban.length(); i++) { if (whitelist.contains(json_ban.getString(i))) continue; ban.add(json_ban.getString(i)); } uploadServerConf = getJSONObject("uploadserver"); dataServerConf = getJSONObject("dataserver"); apiServerConf = getJSONObject("apiserver"); } public boolean isBanned(String ipv4) { return ban.contains(ipv4); } public List getBans() { return ban; } public JSONObject getUploadServerConfig() { return uploadServerConf; } public JSONObject getDataServerConfig() { return dataServerConf; } public JSONObject getApiServerConfig() { return apiServerConf; } }