diff options
Diffstat (limited to 'src/features/serviceProxy/index.ts')
-rw-r--r-- | src/features/serviceProxy/index.ts | 54 |
1 files changed, 54 insertions, 0 deletions
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 @@ | |||
1 | import { autorun, observable } from 'mobx'; | ||
2 | import { session } from '@electron/remote'; | ||
3 | |||
4 | const debug = require('debug')('Ferdi:feature:serviceProxy'); | ||
5 | |||
6 | export const config = observable({ | ||
7 | isEnabled: true, | ||
8 | }); | ||
9 | |||
10 | export 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 | } | ||