159 lines
5.2 KiB
Java
159 lines
5.2 KiB
Java
package de.gurkengewuerz.termbin;
|
|
|
|
import de.gurkengewuerz.termbin.Server.APIHandler;
|
|
import de.gurkengewuerz.termbin.Server.DataHandler;
|
|
import de.gurkengewuerz.termbin.Server.UploadServer;
|
|
import de.gurkengewuerz.termbin.Utils.HashUtils;
|
|
import org.eclipse.jetty.server.Server;
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.SQLException;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
/**
|
|
* Created by gurkengewuerz.de on 02.07.2017.
|
|
*/
|
|
public class Termbin {
|
|
|
|
private static Database db;
|
|
private static Config conf;
|
|
|
|
public static void main(String args[]) {
|
|
File f = new File("." + File.separator + "settings.json");
|
|
|
|
try {
|
|
conf = new Config(f);
|
|
} catch (IOException e) {
|
|
Logger.getLogger(Termbin.class.getName()).log(Level.SEVERE, null, e);
|
|
System.exit(1);
|
|
}
|
|
conf.load();
|
|
|
|
try {
|
|
db = new Database(conf.getString("database"));
|
|
createDatabase();
|
|
} catch (Exception e) {
|
|
Logger.getLogger(Termbin.class.getName()).log(Level.SEVERE, null, e);
|
|
}
|
|
|
|
Runnable uploadServerTask = () -> {
|
|
try {
|
|
JSONObject uploadServerConf = conf.getJSONObject("uploadserver");
|
|
new UploadServer(uploadServerConf.getString("bind"), uploadServerConf.getInt("port"));
|
|
} catch (IOException e) {
|
|
Logger.getLogger(Termbin.class.getName()).log(Level.SEVERE, null, e);
|
|
}
|
|
};
|
|
Thread uploadServerThread = new Thread(uploadServerTask);
|
|
uploadServerThread.start();
|
|
|
|
|
|
Runnable dataServerTask = () -> {
|
|
try {
|
|
JSONObject dataServerConf = conf.getJSONObject("dataserver");
|
|
Server dataServer = new Server(dataServerConf.getInt("port"));
|
|
dataServer.setHandler(new DataHandler());
|
|
|
|
dataServer.start();
|
|
dataServer.join();
|
|
} catch (Exception e) {
|
|
Logger.getLogger(Termbin.class.getName()).log(Level.SEVERE, null, e);
|
|
}
|
|
};
|
|
Thread dataServerThread = new Thread(dataServerTask);
|
|
dataServerThread.start();
|
|
|
|
|
|
Runnable apiServerTask = () -> {
|
|
try {
|
|
JSONObject apiServerConf = conf.getJSONObject("apiserver");
|
|
Server apiServer = new Server(apiServerConf.getInt("port"));
|
|
apiServer.setHandler(new APIHandler());
|
|
|
|
apiServer.start();
|
|
apiServer.join();
|
|
} catch (Exception e) {
|
|
Logger.getLogger(Termbin.class.getName()).log(Level.SEVERE, null, e);
|
|
}
|
|
};
|
|
Thread apiServerThread = new Thread(apiServerTask);
|
|
apiServerThread.start();
|
|
|
|
// TODO: Check if all threads started successfully
|
|
// TODO: log to file
|
|
// TODO: Arguments (Settings file)
|
|
// TODO: print start values (all ports etc.)
|
|
// TODO: Config Max lifetime => Check insert time on request
|
|
// TODO: Maven
|
|
}
|
|
|
|
public static Database getDatabase() {
|
|
return db;
|
|
}
|
|
|
|
|
|
public static Config getConfig() {
|
|
return conf;
|
|
}
|
|
|
|
public static String upload(String ip, String text, byte[] rawData, FileType fileType) throws SQLException {
|
|
String answerID = HashUtils.getSha256(System.currentTimeMillis() + "").substring(0, 8);
|
|
PreparedStatement ps = getDatabase().getPreparedStatement("INSERT INTO data (uniqueid, timestamp, fromClient, filetype, text, rawData) VALUES (?, ?, ?, ?, ?, ?);");
|
|
ps.setString(1, answerID);
|
|
ps.setInt(2, (int) (System.currentTimeMillis() / 1000));
|
|
ps.setString(3, ip);
|
|
ps.setString(4, fileType.toString());
|
|
if (fileType.equals(FileType.TXT)) {
|
|
ps.setString(5, text);
|
|
ps.setBytes(6, null);
|
|
} else {
|
|
ps.setString(5, null);
|
|
ps.setBytes(6, rawData);
|
|
}
|
|
|
|
ps.execute();
|
|
return answerID;
|
|
}
|
|
|
|
public enum FileType {
|
|
TXT,
|
|
PNG,
|
|
JPG,
|
|
GIF;
|
|
|
|
@Override
|
|
public String toString() {
|
|
switch (this) {
|
|
case TXT:
|
|
return "text/plain";
|
|
case PNG:
|
|
return "image/png";
|
|
case JPG:
|
|
return "image/jpeg";
|
|
case GIF:
|
|
return "image/gif";
|
|
default:
|
|
throw new IllegalArgumentException();
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void createDatabase() throws SQLException {
|
|
getDatabase().executeUpdate(
|
|
"CREATE TABLE IF NOT EXISTS data (" +
|
|
" id INTEGER PRIMARY KEY AUTOINCREMENT," +
|
|
" uniqueid char(255) NOT NULL UNIQUE," +
|
|
" text text NULL," +
|
|
" rawData blob NULL," +
|
|
" timestamp float NOT NULL," +
|
|
" fromClient char(255) NOT NULL," +
|
|
" filetype char(255) NOT NULL" +
|
|
");"
|
|
);
|
|
}
|
|
}
|