package org.lantern;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

import org.apache.commons.io.IOUtils;

import com.google.common.base.Charsets;

public class HostSpoofingTest {

    public static void main(final String... args) throws Exception {
        final SSLSocket sock = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
        sock.connect(new InetSocketAddress("github.global.ssl.fastly.net", 443), 10000);
        
        OutputStream os = null;
        try {
            os = sock.getOutputStream();
            writeHttpRequest(os);
            readResponse(sock.getInputStream());
        } finally {
            IOUtils.closeQuietly(os);
        }
    }

    private static void readResponse(InputStream inputStream) throws IOException {
        /*
        BufferedReader br = null;
        br = new BufferedReader(new InputStreamReader(inputStream));
        String cur = br.readLine();
        while (!StringUtils.isEmpty(cur)) {
            System.err.println(cur);
            cur = br.readLine();
        }
        */
        final byte[] body = new byte[1200];
        inputStream.read(body);
        System.err.println(new String(body, "UTF-8"));
    }

    private static void writeHttpRequest(final OutputStream os) throws IOException {
        os.write("GET / HTTP/1.1\r\n".getBytes(Charsets.UTF_8));
        os.write("Host: www.getlantem.org\r\n".getBytes(Charsets.UTF_8));
        os.write("\r\n".getBytes(Charsets.UTF_8));
    }

}

@@ -1506,11 +1506,14 @@
 st: 
-www
+fastly
 .get
@@ -1517,17 +1517,18 @@
 getlante
-m
+rn
 .org%5Cr%5Cn

update to new fastly hostname