Created
June 9, 2020 16:14
-
-
Save raymondjxu/7a8e7c37e6d078409f036b33010c5341 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package[redacted].comlink; | |
import com.google.gson.Gson; | |
import net.dv8tion.jda.api.entities.TextChannel; | |
import org.slf4j.LoggerFactory; | |
import [redacted].MyBit; | |
import [redacted].data.MCData; | |
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.HashMap; | |
public class Satellite extends Thread { | |
private static HashMap<String, DataOutputStream> outputSocketMap = new HashMap<>(); | |
static Gson json = new Gson(); | |
private static Satellite instance; | |
private Satellite(){} | |
public static Satellite getInstance() { | |
if(instance == null) instance = new Satellite(); | |
return instance; | |
} | |
public void run() { | |
try { | |
System.out.println("Started socket!"); | |
ServerSocket serverSocket = new ServerSocket(2566); | |
while (true) { | |
Socket s = serverSocket.accept();//establishes connection | |
DataInputStream inputStream = new DataInputStream(s.getInputStream()); | |
DataOutputStream outputStream = new DataOutputStream(s.getOutputStream()); | |
String str = inputStream.readUTF(); | |
LoggerFactory.getLogger("Satellite").debug(str); | |
HashMap<String, String> data = json.fromJson(str, HashMap.class); | |
if(data.get("type").equals("TOKENREQUEST")) { | |
HashMap<String, String> responseMap = new HashMap<>(); | |
responseMap.put("response", generateToken(64)); | |
outputStream.writeUTF(json.toJson(responseMap)); | |
LoggerFactory.getLogger("Tokens").info("Granted request for a token!"); | |
continue; | |
} | |
//TODO: Allow for PMs to work as well. | |
LoggerFactory.getLogger("Satellite").debug("Channel:" + MCData.getInstance().getEntry(data.get("token"))); | |
if(!MCData.getInstance().hasEntry(data.get("token"))) { | |
LoggerFactory.getLogger("Satellite").debug("Found invalid token, rejecting."); | |
continue; | |
} | |
if(data.get("type").equals("HEARTBEAT")) { | |
TextChannel associatedChannel = (TextChannel) MyBit.bot.getGuildChannelById(MCData.getInstance().getEntry(data.get("token"))); | |
outputSocketMap.put(associatedChannel.getId(), outputStream); | |
continue; | |
} | |
TextChannel associatedChannel = (TextChannel) MyBit.bot.getGuildChannelById(MCData.getInstance().getEntry(data.get("token"))); | |
outputSocketMap.put(associatedChannel.getId(), outputStream); | |
if(associatedChannel == null) { | |
System.out.println("Received message with invalid token!"); | |
continue; | |
} | |
associatedChannel.sendMessage("**" + data.get("user") + "**: " + data.get("content")).queue(); | |
System.out.println("Sent " + data.get("content")); | |
System.out.println("Listening..."); | |
} | |
} | |
catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static boolean hasOutputStream(String channelId) { | |
return outputSocketMap.containsKey(channelId); | |
} | |
public static void passChatMessage(String channelId, String username, String message) { | |
if(!outputSocketMap.containsKey(channelId)) throw new NullPointerException("No socket for that channel found!"); | |
HashMap<String, String> dataMap = new HashMap<>(); | |
dataMap.put("type", "CHAT"); | |
dataMap.put("content", message); | |
dataMap.put("user", username); | |
try { | |
outputSocketMap.get(channelId).writeUTF(json.toJson(dataMap)); | |
} | |
catch (IOException e){ | |
e.printStackTrace(); | |
} | |
} | |
private static String generateToken(int length) { | |
// chose a Character random from this String | |
String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
+ "0123456789" | |
+ "abcdefghijklmnopqrstuvxyz" | |
+ "!#$%()*+,-./"; | |
// create StringBuffer size of AlphaNumericString | |
StringBuilder stringBuilder = new StringBuilder(length); | |
for (int i = 0; i < length; i++) { | |
// generate a random number between | |
// 0 to AlphaNumericString variable length | |
int index = (int)(AlphaNumericString.length() * Math.random()); | |
// add Character one by one in end of stringBuilder | |
stringBuilder.append(AlphaNumericString.charAt(index)); | |
} | |
return stringBuilder.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment