aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/serviceProxy
diff options
context:
space:
mode:
Diffstat (limited to 'src/features/serviceProxy')
-rw-r--r--src/features/serviceProxy/index.js38
-rw-r--r--src/features/serviceProxy/index.ts54
2 files changed, 54 insertions, 38 deletions
diff --git a/src/features/serviceProxy/index.js b/src/features/serviceProxy/index.js
deleted file mode 100644
index b9320cda9..000000000
--- a/src/features/serviceProxy/index.js
+++ /dev/null
@@ -1,38 +0,0 @@
1import { autorun, observable } from 'mobx';
2import { session } from '@electron/remote';
3
4const debug = require('debug')('Ferdi:feature:serviceProxy');
5
6export const config = observable({
7 isEnabled: true,
8});
9
10export default function init(stores) {
11 debug('Initializing `serviceProxy` feature');
12
13 autorun(() => {
14 config.isEnabled = true;
15
16 const services = stores.services.enabled;
17 const proxySettings = stores.settings.proxy;
18
19 debug('Service Proxy autorun');
20
21 for (const service of services) {
22 const s = session.fromPartition(`persist:service-${service.id}`);
23
24 if (config.isEnabled) {
25 const serviceProxyConfig = proxySettings[service.id];
26
27 if (serviceProxyConfig && serviceProxyConfig.isEnabled && serviceProxyConfig.host) {
28 const proxyHost = `${serviceProxyConfig.host}${serviceProxyConfig.port ? `:${serviceProxyConfig.port}` : ''}`;
29 debug(`Setting proxy config from service settings for "${service.name}" (${service.id}) to`, proxyHost);
30
31 s.setProxy({ proxyRules: proxyHost }, () => {
32 debug(`Using proxy "${proxyHost}" for "${service.name}" (${service.id})`);
33 });
34 }
35 }
36 }
37 });
38}
diff --git a/src/features/serviceProxy/index.ts b/src/features/serviceProxy/index.ts
new file mode 100644
index 000000000..f095b286a
--- /dev/null
+++ b/src/features/serviceProxy/index.ts
@@ -0,0 +1,54 @@
1import { autorun, observable } from 'mobx';
2import { session } from '@electron/remote';
3
4const debug = require('debug')('Ferdi:feature:serviceProxy');
5
6export const config = observable({
7 isEnabled: true,
8});
9
10export default function init(stores: {
11 services: { enabled: any };
12 settings: { proxy: any };
13}) {
14 debug('Initializing `serviceProxy` feature');
15
16 autorun(() => {
17 config.isEnabled = true;
18
19 const services = stores.services.enabled;
20 const proxySettings = stores.settings.proxy;
21
22 debug('Service Proxy autorun');
23
24 for (const service of services) {
25 const s = session.fromPartition(`persist:service-${service.id}`);
26
27 if (config.isEnabled) {
28 const serviceProxyConfig = proxySettings[service.id];
29
30 if (
31 serviceProxyConfig &&
32 serviceProxyConfig.isEnabled &&
33 serviceProxyConfig.host
34 ) {
35 const proxyHost = `${serviceProxyConfig.host}${
36 serviceProxyConfig.port ? `:${serviceProxyConfig.port}` : ''
37 }`;
38 debug(
39 `Setting proxy config from service settings for "${service.name}" (${service.id}) to`,
40 proxyHost,
41 );
42
43 s.setProxy({ proxyRules: proxyHost })
44 .then(() => {
45 debug(
46 `Using proxy "${proxyHost}" for "${service.name}" (${service.id})`,
47 );
48 })
49 .catch(error => console.error(error));
50 }
51 }
52 }
53 });
54}