From 812e5151467570f9c7a7b1f209cac9ed42550598 Mon Sep 17 00:00:00 2001 From: Defend Date: Fri, 17 Feb 2017 15:37:51 +0300 Subject: [PATCH] Release 22 --- .idea/workspace.xml | 292 +++++++++++++++++++++++++++++++++++------- src/client.java | 194 +++++++++++----------------- src/console.java | 55 ++++++-- src/ctServer.java | 4 +- src/eventHandler.java | 6 + src/socketInfo.java | 46 ++++--- 6 files changed, 406 insertions(+), 191 deletions(-) create mode 100644 src/eventHandler.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index be1e81c..b3c1550 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -28,8 +28,8 @@ - - + + @@ -40,10 +40,15 @@ - - + + + + + + + @@ -52,31 +57,32 @@ - - + + - - + + - - - - - + + + - - + + - - - + + + + + + @@ -100,10 +106,11 @@ @@ -145,9 +152,8 @@ - - + @@ -166,8 +172,37 @@ + + + + + + + + + + + + + + + + + @@ -180,8 +215,9 @@ + - + @@ -623,7 +659,7 @@ - + - + @@ -661,25 +697,36 @@ + + - + + + + + + + + - + - + - + @@ -689,7 +736,7 @@ - + @@ -712,7 +759,7 @@ - @@ -721,6 +768,110 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -737,6 +888,11 @@ + + + + + @@ -747,6 +903,7 @@ + @@ -767,6 +924,11 @@ + + + + + @@ -777,6 +939,7 @@ + @@ -797,6 +960,11 @@ + + + + + @@ -807,6 +975,7 @@ + @@ -828,6 +997,11 @@ + + + + + @@ -838,6 +1012,7 @@ + @@ -859,6 +1034,11 @@ + + + + + @@ -869,6 +1049,7 @@ + @@ -897,33 +1078,30 @@ - - + - + - - - - - + + + - + - - + + @@ -932,18 +1110,42 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + diff --git a/src/client.java b/src/client.java index 11d55c9..5160f6f 100644 --- a/src/client.java +++ b/src/client.java @@ -41,6 +41,7 @@ public class client extends Thread { if(!socketInfo.isOnline(id)){ return; } + sendSocket("{\"fn\":\"close\",\"reason\":\"\"}"); try { in.close(); out.close(); @@ -57,15 +58,27 @@ public class client extends Thread { return; } while (socketInfo.isOnline(id)) { + // Получение сообщения String msg = readSocket(); + // Сохранение данных из сообщения parseInput(msg); + // Проверка протокола if(!socketInfo.isProtocolAuth(id)){socketInfo.checkProtocol(id);} - if(!socketInfo.isProtocolAuth(id)){sendSocket("{\"fn\":\"bad_protocol\"}");close();} + if(!socketInfo.isProtocolAuth(id)){close();} + //TODO: подключить Event Handler + //TODO: проверить ROLE клиента + //TODO: выполнить событие в зависимости от ROLE клиента + //RoleChecker -> socketInfo + //eventFilter -> eventHandler + //eventFilter > switch > EVENT & ROLE + //eventFilter >> eventMaker >> client.socket + //TODO: убрать ECHO сообщение sendSocket(msg); } } private String bytesToStringUTFCustom(int[] ints) { + //TODO: добавить поддержку русского языка char[] buffer = new char[ints.length]; for (int i = 0; i < buffer.length; i++) { char c = (char) ints[i]; @@ -98,9 +111,7 @@ public class client extends Thread { + "\r\n\r\n") .getBytes("UTF-8"); out.write(response, 0, response.length); - } catch (IOException e) { - console.out("Server Error:", e.toString()); - } catch (NoSuchAlgorithmException e) { + } catch (IOException | NoSuchAlgorithmException e) { console.out("Server Error:", e.toString()); } socket.setSoTimeout(0); @@ -110,7 +121,6 @@ public class client extends Thread { } } catch (SocketException e) { console.debug("исключение 22"); - //TODO: написать исключение если сокет закрыт } catch (NoSuchElementException e) { console.socket(id, "Handshake Timeout (2000ms)"); socketInfo.setHttpAuth(id, false); @@ -138,32 +148,29 @@ public class client extends Thread { System.arraycopy(response, 0, r, 2, response.length); r[0] = (byte) 129; r[1] = (byte) (response.length); + } else if (message.length() < 65535) { + r = new byte[response.length + 4]; + System.arraycopy(response, 0, r, 4, response.length); + r[0] = (byte) 129; + r[1] = (byte) 126; + r[2] = (byte) (response.length >>> 8); + r[3] = (byte) (response.length); + } else if (response.length < Long.MAX_VALUE) { + r = new byte[response.length + 8]; + System.arraycopy(response, 0, r, 8, response.length); + r[0] = (byte) 129; + r[1] = (byte) 127; + r[2] = (byte) (response.length >>> 56); + r[3] = (byte) (response.length >>> 48); + r[4] = (byte) (response.length >>> 40); + r[5] = (byte) (response.length >>> 32); + r[6] = (byte) (response.length >>> 24); + r[7] = (byte) (response.length >>> 16); + r[8] = (byte) (response.length >>> 8); + r[9] = (byte) (response.length); } else { - if (message.length() < 65535) { - r = new byte[response.length + 4]; - System.arraycopy(response, 0, r, 4, response.length); - r[0] = (byte) 129; - r[1] = (byte) 126; - r[2] = (byte) (response.length >>> 8); - r[3] = (byte) (response.length); - } else { - if (response.length > Long.MAX_VALUE) { - console.out("Socket Exception", "Too long message for sending! (>64bit)"); - return false; - } - r = new byte[response.length + 8]; - System.arraycopy(response, 0, r, 8, response.length); - r[0] = (byte) 129; - r[1] = (byte) 127; - r[2] = (byte) (response.length >>> 56); - r[3] = (byte) (response.length >>> 48); - r[4] = (byte) (response.length >>> 40); - r[5] = (byte) (response.length >>> 32); - r[6] = (byte) (response.length >>> 24); - r[7] = (byte) (response.length >>> 16); - r[8] = (byte) (response.length >>> 8); - r[9] = (byte) (response.length); - } + console.out("Socket Exception", "Too long message for sending! (>64bit)"); + return false; } try { out.write(r, 0, r.length); @@ -180,7 +187,6 @@ public class client extends Thread { String out = ""; try { int[] MSG = new int[0]; - int OPCOUNT = 1; int LENGTH; int KEYFRAME = 0; int[] KEY = new int[4]; @@ -194,60 +200,49 @@ public class client extends Thread { return null; case 129: LENGTH = in.read(); //читаем второй бит с данными о размере сообщения - //TODO: which и switch OPCOUNT - необходимо убрать - while (OPCOUNT < 4) { - switch (OPCOUNT) { - case 1: - switch (LENGTH) { //узнаем длину сообщения - default: - KEYFRAME = 2; - break; - case 254: - KEYFRAME = 4; //126-65535 - break; - case 255: - KEYFRAME = 10; //65535-... - break; - } + switch (LENGTH) { //узнаем длину сообщения + default: + KEYFRAME = 2; + break; + case 254: + KEYFRAME = 4; //126-65535 + break; + case 255: + KEYFRAME = 10; //65535-... break; + } + switch (KEYFRAME) { case 2: - switch (KEYFRAME) { - case 2: - MSG = new int[LENGTH - 128]; - break; - case 4: - LENGTH = (in.read() << 8) + (in.read()); - MSG = new int[LENGTH]; - break; - case 10: - LENGTH = (in.read() << 56) - + (in.read() << 48) - + (in.read() << 40) - + (in.read() << 32) - + (in.read() << 24) - + (in.read() << 16) - + (in.read() << 8) - + (in.read()); - MSG = new int[LENGTH]; - break; - } - for (int i = 0; i < 4; i++) { - KEY[i] = in.read(); - } - for (int j = 0; j < (MSG.length); j++) { - int enc = in.read(); - MSG[j] = (byte) (enc ^ KEY[j & 0x3]); - } + MSG = new int[LENGTH - 128]; + break; + case 4: + LENGTH = (in.read() << 8) + (in.read()); + MSG = new int[LENGTH]; + break; + case 10: + LENGTH = (in.read() << 56) + + (in.read() << 48) + + (in.read() << 40) + + (in.read() << 32) + + (in.read() << 24) + + (in.read() << 16) + + (in.read() << 8) + + (in.read()); + MSG = new int[LENGTH]; break; - case 3: - if (MSG.length > 0) { - out = bytesToStringUTFCustom(MSG); - } else { - return null; - } } - OPCOUNT++; - } + for (int i = 0; i < 4; i++) { + KEY[i] = in.read(); + } + for (int j = 0; j < (MSG.length); j++) { + int enc = in.read(); + MSG[j] = (byte) (enc ^ KEY[j & 0x3]); + } + if (MSG.length > 0) { + out = bytesToStringUTFCustom(MSG); + } else { + return null; + } } } catch (IOException e) { console.out("Socket Exception", "Client socket is closed?! Closing socket connection!"); @@ -295,40 +290,3 @@ public class client extends Thread { } } -// public void sControl(){ -// try{ -// while (true) { -// String input = reader.readLine(); -// if(input == null){break;} -// Pattern main = Pattern.compile(";"); -// if(!httpAuth) { -// if (!ctServer.checkClient(this, main.split(input))) { -// writer.println("CONNECT_DENIED"); -// break; -// } -// } -// switch (input) { -// case "EXIT": -// socket.close(); -// break; -// default: -// writer.println("Client info - " + socket.getRemoteSocketAddress().toString()); -// console.out("Socket "+socketID+" Message",input); -// } -// } -// } catch (IOException e) { -// console.out("Server Error:", e.toString()); -// } finally { -// close(); -// try { -// socket.close(); -// console.out("Server", "Socket "+socketID+" disconnected!"); -// } catch (IOException e) { -// console.out("Server","System socket closing error!"); -// }catch (NullPointerException e){ -// -// } -// -// } -// } - diff --git a/src/console.java b/src/console.java index b5ab48e..fa79833 100644 --- a/src/console.java +++ b/src/console.java @@ -53,6 +53,7 @@ public class console extends Thread{ try{ line = in.readLine(); Integer sid; + Integer id; Pattern main = Pattern.compile(" "); String[] sx = main.split(line); switch (sx[0]){ @@ -73,28 +74,60 @@ public class console extends Thread{ break; case "/kick": sid = Integer.valueOf(sx[1]); - if(socketInfo.findSocket(Integer.valueOf(sx[1])) != -1){ + if(socketInfo.findSocket.bySID(Integer.valueOf(sx[1])) != -1){ socketInfo.getClient(sid).close(); } break; - case "/help": - console.out("Server","/kick [socketid] для отключения сокета от сервера"); - console.out("Server","/list для отображения всех клиентов"); - console.out("Server","/info [socketid] для отображения полной информации о данном клиенте"); - console.out("Server","/send [socketid] [msg] отладочная команда"); - console.out("Server","ctServer Proto Defend Development 2016"); - break; case "/send": - sid = socketInfo.findSocket(Integer.valueOf(sx[1])); + sid = socketInfo.findSocket.bySID(Integer.valueOf(sx[1])); if(sid != -1){ - String msg = ""; + String msg = ""; for (int i = 2; i < sx.length ; i++) { msg += " "; msg += sx[i]; } - socketInfo.getClient(Integer.valueOf(sx[1])).sendSocket(msg); + socketInfo.getClient(Integer.valueOf(sx[1])).sendSocket(msg); } break; + case "/info": + if(sx.length < 3){ + console.out("Server","/info [pid/sid] "); + } else { + switch (sx[1]) { + case "sid": + sid = Integer.valueOf(sx[2]); + id = socketInfo.findSocket.bySID(sid); + if(id == -1){break;} + console.socket(sid,"VKID " + socketInfo.VKID.get(id) + + " PROTOCOL " + socketInfo.PROTOCOL.get(id) + + " EVENT " + socketInfo.EVENT.get(id) + + " PARTYID " + socketInfo.PARTYID.get(id) + + " TRACKDURATION " + socketInfo.DURATION.get(id)); + break; + case "pid": + ArrayList a = socketInfo.findSocket.byPID(Integer.valueOf(sx[2])); + for (int i = 0; i < a.size(); i++) { + id = a.get(i); + console.socket(socketInfo.SOCKETID.get(id),"VKID " + socketInfo.VKID.get(id) + + " PROTOCOL " + socketInfo.PROTOCOL.get(id) + + " EVENT " + socketInfo.EVENT.get(id) + + " PARTYID " + socketInfo.PARTYID.get(id) + + " TRACKDURATION " + socketInfo.DURATION.get(id)); + } + break; + default: + console.out("Server", "/info [pid/sid] "); + break; + } + } + break; + case "/help": + console.out("Server","/kick [socketid] для отключения сокета от сервера"); + console.out("Server","/list для отображения всех клиентов"); + console.out("Server","/info pid/sid [id] для отображения полной информации о данном клиенте"); + console.out("Server","/send [socketid] [msg] отладочная команда"); + console.out("Server","ctServer Proto Defend Development 2016"); + break; default: console.out("Server","Введите /help для помощи"); } diff --git a/src/ctServer.java b/src/ctServer.java index a8e19c1..de9d517 100644 --- a/src/ctServer.java +++ b/src/ctServer.java @@ -5,11 +5,11 @@ public class ctServer { public static void main(String[] args) throws IOException{ socketInfo.init(); -// socketID = 0; + //TODO: оптимизация идентификаторов сокетов try{ new console().consoleListner(); } - catch (IOException e){ + catch (IOException ignored){ } int port = 80; diff --git a/src/eventHandler.java b/src/eventHandler.java new file mode 100644 index 0000000..7f92d91 --- /dev/null +++ b/src/eventHandler.java @@ -0,0 +1,6 @@ +/** + * Created by itsmy on 15.08.2016. + */ +public class eventHandler extends client { + +} diff --git a/src/socketInfo.java b/src/socketInfo.java index 380726a..f75da33 100644 --- a/src/socketInfo.java +++ b/src/socketInfo.java @@ -1,4 +1,7 @@ +import com.sun.istack.internal.NotNull; + import java.util.ArrayList; +import java.util.Arrays; import java.util.function.BooleanSupplier; /** @@ -46,7 +49,7 @@ public class socketInfo { } public static void close(Integer sid){ - int s = findSocket(sid); + int s = findSocket.bySID(sid); VKID.remove(s); ROLE.remove(s); PROTOCOL.remove(s); @@ -67,25 +70,38 @@ public class socketInfo { return sid; } - public static Integer findSocket(int s){ - int id = -1; - for (int i = 0; i < SOCKETID.size(); i++) { - if(SOCKETID.get(i) == s){ - id = i; + public static class findSocket extends socketInfo{ + + public static Integer bySID(Integer s){ + int id = -1; + for (int i = 0; i < SOCKETID.size(); i++) { + if(SOCKETID.get(i).equals(s)){ + id = i; + } } + return id; + } + public static ArrayList byPID(Integer pid){ + ArrayList a = new ArrayList<>(); + for (int i = 0; i < PARTYID.size(); i++) { + if(PARTYID.get(i).equals(pid)){ + a.add(i); + } + } + return a; } - return id; + } public static void setHttpAuth(Integer sid, Boolean b){ - int s = findSocket(sid); + int s = findSocket.bySID(sid); if(s == -1) return; httpAuth.set(s, b); } public static void checkProtocol(Integer sid){ - int s = findSocket(sid); + int s = findSocket.bySID(sid); if(s == -1) return; if(PROTOCOL.get(s).equals("1A")){ protocolAuth.set(s, true); @@ -93,31 +109,31 @@ public class socketInfo { } public static Boolean isProtocolAuth(Integer sid){ - int s = findSocket(sid); + int s = findSocket.bySID(sid); if(s == -1) return Boolean.FALSE; return protocolAuth.get(s); } public static Boolean isHttpAuth(Integer sid){ - int s = findSocket(sid); + int s = findSocket.bySID(sid); if(s == -1) return Boolean.FALSE; return httpAuth.get(s); } public static Boolean isOnline(Integer sid){ - int s = findSocket(sid); + int s = findSocket.bySID(sid); if(s == -1) return Boolean.FALSE; return ONLINE.get(s); } public static client getClient(Integer sid){ - int s = findSocket(sid); + int s = findSocket.bySID(sid); if(s == -1) return null; return socketInfo.CLIENT.get(s); } public static void setString(Integer sid, String keyName, String value) { - int s = findSocket(sid); + int s = findSocket.bySID(sid); if (s == -1) return; switch (keyName) { case "fn": @@ -133,7 +149,7 @@ public class socketInfo { } public static void setInteger(Integer sid, String keyName, Integer value){ - int s = findSocket(sid); + int s = findSocket.bySID(sid); if(s == -1) return; switch (keyName){ case "vk_id": VKID.set(s, value);