2016-02-27 124 views
1

我正在尝试构建一个反向代理以与某些API(如Twitter,Github,Instagram)进行对话,然后我可以使用反向代理与任何API进行通信(客户端)应用程序(将其想像为API管理器)。Python-Twisted:反向代理到HTTPS API:无法连接

此外,我正在使用LXC容器来做到这一点。

例如,这里是最简单的,我从在被扭曲文档的例子砍死代码:

from twisted.internet import reactor 
from twisted.web import proxy, server 
from twisted.python.log import startLogging 
from sys import stdout 
startLogging(stdout) 

site = server.Site(proxy.ReverseProxyResource('https://api.github.com/users/defunkt', 443, b'')) 
reactor.listenTCP(8080, site) 
reactor.run() 

当我做容器内卷曲,我得到一个有效的请求(这意味着我得到适当的JSON响应)。

这是我如何使用curl命令:

curl https://api.github.com/users/defunkt 

,这里是输出我得到:

{ 
    "login": "defunkt", 
    "id": 2, 
    "avatar_url": "https://avatars.githubusercontent.com/u/2?v=3", 
    "gravatar_id": "", 
    "url": "https://api.github.com/users/defunkt", 
    "html_url": "https://github.com/defunkt", 
    "followers_url": "https://api.github.com/users/defunkt/followers", 
    "following_url": "https://api.github.com/users/defunkt/following{/other_user}", 
    "gists_url": "https://api.github.com/users/defunkt/gists{/gist_id}", 
    "starred_url": "https://api.github.com/users/defunkt/starred{/owner}{/repo}", 
    "subscriptions_url": "https://api.github.com/users/defunkt/subscriptions", 
    "organizations_url": "https://api.github.com/users/defunkt/orgs", 
    "repos_url": "https://api.github.com/users/defunkt/repos", 
    "events_url": "https://api.github.com/users/defunkt/events{/privacy}", 
    "received_events_url": "https://api.github.com/users/defunkt/received_events", 
    "type": "User", 
    "site_admin": true, 
    "name": "Chris Wanstrath", 
    "company": "GitHub", 
    "blog": "http://chriswanstrath.com/", 
    "location": "San Francisco", 
    "email": "[email protected]", 
    "hireable": true, 
    "bio": null, 
    "public_repos": 107, 
    "public_gists": 280, 
    "followers": 15153, 
    "following": 208, 
    "created_at": "2007-10-20T05:24:19Z", 
    "updated_at": "2016-02-26T22:34:27Z" 
} 

然而,当我尝试通过Firefox中使用获取代理:

http://10.5.5.225:8080/

我得到:“无法连接”

这是我扭日志是什么样子:

2016-02-27 [-] Log opened.

2016-02-27 [-] Site starting on 8080

2016-02-27 [-] Starting factory

2016-02-27 [-] Starting factory

2016-02-27 [-] "10.5.5.225" - - [27/Feb/2016: +0000] "GET/HTTP/1.1" 501 26 "-" "Mozilla/5.0 (X11; Debian; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0"

2016-02-27 [-] Stopping factory

如何使用Twisted来进行API调用(大多数API是HTTPS现在反正),并得到所需要的反应(基本上,什么是“200”响应/ JSON应该是)?

我试图寻找这个问题:Convert HTTP Proxy to HTTPS Proxy in Twisted

但它并没有从编码点的视图多大意义(或提及关于反向代理的任何东西)。

**编辑:我也尝试过使用切换了HTTPS API调用一个普通HTTP调用:

curl http[colon][slash][slash]openlibrary[dot]org[slash]authors[slash]OL1A.json

(URL上面已被格式化,以避免链路冲突的问题)

然而,我的浏览器仍然出现同样的错误(如上所述)。

** EDIT2:我已经尝试运行你的代码,但我得到这个错误:

Error-screenshot

如果你看一下图片,你会看到错误的(当运行代码时):

builtins.AttributeError: 'str' object has no attribute 'decode'

+0

当我运行这个例子时,'curl'和Firefox都会说“无法连接”,所以我不知道你在做什么来获得正确的JSON响应。你是否按照书面形式运行代码示例? – Glyph

+0

哇,你是Twisted的创始人,很高兴认识你,先生!我正在运行代码:'python3 file.py'。就输出而言,我正在编辑我的问题,以便说明如何使用卷曲和输出。这可能是因为你被github API限制了速度(没有API密钥的公共调用有一些限制),但是我成功设法获得了JSON响应。 – coolpy

+0

也很高兴见到你。感谢您使用Twisted :)。现在你已经明确了你如何运行你的命令,我可以回答它... – Glyph

回答

3

如果你读了API documentation for ReverseProxyResource,你会看到的__init__的签名是:

def __init__(self, host, port, path, reactor=reactor): 

和“host”被记录为“要代理的Web服务器的主机”。

所以你传递一个URI,Twisted需要一个主机。

更糟糕的是,ReverseProxyResource是专为在Web服务器上本地使用,并且不相当支持https://网址开箱。

确实有一个(非常有限)可扩展性挂钩,但 - proxyClientFactoryClass - 和ReverseProxyResource没有你需要开箱的事道歉,我会告诉你如何用它来扩展ReverseProxyResource添加https://支持所以你可以使用GitHub API :)。

from twisted.web import proxy, server 
from twisted.logger import globalLogBeginner, textFileLogObserver 
from twisted.protocols.tls import TLSMemoryBIOFactory 
from twisted.internet import ssl, defer, task, endpoints 
from sys import stdout 
globalLogBeginner.beginLoggingTo([textFileLogObserver(stdout)]) 

class HTTPSReverseProxyResource(proxy.ReverseProxyResource, object): 
    def proxyClientFactoryClass(self, *args, **kwargs): 
     """ 
     Make all connections using HTTPS. 
     """ 
     return TLSMemoryBIOFactory(
      ssl.optionsForClientTLS(self.host.decode("ascii")), True, 
      super(HTTPSReverseProxyResource, self) 
      .proxyClientFactoryClass(*args, **kwargs)) 
    def getChild(self, path, request): 
     """ 
     Ensure that implementation of C{proxyClientFactoryClass} is honored 
     down the resource chain. 
     """ 
     child = super(HTTPSReverseProxyResource, self).getChild(path, request) 
     return HTTPSReverseProxyResource(child.host, child.port, child.path, 
             child.reactor) 

@task.react 
def main(reactor): 
    import sys 
    forever = defer.Deferred() 
    myProxy = HTTPSReverseProxyResource('api.github.com', 443, 
             b'/users/defunkt') 
    myProxy.putChild("", myProxy) 
    site = server.Site(myProxy) 
    endpoint = endpoints.serverFromString(
     reactor, 
     dict(enumerate(sys.argv)).get(1, "tcp:8080:interface=127.0.0.1") 
    ) 
    endpoint.listen(site) 
    return forever 

如果你运行这个,curl http://localhost:8080/应该做你的期望。

我冒昧地将你的Twisted代码现代化了一些; endpoints而不是listenTCP,logger而不是twisted.python.logreact,而不是自己启动反应器。

怪异的小putChild片末出现,因为当我们通过b"https://stackoverflow.com/users/defunkt"的路径,这意味着对于/的请求会导致客户端请求/users/defunkt/(注意尾随斜线),这是一个404 GitHub的API 。如果我们明确地将空子段路径代理为好像它没有尾部段,我相信它会做你期望的。

请注意:从纯文本HTTP代理到加密的HTTPS可以极其危险,所以我添加了一个默认这里只有本地主机,监听接口。如果您的字节通过实际网络传输,则应确保使用TLS正确加密它们。

+0

谢谢你的详细解答先生。我首先想说的是,没有必要为软件不能开箱就道歉,但没有解决我的明确使用案例不是任何人的错误:)我成功地能够运行代码,但是我得到一个错误,我将在上面的输入中作为编辑/图像粘贴。我试图解决它,因为我认为这是python3的一个字节/字符串问题,但目前为止没有运气。 – coolpy

+0

由于我的分数低,我不能满足您的答案,但我将其标记为选定的答案。 – coolpy