diff --git a/docs/install/configuration.md b/docs/install/configuration.md index 36de449a..5f46caf4 100644 --- a/docs/install/configuration.md +++ b/docs/install/configuration.md @@ -67,3 +67,24 @@ list of strings you can override. > warning "Restart needed" > Note that a restart is needed when you change user_locale's content. + +### Proxy +Proxy outward request from gancio. +Look [here](https://www.npmjs.com/package/https-proxy-agent) for options. +Note: `hostname` option takes precedence over `host`. + +```json +"proxy": { + "protocol": "http:", + "hostname": "", + "host": "127.0.0.1", + "port": "8118", + "auth": { + "username": "user", + "password": "password" + }, + "headers": { + "X-Proxy-Header": "example" + } +} +``` diff --git a/modules/axios-proxy.js b/modules/axios-proxy.js new file mode 100644 index 00000000..4a617857 --- /dev/null +++ b/modules/axios-proxy.js @@ -0,0 +1,26 @@ +const HttpsProxyAgent = require("https-proxy-agent"), + axios = require("axios"), + config = require('../server/config') + +function axiosProxy (_moduleOptions) { + + if (config.proxy && !(!config.proxy.hostname && !config.proxy.host)) { + const httpsAgent = new HttpsProxyAgent({ + protocol: config.proxy.protocol, + hostname: config.proxy.hostname, + host: config.proxy.host, + port: config.proxy.port, + auth: config.proxy.auth.username + ':' + config.proxy.auth.password, + headers: config.proxy.headers + }) + + // Use axios as you normally would, but specify httpsAgent in the default configs + // https://github.com/nuxt-community/axios-module/pull/428#issuecomment-743313813 + // Nuxt 2: https://github.com/axios/axios/issues/925#issuecomment-513028175 + // Nuxt 3: https://github.com/unjs/ofetch#-adding-https-agent + axios.defaults.httpsAgent = httpsAgent + } + +} + +module.exports = axiosProxy \ No newline at end of file diff --git a/nuxt.config.js b/nuxt.config.js index 6036a7ff..83f74987 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -55,6 +55,7 @@ module.exports = { modules: [ // Doc: https://axios.nuxtjs.org/usage '@nuxtjs/i18n', + '~/modules/axios-proxy.js', // Note: import this before @nuxtjs/axios to override defaults of both instances: `$axios` available in context, and `axios` used in controllers '@nuxtjs/axios', '@nuxtjs/auth', '@nuxtjs/sitemap', diff --git a/server/config.js b/server/config.js index 2388311d..50ff6474 100644 --- a/server/config.js +++ b/server/config.js @@ -15,6 +15,17 @@ let config = { db: {}, user_locale: path.resolve(process.env.cwd || '', 'user_locale'), upload_path: path.resolve(process.env.cwd || '', 'uploads'), + proxy: { + protocol: process.env.GANCIO_PROXY_PROTOCOL || '', + hostname: process.env.GANCIO_PROXY_HOSTNAME || '', + host: process.env.GANCIO_PROXY_HOST || '', + port: process.env.GANCIO_PROXY_PORT || '', + auth: { + username: process.env.GANCIO_PROXY_USERNAME || '', + password: process.env.GANCIO_PROXY_PASSWORD || '' + }, + headers: process.env.GANCIO_PROXY_HEADERS && JSON.parse(process.env.GANCIO_PROXY_HEADERS) || {} + }, write (config_path= process.env.config_path || './config.json') { delete config.status return fs.writeFileSync(config_path, JSON.stringify(config, null, 2))