Changeset 6 for trunk


Ignore:
Timestamp:
Apr 21, 2010, 8:38:47 AM (14 years ago)
Author:
fmguler
Message:

SimpleJavaProxy? safari üzerinden çalışabilen versiyonu. Browser (User Agent) cookie davranışını simüle eder. Sunucudan gelen cookie'leri domainlere göre maplerde tutar. Bu domainlere cookieleri geri gönderir. Sunucu bu proxyden geçen browserları tek browser zannetmesi için bunu yapar.

Çalışmıyordu, eğer bizde bu site için cookie yoksa browserda varsa (origCookie) onu gönder deyince çalışmaya başladı. Javascriptten login sırasında cookie manipüle ediyor sanırsam. Key değeri hiç bir zaman sunucuda set edilmiyor.

Çalışması için, browserdan bir kere normal login olup sonra proxy ayarını yapmak lazım.

Location:
trunk/SimpleJavaProxy/src/simplejavaproxy
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/SimpleJavaProxy/src/simplejavaproxy/Main.java

    r4 r6  
    1717    public static void main(String[] args) { 
    1818        // TODO code application logic here 
    19         //SimpleProxy.main(new String[]{"9999","127.0.0.1","8888"}); 
    20         SimpleProxy.main(new String[]{"9999"}); 
     19        SimpleProxy.main(new String[]{"9999","127.0.0.1","8888"}); 
     20        //SimpleProxy.main(new String[]{"9999"}); 
    2121    } 
    2222 
  • trunk/SimpleJavaProxy/src/simplejavaproxy/SimpleProxy.java

    r4 r6  
    5555import java.net.*; 
    5656import java.lang.reflect.Array; 
     57import java.util.HashMap; 
     58import java.util.Iterator; 
     59import java.util.Map; 
     60import java.util.concurrent.ConcurrentHashMap; 
    5761 
    5862public class SimpleProxy extends Thread { 
    59  
    6063    public static final int DEFAULT_PORT = 8080; 
    6164    private ServerSocket server = null; 
     
    6467    private int fwdPort = 0; 
    6568    private int ptTimeout = ProxyThread.DEFAULT_TIMEOUT; 
    66     private int debugLevel = 0; 
     69    private int debugLevel = 1; 
    6770    private PrintStream debugOut = System.out; 
    6871    //FMG>> 
    6972    public static String safariKey = ""; 
     73    public static Map cookieMap = new ConcurrentHashMap(); 
    7074 
    7175 
     
    226230 */ 
    227231class ProxyThread extends Thread { 
    228  
    229232    private Socket pSocket; 
    230233    private String fwdServer = ""; 
     
    352355            // keep in mind that because we're using threads, the output won't 
    353356            // necessarily be synchronous 
    354             if (debugLevel > 0) { 
     357            if (debugLevel > 5) { 
    355358                long endTime = System.currentTimeMillis(); 
    356359                debugOut.println("Request from " + pSocket.getInetAddress().getHostAddress() 
     
    362365                debugOut.flush(); 
    363366            } 
    364             if (debugLevel > 1) { 
     367            if (debugLevel > 10) { 
    365368                debugOut.println("REQUEST:\n" + (new String(request))); 
    366369                debugOut.println("RESPONSE:\n" + (new String(response))); 
     
    416419        int pos = -1; 
    417420        int byteCount = 0; 
     421        String originalCookies = null; 
    418422 
    419423        try { 
     
    457461 
    458462                //************************************************************** 
    459                 //FMG>>: cookie al, Cookie: ya da Set-Cookie: header'larını al. 
    460                 //Cookie içindeki Safari->key değerini olması gerekenle değiştir (safariKey) 
    461                 //Set-Cookie ile gelen Safari->key değerini güncelle (safariKey) 
     463                //FMG>>: simulate browser (User Agent) cookie behaviour 
     464                //We want the server beleive that we are a single browser. 
    462465 
    463466                //Set-Cookie: Safari=cookieversion=2&portal=my&key=E894F897E4A4912E6D683D6939BE74D192731C8F4191A3ACAB6B3FEE16588D2954DBF3B9BF495294EE68A4477E3D772EAA5B0E89433D9926603960942389D153574A4D8BAB4095687832999AECDC2C751104CB8D1A30141A9BCD769E3A126390FFDD02DA9C28307232CD000C64D4F0C3D2993A357CCBED2A19E0467900E3884D71&sessionid=1a923806-a923-4442-9579-325c77ae39db&ref=Google&logged=true&oref=http%3a%2f%2fwww.google.com.tr%2furl%3fsa%3dt%26source%3dweb%26ct%3dres%26cd%3d1%26ved%3d0CAYQFjAA%26url%3dhttp%253A%252F%252Fmy.safaribooksonline.com%252F%26rct%3dj%26q%3dsafari%2bbook%26ei%3d2ZHNS_XcBIX0ObjplcwP%26usg%3dAFQjCNFeqvBpwDvMtcrCBnfHPPiUQofzPA; Path=/; Domain=my.safaribooksonline.com 
     
    468471                if (pos >= 0) { 
    469472                    String setCookie = data.substring(pos + 11).trim(); 
    470                     int safariKeyIndexStart = setCookie.indexOf("&key=") + 5; 
    471                     int safariKeyIndexEnd = setCookie.indexOf("&", safariKeyIndexStart); 
    472  
    473                     if (safariKeyIndexStart >= 5) { //YAP: add extra controls, key cookie may be present in other sites as well. may be limit this to safari host 
    474                         //get the key from response header 
    475                         String key = setCookie.substring(safariKeyIndexStart, safariKeyIndexEnd); 
    476                         SimpleProxy.safariKey = key; 
    477                         System.out.println("Safari Key in the set-cookie is: " + SimpleProxy.safariKey); 
    478                     } 
    479                 } 
    480  
    481                 // check for the Cookie header 
     473                    handleSetCookie(setCookie); 
     474                } 
     475 
     476                //YAP: cookie headerını iptal et, aşağıda biz gönderiyoruz çünkü 
    482477                pos = data.toLowerCase().indexOf("cookie:"); 
    483478                if (pos >= 0) { 
    484                     boolean logged = false; 
    485  
    486                     int loggedStart = data.indexOf("&logged=") + 8; 
    487                     int loggedEnd = data.indexOf("&", loggedStart); 
    488                     if (loggedStart >= 8) { 
    489                         logged = data.substring(loggedStart, loggedEnd).equals("true"); 
    490                     } 
    491  
    492                     int safariKeyIndexStart = data.indexOf("&key=") + 5; 
    493                     int safariKeyIndexEnd = data.indexOf("&", safariKeyIndexStart); 
    494  
    495                     if (logged && (safariKeyIndexStart >= 5)) { //YAP: add extra controls, key cookie may be present in other sites as well. may be limit this to safari host 
    496                         //replace the key in the cookie with safariKey 
    497                         String cookie1 = data.substring(0, safariKeyIndexStart); //first part 
    498                         String cookie2 = data.substring(safariKeyIndexEnd); //remaining part 
    499  
    500                         System.out.println("Old cookie:        '" + data + "'"); 
    501                         data = (cookie1 + SimpleProxy.safariKey + cookie2); 
    502                         System.out.println("Changed cookie to: '" + data + "'"); 
    503                     } 
     479                    //we do not send original cookies 
     480                    originalCookies = data.substring(pos + 7); 
     481                } else { 
     482                    header.append(data + "\r\n"); 
    504483                } 
    505484 
    506485                //<<FMG 
    507486                //************************************************************** 
    508  
    509                 header.append(data + "\r\n"); 
    510                 data = ""; 
    511             } 
     487            } 
     488 
     489            //FMG>> 
     490            //lastly send cookie if applicable for this host 
     491            String cookie = getCookiesForHost(host.toString()); 
     492            if (cookie != null) header.append(cookie + "\r\n"); 
     493            if (originalCookies != null) System.out.println("\t _ookie:" + originalCookies); 
     494            if (cookie == null && originalCookies != null) header.append("Cookie: " + originalCookies + "\r\n"); 
     495            //<<FMG 
    512496 
    513497            // add a blank line to terminate the header info 
     
    584568                    break; 
    585569                } else { 
    586                     data.append((char) c); 
     570                    data.append((char)c); 
    587571                } 
    588572            } 
     
    605589        return data.toString(); 
    606590    } 
     591 
     592    //FMG>> 
     593    //We are simulating the User Agent role as in RFC2965 
     594    //the cookies coming from server, for a host 
     595    private void handleSetCookie(String setCookie) { 
     596        //global cookie map for each domain 
     597        Map cookieMap = SimpleProxy.cookieMap; 
     598 
     599        //parsed attribute value pair for setCookie (we should not send Domain, Path, Expires cookies back to server, so remove them) 
     600        Map cookieValues = parseCookies(setCookie); 
     601        String domain = (String)cookieValues.remove("Domain"); 
     602        if (domain == null) domain = (String)cookieValues.remove("domain"); 
     603        if (domain == null) domain = ""; 
     604        cookieValues.remove("Path"); 
     605        cookieValues.remove("Expires"); 
     606        cookieValues.remove("path"); 
     607        cookieValues.remove("expires"); 
     608 
     609        //get cookies for this domain 
     610        Map cookiesForDomain = (Map)cookieMap.get(domain); 
     611        //create if not exists 
     612        if (cookiesForDomain == null) cookiesForDomain = new ConcurrentHashMap(); 
     613        //put & override the existing values 
     614        cookiesForDomain.putAll(cookieValues); 
     615        //set cookies for this host 
     616        cookieMap.put(domain, cookiesForDomain); 
     617 
     618        if (debugLevel > 0) { 
     619            System.out.println(">>>> Got new cookie for domain: '" + domain); 
     620            Iterator it = cookiesForDomain.keySet().iterator(); 
     621            while (it.hasNext()) { 
     622                String key = (String)it.next(); 
     623                System.out.print("\tKey: " + key); 
     624                System.out.println("  Value: " + cookiesForDomain.get(key)); 
     625            } 
     626        } 
     627 
     628    } 
     629 
     630    private Map parseCookies(String cookies) { 
     631        Map result = new HashMap(); 
     632        String[] cookieArray = cookies.split(";"); 
     633        for (int i = 0; i < cookieArray.length; i++) { 
     634            String attributeValue = cookieArray[i]; 
     635            int eqIndex = attributeValue.indexOf("="); 
     636            if (eqIndex == -1) continue; 
     637            String attribute = attributeValue.substring(0, eqIndex); 
     638            String value = attributeValue.substring(eqIndex + 1); 
     639            result.put(attribute.trim(), value.trim()); 
     640        } 
     641 
     642        return result; 
     643    } 
     644 
     645    //returns the cookies to be sent for a host 
     646    private String getCookiesForHost(String host) { 
     647        //global cookie map for each domain 
     648        Map cookieMap = SimpleProxy.cookieMap; 
     649 
     650        //get cookies for this domain 
     651        Map cookiesForDomain = (Map)cookieMap.get(host); 
     652        if (cookiesForDomain == null) return null; 
     653 
     654        StringBuffer cookie = new StringBuffer(); 
     655        cookie.append("Cookie: "); 
     656 
     657        Iterator it = cookiesForDomain.keySet().iterator(); 
     658        while (it.hasNext()) { 
     659            String key = (String)it.next(); 
     660            cookie.append(key); 
     661            cookie.append("="); 
     662            cookie.append(cookiesForDomain.get(key)); 
     663            if (it.hasNext()) cookie.append(";"); 
     664        } 
     665 
     666        if (debugLevel > 0) { 
     667            System.out.println("Sending cookie for host: " + host); 
     668            System.out.println("\t " + cookie.toString()); 
     669        } 
     670 
     671 
     672        return cookie.toString(); 
     673    } 
    607674} 
Note: See TracChangeset for help on using the changeset viewer.