Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    [JAVA] Un proxyserver dal vostro moderatore

    Beh e' un po' di tempo che mi diletto con java e ho fatto un piccolo ProxyServer per l'università ed ho pensato "perchè non condividere questo bel programmino con i forumisti"?

    Ditemi cosa ne pensate!!!

    PS: lo so che non e' eccezionale (e che magari qualche struttura non e' proprio "standard") però può essere richiamata sia dalla barra degli indirizzi "localhost:65001/www.html.it", sia inserendo l'indirizzo e la relativa porta all'interno del proxy di un qualsiasi browser web; inoltre gestisce sia il testo che i file (come immagini) all'interno di una pagina e restituisce anche il codice 404 per gli indirizzi sbagliati.
    Non funziona invece con i browser (come GALEON) che utilizzano gli indirizzi relativi.

    Codice PHP:
    // Classi di IMPORT
    import java.io.*;
    import java.net.*;
    import java.util.*;

    class 
    ProxyServer 
    // Classe ProxyServer OPEN

        
    public static void main(String[] argsthrows Exception 
        
    // Metodo main() OPEN

        // Stringa usata per la richiesta GET
        
    String requestMessageLine
        
    // nome del sito che si vuole contattare
        
    String SitoWeb
        
    // porta su cui si accetta la connessione dal client
        
    ServerSocket listenSocket = new ServerSocket(65001);

        while(
    true) {
             
    // WHILE - OPEN

             // Accetto la richiesta dal Client
             
    Socket ConnessioneClient2Server listenSocket.accept();

             
    // Creo gli Stream per il Client
             
    BufferedReader inFromClient = new BufferedReader(new InputStreamReader(ConnessioneClient2Server.getInputStream()));

             
    // Legge la prima riga dell'intestazione http
             
    requestMessageLine inFromClient.readLine();
             
    System.out.println(requestMessageLine);

             
    // Processa la stringa per determinare il nome del sito da contattare
             
    StringTokenizer tokenizedLine = new StringTokenizer(requestMessageLine);
             if(
    tokenizedLine.nextToken().equals("GET")) { 
              
    // IF - OPEN

              
    DataOutputStream outToClient = new DataOutputStream(ConnessioneClient2Server.getOutputStream());
              
    BufferedOutputStream bos = new BufferedOutputStream(outToClient);

              
    SitoWeb tokenizedLine.nextToken();
              if(
    SitoWeb.startsWith("/") == true) {
                  
    SitoWeb SitoWeb.substring(1);
                  
    System.out.println("Sito richiesto: "+SitoWeb);
              }

              
    // Faccio la richiesta del file al sito indicato
              
    try {
                  
    URL u null;
                  if (
    SitoWeb.startsWith("http://")) {
                   
    = new URL(SitoWeb);
                  } else {
                   
    = new URL("http://"+SitoWeb);
                  }
                  
    URLConnection uc u.openConnection();
                  
    InputStream raw  uc.getInputStream();
                  
    InputStream in = new BufferedInputStream(raw);
                  
    int contentLength uc.getContentLength();
                  
    String contentType uc.getContentType();

                  
    // Mando l'intestazione al Client
                  
    outToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n");
                  
    outToClient.writeBytes("Content-Type: "+contentType+"\r\n");
                  
    outToClient.writeBytes("Content-Length: "+contentLength+"\r\n");
                  
    outToClient.writeBytes("\r\n");

                  if (
    contentType.startsWith("text/") || contentLength == -1) {
                   while(
    true) {
                       
    int data raw.read();
                       if (
    data == -1) break;
                       
    bos.write(data);
                   }
                  } else {
                   
    byte[] data = new byte[contentLength];
                   
    int bytesRead 0;
                   
    int offset 0;
                   while (
    offset contentLength) {
                       
    bytesRead in.read(dataoffsetdata.length-offset);
                       if (
    bytesRead == -1) break;
                       
    offset += bytesRead;
                   }
                   
    bos.write(data0data.length);
                  }

                  
    in.close();

              } catch (
    SocketException se) {
                  
    System.out.println("*** ERRORE *** - "+se);
              } catch (
    Exception e) {
                  
    System.out.println("*** ERRORE *** - "+e);
                  
    // Mando l'intestazione al Client
                  
    outToClient.writeBytes("HTTP/1.0 404 \r\n");
                  
    outToClient.writeBytes("\r\n");
                  
    outToClient.writeBytes("<HTML><HEAD><TITLE>ERRORE</TITLE></HEAD>");
                  
    outToClient.writeBytes("<BODY><CENTER><FONT SIZE=+2>ERRORE 404
    "
    );
                  
    outToClient.writeBytes("PAGINA NON TROVATA</FONT></CENTER></BODY></HTML>");
              }

              
    // Chiudo il Socket
              
    ConnessioneClient2Server.close();

             } else {

              
    // Se la risposta e' sbagliata mando un messaggio di errore al client
              
    System.out.println("Bad Request Message");

              
    // Chiudo il Socket
              
    ConnessioneClient2Server.close();

             } 
    // IF - CLOSE

         
    // WHILE - CLOSE

        
    // Metodo main() CLOSE

    // Classe ProxyServer CLOSE 
    [Homepage] [Contattami]
    Powered by: Ubuntu - Debian - Gentoo
    Developing: Java - C++ - PHP

    [supersaibal]"Perchè tanto Debian è meglio"
    [/supersaibal]

  2. #2
    fa, non ci ho capito molto ... ma sei una mente

    'scolta ... se lo usassi ... il proxy aziendale ... mi rileva lo stesso l'url di destinazione ???

  3. #3
    Originariamente inviato da innovatel
    fa, non ci ho capito molto ... ma sei una mente

    'scolta ... se lo usassi ... il proxy aziendale ... mi rileva lo stesso l'url di destinazione ???
    dipende....

    cmq se lo avvii da un altra macchina che non e' la tua, il tuo proxy aziendale riconoscerebbe l'indirizzo del proxy java e non del tuo pc
    [Homepage] [Contattami]
    Powered by: Ubuntu - Debian - Gentoo
    Developing: Java - C++ - PHP

    [supersaibal]"Perchè tanto Debian è meglio"
    [/supersaibal]

  4. #4
    è da bastardi dentro

  5. #5
    Originariamente inviato da innovatel
    è da bastardi dentro
    E' questo il lavoro principale del proxy!!!
    [Homepage] [Contattami]
    Powered by: Ubuntu - Debian - Gentoo
    Developing: Java - C++ - PHP

    [supersaibal]"Perchè tanto Debian è meglio"
    [/supersaibal]

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.