186 lines
6.7 KiB
Java
186 lines
6.7 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 java.io.File;
|
|
import java.io.IOException;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.SQLException;
|
|
import java.util.concurrent.CountDownLatch;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.logging.*;
|
|
|
|
/**
|
|
* Created by gurkengewuerz.de on 02.07.2017.
|
|
*/
|
|
public class Termbin {
|
|
|
|
private static Database db;
|
|
private static Config conf;
|
|
private static final Logger accesslog = Logger.getLogger("Accesslog");
|
|
|
|
public static void main(String args[]) throws Exception {
|
|
String settingsFile = "." + File.separator + "settings.json";
|
|
if (args.length >= 1) {
|
|
settingsFile = args[0];
|
|
}
|
|
File f = new File(settingsFile);
|
|
|
|
conf = new Config(f);
|
|
conf.load();
|
|
|
|
db = new Database(conf.getString("database"));
|
|
createDatabase();
|
|
|
|
FileHandler fh = new FileHandler(conf.getString("accesslog") + ".%u.log", 5242880, 5, true);
|
|
// FileHandler fh = new FileHandler(conf.getString("accesslog") + ".log", true);
|
|
SimpleFormatter sf = new SimpleFormatter();
|
|
fh.setFormatter(sf);
|
|
accesslog.setUseParentHandlers(false);
|
|
accesslog.addHandler(fh);
|
|
|
|
CountDownLatch lock = new CountDownLatch(3);
|
|
|
|
Thread uploadServerThread = new Thread(() -> {
|
|
try {
|
|
UploadServer uploadServer = new UploadServer(conf.getUploadServerConfig().getString("bind"), conf.getUploadServerConfig().getInt("port"));
|
|
lock.countDown();
|
|
uploadServer.start();
|
|
} catch (IOException e) {
|
|
Logger.getLogger(Termbin.class.getName()).log(Level.SEVERE, null, e);
|
|
}
|
|
});
|
|
uploadServerThread.start();
|
|
|
|
Thread dataServerThread = new Thread(() -> {
|
|
try {
|
|
Server dataServer = new Server(conf.getDataServerConfig().getInt("port"));
|
|
dataServer.setHandler(new DataHandler());
|
|
|
|
dataServer.start();
|
|
lock.countDown();
|
|
} catch (Exception e) {
|
|
Logger.getLogger(Termbin.class.getName()).log(Level.SEVERE, null, e);
|
|
}
|
|
});
|
|
dataServerThread.start();
|
|
|
|
Thread apiServerThread = new Thread(() -> {
|
|
try {
|
|
Server apiServer = new Server(conf.getApiServerConfig().getInt("port"));
|
|
apiServer.setHandler(new APIHandler());
|
|
|
|
apiServer.start();
|
|
lock.countDown();
|
|
} catch (Exception e) {
|
|
Logger.getLogger(Termbin.class.getName()).log(Level.SEVERE, null, e);
|
|
}
|
|
});
|
|
apiServerThread.start();
|
|
|
|
boolean counts = lock.await(4, TimeUnit.SECONDS);
|
|
if (counts) {
|
|
String cr = System.getProperty("line.separator");
|
|
String stringInfo = cr +
|
|
"########################################" + cr +
|
|
" Configfile: " + f.getAbsolutePath() + cr +
|
|
" Domain: " + conf.getString("domain") + cr +
|
|
" Upload Lifetime: " + conf.getInt("uploadlifetime") + "h" + cr +
|
|
" Upload Size: " + conf.getInt("maxkb") + "kb" + cr +
|
|
" Bans listed: " + conf.getBans().size() + cr +
|
|
" Accesslog: " + conf.getString("accesslog") + cr +
|
|
" Upload Server: " + conf.getUploadServerConfig().getString("bind") + ":" + conf.getUploadServerConfig().getInt("port") + cr +
|
|
" API Server: 0.0.0.0:" + conf.getApiServerConfig().getInt("port") + cr +
|
|
" Data Server: 0.0.0.0:" + conf.getDataServerConfig().getInt("port") + cr +
|
|
"########################################" + cr;
|
|
|
|
Logger.getLogger(Termbin.class.getName()).log(Level.INFO, stringInfo);
|
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
for (Handler h : accesslog.getHandlers()) {
|
|
h.close();
|
|
}
|
|
}));
|
|
} else {
|
|
Logger.getLogger(Termbin.class.getName()).log(Level.SEVERE, "Servers couldn't started!");
|
|
System.exit(1);
|
|
}
|
|
}
|
|
|
|
public static Database getDatabase() {
|
|
return db;
|
|
}
|
|
|
|
|
|
public static Config getConfig() {
|
|
return conf;
|
|
}
|
|
|
|
public static Logger getAccesslog() {
|
|
return accesslog;
|
|
}
|
|
|
|
public static String upload(String ip, String text, byte[] rawData, FileType fileType) throws SQLException {
|
|
String answerID = HashUtils.getSha256(System.currentTimeMillis() + ip).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);
|
|
getAccesslog().log(Level.INFO, ip + " - - upload with text " + " id#" + answerID);
|
|
|
|
} else {
|
|
ps.setString(5, null);
|
|
ps.setBytes(6, rawData);
|
|
getAccesslog().log(Level.INFO, ip + "upload with " + rawData.length + " bytes " + " id#" + answerID);
|
|
}
|
|
|
|
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" +
|
|
");"
|
|
);
|
|
}
|
|
}
|