package org.pdown.gui.extension.mitm.intercept;

import com.github.monkeywie.proxyee.intercept.HttpProxyIntercept;
import com.github.monkeywie.proxyee.intercept.HttpProxyInterceptPipeline;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.DefaultLastHttpContent;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.util.AsciiString;

/**
 * 嗅探目标网站cookie，支持HTTP only
 */
public class CookieIntercept extends HttpProxyIntercept {

  private boolean sniffFlag;

  @Override
  public void beforeRequest(Channel clientChannel, HttpRequest httpRequest, HttpProxyInterceptPipeline pipeline) throws Exception {
    String acceptValue = httpRequest.headers().get(HttpHeaderNames.ACCEPT);
    if (acceptValue != null && acceptValue.contains("application/x-sniff-cookie")) {
      sniffFlag = true;
    }
    super.beforeRequest(clientChannel, httpRequest, pipeline);
  }

  @Override
  public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) throws Exception {
    if (sniffFlag) {
      httpResponse.setStatus(HttpResponseStatus.OK);
      httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, 0);
      //https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Access-Control-Expose-Headers
      AsciiString customHeadKey = AsciiString.cached("X-Sniff-Cookie");
      httpResponse.headers().set(customHeadKey, pipeline.getHttpRequest().headers().get(HttpHeaderNames.COOKIE));
      httpResponse.headers().set(HttpHeaderNames.ACCESS_CONTROL_EXPOSE_HEADERS, customHeadKey);
      proxyChannel.close();
      super.afterResponse(clientChannel, proxyChannel, httpResponse, pipeline);
      clientChannel.write(new DefaultLastHttpContent());
    } else {
      super.afterResponse(clientChannel, proxyChannel, httpResponse, pipeline);
    }
  }

  @Override
  public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpContent httpContent, HttpProxyInterceptPipeline pipeline) throws Exception {
    if (!sniffFlag) {
      super.afterResponse(clientChannel, proxyChannel, httpContent, pipeline);
    }
  }

}

@@ -257,31 +257,83 @@
 .Default
-LastHttpContent
+HttpHeaders;%0Aimport io.netty.handler.codec.http.DefaultHttpResponse
 ;%0Aimport
@@ -357,24 +357,35 @@
 .codec.http.
+DefaultLast
 HttpContent;
@@ -609,24 +609,135 @@
 tty.
-util.AsciiString
+handler.codec.http.HttpVersion;%0Aimport io.netty.util.AsciiString;%0Aimport io.netty.util.internal.StringUtil;%0Aimport java.net.URL
 ;%0A%0A/
@@ -834,38 +834,8 @@
  %7B%0A%0A
-  private boolean sniffFlag;%0A%0A
   @O
@@ -1145,199 +1145,48 @@
     
-sniffFlag = true;%0A    %7D%0A    super.beforeRequest(clientChannel, httpRequest, pipeline);%0A  %7D%0A%0A  @Override%0A  public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpResponse h
+HttpResponse httpResponse = new DefaultH
 ttpR
@@ -1196,94 +1196,32 @@
 onse
-, 
+(
 Http
-ProxyInterceptPipeline pipeline) throws Exception %7B%0A    if (sniffFlag) %7B%0A      h
+Version.HTTP_1_1, H
 ttpR
@@ -1231,40 +1231,43 @@
 onse
-.set
 Status
-(HttpResponseStatus.OK
+.OK, new DefaultHttpHeaders()
 );%0A 
@@ -1722,456 +1722,604 @@
     
-proxyChannel.close();%0A      super.afterResponse(clientChannel, proxyChannel, httpResponse, pipeline);%0A      clientChannel.write(new DefaultLastHttpConten
+String origin = httpRequest.headers().get(HttpHeaderNames.ORIGIN);%0A      if (StringUtil.isNullOrEmpty(origin)) %7B%0A        String referer = httpRequest.headers().get(HttpHeaderNames.REFERER);%0A        URL url = new URL(referer);%0A        origin = url.getHos
 t()
-)
 ;%0A    
-%7D else %7B%0A      super.afterResponse(clientChannel, proxyChannel, httpResponse, pipeline);%0A    %7D%0A  %7D%0A%0A  @Override%0A  public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpContent httpContent, HttpProxyInterceptPipeline pipeline) throws Exception %7B%0A    if (!sniffFlag)
+  %7D%0A      httpResponse.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, origin);%0A      httpResponse.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, true);%0A      clientChannel.writeAndFlush(httpResponse);%0A      clientChannel.writeAndFlush(new DefaultLastHttpContent());%0A      clientChannel.close();%0A    %7D else
  %7B%0A 
@@ -2333,21 +2333,21 @@
 per.
-afterResponse
+beforeRequest
 (cli
@@ -2362,32 +2362,18 @@
 el, 
-proxyChannel, httpConten
+httpReques
 t, p
@@ -2392,11 +2392,10 @@
   %7D%0A  %7D%0A
-%0A
 %7D%0A

优化cookie拦截器速度，不需要等目标服务器返回响应