0

我想实现像 kproxy.com 这样的代理网站,这样我就可以在我网站的 iframe 中加载任何网站,并从我的服务器代理网站的所有数据

我检查了它们的功能,发现它们替换了所有脚本标签、链接(css)标签和图像标签以从他们的服务器获取内容

例如。如果原始网站包含标签

<script src="http://google.com/abc.js"></script>

他们会用

<script src="http://kproxy.com/redirect/foo/bar/abc.js"></script>

我已经通过替换所有节点来完成这种功能,因此它们可以通过我的服务器进行代理

但现在问题仍然存在于 ajax 调用,它将由 javascript 发起并调用原始服务器,所以在我的 iframe 中我有时会"x-frame-options = SAMEORIGIN"出错

那么我该怎么做才能拥有像 kproxy 一样的功能呢?并仅通过我的服务器代理所有流量。

4

1 回答 1

1

Your problem arises from some links (probably AJAX ones) being generated towards a different domain.

You should check the scripts you download for URLs being built at runtime like

...a="https:"==g.location.protocol?"https://csi.gstatic.com/csi":"http://csi.gstatic.com/csi");return...

(example taken from Google Analytics). Same goes for some jQuery applets.

Also, you should verify that some of the scripts don't make AJAX calls of their own to retrieve further URLs. If they do, you need to check whether you want to proxy those calls as well.

Basically, for each call that gives you a same-origin failure, you need to track whence it came from, and instruct your proxy to recognize and rewrite it.

Or you can try and do the same in Javascript, i.e., inject Javascript code that will rewrite those URLs at runtime. For example you could have an explicit check for, say, CKEditor

// client requested a script and you, the proxy, fetched it in 'script'
if (script contains 'CKEDIT.options') {
    determine init call position in script
    split the script in two, A and B, before and after the init call
    make 'script' equal to a new script made up of B plus C plus A concatenated
    where C is the line "CKEDIT.options.url = 'http://mysite/proxy...';\n"
    so that, user side, the initialization will happen with your site's address
    The script will then propagate that address in lots of places without need
    for you to rewrite anything else.
} else {
    // script is unmodified
}
... other checks like the above...

... finally:
send 'script' to the client
于 2014-11-03T12:46:19.573 回答