Kirjautuminen

Haku

Tehtävät

Keskustelu: Koodit: Java "TCP putkittaja"

_Pete_ [02.02.2011 10:18:48]

#

Putkittaa liikenteen valittuun targettiin...

Testaus:

1) Aja
2) telnet localhost 2222
3) ihmettele

IrcProxy.java

package com.freakz.ircproxy;

import sun.security.krb5.internal.LoginOptions;
import sun.util.LocaleServiceProviderPool;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by IntelliJ IDEA.
 * User: petria
 * Date: 2/1/11
 * Time: 3:30 PM
 */
public class IrcProxy {

    private static java.util.logging.Logger LOG
            = java.util.logging.Logger.getLogger("com.freakz.ircproxy");


    public static void main(String[] args) throws Exception {

        ServerSocket sSocket = new ServerSocket(2222);

        while (true) {

            LOG.info("Waiting connection....");
            Socket socket = sSocket.accept();
            LOG.info("Got connection: " + socket);

            Forwarder f = new Forwarder("irc.elisa.fi", 6667);
            f.startForward(socket);


        }
    }
}

Forwarder.java

package com.freakz.ircproxy;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Created by IntelliJ IDEA.
 * User: petria
 * Date: 2/1/11
 * Time: 3:36 PM
 */
public class Forwarder {

    private static java.util.logging.Logger LOG
        = java.util.logging.Logger.getLogger("com.freakz.ircproxy");

    private String _host;
    private int _port;

    public static class Runner implements Runnable {

        Socket _fromSocket;
        Socket _toSocket;

        public Runner(Socket fromSocket, Socket toSocket) {
            _fromSocket = fromSocket;
            _toSocket = toSocket;
        }

        public void start() {
            Thread t = new Thread(this);
            t.start();
        }

        public void run() {

            LOG.info("Starting forward FROM: " + _fromSocket + " --> TO: " + _toSocket);
            try {
                BufferedReader fromReader = new BufferedReader(new InputStreamReader(_fromSocket.getInputStream()));
                BufferedWriter toWriter = new BufferedWriter(new OutputStreamWriter(_toSocket.getOutputStream()));

                String line = null;
                while (true) {
                    line = fromReader.readLine();
                    if (line != null) {
                        LOG.info("Forward line: '" + line + "'");
                        toWriter.write(line + "\n");
                        toWriter.flush();
                    } else {
                        break;
                    }
                }
                LOG.info("EXITING loop!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    public Forwarder(String host, int port) {
        _host = host;
        _port = port;
    }

    public void startForward(Socket fromSocket) throws IOException {
        Socket toSocket = new Socket(_host, _port);
        Runner toFrom = new Runner(toSocket, fromSocket);
        Runner fromTo = new Runner(fromSocket, toSocket);

        toFrom.start();
        fromTo.start();
    }
}

Vastaus

Aihe on jo aika vanha, joten et voi enää vastata siihen.

Tietoa sivustosta