package dareka.processor;

import dareka.Main;
import dareka.common.CloseUtil;
import dareka.common.Config;
import java.io.IOException;
import java.net.Socket;

public class MitmResource extends Resource {
    @Override
    public boolean transferTo(Socket receiver, HttpRequestHeader requestHeader,
            Config config) throws IOException {
        
        HttpResponseHeader responseHeader =
                new HttpResponseHeader(
                        "HTTP/1.1 200 Connection established\r\n\r\n");
        
        execSendingHeaderSequence(receiver.getOutputStream(), responseHeader);
        
        Socket tlsSocket = Main.getTlsEndPoint().upgrade(receiver);
        try {
            Main.handleTlsLoopback(tlsSocket);
        } finally {
            CloseUtil.close(tlsSocket);
        }
        return false;
    }
}
