diff options
Diffstat (limited to 'src/features/serviceProxy/index.js')
-rw-r--r-- | src/features/serviceProxy/index.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/features/serviceProxy/index.js b/src/features/serviceProxy/index.js new file mode 100644 index 000000000..cad9844fd --- /dev/null +++ b/src/features/serviceProxy/index.js | |||
@@ -0,0 +1,45 @@ | |||
1 | import { autorun, observable } from 'mobx'; | ||
2 | import { remote } from 'electron'; | ||
3 | |||
4 | import { DEFAULT_FEATURES_CONFIG } from '../../config'; | ||
5 | |||
6 | const { session } = remote; | ||
7 | |||
8 | const debug = require('debug')('Franz:feature:serviceProxy'); | ||
9 | |||
10 | export const config = observable({ | ||
11 | isEnabled: DEFAULT_FEATURES_CONFIG.isServiceProxyEnabled, | ||
12 | isPremium: DEFAULT_FEATURES_CONFIG.isServiceProxyPremiumFeature, | ||
13 | }); | ||
14 | |||
15 | export default function init(stores) { | ||
16 | debug('Initializing `serviceProxy` feature'); | ||
17 | |||
18 | autorun(() => { | ||
19 | const { isServiceProxyEnabled, isServiceProxyPremiumFeature } = stores.features.features; | ||
20 | |||
21 | config.isEnabled = isServiceProxyEnabled !== undefined ? isServiceProxyEnabled : DEFAULT_FEATURES_CONFIG.isServiceProxyEnabled; | ||
22 | config.isPremium = isServiceProxyPremiumFeature !== undefined ? isServiceProxyPremiumFeature : DEFAULT_FEATURES_CONFIG.isServiceProxyPremiumFeature; | ||
23 | |||
24 | const services = stores.services.all; | ||
25 | const isPremiumUser = stores.user.data.isPremium; | ||
26 | |||
27 | services.forEach((service) => { | ||
28 | const s = session.fromPartition(`persist:service-${service.id}`); | ||
29 | let proxyHost = 'direct://'; | ||
30 | |||
31 | if (config.isEnabled && (isPremiumUser || !config.isPremium)) { | ||
32 | const serviceProxyConfig = stores.settings.proxy[service.id]; | ||
33 | |||
34 | if (serviceProxyConfig && serviceProxyConfig.isEnabled && serviceProxyConfig.host) { | ||
35 | proxyHost = serviceProxyConfig.host; | ||
36 | } | ||
37 | } | ||
38 | |||
39 | s.setProxy({ proxyRules: proxyHost }, (e) => { | ||
40 | debug(`Using proxy "${proxyHost}" for "${service.name}" (${service.id})`, e); | ||
41 | }); | ||
42 | }); | ||
43 | }); | ||
44 | } | ||
45 | |||