From 62972747866740dae84fc7b519fcedd731572329 Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Tue, 27 Nov 2018 18:06:14 +0100 Subject: feat(App): Add proxy support for services --- src/features/serviceProxy/index.js | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/features/serviceProxy/index.js (limited to 'src/features/serviceProxy') diff --git a/src/features/serviceProxy/index.js b/src/features/serviceProxy/index.js new file mode 100644 index 000000000..edb1c9367 --- /dev/null +++ b/src/features/serviceProxy/index.js @@ -0,0 +1,56 @@ +import { autorun, reaction, observable } from 'mobx'; +import { remote } from 'electron'; + +const { session } = remote; + +const debug = require('debug')('Franz:feature:serviceProxy'); + +const DEFAULT_ENABLED = false; +const DEFAULT_IS_PREMIUM = true; + +export const config = observable({ + isEnabled: DEFAULT_ENABLED, + isPremium: DEFAULT_IS_PREMIUM, +}); + +export default function init(stores) { + reaction( + () => stores.features.features.isServiceProxyEnabled, + (enabled, r) => { + if (enabled) { + debug('Initializing `serviceProxy` feature'); + + // Dispose the reaction to run this only once + r.dispose(); + + const { isServiceProxyEnabled, isServiceProxyPremiumFeature } = stores.features.features; + + config.isEnabled = isServiceProxyEnabled !== undefined ? isServiceProxyEnabled : DEFAULT_ENABLED; + config.isPremium = isServiceProxyPremiumFeature !== undefined ? isServiceProxyPremiumFeature : DEFAULT_IS_PREMIUM; + + autorun(() => { + const services = stores.services.all; + const isPremiumUser = stores.user.isPremium; + + if (config.isPremium && !isPremiumUser) return; + + services.forEach((service) => { + const s = session.fromPartition(`persist:service-${service.id}`); + let proxyHost = 'direct://'; + + const serviceProxyConfig = stores.settings.proxy[service.id]; + + if (serviceProxyConfig && serviceProxyConfig.isEnabled && serviceProxyConfig.host) { + proxyHost = serviceProxyConfig.host; + } + + s.setProxy({ proxyRules: proxyHost }, (e) => { + debug(`Using proxy "${proxyHost}" for "${service.name}" (${service.id})`, e); + }); + }); + }); + } + }, + ); +} + -- cgit v1.2.3-54-g00ecf