问题描述
我想创建一个代理服务器,该服务器可以根据用户的基本身份验证令牌过滤网站,并能够获得他们使用多少互联网的配额。
我在NPM上找到了几个软件包(http-proxy,proxy等),当我使用cURL测试服务器时,它们都工作得很好。我能够拦截请求标头,并从令牌中获得授权用户使用的白名单URL,然后从那里继续我的逻辑。
但是,一旦我使用移动设备对其进行测试,服务器就无法从标头中获取basic64令牌。由于某些原因,它永远不会存在。
我的问题是,如何成功创建对移动设备上的用户进行身份验证的代理服务器?
使用http-proxy的代码:
const http = require('http');
const httpProxy = require('http-proxy');
const port = 8128;
const username = 'username';
const password = 'password';
const authBuffer = Buffer.from(username + ':' + password,'ascii');
const authToken = 'Basic ' + authBuffer.toString('base64');
const proxy = httpProxy.createProxyServer({});
const server = http.createServer((req,res) => {
console.log("here")
var authHeader = req.headers['proxy-authorization'];
console.log(req.headers)
if (authHeader === authToken) {
console.log('[%s] [forward] %s',new Date,req.url);
}
else {
console.error('[%s] [auth failed %s] %s',authHeader,req.url);
res.statusCode = 404;
res.end();
}
});
server.timeout = 600000;
server.on('clientError',(err,socket) => {
console.error('[%s] [error %s] %s',err.message);
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
console.log("listening on port %d",port)
server.listen(port);
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)