Advertisement
Guest User

Untitled

a guest
Nov 10th, 2014
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. import static io.netty.buffer.Unpooled.buffer;
  2. import io.netty.buffer.ByteBuf;
  3. import io.netty.handler.codec.http.DefaultFullHttpResponse;
  4. import io.netty.handler.codec.http.FullHttpResponse;
  5. import io.netty.handler.codec.http.HttpContent;
  6. import io.netty.handler.codec.http.HttpHeaders;
  7. import io.netty.handler.codec.http.HttpObject;
  8. import io.netty.handler.codec.http.HttpRequest;
  9. import io.netty.handler.codec.http.HttpResponse;
  10.  
  11. import java.nio.charset.Charset;
  12.  
  13. import org.littleshoot.proxy.HttpFilters;
  14. import org.littleshoot.proxy.HttpFiltersAdapter;
  15. import org.littleshoot.proxy.HttpFiltersSourceAdapter;
  16. import org.littleshoot.proxy.HttpProxyServer;
  17. import org.littleshoot.proxy.extras.SelfSignedMitmManager;
  18. import org.littleshoot.proxy.impl.DefaultHttpProxyServer;
  19.  
  20. /**
  21.  * Simple proxy example -- changes MOB to MOD in responses
  22.  * @author Bruno Candido Volpato da Cunha
  23.  *
  24.  */
  25. public class SimpleProxy {
  26.     public static void main(String[] args) {
  27.  
  28.         HttpProxyServer server = DefaultHttpProxyServer.bootstrap().withPort(8090).withAllowLocalOnly(false)
  29.                 .withListenOnAllAddresses(true).withManInTheMiddle(new SelfSignedMitmManager())
  30.                 .withFiltersSource(new HttpFiltersSourceAdapter() {
  31.  
  32.                     @Override
  33.                     public HttpFilters filterRequest(HttpRequest originalRequest) {
  34.                         System.out.println("Filter " + originalRequest.getUri());
  35.  
  36.                         HttpFilters filter = new HttpFiltersAdapter(originalRequest) {
  37.  
  38.                             @Override
  39.                             public HttpResponse requestPre(HttpObject httpObject) {
  40.  
  41.                                 if (!doFilter(originalRequest.getUri())) {
  42.                                     return null;
  43.                                 }
  44.  
  45.                                 if (httpObject instanceof HttpRequest) {
  46.                                     HttpRequest httpRequest = (HttpRequest) httpObject;
  47.  
  48.                                     if (httpRequest.headers().contains("Accept-Encoding")) {
  49.                                         httpRequest.headers().remove("Accept-Encoding");
  50.                                     }
  51.                                 }
  52.  
  53.                                 return null;
  54.                             }
  55.  
  56.                             @Override
  57.                             public HttpObject responsePost(HttpObject httpObject) {
  58.  
  59.                                 if (!doFilter(originalRequest.getUri())) {
  60.                                     return httpObject;
  61.                                 }
  62.  
  63.                                 System.out.println("Response post -> " + originalRequest.getUri() + " - "
  64.                                         + httpObject.getClass() + " - " + (httpObject instanceof HttpContent));
  65.  
  66.                                 if (httpObject instanceof HttpContent) {
  67.                                     return proxiedContent((HttpContent) httpObject);
  68.                                 }
  69.  
  70.                                 return httpObject;
  71.                             }
  72.  
  73.                             private HttpContent proxiedContent(HttpContent response) {
  74.                                 String text = response.content().toString(Charset.defaultCharset());
  75.                                
  76.                                 ProxiedValueVO proxyVO = doProxyValue(originalRequest.getUri(), text);
  77.                                
  78.                                 System.out.println("Returning content... Changed? " + proxyVO.isChanged());
  79.                                 if (proxyVO.isChanged()) {
  80.                                    
  81.                                     ByteBuf contentNew = buffer(1024);
  82.                                     contentNew.writeBytes(proxyVO.getText().getBytes());
  83.                                    
  84.                                     System.out.println("[changed] " + proxyVO.getText());
  85.                                    
  86.                                     HttpContent newResponse = response.copy();
  87.                                    
  88.                                     if (newResponse instanceof DefaultFullHttpResponse) {
  89.                                         DefaultFullHttpResponse fullResponse = (DefaultFullHttpResponse) newResponse;
  90.                                         fullResponse.headers().set("Content-Length", proxyVO.getText().length());
  91.                                     }
  92.                                     newResponse.content().clear().writeBytes(contentNew);
  93.                                     return newResponse;
  94.                                 }
  95.                                
  96.                                 return response;                           
  97.                                 }
  98.                         };
  99.  
  100.                         return filter;
  101.                     }
  102.  
  103.                     @Override
  104.                     public int getMaximumRequestBufferSizeInBytes() {
  105.                         return Integer.MAX_VALUE;
  106.                     };
  107.  
  108.                     @Override
  109.                     public int getMaximumResponseBufferSizeInBytes() {
  110.                         return Integer.MAX_VALUE;
  111.                     };
  112.  
  113.                 }).start();
  114.  
  115.     }
  116.  
  117.    
  118.     private static ProxiedValueVO doProxyValue(String url, String text) {
  119.         String oldText = text.intern();
  120.        
  121.         ProxiedValueVO vo = new ProxiedValueVO();
  122.         text = fromTo(text, "MOB", "MOD");
  123.        
  124.         vo.setChanged(!text.equals(oldText));
  125.         vo.setText(text);
  126.        
  127.         return vo;
  128.     }
  129.    
  130.     private static String fromTo(String text, String from, String to) {
  131.         if (text.contains(from)) {
  132.             text = text.replace(from, to);
  133.         }
  134.        
  135.         return text;
  136.     }
  137.    
  138.     private static boolean doFilter(String url) {
  139.         if (url.contains("mob.com")) {
  140.             System.out.println("Filter --> " + url);
  141.             return true;
  142.         }
  143.  
  144.         return false;
  145.     }
  146.  
  147. }
  148.  
  149. class ProxiedValueVO {
  150.     private String text;
  151.     private boolean changed;
  152.  
  153.     public String getText() {
  154.         return text;
  155.     }
  156.  
  157.     public void setText(String text) {
  158.         this.text = text;
  159.     }
  160.  
  161.     public boolean isChanged() {
  162.         return changed;
  163.     }
  164.  
  165.     public void setChanged(boolean changed) {
  166.         this.changed = changed;
  167.     }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement