Changeset 6 for trunk/SimpleJavaProxy/src/simplejavaproxy
- Timestamp:
- Apr 21, 2010, 8:38:47 AM (15 years ago)
- Location:
- trunk/SimpleJavaProxy/src/simplejavaproxy
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SimpleJavaProxy/src/simplejavaproxy/Main.java
r4 r6 17 17 public static void main(String[] args) { 18 18 // 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"}); 21 21 } 22 22 -
trunk/SimpleJavaProxy/src/simplejavaproxy/SimpleProxy.java
r4 r6 55 55 import java.net.*; 56 56 import java.lang.reflect.Array; 57 import java.util.HashMap; 58 import java.util.Iterator; 59 import java.util.Map; 60 import java.util.concurrent.ConcurrentHashMap; 57 61 58 62 public class SimpleProxy extends Thread { 59 60 63 public static final int DEFAULT_PORT = 8080; 61 64 private ServerSocket server = null; … … 64 67 private int fwdPort = 0; 65 68 private int ptTimeout = ProxyThread.DEFAULT_TIMEOUT; 66 private int debugLevel = 0;69 private int debugLevel = 1; 67 70 private PrintStream debugOut = System.out; 68 71 //FMG>> 69 72 public static String safariKey = ""; 73 public static Map cookieMap = new ConcurrentHashMap(); 70 74 71 75 … … 226 230 */ 227 231 class ProxyThread extends Thread { 228 229 232 private Socket pSocket; 230 233 private String fwdServer = ""; … … 352 355 // keep in mind that because we're using threads, the output won't 353 356 // necessarily be synchronous 354 if (debugLevel > 0) {357 if (debugLevel > 5) { 355 358 long endTime = System.currentTimeMillis(); 356 359 debugOut.println("Request from " + pSocket.getInetAddress().getHostAddress() … … 362 365 debugOut.flush(); 363 366 } 364 if (debugLevel > 1 ) {367 if (debugLevel > 10) { 365 368 debugOut.println("REQUEST:\n" + (new String(request))); 366 369 debugOut.println("RESPONSE:\n" + (new String(response))); … … 416 419 int pos = -1; 417 420 int byteCount = 0; 421 String originalCookies = null; 418 422 419 423 try { … … 457 461 458 462 //************************************************************** 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. 462 465 463 466 //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 … … 468 471 if (pos >= 0) { 469 472 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ü 482 477 pos = data.toLowerCase().indexOf("cookie:"); 483 478 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"); 504 483 } 505 484 506 485 //<<FMG 507 486 //************************************************************** 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 512 496 513 497 // add a blank line to terminate the header info … … 584 568 break; 585 569 } else { 586 data.append((char) 570 data.append((char)c); 587 571 } 588 572 } … … 605 589 return data.toString(); 606 590 } 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 } 607 674 }
Note: See TracChangeset
for help on using the changeset viewer.