使用国外虚拟主机上网

/ 0评 / 0

GitHub学生包新成员里面有一个EducationHost,虚拟主机产品,源自英国,速度就很一般了,把GoAgent翻出来重新搞一搞.

大致配置参数看图就好了.

主要适配修改.

放置一个PHP脚本到虚拟主机的根目录,名字为index.php,打开后显示白页就算成功了.

<?php



$content_type = '';

$content_body = '';



function decode_request($data) {

    list($headers_length) = array_values(unpack('n', substr($data, 0, 2)));

    $headers_data = gzinflate(substr($data, 2, $headers_length));

    $body = substr($data, 2 + intval($headers_length));

    $lines = explode("
", $headers_data);

    $request_line_items = explode(" ", array_shift($lines));

    $method = $request_line_items[0];

    $url = $request_line_items[1];

    $headers = array();

    $kwargs = array();

    $kwargs_prefix = 'X-URLFETCH-';

    foreach ($lines as $line) {

        if (!$line) continue;

        $pair = explode(':', $line, 2);

        $key = $pair[0];

        $value = trim($pair[1]);

        if (stripos($key, $kwargs_prefix) === 0) {

            $kwargs[strtolower(substr($key, strlen($kwargs_prefix))) ] = $value;

        } else if ($key) {

            $key = join('-', array_map('ucfirst', explode('-', $key)));

            $headers[$key] = $value;

        }

    }

    if (isset($headers['Content-Encoding'])) {

        if ($headers['Content-Encoding'] == 'deflate') {

            $body = gzinflate($body);

            $headers['Content-Length'] = strval(strlen($body));

            unset($headers['Content-Encoding']);

        }

    }

    return array(

        $method,

        $url,

        $headers,

        $kwargs,

        $body

    );

}



function echo_content($content) {

    global $content_type;

    if ($content_type == 'image/gif') {

        echo $content ^ str_repeat("1", strlen($content));

    } else {

        echo $content;

    }

}



function curl_header_function($ch, $header) {

    global $content_body, $content_type;

    $pos = strpos($header, ':');

    if ($pos == false) {

        $content_body.= $header;

    } else {

        $key = join('-', array_map('ucfirst', explode('-', substr($header, 0, $pos))));

        if ($key != 'Transfer-Encoding') {

            $content_body.= $key . substr($header, $pos);

        }

    }

    if (preg_match('@^Content-Type: ?(audio/|image/|video/|application/octet-stream)@i', $header)) {

        $content_type = 'image/x-png';

    }

    if (!trim($header)) {

        header('Content-Type: ' . $content_type);

    }

    return strlen($header);

}



function curl_write_function($ch, $content) {

    global $content_body;

    if ($content_body) {

        echo_content($content_body);

        $content_body = '';

    }

    echo_content($content);

    return strlen($content);

}



if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    list($method, $url, $headers, $kwargs, $body) = @decode_request(@file_get_contents('php://input'));

    if ($body) {

        $headers['Content-Length'] = strval(strlen($body));

    }

    if (isset($headers['Connection'])) {

        $headers['Connection'] = 'close';

    }

    $header_array = array();

    foreach ($headers as $key => $value) {

        $header_array[] = join('-', array_map('ucfirst', explode('-', $key))) . ': ' . $value;

    }

    $curl_opt = array();

    switch (strtoupper($method)) {

        case 'HEAD':

            $curl_opt[CURLOPT_NOBODY] = true;

            break;



        case 'GET':

            break;



        case 'POST':

            $curl_opt[CURLOPT_POST] = true;

            $curl_opt[CURLOPT_POSTFIELDS] = $body;

            break;



        case 'PUT':

        case 'DELETE':

            $curl_opt[CURLOPT_CUSTOMREQUEST] = $method;

            $curl_opt[CURLOPT_POSTFIELDS] = $body;

            break;



        default:

            exit(0);

    }

    $curl_opt[CURLOPT_HTTPHEADER] = $header_array;

    $curl_opt[CURLOPT_RETURNTRANSFER] = true;

    $curl_opt[CURLOPT_BINARYTRANSFER] = true;

    $curl_opt[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1;

    $curl_opt[CURLOPT_HEADER] = false;

    $curl_opt[CURLOPT_HEADERFUNCTION] = 'curl_header_function';

    $curl_opt[CURLOPT_WRITEFUNCTION] = 'curl_write_function';

    $curl_opt[CURLOPT_FAILONERROR] = false;

    $curl_opt[CURLOPT_FOLLOWLOCATION] = false;

    $curl_opt[CURLOPT_CONNECTTIMEOUT] = 30;

    $curl_opt[CURLOPT_TIMEOUT] = 30;

    $curl_opt[CURLOPT_SSL_VERIFYPEER] = false;

    $curl_opt[CURLOPT_SSL_VERIFYHOST] = false;

    $ch = curl_init($url);

    curl_setopt_array($ch, $curl_opt);

    $ret = curl_exec($ch);

    $errno = curl_errno($ch);

    if ($GLOBALS['content_body']) {

        echo_content($GLOBALS['__content__']);

    }

    curl_close($ch);

}else{

    

}



下载改装版的GoAgent.

https://github.com/bclswl0827/goagent-php-bclswl/archive/download.zip

修改php字段.(密码无所谓,1开头后面随便写)

关闭autodetect

关闭GAE.

以管理员启动,就可以用了,但是,貌似Chrome有什么限制,推荐Firefox.

关于路由,不是绕地球一圈,已经谢天谢地.

至于关于速度问题...

可能流量才是更大的问题吧,英国机房速度肯定惨不忍睹的了...