Advertisement
Guest User

Untitled

a guest
Jun 18th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.35 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.PrintWriter;
  6. import java.io.FileReader;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9.  
  10. public class MiniServerSocketExample {
  11.     private static final int PORT = 8080;
  12.     public static void main(String[] args) {
  13.         try {
  14.             ServerSocket server = new ServerSocket(PORT);
  15.             System.out.println("MiniServer active " + PORT);
  16.             PrintWriter writer = new PrintWriter("src/autentificado.txt", "UTF-8");
  17.             writer.close();
  18.             while (true) {
  19.                 new ThreadSocket(server.accept());
  20.             }
  21.         } catch (Exception e) {
  22.         }
  23.     }
  24. }
  25. class ThreadSocket extends Thread {
  26.     private Socket insocket;
  27.     ThreadSocket(Socket insocket) {
  28.         this.insocket = insocket;
  29.         this.start();
  30.     }
  31.     @Override
  32.     public void run() {
  33.         try {
  34.             InputStream is = insocket.getInputStream();
  35.             PrintWriter out = new PrintWriter(insocket.getOutputStream());
  36.             BufferedReader in = new BufferedReader(new InputStreamReader(is));
  37.             String line;
  38.             String lineaArchivo;
  39.             line = in.readLine();
  40.             Integer encontreLogin = 0;
  41.             String request_method = line;
  42.             String[] listaPartes = request_method.split(" ");
  43.             System.out.println("HTTP-HEADER: " + line);
  44.             line = "";
  45.  
  46.             System.out.println("==METODO: "+listaPartes[0]+" URL: "+ listaPartes[1]);
  47.  
  48.             //==========================================================================================================
  49.             //AQUÍ SE EMPIEZAN A FILTRAR EL TIPO DE REQUEST
  50.             //==========================================================================================================
  51.  
  52.             if (listaPartes[0].equals("GET")) {
  53.  
  54.                 if (listaPartes[1].equals("/home_old")) {
  55.  
  56.                     out.print("HTTP/1.0 302 Moved Permanently\r\n" +
  57.                             "Location: http://" +
  58.                             insocket.getLocalAddress().getHostAddress() + ":" +
  59.                             insocket.getLocalPort() + "/\r\n\r\n");
  60.                 }
  61.  
  62.                 else if (listaPartes[1].equals("/secret")) {
  63.  
  64.                     try (BufferedReader br = new BufferedReader(new FileReader("src/autentificado.txt"))) {
  65.                         while ((lineaArchivo = br.readLine()) != null) {
  66.                             if (lineaArchivo.equals("Logeo detectado")){
  67.                                 encontreLogin = 1;
  68.                                 out.println("HTTP/1.0 200 OK");
  69.                                 out.println("Content-Type: text/html; charset=utf-8");
  70.                                 out.println("Server: T1REDES");
  71.                                 out.println("");
  72.                                 out.println("<H1>Autentificación Completada</H1>");
  73.  
  74.                             }
  75.                         }
  76.                         if (encontreLogin == 0){
  77.                             out.println("HTTP/1.0 403 Forbidden");
  78.                             out.println("Content-Type: text/html; charset=utf-8");
  79.                             out.println("Server: T1REDES");
  80.                             out.println("");
  81.                             out.println("<H1>ACCESO DENEGADO</H1>");
  82.                         }
  83.                     }
  84.  
  85.                 }
  86.  
  87.                 else if (listaPartes[1].equals("/login")) {
  88.  
  89.                     out.println("HTTP/1.0 200 OK");
  90.                     out.println("Content-Type: text/html; charset=utf-8");
  91.                     out.println("Server: T1REDES");
  92.                     out.println("");
  93.  
  94.                     try (BufferedReader br = new BufferedReader(new FileReader("src/login.html"))) {
  95.                         while ((lineaArchivo = br.readLine()) != null) {
  96.                             out.println(lineaArchivo);
  97.                         }
  98.                     }
  99.  
  100.                 }
  101.                 //CASO DE LA PÁGINA DE INICIO
  102.                 else if (listaPartes[1].equals("/")){
  103.                     out.println("HTTP/1.0 200 OK");
  104.                     out.println("Content-Type: text/html; charset=utf-8");
  105.                     out.println("Server: MINISERVER");
  106.                     // este linea en blanco marca el final de los headers de la response
  107.                     out.println("");
  108.                     // Envía el HTML
  109.                     try (BufferedReader br = new BufferedReader(new FileReader("src/home.html"))) {
  110.                         while ((lineaArchivo = br.readLine()) != null) {
  111.                             out.println(lineaArchivo);
  112.                         }
  113.                     }
  114.  
  115.                 }
  116.                 //CASO EN EL QUE INTENTEN ACCEDER A URL NO EXISTENTE
  117.                 else{
  118.                     out.println("HTTP/1.0 404 Not Found");
  119.                     out.println("Content-Type: text/html; charset=utf-8");
  120.                     out.println("Server: T1REDES");
  121.                     out.println("");
  122.                     out.println("<H1>Error 404 bad request</H1>");
  123.                 }
  124.             }
  125.  
  126.             else if (listaPartes[0].equals("POST")) {
  127.                 //======================================================================================================
  128.                 //BUSQUEDA DATA POST
  129.                 //======================================================================================================
  130.                 int postDataI = -1;
  131.                 while ((line = in.readLine()) != null && (line.length() != 0)) {
  132.                     System.out.println("HTTP-HEADER: " + line);
  133.                     if (line.indexOf("Content-Length:") > -1) {
  134.                         postDataI = new Integer(
  135.                                 line.substring(
  136.                                         line.indexOf("Content-Length:") + 16,
  137.                                         line.length())).intValue();
  138.                     }
  139.                 }
  140.                 String postData = "";
  141.                 //======================================================================================================
  142.                 //LECTURA DATA POST
  143.                 //======================================================================================================
  144.                 if (postDataI > 0) {
  145.                     char[] charArray = new char[postDataI];
  146.                     in.read(charArray, 0, postDataI);
  147.                     postData = new String(charArray);
  148.                 }
  149.  
  150.                 //System.out.println("Metodo de entrada " + request_method);
  151.                 //System.out.println("Post-> " + postData);
  152.  
  153.                 if (listaPartes[1].equals("/secret")) {
  154.                     String[] campos = postData.split("&");
  155.                     String user = campos[0].split("=")[1];
  156.                     String pass = campos[1].split("=")[1];
  157.  
  158.                     //LOGEO EXITOSO
  159.                     if (user.equals("admin") && pass.equals("password")){
  160.                         try{
  161.                             PrintWriter writer = new PrintWriter("src/autentificado.txt", "UTF-8");
  162.                             writer.println("Logeo detectado");
  163.                             writer.close();
  164.                         } catch (IOException e) {
  165.                             System.out.println("ERROR: "+e);
  166.                         }
  167.  
  168.                         out.println("HTTP/1.0 200 OK");
  169.                         out.println("Content-Type: text/html; charset=utf-8");
  170.                         out.println("Server: T1REDES");
  171.                         out.println("");
  172.                         out.println("<H1>Autentificación Completada</H1>");
  173.  
  174.  
  175.                     }
  176.                     //LOGGEO FALLIDO
  177.                     else{
  178.                         out.println("HTTP/1.0 403 Forbidde");
  179.                         out.println("Content-Type: text/html; charset=utf-8");
  180.                         out.println("Server: T1REDES");
  181.                         out.println("");
  182.                         out.println("<H1>ACCESO DENEGADO</H1>");
  183.                     }
  184.                 }
  185.             }
  186.             out.close();
  187.             insocket.close();
  188.         } catch (IOException e) {
  189.             e.printStackTrace();
  190.         }
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement