tunnel java avec interface graphique
Comment espionner tout les protocoles texte, voici la solution.
| L'écran est divisé en deux parties, à gauche
les messages envoyés par le client, à droite par le serveur
vous pouvez ainsi voir tout ce qui passe et mieux comprendre comment ça marche.
|
|
Pour configurer, c'est très simple il suffit de choisir le port d'écoute
du tunnel, et de donner l'adresse et port du serveur.
|
import java.io.*;
import java.lang.*;
import java.util.*;
import java.net.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.*;
public class Tunnel extends JFrame
{
public class ProxyDialog extends JDialog
{
JTextField hostTF, portTF;
public ProxyDialog(Frame parent, boolean modal)
{
super(parent, modal);
JPanel panel1, panel3;
JButton okButton, cancelButton;
Container contentPane = this.getContentPane();
panel1 = new JPanel();
panel1.setLayout(new FlowLayout(FlowLayout.CENTER));
contentPane.add(panel1, BorderLayout.SOUTH);
okButton = new JButton("OK");
panel1.add(okButton);
cancelButton = new JButton("Cancel");
panel1.add(cancelButton);
panel3 = new JPanel();
panel3.setLayout(new FlowLayout(FlowLayout.CENTER));
contentPane.add(panel3, BorderLayout.CENTER);
hostTF = new JTextField(proxy, 20);
portTF = new JTextField("" + proxyport, 5);
panel3.add(hostTF);
panel3.add(portTF);
setTitle("Target info");
pack();
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
dispose();
setVisible(false);
}
});
okButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
proxy = hostTF.getText();
proxyport = Integer.parseInt(portTF.getText());
dispose();
setVisible(false);
}
});
}
}
public class PortDialog extends JDialog
{
JTextField portTF;
public PortDialog(Frame parent, boolean modal)
{
super(parent, modal);
JPanel panel1, panel3;
JButton okButton, cancelButton;
Container contentPane = this.getContentPane();
panel1 = new JPanel();
panel1.setLayout(new FlowLayout(FlowLayout.CENTER));
contentPane.add(panel1, BorderLayout.SOUTH);
okButton = new JButton("OK");
panel1.add(okButton);
cancelButton = new JButton("Cancel");
panel1.add(cancelButton);
panel3 = new JPanel();
panel3.setLayout(new FlowLayout(FlowLayout.CENTER));
contentPane.add(panel3, BorderLayout.CENTER);
panel3.add(new JLabel("Proxy port"));
portTF = new JTextField("" + port, 5);
panel3.add(portTF);
setTitle("Port info");
pack();
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
dispose();
setVisible(false);
}
});
okButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
port = Integer.parseInt(portTF.getText());
try {
s.close();
s = new ServerSocket(port);
} catch (Exception e) {};
dispose();
setVisible(false);
}
});
}
}
class OneServeur extends Thread {
private JTextArea TA;
private BufferedReader from;
private PrintWriter to;
private Socket socketFrom, socketTo;
private OneServeur cli;
public OneServeur( Socket fromW,
Socket toW,
JTextArea display,
OneServeur cliW) throws IOException {
TA = display ;
socketFrom = fromW;
socketTo = toW ;
from = new BufferedReader(
new InputStreamReader ( socketFrom.getInputStream())) ;
to = new PrintWriter(
new BufferedWriter (
new OutputStreamWriter (
socketTo.getOutputStream())),true);
cli = cliW;
start();
}
public void run() {
String str = "";
try {
TA.append("\r\n----------"+ new Date() +"----------------\r\n");
while ( true ) {
str = from.readLine();
if ( str == null ) break;
TA.append(str+"\r\n");
to.print(str+"\r\n");
to.flush();
}
} catch ( Exception e) {
// e.printStackTrace();
}
TA.append("\r\n---------- End ----------------\r\n");
close();
if ( cli != null ) cli.close();
}
public void close() {
try {
socketFrom.close();
from.close();
} catch ( Exception e) {}
}
}
class TheServeur extends Thread {
private Socket socket, socketToWeb;
public TheServeur() throws IOException {
start();
}
public void run() {
try {
while ( true ) {
socket = s.accept();
socketToWeb = new Socket(InetAddress.getByName(proxy),proxyport);
OneServeur cli = new OneServeur(socket, socketToWeb , TAfromBw, null);
new OneServeur(socketToWeb, socket , TAfromWeb, cli);
}
} catch ( Exception e) {
e.printStackTrace();
return ;
}
}
}
public int port = 12345;
public int proxyport = 80;
public String proxy = "target";
private ServerSocket s;
private JTextArea TAfromBw = new JTextArea(50,30);
private JTextArea TAfromWeb = new JTextArea(50,30);
public Tunnel() throws IOException {
JMenuBar menuBar = new JMenuBar();
JButton closeButton = new JButton("Close");
JScrollPane webPane, bwPane;
Container contentPane = this.getContentPane();
JPanel Core = new JPanel();
Core.setLayout(new BorderLayout());
setJMenuBar(menuBar);
JMenu fMenu = new JMenu("File");
menuBar.add(fMenu);
JMenuItem proxyItem = new JMenuItem("Set Target", 2);
fMenu.add(proxyItem);
JMenuItem portItem = new JMenuItem("Set Port", 2);
fMenu.add(portItem);
JMenuItem clearItem = new JMenuItem("Clear", 2);
fMenu.add(clearItem);
fMenu.addSeparator();
JMenuItem exitItem = new JMenuItem("Exit", 2);
fMenu.add(exitItem);
webPane = new JScrollPane(TAfromWeb,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
bwPane = new JScrollPane(TAfromBw,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
Core.add(bwPane, BorderLayout.WEST);
Core.add(webPane, BorderLayout.EAST);
contentPane.add(Core);
// add listeners for the window and the various buttons
this.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
dispose();
System.exit(0);
}});
exitItem.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}});
proxyItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
changeProxy();
}});
portItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
changePort();
}});
clearItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
clearPage();
}});
pack();
s = new ServerSocket(port);
new TheServeur();
}
protected void changeProxy()
{
new ProxyDialog(this, true).show();
}
protected void clearPage()
{
TAfromBw.setText("");
TAfromWeb.setText("");
}
protected void changePort()
{
new PortDialog(this, true).show();
}
public static void main ( String args[] ) {
try {
(new Tunnel()).setVisible(true);
} catch ( Exception e ) {}
}
|