Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. @WebSocket
  2. public class ConsoleSocket {
  3. private final PanelSessions sessions;
  4. private PanelPlugin plugin;
  5. private ArrayList<String> oldMsgs = new ArrayList();
  6.  
  7. private ConcurrentHashMap<Session, String> sockets = new ConcurrentHashMap<>();
  8.  
  9. public ConsoleSocket() throws UnknownHostException {
  10. this.plugin = PanelPlugin.getInstance();
  11. sessions = PanelSessions.getInstance();
  12. plugin.getServerLogger().addAppender(new LogHandler(this));
  13. plugin.getServerLogger().info("[JPanel] WebSocket started");
  14. }
  15.  
  16. @OnWebSocketConnect
  17. public void connected(Session session) throws IOException {
  18. List<HttpCookie> cookies = session.getUpgradeRequest().getCookies();
  19. String token = "";
  20.  
  21. for (HttpCookie cookie : cookies) {
  22. if (cookie.getName().equalsIgnoreCase("loggedin")) {
  23. token = cookie.getValue();
  24. }
  25. }
  26.  
  27. if (sessions.isLoggedIn(token)) {
  28. sockets.put(session, sessions.getAuthedUsername(token));
  29. session.getRemote().sendString("SCROLLBACK " + oldMsgs.size());
  30. for (String msg : oldMsgs) {
  31. session.getRemote().sendString(msg);
  32. }
  33. session.getRemote().sendString("Connected!");
  34. } else {
  35. session.getRemote().sendString("Failed to authenticate with the web socket!");
  36. session.close();
  37. }
  38. }
  39.  
  40.  
  41. @OnWebSocketClose
  42. public void onClose(Session session, int statusCode, String reason) {
  43. sockets.remove(session);
  44. }
  45.  
  46. @OnWebSocketMessage
  47. public void message(Session session, String message) throws IOException {
  48. String username = sockets.get(session);
  49.  
  50. if (!sessions.getUser(username).canSendCommands) {
  51. session.getRemote().sendString("You're not allowed to send commands! Contact the server admin if this is in error.");
  52. return;
  53. }
  54.  
  55. new BukkitRunnable() {
  56. @Override
  57. public void run() {
  58. plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), message);
  59. }
  60. }.runTask(plugin);
  61.  
  62. if (!message.contains("passwd"))
  63. plugin.getServerLogger().log(Level.INFO, "Console user " + username + " ran the command " + message);
  64. }
  65.  
  66. public void appendMessage(String message) throws IOException {
  67. oldMsgs.add(message);
  68. if (oldMsgs.size() > 1000) {
  69. oldMsgs.remove(0);
  70. oldMsgs.trimToSize();
  71. }
  72. for (Session socket : sockets.keySet()) {
  73. socket.getRemote().sendString(message);
  74. }
  75. }
  76.  
  77. private class LogHandler extends AbstractAppender {
  78. private ConsoleSocket socket;
  79.  
  80. public LogHandler(ConsoleSocket socket) {
  81. super("RemoteController", null, null);
  82. this.socket = socket;
  83. start();
  84. }
  85.  
  86. @Override
  87. public void append(LogEvent event) {
  88. DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
  89. Date date = new Date();
  90. String message = event.getMessage().getFormattedMessage();
  91. //message = message.replaceAll("\\e\\[[\\d;]*[^\\d;]",""); // remove ansi characters as they don't work well
  92. try {
  93. appendMessage(dateFormat.format(date) + " [" + event.getLevel().toString() + "] " + message);
  94. } catch (IOException e) {
  95. e.printStackTrace();
  96. }
  97. }
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement