Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[settings] Apply proxy on dcrd/dcrwallet #3803

Merged
merged 4 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions app/actions/SettingsActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ export const saveSettings = (settings) => async (dispatch, getState) => {
wallet.setupProxy();
}

if (needNetworkReset) {
dispatch(closeWalletRequest());
if (needNetworkReset || updatedProxy) {
await dispatch(closeWalletRequest());
await dispatch(closeDaemonRequest());
dispatch(backToCredentials());
}
Expand Down Expand Up @@ -156,7 +156,9 @@ export function updateStateSettingsChanged(settings, norestart) {
const networkChange = {
network: true,
spvMode: true,
daemonStartAdvanced: true
daemonStartAdvanced: true,
proxyType: true,
proxyLocation: true
};

const newDiffersFromTemp = settingsFields.reduce(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
} from "constants";
import styles from "./ProxySettings.module.css";
import { Label, Box } from "../../helpers";
import { useProxySettings } from "./hooks";
import { Button } from "pi-ui";

const availableProxyTypes = [
{ name: <T id="settings.proxy.type.none" m="No Proxy" />, value: null },
Expand All @@ -17,41 +19,57 @@ const availableProxyTypes = [
{ name: "SOCKS5", value: PROXYTYPE_SOCKS5 }
];

const ProxySettings = ({ tempSettings, onChangeTempSettings }) => (
<Box className={styles.box}>
<div>
<Label id="proxy-type-input">
<T id="settings.proxy.type" m="Proxy Type" />
</Label>
<SettingsInput
selectWithBigFont
className={styles.input}
value={tempSettings.proxyType}
onChange={(newProxyType) =>
onChangeTempSettings({ proxyType: newProxyType.value })
}
valueKey="value"
labelKey="name"
ariaLabelledBy="proxy-type-input"
options={availableProxyTypes}
/>
</div>

<div>
<Label id="proxy-location">
<T id="settings.proxy.location" m="Proxy Location" />
</Label>
<SettingsTextInput
newBiggerFontStyle
inputClassNames={styles.settingsTextInput}
id="proxyLocationInput"
value={tempSettings.proxyLocation}
ariaLabelledBy="proxy-location"
onChange={(value) => onChangeTempSettings({ proxyLocation: value })}
/>
</div>
</Box>
);
const ProxySettings = ({ tempSettings, onChangeTempSettings }) => {
const { proxyType, proxyLocation, setProxyType, setProxyLocation } =
useProxySettings(tempSettings);

const isProxySettingsChanged =
proxyType !== tempSettings.proxyType ||
proxyLocation !== tempSettings.proxyLocation;

return (
<Box className={styles.box}>
<div>
<Label id="proxy-type-input">
<T id="settings.proxy.type" m="Proxy Type" />
</Label>
<SettingsInput
selectWithBigFont
className={styles.input}
value={proxyType}
onChange={(newProxyType) => setProxyType(newProxyType.value)}
valueKey="value"
labelKey="name"
ariaLabelledBy="proxy-type-input"
options={availableProxyTypes}
/>
</div>

<div>
<Label id="proxy-location">
<T id="settings.proxy.location" m="Proxy Location" />
</Label>
<SettingsTextInput
newBiggerFontStyle
inputClassNames={styles.settingsTextInput}
id="proxyLocationInput"
value={proxyLocation}
ariaLabelledBy="proxy-location"
onChange={(value) => setProxyLocation(value)}
/>
</div>

{isProxySettingsChanged && (
<Button
className={styles.submitButton}
size="sm"
onClick={() => onChangeTempSettings({ proxyType, proxyLocation })}>
<T id="settings.proxy.save" m="Save proxy settings" />
</Button>
)}
</Box>
);
};

ProxySettings.propTypes = {
tempSettings: PropTypes.object.isRequired,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
.box {
display: grid;
grid-gap: 3rem;
grid-gap: 1rem 3rem;
grid-template-columns: repeat(2, 1fr);
}

.settingsTextInput {
color: var(--main-dark-blue) !important;
}

.submitButton {
grid-column: 2;
}

@media (--sm-viewport) {
.box {
grid-template-columns: 1fr;
}

.submitButton {
grid-column: 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useState } from "react";

export const useProxySettings = (tempSettings) => {
const [proxyType, setProxyType] = useState(tempSettings.proxyType);
const [proxyLocation, setProxyLocation] = useState(
tempSettings.proxyLocation
);

return {
proxyType,
proxyLocation,
setProxyType,
setProxyLocation
};
};
22 changes: 21 additions & 1 deletion app/main_dev/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import {
UPGD_ELECTRON8,
CSPP_URL,
CSPP_PORT_TESTNET,
CSPP_PORT_MAINNET
CSPP_PORT_MAINNET,
PROXYTYPE_SOCKS5
} from "constants";
import * as cfgConstants from "constants/config";
import os from "os";
Expand All @@ -43,6 +44,7 @@ import ini from "ini";
import { makeRandomString, isPlainString as isString } from "helpers/strings";
import { makeFileBackup } from "helpers/files";
import { DEX_LOCALPAGE } from "./externalRequests";
import { getProxyTypeAndLocation } from "./proxy";

const argv = parseArgs(process.argv.slice(1), OPTIONS);
const debug = argv.debug || process.env.NODE_ENV === "development";
Expand Down Expand Up @@ -390,6 +392,15 @@ export const launchDCRD = (reactIPC, testnet, appdata) =>
args.push("--testnet");
}

const { proxyType, proxyLocation } = getProxyTypeAndLocation();
logger.log(
"info",
`ProxyType: ${proxyType}, ProxyLocation: ${proxyLocation}`
);
if (proxyType === PROXYTYPE_SOCKS5 && proxyLocation) {
args.push(`--proxy=${proxyLocation}`);
}

rpcuser = rpc_user;
rpcpass = rpc_pass;
rpccert = rpc_cert;
Expand Down Expand Up @@ -644,6 +655,15 @@ export const launchDCRWallet = async (
: "--csppserver=" + CSPP_URL + ":" + CSPP_PORT_TESTNET
);

const { proxyType, proxyLocation } = getProxyTypeAndLocation();
logger.log(
"info",
`ProxyType: ${proxyType}, ProxyLocation: ${proxyLocation}`
);
if (proxyType === PROXYTYPE_SOCKS5 && proxyLocation) {
args.push(`--proxy=${proxyLocation}`);
}

const dcrwExe = getExecutablePath("dcrwallet", argv.custombinpath);
if (!fs.existsSync(dcrwExe)) {
const msg = `The dcrwallet executable does not exist. Expected to find it at ${dcrwExe}`;
Expand Down
14 changes: 10 additions & 4 deletions app/main_dev/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import {

export const setupProxy = (logger) =>
new Promise((resolve, reject) => {
const cfg = getGlobalCfg();

const proxyType = cfg.get(cfgConstants.PROXY_TYPE);
const proxyLocation = cfg.get(cfgConstants.PROXY_LOCATION);
const { proxyType, proxyLocation } = getProxyTypeAndLocation();

const proxyConfig = {
pacScript: null,
Expand Down Expand Up @@ -58,3 +55,12 @@ export const setupProxy = (logger) =>
})
.catch(reject);
});

export const getProxyTypeAndLocation = () => {
const cfg = getGlobalCfg();

const proxyType = cfg.get(cfgConstants.PROXY_TYPE);
const proxyLocation = cfg.get(cfgConstants.PROXY_LOCATION);

return { proxyType, proxyLocation };
};
80 changes: 74 additions & 6 deletions test/unit/actions/SettingsActions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ const testSettings = {
EXTERNALREQUEST_UPDATE_CHECK,
EXTERNALREQUEST_DEX
],
proxyType: "new-proxy-type",
proxyLocation: "new-proxy-location",
spvMode: false,
spvModeFromCli: false,
spvConnect: [],
Expand Down Expand Up @@ -239,6 +237,74 @@ test("test saveSettings", async () => {
// locale has not been changed
// dcrdata is enabled now
test("test saveSettings - save alternative data", async () => {
const testSettingsCopy = {
...testSettings,
allowedExternalRequests: [
...testSettings.allowedExternalRequests,
EXTERNALREQUEST_POLITEIA,
EXTERNALREQUEST_DCRDATA
]
};
mockGlobalCfgGet = jest.fn((key) => {
switch (key) {
case ALLOWED_EXTERNAL_REQUESTS:
return testSettingsCopy.allowedExternalRequests;
}
});
const store = createStore(
cloneDeep({
...initialState,
daemon: {},
settings: {
...initialState.settings,
needNetworkReset: false,
currentSettings: {
...initialState.settings.currentSettings,
network: TESTNET,
locale: testSettingsCopy.locale
}
}
})
);
await store.dispatch(settingsActions.saveSettings(testSettingsCopy));

expect(mockGetGlobalCfg).toHaveBeenCalled();
// locale has not been changed
expect(mockChangeMenuLocale).not.toHaveBeenCalled();

// politeia is enabled now
expect(mockGetTokenAndInitialBatch).toHaveBeenCalled();
expect(mockResetInventoryAndProposals).not.toHaveBeenCalled();

// dcrdata is enabled now
expect(mockGetTreasuryBalance).toHaveBeenCalled();
expect(mockResetTreasuryBalance).not.toHaveBeenCalled();

// proxy has been changed, even though just the proxy location has been changed
expect(mockSetupProxy).not.toHaveBeenCalled();

// don't a need network reset
expect(mockCloseWalletRequest).not.toHaveBeenCalled();
expect(mockCloseDaemonRequest).not.toHaveBeenCalled();
expect(mockBackToCredentials).not.toHaveBeenCalled();

// allowed external requests has not been changed
expect(mockReloadAllowedExternalRequests).not.toHaveBeenCalled();

// wallet is not openned
expect(mockGetWalletCfg).not.toHaveBeenCalled();
expect(mockWalletCfgSet).not.toHaveBeenCalled();

expect(
isEqual(store.getState().settings.currentSettings, testSettingsCopy)
).toBeTruthy();
expect(
isEqual(store.getState().settings.tempSettings, testSettingsCopy)
).toBeTruthy();
expect(store.getState().settings.settingsChanged).toBeFalsy();
});

test("test saveSettings - proxy change needs wallet restart", async () => {
const testSettingsCopy = {
...testSettings,
allowedExternalRequests: [
Expand Down Expand Up @@ -267,7 +333,9 @@ test("test saveSettings - save alternative data", async () => {
currentSettings: {
...initialState.settings.currentSettings,
network: TESTNET,
locale: testSettingsCopy.locale
locale: testSettingsCopy.locale,
proxyType: "new-proxy-type",
proxyLocation: "new-proxy-location"
}
}
})
Expand All @@ -290,9 +358,9 @@ test("test saveSettings - save alternative data", async () => {
expect(mockSetupProxy).toHaveBeenCalled();

// don't a need network reset
expect(mockCloseWalletRequest).not.toHaveBeenCalled();
expect(mockCloseDaemonRequest).not.toHaveBeenCalled();
expect(mockBackToCredentials).not.toHaveBeenCalled();
expect(mockCloseWalletRequest).toHaveBeenCalled();
expect(mockCloseDaemonRequest).toHaveBeenCalled();
expect(mockBackToCredentials).toHaveBeenCalled();

// allowed external requests has not been changed
expect(mockReloadAllowedExternalRequests).not.toHaveBeenCalled();
Expand Down
Loading