From bfe8847d72cd0893230f2e654242658214943e61 Mon Sep 17 00:00:00 2001 From: Markus Hatvan Date: Sat, 2 Oct 2021 09:24:32 +0200 Subject: chore: convert various files from JS to TS (#2010) --- src/features/serviceProxy/index.ts | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/features/serviceProxy/index.ts (limited to 'src/features/serviceProxy/index.ts') 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 @@ +import { autorun, observable } from 'mobx'; +import { session } from '@electron/remote'; + +const debug = require('debug')('Ferdi:feature:serviceProxy'); + +export const config = observable({ + isEnabled: true, +}); + +export default function init(stores: { + services: { enabled: any }; + settings: { proxy: any }; +}) { + debug('Initializing `serviceProxy` feature'); + + autorun(() => { + config.isEnabled = true; + + const services = stores.services.enabled; + const proxySettings = stores.settings.proxy; + + debug('Service Proxy autorun'); + + for (const service of services) { + const s = session.fromPartition(`persist:service-${service.id}`); + + if (config.isEnabled) { + const serviceProxyConfig = proxySettings[service.id]; + + if ( + serviceProxyConfig && + serviceProxyConfig.isEnabled && + serviceProxyConfig.host + ) { + const proxyHost = `${serviceProxyConfig.host}${ + serviceProxyConfig.port ? `:${serviceProxyConfig.port}` : '' + }`; + debug( + `Setting proxy config from service settings for "${service.name}" (${service.id}) to`, + proxyHost, + ); + + s.setProxy({ proxyRules: proxyHost }) + .then(() => { + debug( + `Using proxy "${proxyHost}" for "${service.name}" (${service.id})`, + ); + }) + .catch(error => console.error(error)); + } + } + } + }); +} -- cgit v1.2.3-70-g09d2