Hi daspears,

Ok  I attached whole script at the end of this post.

It has been tested on
Linux Fedora Core 6 + PHP5 +Android m5rc15
and
Linux Fedora Core 9 + PHP5 +Android 0.9beta .

Please keep a mind this is a workaround and possibly be dangerous:-).

This script runs as proxy's proxy.
1.
Please cut and paste this code and save it.
The file name is "AndroidMapProxy.php" in this example.

2.
For example, your site proxy http://aaa.bbb.com:8000 and your Linux
host (local PC) xxx.yyy.com, please run this program on your local PC
like this;

 [EMAIL PROTECTED] AndroidMapProxy.php http://aaa.bb.com:8000 8080

3.
then, run android emulator like this;

  [root:@xxx]$ emulator -http-proxy http://xxx.yyy.com:8080


The script begins here.

#!/usr/bin/php
<?php
class GoogleMapProxyServer {
        private $socket = null;
        private $outbound = null;
        private $inbound = null;
        private $active = false;
        private $site_proxy = null;

        public function __construct($site_proxy, $port = 8080) {
                // open an out-bound socket to connect the site proxy.
                $c = stream_socket_client($site_proxy, $errno, $errstr, 30);
                if ($c === false) {
                        $message = sprintf("error:%d %s", $errstr, $errno);
                        $this->error($message);
                }
                fclose($c);
                $this->site_proxy = $site_proxy;

                // create in-bound socket to connect emulator.
                $this->socket = stream_socket_server("tcp://0.0.0.0:".$port, 
$errno,
$errstr);
                if (!$this->socket) {
                        $message = sprintf("error:%d %s", $errstr, $errno);
                        $this->error($message);
                }
        }

        public function __destruct() {
                if (is_resource($this->socket)) {
                        fclose($this->socket);
                }
                if (is_resource($this->inbound)) {
                        fclose($this->inbound);
                }
                if (is_resource($this->outbound)) {
                        fclose($this->outbound);
                }
        }

        public function execute() {
                while (true) {
                        // wait for a message from emulator.
                        $timeout = 9999.0;
                        $this->inbound = stream_socket_accept($this->socket, 
$timeout);
                        if ($this->inbound === false) {
                                $this->error("socket accept error");
                        }
                        // echo "new connection.\n";

                        // set the timeout value and the non-blocking mode on 
the stream.
                        stream_set_timeout($this->inbound, 300);
                        if (false === stream_set_blocking($this->inbound, 0)) {
                                echo "warning: cannot set  non blocking on 
in-bound stream\n";
                        }

                        // open an out-bound socket to connect the site proxy.
                        $this->outbound = 
stream_socket_client($this->site_proxy, $errno,
$errstr, 30);
                        if ($this->outbound === false) {
                                $message = sprintf("error:%d %s", $errstr, 
$errno);
                                $this->error($message);
                        }
                        // set the timeout value and the non-blocking mode on 
the stream.
                        stream_set_timeout($this->outbound, 300);
                        if (false === stream_set_blocking($this->outbound, 0)) {
                                echo "warning: cannnot set non blocking on 
out-bound stream\n";
                        }

                        // set flag.
                        $this->active = true;
                        while ($this->active) {
                                // use a select function because PHP does not 
support a thread.
                                $read = array($this->inbound, $this->outbound);
                                $write = NULL;
                                $except = NULL;
                                if (false === ($n = stream_select($read, 
$write, $except, 10)))
                                {
                                        $this->error("select error");
                                }
                                switch ($n) {
                                case 0:
                                        $this->active = false;
                                        break;
                                case 1:
                                        if ($read[0] === $this->inbound)
                                        {
                                                if (!feof($this->inbound)) {
                                                        $request = 
$this->in_to_out();
                                                }
                                                else {
                                                        $this->active = false;
                                                }
                                        }
                                        if ($read[0] === $this->outbound)
                                        {
                                                if (!feof($this->outbound)) {
                                                        $request = 
$this->out_to_in();
                                                }
                                                else {
                                                        $this->active = false;
                                                }
                                        }
                                        break;
                                case 2:
                                        $request = $this->in_to_out();
                                        $request = $this->out_to_in();
                                        if ((feof($this->inbound)) && 
feof($this->outbound)) {
                                                $this->active = false;
                                        }
                                        break;
                                default:
                                        // never get to here.
                                        $this->error("select unknown error");
                                        break;
                                }
                        }

//                      echo "close sockets.\n";
                        fclose($this->outbound);
                        fclose($this->inbound);
                }
        }

        private function out_to_in() {
//              echo "out to in.\n";
                while (!feof($this->outbound)) {
                        $buf = fread($this->outbound, 2048);
                        if ($buf === false) {
                                $this->error("read error on out-bound stream");
                                break;
                        }
                        if (strlen($buf) == 0) {
                                break;
                        }
                        fwrite($this->inbound, $buf);
                }
        }

        private function in_to_out() {
//              echo "in to out.\n";
                while (!feof($this->inbound)) {
                        $req = fread($this->inbound, 2048);

                        if ($req === false) {
                                $this->error("read error on in-bound stream");
                        }
                        if (strlen($req) == 0) {
                                break;
                        }
                        // ignore CONNECT method because it is most likely no 
use.
                        if (preg_match("/CONNECT\s+.*:80/", $req)) {
                                $buf = "HTTP/1.1 200 OK\r\n\r\n";
                                fwrite($this->inbound, $buf);
                                continue;
                        }
                        // add a protocol and a host address to be a full URI 
in a POST and/
or GET request.
                        if (preg_match("/(POST|GET)\s+(\S+:|)/", $req, 
$matches)) {
                                if ($matches[2] == "") {        /* no protocol 
specified */
                                        $method = $matches[1];
                                        if (preg_match("/Host:\s*(\S.*)\s*/", 
$req, $matches)) {        /* host
retrieved from header */
                                                $host = rtrim($matches[1]);
                                                $req =  preg_replace("/$method 
\/(.*)/", "$method http://$host/$1";,
$req);
                                        }
                                }
                        }
                        // display received message. only for debugging.
//                      echo $req;
                        fwrite($this->outbound, $req);
//                      echo "size of req ".strlen($req).".\n";
                }
        }

        private function error($message) {
                die($message);
        }
}

// Server main process
if ((isset($argv[1])) && (preg_match("/^http:\/\/(.+)\.(.+):(\d+)/",
$argv[1], $matches)))
{
        $http_proxy =  preg_replace("/http:\/\//", 'tcp://', $argv[1]);
        $local_port = 8080;
        if (isset($argv[2]) && (preg_match("^\d+$", $argv[2])))
        {
                $local_port = $argv[2];
        }
        $server = new GoogleMapProxyServer($http_proxy, $local_port);
        $server->execute();
}
else
{
        die("usage: " . $argv[0] . " http://your-proxy:port\n";);
}
?>


On 8月22日, 午前7:23, daspears <[EMAIL PROTECTED]> wrote:
> Jokochi,
>
> I would be interested in seeing your solution as well.  Thanks.
>
> On Aug 21, 5:12 am, jokochi <[EMAIL PROTECTED]> wrote:
>
> > I am in the same situation that the browser in 0.9 couldn't get any
> > data behind proxy.
> > Also I recognize this problem is same as map view.
>
> > Because of most proxy server will not convey a packet besides SSL(443)
> > port with CONNECT method but MapView is trying to connect with CONNECT
> > method first.
> > The browser in 0.9 has changed same as MapView.
>
> > As for the solution, I make a small program that ignore the CONNECT
> > method and change the POST's URI.
> > It is written in PHP and it is only 200 lines.
> > Please let me know if you are interesting in my program, and I will
> > give it.
>
> > On 8月21日, 午後2:26, shan <[EMAIL PROTECTED]> wrote:
>
> > > No the browser is not working till now, the -http-proxystartup option
> > > does'nt seem to work(for the 0.9).
>
> > > But in the m5r15 i could get the browser to work via the http_proxy
> > > field in the system table, but the maps application could not connect
> > > to the server. I think that it uses a config totally different from
> > > the standard ones, to which we dont have access.
>
> > > I am guessing that the situation would be same in 0.9 as well. Only
> > > hope being that the maps API would recognise the http-proxyoption.
>
> > > On Aug 21, 1:34 am, "Justin (Google Employee)" <[EMAIL PROTECTED]>
> > > wrote:
>
> > > > Does the browser work in the emulator for you? The emulator doesn't
> > > > deal well with being behind aproxyand my guess would be that you
> > > > aren't able to use any data at all.
>
> > > > Cheers,
> > > > Justin
> > > > Android Team @ Google
>
> > > > On Aug 20, 5:40 am, shan <[EMAIL PROTECTED]> wrote:
>
> > > > > Can the maps API  work behindproxyin the 0.9 beta?- Hide quoted text -
>
> > > > - Show quoted text -

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new Android 0.9 SDK beta!
http://android-developers.blogspot.com/2008/08/announcing-beta-release-of-android-sdk.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to