diff --git a/bin/anyproxy b/bin/anyproxy index 76e853c38..35c6c2203 100755 --- a/bin/anyproxy +++ b/bin/anyproxy @@ -26,7 +26,7 @@ program if (program.clear) { require('../lib/certMgr').clearCerts(() => { - util.deleteFolderContentsRecursive(util.getAnyProxyPath('cache')); + util.deleteFolderContentsRecursive(util.getAnyProxyTmpPath()); console.log(color.green('done !')); process.exit(0); }); diff --git a/lib/recorder.js b/lib/recorder.js index 728afc5a3..cd9707c35 100644 --- a/lib/recorder.js +++ b/lib/recorder.js @@ -31,7 +31,7 @@ const WS_MESSAGE_FILE_PRFIX = 'ws_message_'; const CACHE_DIR_PREFIX = 'cache_r'; function getCacheDir() { const rand = Math.floor(Math.random() * 1000000), - cachePath = path.join(proxyUtil.getAnyProxyPath('cache'), './' + CACHE_DIR_PREFIX + rand); + cachePath = path.join(proxyUtil.getAnyProxyTmpPath(), './' + CACHE_DIR_PREFIX + rand); fs.mkdirSync(cachePath); return cachePath; diff --git a/lib/ruleLoader.js b/lib/ruleLoader.js index 1d498ff67..3dfa4a536 100644 --- a/lib/ruleLoader.js +++ b/lib/ruleLoader.js @@ -5,7 +5,7 @@ const path = require('path'); const fs = require('fs'); const request = require('request'); -const cachePath = proxyUtil.getAnyProxyPath('cache'); +const cachePath = proxyUtil.getAnyProxyTmpPath(); /** * download a file and cache diff --git a/lib/util.js b/lib/util.js index d06536bad..4048e6c9d 100644 --- a/lib/util.js +++ b/lib/util.js @@ -5,9 +5,11 @@ const fs = require('fs'), mime = require('mime-types'), color = require('colorful'), child_process = require('child_process'), + os = require('os'), Buffer = require('buffer').Buffer, logUtil = require('./log'); -const networkInterfaces = require('os').networkInterfaces(); + +const networkInterfaces = os.networkInterfaces(); // {"Content-Encoding":"gzip"} --> {"content-encoding":"gzip"} module.exports.lower_keys = (obj) => { @@ -52,6 +54,14 @@ module.exports.getAnyProxyPath = function (pathName) { return targetPath; } +module.exports.getAnyProxyTmpPath = function () { + const targetPath = path.join(os.tmpdir(), 'anyproxy', 'cache'); + if (!fs.existsSync(targetPath)) { + fs.mkdirSync(targetPath, { recursive: true }); + } + return targetPath; +} + module.exports.simpleRender = function (str, object, regexp) { return String(str).replace(regexp || (/\{\{([^{}]+)\}\}/g), (match, name) => { if (match.charAt(0) === '\\') {