Ca fait 4 jours que je me bats avec mon proxy et là je craque!!
Ce que je voudrais, c'est récupérer les traces du client et du serveur web.
Je crée ma connexion avec le client, tout va bien.
Le problème c'est qu'après je ne sais pas comment faire la connexion entre mon proxy et le serveur web demandé par le client.
Je fais :
Socket socket = new Socket(InetAddress.getByName(host),port);
Mais qu'est-ce que je mets comme host et comme port ?
Le host c'est l'url demandé ? C'est localhost ?
Le port, c'est le même que celui de la connexion client ? C'est un autre ?
J'ai l'impression d'avoir essayé toutes les combinaisons de port possibles et rien ne marche.
Si quelqu'un peut m'aider, je l'en remercie d'avance.
Je file le code si ça peut vous éclairer
public class PServer {
final static int localport = 8080;
public static void main(String[] args) throws IOException {
ServerSocket serveur = null;
try{
serveur = new ServerSocket(localport);
System.out.println("Serveur proxy démarré sur le port " + localport);
// Ecoute infinie des requêtes des clients
while(true){
Socket client = serveur.accept();
Clients c = new Clients(client);
}
}
catch (Exception e){System.err.println(e);}
finally{
try{
if (serveur != null) serveur.close();
}
catch (Exception e){System.err.println(e);}
}
}
} // ! PServ class
class Clients extends Thread {
private Socket client;
private String url;
private URL Url;
private InputStream sin;
private OutputStream sout;
public Clients(Socket client){
try{
this.client = client;
start();
}
catch (Exception e){System.err.println(e);}
} // !constructeur
public void run(){
try{
sin = client.getInputStream();
BufferedReader from_client = new BufferedReader(new InputStreamReader(sin));
sout = client.getOutputStream();
byte [] buffer = new byte[4096];
int lus;
String tmp;
String requete = "";
int position;
boolean fini = false;
int cpt = 0;
// Lecture de l'url que le client demande
while (((tmp = from_client.readLine()) != null) && !fini){
System.out.println(tmp);
//System.out.println((tmp != null) && !fini);
//System.out.println("ICI" + tmp.trim().toUpperCase());
if (tmp.trim().toUpperCase().startsWith("GET")) {
position = tmp.trim().toUpperCase().lastIndexOf("HTTP") > 0 ? tmp.toUpperCase().lastIndexOf("HTTP") : tmp.length();
url = tmp.trim().substring(3,position).trim();
requete = requete + tmp;
//System.out.println("URL :" + url);
//fini = true;
}
// il faut recuperer le User-Agent et le Host
String regex = "^User-Agent: +(.*\\))$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(tmp);
while (m.find()){
requete = requete + m.group(0);
/*for (int i=0; i <= m.groupCount(); i++){
System.out.println("Groupe " + i + " : " + m.group(i));
}*/
}
String regex2 = "^Host: +(.*)";
Pattern p2 = Pattern.compile(regex2);
Matcher m2 = p2.matcher(tmp);
while (m2.find()){
requete = requete + m2.group(0);
/*for (int i=0; i <= m2.groupCount(); i++){
System.out.println("Groupe " + i + " : " + m2.group(i));
}*/
}
cpt++;
//System.out.println(cpt);
if (cpt >= 5) {
fini = true;
//System.out.println("fini" + fini);
//System.out.println("URL ?" + url);
}
//System.out.println("URL ? " + url);
//System.out.println("la requete envoyée au serveur : " + requete);
}
// création d'une connexion sur le serveur http cible
System.out.println("Url demandée : " + url);
//appel à la classe proxyClient
proxyClient p = new proxyClient(client, requete, url);
/*URL Url = new URL(url);
InputStream httpin = Url.openStream();
while( (lus = httpin.read(buffer)) != -1) sout.write(buffer,0,lus);
httpin.close();
client.close();*/
}
catch (Exception e){System.err.println(e);}
} // ! run() method
} // ! Clients class
class proxyClient extends Thread {
private Socket client;
private String requete;
private String url;
public static int port = 8080;
public static String host = "localhost";
public proxyClient(Socket client, String requete, String url){
try{
this.client = client;
this.requete = requete;
this.url = url;
start();
}
catch (Exception e){System.err.println(e);}
} // !constructeur
public void run(){
String tmp = "";
int position;
int count=-1;
try {
/*URL Url = new URL(url);
InputStream httpin = Url.openStream();
while( (lus = httpin.read(buffer)) != -1) sout.write(buffer,0,lus);
httpin.close();*/
Socket socket = new Socket(InetAddress.getByName(host),port);
System.out.println("socket créé");
socket.close();
client.close();
}
catch (Exception e){System.err.println(e);}
} // ! run() method
} // ! proxyClient class


