diff --git a/.idea/workspace (1).xml b/.idea/workspace (1).xml new file mode 100644 index 0000000..ed098dd --- /dev/null +++ b/.idea/workspace (1).xml @@ -0,0 +1,899 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + project + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1470646840402 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1.8 + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index aff860f..0983aa1 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -28,8 +28,8 @@ - - + + @@ -37,11 +37,11 @@ - - + + - - + + @@ -52,8 +52,20 @@ - - + + + + + + + + + + + + + + @@ -81,7 +93,9 @@ @@ -95,8 +109,8 @@ @@ -131,6 +145,34 @@ - + + + + + + + + - - + + - - + + - + - + + - + @@ -673,7 +729,6 @@ - @@ -691,13 +746,13 @@ - file://$PROJECT_DIR$/src/console.java - 48 + file://$PROJECT_DIR$/src/client.java + 53 - - @@ -829,46 +884,74 @@ - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + - - - - - - + - - + + - - + + @@ -877,8 +960,8 @@ - - + + @@ -887,8 +970,8 @@ - - + + diff --git a/ctServer.iml b/ctServer.iml index c90834f..abd9608 100644 --- a/ctServer.iml +++ b/ctServer.iml @@ -7,5 +7,6 @@ + \ No newline at end of file diff --git a/lib/javax.json.jar b/lib/javax.json.jar new file mode 100644 index 0000000..5a5ebc6 Binary files /dev/null and b/lib/javax.json.jar differ diff --git a/out/production/ctServer/client.class b/out/production/ctServer/client.class deleted file mode 100644 index 220a390..0000000 Binary files a/out/production/ctServer/client.class and /dev/null differ diff --git a/out/production/ctServer/console.class b/out/production/ctServer/console.class deleted file mode 100644 index 59e3a3e..0000000 Binary files a/out/production/ctServer/console.class and /dev/null differ diff --git a/out/production/ctServer/ctServer$ClientInfo.class b/out/production/ctServer/ctServer$ClientInfo.class deleted file mode 100644 index c15da56..0000000 Binary files a/out/production/ctServer/ctServer$ClientInfo.class and /dev/null differ diff --git a/out/production/ctServer/ctServer.class b/out/production/ctServer/ctServer.class deleted file mode 100644 index a3e3c59..0000000 Binary files a/out/production/ctServer/ctServer.class and /dev/null differ diff --git a/src/client.java b/src/client.java index 36251e7..d8bb07f 100644 --- a/src/client.java +++ b/src/client.java @@ -1,5 +1,5 @@ -import jdk.nashorn.internal.parser.JSONParser; - +import javax.json.Json; +import javax.json.JsonObject; import javax.xml.bind.DatatypeConverter; import java.io.*; import java.net.*; @@ -15,61 +15,47 @@ import java.util.regex.Pattern; */ public class client extends Thread { private Socket socket; - private BufferedReader reader; - private OutputStreamWriter writer; private OutputStream out; private InputStream in; - public Boolean httpAuthed = false; - public Boolean socketAuthed = false; - public int socketID; + private Integer id; public void openSocket(Socket s) throws IOException { socket = s; out = socket.getOutputStream(); in = socket.getInputStream(); + id = socketInfo.getNewID(); + console.debug("Create new socket "+ id); + socketInfo.create(this,id); start(); } public void run() { - socketID = ctServer.socketID; - ctServer.sockets.add(socketID, this); - console.socket(socketID,"New connection"); + console.socket(id,"New connection"); wsControl(); } public void close(){ - try{ - if(socket.isClosed()){ - return; - } + try { + in.close(); + out.close(); socket.close(); + }catch (IOException e){ } - catch(IOException e){ - } - if(socketAuthed){ - ctServer.clientInfoList.remove(socketID); - } - ctServer.sessions[socketID] = false; - httpAuthed = false; - socketAuthed = false; - console.socket(socketID,"Disconnected"); - this.interrupt(); + console.socket(id,"Disconnected"); + socketInfo.close(id); } public void wsControl(){ - try{ - handshake(); - while (httpAuthed) { - String msg = readSocket(); - sendSocket(msg); - //TODO: сделать проверку протокола - } - } finally { - close(); + handshake(); + if(!socketInfo.isHttpAuth(id)){ + return; + } + while (socketInfo.isOnline(id)) { + String msg = readSocket(); + sendSocket(msg); + //TODO: сделать проверку протокола } - - } private String bytesToStringUTFCustom(int[] ints) { @@ -83,7 +69,7 @@ public class client extends Thread { private void handshake(){ try{ - if(!httpAuthed) { + if(!socketInfo.isHttpAuth(id)) { socket.setSoTimeout(2000); String data = new Scanner(in, "UTF-8").useDelimiter("\\r\\n\\r\\n").next(); Matcher get = Pattern.compile("^GET").matcher(data); @@ -110,16 +96,16 @@ public class client extends Thread { console.out("Server Error:", e.toString()); } socket.setSoTimeout(0); - httpAuthed = true; - console.socket(socketID,"Handshake accepted!"); + socketInfo.setHttpAuth(id, true); + console.socket(id,"Handshake accepted!"); } } }catch (SocketException e){ console.debug("исключение 22"); //TODO: написать исключение если сокет закрыт }catch (NoSuchElementException e){ - console.socket(socketID,"Handshake Timeout (2000ms)"); - httpAuthed = false; + console.socket(id,"Handshake Timeout (2000ms)"); + socketInfo.setHttpAuth(id, false); } } @@ -129,7 +115,7 @@ public class client extends Thread { try{ response = message.getBytes("UTF-8"); }catch (UnsupportedEncodingException e){ - console.socket(socketID, "Unsupported encoding!"); + console.socket(id, "Unsupported encoding!"); return false; } catch (NullPointerException e){ @@ -252,16 +238,13 @@ public class client extends Thread { console.out("Socket Exception", "Client socket is closed?! Closing socket connection!"); close(); } - console.socket(socketID, "Message: "+out); + console.socket(id, "Message: "+out); return out; } // -// public Socket getSocketbyId(socketID){ -// if(socket.closed) -// return this.socket; -// } - - +// public static Boolean checkProtocol(String input){ +// JsonObject first = Json.createParser(new StringReader(input)).getStrint; +// } // public void sControl(){ // try{ @@ -269,7 +252,7 @@ public class client extends Thread { // String input = reader.readLine(); // if(input == null){break;} // Pattern main = Pattern.compile(";"); -// if(!httpAuthed) { +// if(!httpAuth) { // if (!ctServer.checkClient(this, main.split(input))) { // writer.println("CONNECT_DENIED"); // break; diff --git a/src/console.java b/src/console.java index 8233b49..525e795 100644 --- a/src/console.java +++ b/src/console.java @@ -1,5 +1,3 @@ -import javafx.application.Application; - import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -23,45 +21,46 @@ public class console extends Thread{ } public static void out(String parent, String msg){ - Instant instant2 = Clock.systemUTC().instant(); - String time = instant2.toString(); + String time = Clock.systemUTC().instant().toString(); System.out.println("["+time+"|"+parent+"]: "+msg); } public static void socket(Integer socketId, String msg){ - Instant instant2 = Clock.systemUTC().instant(); - String time = instant2.toString(); + String time = Clock.systemUTC().instant().toString(); System.out.println("["+time+"|"+"Socket "+socketId+"]: "+msg); } public static void debug(String msg){ - Instant instant2 = Clock.systemUTC().instant(); - String time = instant2.toString(); - System.out.println("["+time+"|DEBUG]: "+msg); + String out = "["; + out += Clock.systemUTC().instant().toString(); + out += "|DEBUG]: "; + out += msg; + System.out.println(out); } public void run() { while (line.equalsIgnoreCase("quit") == false) { try{ line = in.readLine(); + Integer sid; Pattern main = Pattern.compile(" "); String[] sx = main.split(line); switch (sx[0]){ case "/list": - ArrayList cil = ctServer.clientInfoList; console.out("Server","/list"); - for(int i = 0; ctServer.sessions[i]; i++){ - if(ctServer.sockets.get(Integer.valueOf(i)).socketAuthed) { - ctServer.ClientInfo ci = cil.get(i); - console.out("Socket " + i, "VKID " + ci.VKID + " and PROTOCOL " + ci.PROTOCOL); + for(int i = 0; socketInfo.SOCKETID.size() > i; i++){ + int id = socketInfo.SOCKETID.get(i); + if(socketInfo.isProtocolAuth(id)) { + console.socket(id,"VKID " + socketInfo.VKID.get(id) + " and PROTOCOL " + socketInfo.PROTOCOL.get(id)); }else{ - console.out("Socket "+ i,"Not authed!"); + console.socket(id,"Protocol not checked!"); } } break; case "/kick": - if(sx.length < 2) { - console.out("Server","/kick - Укажите имя сокета"); + sid = Integer.valueOf(sx[1]); + if(socketInfo.findSocket(Integer.valueOf(sx[1])) != -1){ + socketInfo.getClient(sid).close(); } break; case "/help": @@ -72,17 +71,23 @@ public class console extends Thread{ console.out("Server","ctServer Proto Defend Development 2016"); break; case "/send": - if(ctServer.sessions[Integer.valueOf(sx[1])]) { - if ((ctServer.sockets.size() >= Integer.valueOf(sx[1])) && (Integer.valueOf(sx[1]) >= 0)) { - ctServer.sockets.get(Integer.valueOf(sx[1])).sendSocket(sx[2]); + sid = socketInfo.findSocket(Integer.valueOf(sx[1])); + if(sid != -1){ + String msg = ""; + for (int i = 2; i < sx.length ; i++) { + msg += " "; + msg += sx[i]; } + socketInfo.getClient(Integer.valueOf(sx[1])).sendSocket(msg); } break; default: console.out("Server","Введите /help для помощи"); } }catch (IOException e){ - + console.out("Console","Something goes wrong..."); + }catch (NumberFormatException e){ + console.out("Console","Hey guy, don't be so crazy!!!"); } } } diff --git a/src/ctServer.java b/src/ctServer.java index f0800a5..9162b71 100644 --- a/src/ctServer.java +++ b/src/ctServer.java @@ -1,68 +1,53 @@ import java.net.*; import java.io.*; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.regex.Pattern; public class ctServer { - public static Boolean[] sessions; - public static List sockets; - public static int socketID; - public static ArrayList clientInfoList; +// public static int socketID; +// public static ArrayList socketInfoList; - public static class ClientInfo{ - public static String VKID, ROLE, PROTOCOL, EVENT, TRACKID; - public static int TIME; - public static Boolean notEmpty = false; - public static void destroy(){ - VKID = null; - ROLE = null; - PROTOCOL = null; - EVENT = null; - TRACKID = null; - TIME = 0; - notEmpty = false; - } - } - public static boolean checkClient(client c, String[] args){ - ClientInfo ci = new ClientInfo(); - ci.destroy(); + public static boolean checkClient(socketInfo socketID, String[] args){ + /* + socketInfo flow = new socketInfo(); + //TODO: Устарело + //TODO: перенести в console.java try { for (String name : args) { Pattern main = Pattern.compile(":"); String[] sx = main.split(name); switch (sx[0]) { case "PROTOCOL": - ci.PROTOCOL = sx[1]; + flow.PROTOCOL = sx[1]; break; case "VKID": - ci.VKID = sx[1]; + flow.VKID = sx[1]; break; } } - if (!ci.VKID.equals("")) { - if (!ci.PROTOCOL.equals("")) { - clientInfoList.add(c.socketID, ci); - c.httpAuthed = true; + if (!flow.VKID.equals("")) { + if (!flow.PROTOCOL.equals("")) { + flow.httpAuth = true; + socketInfoList.set(flow.SOCKETID, flow); console.debug("Авторизация произошла!"); return true; } } } catch (NullPointerException e){ return false; + }finally { + flow.close(); + System.gc(); } + */ return false; } public static void main(String[] args) throws IOException{ - sessions = new Boolean[1000]; - for (int i = 0; i < sessions.length; i++) { - sessions[i] = false; - } - clientInfoList = new ArrayList<>(); - sockets = new ArrayList<>(); + socketInfo.init(); +// socketID = 0; try{ new console().consoleListner(); } @@ -77,19 +62,7 @@ public class ctServer { while(true) { Socket socket = ss.accept(); try { - for(int i = 0; ctServer.sessions.length > i; i++){ - if(ctServer.sessions[i] == null){ - ctServer.socketID = i; - ctServer.sessions[i] = true; - break; - } - if(ctServer.sessions[i] == false){ - ctServer.socketID = i; - ctServer.sessions[i] = true; - break; - } - } - new client().openSocket(socket); + new client().openSocket(socket); } catch (IOException e) { socket.close(); } diff --git a/src/socketInfo.java b/src/socketInfo.java new file mode 100644 index 0000000..9b5e14b --- /dev/null +++ b/src/socketInfo.java @@ -0,0 +1,118 @@ +import java.util.ArrayList; +import java.util.function.BooleanSupplier; + +/** + * Created by itsmy_000 on 13.08.2016. + */ +public class socketInfo { + public static ArrayList ROLE, PROTOCOL, EVENT, TRACKID; + public static ArrayList VKID, TIME, SOCKETID, PARTYID; + public static ArrayList ONLINE, httpAuth, protocolAuth; + public static ArrayList CLIENT; + public static Integer sid; + + + public static void init(){ + sid = 0; + VKID = new ArrayList<>(); + TIME = new ArrayList<>(); + ROLE = new ArrayList<>(); + EVENT = new ArrayList<>(); + CLIENT = new ArrayList<>(); + PARTYID = new ArrayList<>(); + TRACKID = new ArrayList<>(); + PROTOCOL = new ArrayList<>(); + SOCKETID = new ArrayList<>(); + ONLINE = new ArrayList<>(); + httpAuth = new ArrayList<>(); + protocolAuth = new ArrayList<>(); + } + + public static void create(client c, Integer sid){ + VKID.add(0); + TIME.add(0); + ROLE.add(""); + EVENT.add(""); + ONLINE.add(true); + CLIENT.add(c); + TRACKID.add(""); + PROTOCOL.add(""); + PARTYID.add(0); + SOCKETID.add(sid); + httpAuth.add(false); + protocolAuth.add(false); + } + + public static void close(Integer sid){ + int s = findSocket(sid); + VKID.remove(s); + ROLE.remove(s); + PROTOCOL.remove(s); + EVENT.remove(s); + TRACKID.remove(s); + SOCKETID.remove(s); + TIME.remove(s); + PARTYID.remove(s); + CLIENT.remove(s); + ONLINE.remove(s); + httpAuth.remove(s); + protocolAuth.remove(s); + } + + public static Integer getNewID(){ + sid++; + 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; + } + + } + return id; + } + + public static void setHttpAuth(Integer sid, Boolean b){ + int s = findSocket(sid); + if(s == -1) return; + httpAuth.set(s, b); + } + + public static void setProtocol(Integer sid, Integer vkid, Integer pid, String role){ + int s = findSocket(sid); + if(s == -1) return; + protocolAuth.set(s, true); + VKID.set(s, vkid); + PARTYID.set(s, pid); + ROLE.set(s, role); + } + + public static Boolean isProtocolAuth(Integer sid){ + int s = findSocket(sid); + if(s == -1) return Boolean.FALSE; + return protocolAuth.get(s); + } + + public static Boolean isHttpAuth(Integer sid){ + int s = findSocket(sid); + if(s == -1) return Boolean.FALSE; + return httpAuth.get(s); + } + + public static Boolean isOnline(Integer sid){ + int s = findSocket(sid); + if(s == -1) return Boolean.FALSE; + return ONLINE.get(s); + } + + public static client getClient(Integer sid){ + int s = findSocket(sid); + if(s == -1) return null; + return socketInfo.CLIENT.get(s); + } + + +}