Add proxy support

This commit is contained in:
Yeechan Lu
2017-08-02 15:22:41 +08:00
parent 8dc2b6e8e1
commit 1aa4c8673e
7 changed files with 33 additions and 7 deletions

View File

@@ -31,3 +31,5 @@ exports.verifyPayment = function (platform, payment, cb) {
cb(null, result);
});
};
exports.setProxy = require('./lib/config').setProxy

View File

@@ -125,10 +125,10 @@ exports.verifyPayment = function (payment, cb) {
}
verify(apiUrls.production, { json: jsonData }, function (error, resultString) {
verify(apiUrls.production, { json: jsonData, platform: "apple" }, function (error, resultString) {
// 21007: this is a sandbox receipt, so take it there
if (error && error.status === 21007) {
return verify(apiUrls.sandbox, { json: jsonData }, checkReceipt);
return verify(apiUrls.sandbox, { json: jsonData, platform: "apple" }, checkReceipt);
}
return checkReceipt(error, resultString);

18
lib/config.js Normal file
View File

@@ -0,0 +1,18 @@
var proxyingAgent = require('proxying-agent')
var agents = {
apple: null,
google: null,
}
exports.setProxy = function (platform, config) {
if (config) {
agents[platform] = proxyingAgent.create(config, "https://localhost");
} else {
agents[platform] = null;
}
}
exports.getAgent = function (platform) {
return agents[platform];
}

View File

@@ -58,7 +58,7 @@ exports.verifyPayment = function (payment, cb) {
);
}
https.get(requestUrl, null, function (error, res, responseString) {
https.get(requestUrl, { platform: "google" }, function (error, res, responseString) {
if (error) {
return cb(error);
}

View File

@@ -33,7 +33,7 @@ exports.getToken = function (iss, key, scope, cb) {
assertion: jwtToken
};
https.post(apiUrls.tokenRequest, { form: formData }, function (error, res, responseString) {
https.post(apiUrls.tokenRequest, { form: formData, platform: "google" }, function (error, res, responseString) {
if (error) {
return cb(error);
}

View File

@@ -1,6 +1,6 @@
var url = require('url');
var https = require('https');
var config = require('../config')
module.exports = function (requestUrl, options, data, cb) {
options = options || {};
@@ -19,6 +19,11 @@ module.exports = function (requestUrl, options, data, cb) {
options.path = parsedUrl.path;
}
if (options.platform) {
options.agent = config.getAgent(options.platform);
delete options.platform;
}
var req = https.request(options, function (res) {
res.setEncoding('utf8');

View File

@@ -21,9 +21,10 @@
"author": "Ron Korving <rkorving@wizcorp.jp>",
"license": "MIT",
"dependencies": {
"minimist": "0.0.8",
"form-urlencoded": "0.0.6",
"jwt-simple": "0.2.0",
"form-urlencoded": "0.0.6"
"minimist": "0.0.8",
"proxying-agent": "^2.1.1"
},
"devDependencies": {
"jsdoc": "3.3.0-alpha4",