package exploitpack;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;

/**
 *
 * @author jsacco
 */
public class MultiThreadServer implements Runnable {

    Socket csocket;
    private final String serverIP;
    private final String serverPort;
    private String command = "empty";

    public MultiThreadServer(Socket socket, String ServerIP, String Port) {
        this.csocket = socket;
        this.serverIP = ServerIP;
        this.serverPort = Port;
    }

    @Override
    public void run() {

        try {
            File file = new File("output/exploitUpload");
            PrintStream pstream = new PrintStream(csocket.getOutputStream());
            BufferedReader br = new BufferedReader(new FileReader(file));
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            command = sb.toString();
            if ("".equals(serverIP)) {
                pstream.println("1\n");
            } else {
                if (command == null) {
                    pstream.println(serverIP + ":" + serverPort);
                } else {
                    pstream.println(serverIP + ":" + serverPort + ":" + command);
                }
            }
            pstream.close();
            csocket.close();
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}
