首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >木星中心配置Http代理问题

木星中心配置Http代理问题
EN

Stack Overflow用户
提问于 2015-12-10 14:57:10
回答 1查看 2K关注 0票数 1

我一直在使用木星中心的Configurable Http代理,并且添加了代理处理客户端ssl证书所需的选项,而不必使用命令行选项。

我的主要目标是,我希望接收客户端向代理的请求,并将他们的证书信息添加到标头。在标题中,我将使用jupyterhub的身份验证器来创建用户名。

我的问题是,当我使用http-代理可用的proxy.on('proxyReq方法设置标头时,我得到了以下错误:[Error: Can't set headers after they are sent.]

我一直在查找所有的代码,以查看在哪里写入或发送响应/请求,但我找不到它。

下面是ConfigurableProxy函数代码,如果需要的话,我可以提供更多:

代码语言:javascript
复制
function ConfigurableProxy (options) {

var that = this;
this.options = options || {};
this.trie = new trie.URLTrie();
this.auth_token = this.options.auth_token;
this.includePrefix = options.includePrefix === undefined ? true : options.includePrefix;
this.routes = {};
this.host_routing = this.options.host_routing;
this.error_target = options.error_target;
if (this.error_target && this.error_target.slice(-1) !== '/') {
    this.error_target = this.error_target + '/'; // ensure trailing /
}
this.error_path = options.error_path || path.join(__dirname, 'error');

if (this.options.default_target) {
    this.add_route('/', {
        target: this.options.default_target
    });
}

options.ws = true;
options.secure= true;
// These are the ssl options

options.ssl = {
    //Right the key and cert are relative path on my computer
    //but these can be changed.
    key: fs.readFileSync('/Users/grantherman/Desktop/jupyterHubCSProject/ssl/server.key'),
    cert: fs.readFileSync('/Users/grantherman/Desktop/jupyterHubCSProject/ssl/server.crt'),
    requestCert: true,
    //Right now this is set to false, but if we add a CA to these options
    // and set this to true, the proxy will reject all unkown ssl certs
    rejectUnauthorized: false
};
var response = [];
var data = [];
var proxy = this.proxy = httpProxy.createProxyServer(options);

proxy.on('proxyReq', function(proxyReq, req, res, options) {
    console.log("proxy request");
    try{
  proxyReq.setHeader('X-Special-Proxy-Header', req.socket.getPeerCertificate());

  }catch(err){
    console.log(err);
    }

});

 proxy.on('data', function(data, req, res, options) {
  data.push(data);
  });

proxy.on('proxyRes', function(proxyRes, req, res, options) {
  response.push(proxyRes);
  });

proxy.on('error', function(error, req, res, options) {
    log.add(error);

});

proxy.on('close', function (req, socket, head) {
  // view disconnected websocket connections
  console.log('Client disconnected');
});


// tornado-style regex routing,
// because cross-language cargo-culting is always a good idea

this.api_handlers = [
    [ /^\/api\/routes(\/.*)?$/, {
        get : bound(this, authorized(this.get_routes)),
        post : json_handler(bound(this, authorized(this.post_routes))),
        'delete' : bound(this, authorized(this.delete_routes))
    } ]
];
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-10 23:16:45

我认为这将需要修改可配置-http-代理本身。在启动代理请求这里之前,添加标头的位置位于原始的这里对象上。

它看起来应该是:

代码语言:javascript
复制
ConfigurableProxy.prototype.handle_proxy = function (kind, req, res) {
    ...
    req.headers['X-My-Header'] = 'My-Value';
    // dispatch the actual method
    this.proxy[kind].apply(this.proxy, args);

在CHP中添加一个钩子,以便在此修改请求,应该可以在不修改CHP源代码的情况下完成此操作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34205066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档