Grundlage DB und Socketverbindung
This commit is contained in:
157
src/main/java/Socket/shadowrun.java
Executable file
157
src/main/java/Socket/shadowrun.java
Executable file
@ -0,0 +1,157 @@
|
||||
package Socket;
|
||||
|
||||
import net.dv8tion.jda.core.EmbedBuilder;
|
||||
import net.dv8tion.jda.core.JDA;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.*;
|
||||
import java.net.ServerSocket;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.net.Socket;
|
||||
|
||||
|
||||
public class shadowrun implements Runnable{
|
||||
|
||||
private JDA jda;
|
||||
|
||||
public shadowrun(JDA jda) {
|
||||
this.jda = jda;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
int port = 11112;
|
||||
try (ServerSocket listener = new ServerSocket(port)) {
|
||||
System.out.println("Socket Server laeuft...");
|
||||
ExecutorService pool = Executors.newFixedThreadPool(20);
|
||||
while (true) {
|
||||
pool.execute(new iniEcho(listener.accept(), jda));
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
System.err.println("Unable to process client request");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static class iniEcho implements Runnable {
|
||||
private Socket socket;
|
||||
private JDA jda;
|
||||
|
||||
private boolean checkin;
|
||||
private Scanner in;
|
||||
private PrintWriter out;
|
||||
|
||||
private String salt;
|
||||
|
||||
iniEcho(Socket socket, JDA jda) {
|
||||
this.socket = socket;
|
||||
this.jda = jda;
|
||||
this.checkin = false;
|
||||
this.salt = "SamIstDerKoenigDerMatrix";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Connected: " + socket);
|
||||
try {
|
||||
in = new Scanner(socket.getInputStream());
|
||||
out = new PrintWriter(socket.getOutputStream(), true);
|
||||
String message = "";
|
||||
while (in.hasNextLine()) {
|
||||
if(this.checkin == false) {
|
||||
chekin();
|
||||
continue;
|
||||
}
|
||||
message = in.nextLine().trim();
|
||||
switch (message) {
|
||||
case "get":
|
||||
break;
|
||||
case "send":
|
||||
send();
|
||||
break;
|
||||
}
|
||||
|
||||
//out.println(message.toUpperCase() + "...");
|
||||
//jda.getGuildById("404979829537243148").getTextChannelById("542202034330271759").sendMessage(message).queue();
|
||||
//jda.getTextChannelById("542202034330271759").sendMessage(message).queue();
|
||||
//jda.sen
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error:" + socket);
|
||||
} finally {
|
||||
try { socket.close(); } catch (IOException e) { e.printStackTrace(); }
|
||||
System.out.println("Closed: " + socket);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* bearbeitet Checkin
|
||||
*/
|
||||
private void chekin() {
|
||||
System.out.println("checkin");
|
||||
String message = "";
|
||||
message = in.nextLine().trim();
|
||||
System.out.println(message);
|
||||
if(message.startsWith("SRCheckin")) {
|
||||
String[] msgParts = message.split("\\|", 3);
|
||||
String Hash = getHash(msgParts[1] + salt);
|
||||
System.out.println(msgParts[2].trim() + " -- " + Hash);
|
||||
if(msgParts[2].trim().equals(Hash)) {
|
||||
checkin = true;
|
||||
out.println("ACK");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wandelt String in SHA-1 Hash um
|
||||
* @param Input
|
||||
* @return
|
||||
*/
|
||||
private String getHash(String Input) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-1");
|
||||
md.update(salt.getBytes());
|
||||
byte[] bytes = md.digest(Input.getBytes());
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for(int i=0; i< bytes.length ;i++) {
|
||||
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private void send() {
|
||||
out.println("ACKSend");
|
||||
String message;
|
||||
EmbedBuilder outputBox = new EmbedBuilder();
|
||||
outputBox.setColor(Color.BLACK);
|
||||
outputBox.setTitle("Initiative:");
|
||||
|
||||
while (in.hasNextLine()) {
|
||||
message = in.nextLine().trim();
|
||||
if(message.trim().equals("FIN")) {
|
||||
break;
|
||||
}
|
||||
String[] msgParts = message.split("\\|",3);
|
||||
//jda.getUserById(msgParts[0]).getName();
|
||||
|
||||
outputBox.addField( msgParts[0], msgParts[1], false);
|
||||
out.println("ACKSend");
|
||||
}
|
||||
jda.getTextChannelById("542202034330271759").sendMessage(outputBox.build()).queue();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user