Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.30 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.io.PrintWriter;
  7. import java.net.HttpURLConnection;
  8. import java.net.InetAddress;
  9. import java.net.InetSocketAddress;
  10. import java.net.MalformedURLException;
  11. import java.net.ProtocolException;
  12. import java.net.Proxy;
  13. import java.net.ServerSocket;
  14. import java.net.Socket;
  15. import java.net.URL;
  16. import java.net.URLEncoder;
  17. import java.nio.file.Files;
  18. import java.nio.file.Paths;
  19. import java.util.ArrayList;
  20. import java.util.Scanner;
  21. import java.util.StringTokenizer;
  22. import javax.swing.JFrame;
  23. import javax.swing.JLabel;
  24.  
  25. public class FLED extends JFrame implements Runnable{
  26.     /**
  27.      *
  28.      */
  29.     private static final long serialVersionUID = -1318204057094959103L;
  30.     private static ServerSocket sc;
  31.     public static JLabel l1;
  32.     public static int i=0;
  33.     private static String ID="C:\\Android\\DATABASE.bad";
  34.     private static String URLSend="http://www.sitehere.com/adder.php";
  35.     public static ArrayList<Client_To> clients;
  36.     public static void main(String[] args) {
  37.         (new Thread(new FLED())).start();
  38.     }
  39.    
  40.     public FLED(){
  41.         super("SERVER");
  42.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43.         l1=new JLabel("NOTHING");
  44.         l1.setBounds(0, 0, 200, 30);
  45.         setSize(400, 400);
  46.         setVisible(true);
  47.         add(l1);
  48.         clients=new ArrayList<>();
  49.        
  50.         try {
  51.             sc=new ServerSocket(5050);
  52.         } catch (IOException e1) {}
  53.         //if(new Connect(URLSend,sc.getInetAddress()).getSuccess())
  54.         //  l1.setText("Error of send ip address");
  55.        
  56.         while(true){
  57.             try {
  58.                 clients.add(new Client_To(sc.accept()));
  59.                 (new Thread(clients.get(i))).start();
  60.                 i++;
  61.             } catch (IOException e) {
  62.                
  63.             }
  64.         }
  65.     }
  66.  
  67.     @Override
  68.     public void run() {
  69.        
  70.     }
  71.     public static int SearchID(String id){
  72.         int l=-1;
  73.         int b=clients.size();
  74.         for(int i=0;i<b;i++){
  75.             if(clients.get(i).getID().equals(id)){
  76.                 l=i;
  77.             }
  78.         }
  79.         return l;
  80.        
  81.     }
  82.     public static Double<String,String> Auth(String log,String pass){
  83.         String database,user,user_i,user_log,user_pass;
  84.         int i_log;
  85.         try {
  86.             database =new String(Files.readAllBytes(Paths.get(ID)));
  87.         } catch (IOException e) {
  88.             return new Double<>("FAIL OF AUTH:FILE NOT FOUND","ERROR");
  89.         }
  90.         try{
  91.         i_log=database.indexOf(log);
  92.         if(i_log!=-1){
  93.         user=database.substring(i_log-1, database.length());
  94.         user_i=user.substring(0,user.indexOf(";"));
  95.         user_log=user_i.substring(user_i.indexOf("(")+1, user_i.indexOf(")"));
  96.         user_pass=user_i.substring(user_i.indexOf("{")+1, user_i.indexOf("}"));
  97.         if((pass.equals(user_pass))&&(log.equals(user_log))){
  98.             String friends=user_i.substring(user_i.indexOf("FRIEND_LIST_ID:")+15, user_i.indexOf("&"));
  99.             user_i.replaceAll("offline", "online");
  100.             return new Double<>(user_i.substring(user_i.indexOf("[")+1, user_i.indexOf("]"))+":"+
  101.         user_i.substring(user_i.indexOf("<")+1,user_i.indexOf(">")),friends);
  102.         }else{
  103.             return new Double<>("","ERROR");
  104.         }
  105.        
  106.         }else
  107.             return new Double<>("","ERROR");
  108.        
  109.         }catch(Exception e){
  110.             return new Double<>("","ERROR");
  111.         }
  112.        
  113.     }
  114.    
  115.      static Triple<String,Boolean,String> processFriend(String id){
  116.          Scanner scn=null;
  117.          String s=null;
  118.          try {
  119.             scn=new Scanner(new FileInputStream(ID));
  120.         } catch (FileNotFoundException e) {}
  121.          while(scn.hasNext()){
  122.               s=scn.nextLine();
  123.              if(s.indexOf(id)!=-1) break;
  124.          }
  125.          scn.close();
  126.          String name=s.substring(s.indexOf("<")+1, s.indexOf(">"));
  127.          boolean on_off_line=s.substring(s.indexOf("*,")+1, s.indexOf(",*")).equals("online")?true:false;
  128.         return new Triple<>(name,on_off_line,"Friend");
  129.     }
  130.    
  131.  
  132. }
  133.  class Client_To implements Runnable{
  134.      private String mes;
  135.      private StringTokenizer st;
  136.      private ObjectOutputStream out;
  137.      private ObjectInputStream in;
  138.      private Socket inc;
  139.      private String ID="";
  140.      private ArrayList<Triple<String,Boolean,String>> friends;
  141.      private Double<String ,String> dbl;
  142.      public Client_To(Socket s){
  143.          inc=s;
  144.          FLED.l1.setText(inc.getInetAddress()+"");
  145.      }
  146.      public String getID(){
  147.          return ID;
  148.      }
  149.      
  150.      
  151.      public void setID(String s){
  152.          ID=s;
  153.      }
  154.      
  155.     @Override
  156.     public void run() {
  157.         try {
  158.             out=new ObjectOutputStream(inc.getOutputStream());
  159.             in=new ObjectInputStream(inc.getInputStream());
  160.         } catch (IOException e) {
  161.            
  162.             }
  163.         realise();
  164.     }
  165.     public void realise(){
  166.         int IdForMes;
  167.         String b;
  168.         while(true){
  169.         try{   
  170.                 mes=(String) in.readObject();
  171.                 if(mes.indexOf("AUTH:")!=-1){
  172.                     dbl=FLED.Auth(mes.substring(mes.indexOf("<")+1,mes.indexOf(">"))
  173.                             ,mes.substring(mes.indexOf("[")+1, mes.indexOf("]")));
  174.                     b=dbl.first;
  175.                     if(!dbl.second.equals("ERROR")){
  176.                         setID(b.substring(0, b.indexOf(":")));
  177.                         FLED.l1.setText(FLED.l1.getText()+" || "+getID()+" "+dbl.second);
  178.                         out.writeObject("Welcome  " +b.substring(b.indexOf(":")+1, b.length()));
  179.                         st=new StringTokenizer(dbl.second,",");
  180.                         while(st.hasMoreTokens()) {
  181.                             String sr;
  182.                             FLED.l1.setText(FLED.l1.getText()+" "+(sr=dbl.second));
  183.                             friends.add(FLED.processFriend(sr));
  184.                         }
  185.                         if(!friends.isEmpty()){
  186.                             String friend="FRIEND_LIST:";
  187.                             for(int i=0;i<friends.size();i++)
  188.                                 friend+=friends.get(i).t+"&"+friends.get(i).v+"&"+friends.get(i).i+";";
  189.                             out.writeObject(friend);
  190.                         }
  191.                         }else out.writeObject(b);
  192.                     FLED.l1.setText(FLED.l1.getText()+b);
  193.                     //read inf about user and send to user
  194.                     }
  195.                 if(mes.indexOf("MES")!=-1){
  196.                     IdForMes=FLED.SearchID(mes.substring(mes.indexOf("_")+1,mes.indexOf(":")));
  197.                     FLED.l1.setText(IdForMes+"");
  198.                     FLED.clients.get(IdForMes==-1?1:IdForMes).out.writeObject(mes.substring(mes.indexOf(":")+1,mes.length()));
  199.                 }
  200.             }catch(Exception e){
  201.                 for(int i=0;i<FLED.clients.size();i++){
  202.                     if(FLED.clients.get(i).ID==ID){
  203.                         FLED.clients.remove(i);
  204.                         break;
  205.                     }
  206.                 }
  207.                 try {
  208.                     inc.close();
  209.                 } catch (IOException e1) {/** error of disconnect user*/}
  210.                
  211.                
  212.                
  213.             }
  214.            
  215.         }
  216.        
  217.     }
  218. }
  219.  
  220. class Connect{
  221.     private URL uri;
  222.     private HttpURLConnection uri1;
  223.     private PrintWriter pw=null;
  224.     private Scanner scn=null;
  225.     private boolean met=true;
  226.     public Connect(String url,InetAddress in){
  227.         try {
  228.             uri=new URL(url);
  229.         } catch (MalformedURLException e) {
  230.            
  231.         }
  232.         try {
  233.             uri1=(HttpURLConnection)uri.openConnection(new Proxy(Proxy.Type.HTTP,new InetSocketAddress("",8080)));//proxy here
  234.         } catch (IOException e) {
  235.             //print error
  236.         }
  237.         uri1.setDoInput(true);
  238.         uri1.setDoOutput(true);
  239.         try {
  240.             uri1.setRequestMethod("GET");
  241.         } catch (ProtocolException e1) {}
  242.         try {
  243.             uri1.connect();
  244.         } catch (IOException e) {
  245.             //print error
  246.         }
  247.         try {
  248.             scn=new Scanner(uri1.getInputStream());
  249.         } catch (IOException e) {}
  250.         try {
  251.             pw=new PrintWriter(uri1.getOutputStream());
  252.         } catch (IOException e) {}
  253.         pw.println(URLEncoder.encode("ip="+in.toString()));
  254.         String ans="";
  255.         while(scn.hasNext()) ans+=scn.nextLine();
  256.         if(ans.contains("")) met=false; //check here error
  257.         scn.close();
  258.         pw.close();
  259.     }
  260.    
  261.     boolean getSuccess(){
  262.         return met;
  263.     }
  264. }
  265.  
  266. class Triple<T,V,I>{
  267.     T t;
  268.     V v;
  269.     I i;
  270.     public Triple(T one,V two,I three){
  271.         t=one;
  272.         v=two;
  273.         i=three;
  274.     }
  275. }
  276.  
  277. class Double<T,I>{
  278.     T first;
  279.     I second;
  280.     public Double(T f,I s){
  281.         first=f;
  282.         second=s;
  283.     }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement