Skip to content
Open
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
27 changes: 22 additions & 5 deletions electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import * as electronRemote from '@electron/remote/main';
import {is, optimizer} from '@electron-toolkit/utils';
import windowStateKeeper from 'electron-window-state';
import {autoUpdater} from 'electron-updater';
import sudo from 'sudo-prompt';
import './appinfo';
import * as ipc from './ipc';
import {PID_FILE_OPENVPN, PID_FILE_WIREGUARD} from './globals';
import {DATA_PATH, PID_FILE_OPENVPN, PID_FILE_WIREGUARD} from './globals';
import settings from './settings';
import {getCurrentMonitor} from './vpn/monitor';
import {Status} from './vpn/status';
Expand Down Expand Up @@ -208,10 +209,26 @@ export async function stopVPN(isSwitchingServer = false): Promise<void> {
if (!isSwitchingServer) {
getCurrentMonitor()?.setStatus(Status.DISCONNECTING);
}
await Promise.all([
stopVPNFromPidFile(PID_FILE_WIREGUARD),
stopVPNFromPidFile(PID_FILE_OPENVPN)
]);
if (process.platform === 'linux') {
sudo.exec(`wg-quick down "${DATA_PATH}/dicyvpn.conf"`, {
name: 'DicyVPN'
}, (error, stdout, stderr) => {
if (error) {
console.error(`[WireGuard] error: ${error.message}`);
return;
}
if (stderr) {
console.error(`[WireGuard stderr] ${stderr}`);
return;
}
console.log(`[WireGuard stdout] ${stdout}`);
});
} else {
await Promise.all([
stopVPNFromPidFile(PID_FILE_WIREGUARD),
stopVPNFromPidFile(PID_FILE_OPENVPN)
]);
}
updateTray(false);
}

Expand Down
60 changes: 60 additions & 0 deletions electron/main/vpn/linux/wireguard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import sudo from 'sudo-prompt';
import {WireGuard} from '../wireguard';
import {DATA_PATH} from '../../globals';

export class WireGuardLinux extends WireGuard {
constructor(
private ip: string,
private port: number,
private privateKey: string,
private publicKey: string,
private internalIp: string,
private ips: string[],
private isIpsAllowlist: boolean,
private dns: string[]
) {
super();
}

async start(): Promise<void> {
this.writeConfig('dicyvpn.conf');
sudo.exec(`wg-quick up "${DATA_PATH}/dicyvpn.conf"`, {
name: 'DicyVPN'
}, (error, stdout, stderr) => {
if (error) {
console.error(`[WireGuard] error: ${error.message}`);
return;
}
if (stderr) {
console.error(`[WireGuard stderr] ${stderr}`);
return;
}
console.log(`[WireGuard stdout] ${stdout}`);
});
}

protected getConfig(): string {
let allowedIPs = 'AllowedIPs = ';
if (this.ips.length > 0) { // enable split tunneling for IPs
if (this.isIpsAllowlist) { // allowlist
allowedIPs += this.ips.join(', ');
}
} else {
allowedIPs += '0.0.0.0/0';
}
// route all IPv6 traffic, prevents leaks through IPv6
allowedIPs += ', ::/0';

return `
[Interface]
PrivateKey = ${this.privateKey}
Address = ${this.internalIp}/32
DNS = ${this.dns.join(', ')}

[Peer]
PublicKey = ${this.publicKey}
Endpoint = ${this.ip}:${this.port}
PersistentKeepalive = 15
${allowedIPs}`;
}
}
6 changes: 6 additions & 0 deletions electron/main/vpn/vpn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {Status} from './status';
import {createApi} from '../../../common/api';
import {INVALID_REFRESH_TOKEN} from '../../../common/channels';
import {WireGuardWindows} from './windows/wireguard';
import {WireGuardLinux} from "./linux/wireguard";

export interface VPN {
start(): Promise<void>;
Expand Down Expand Up @@ -112,6 +113,11 @@ function getNewWireGuardInstance(serverIp: string, port: number, privateKey: str
serverIp, port, privateKey, publicKey, internalIp,
ips, isIpsAllowlist, apps, isAppsAllowlist, dns
);
case 'linux':
return new WireGuardLinux(
serverIp, port, privateKey, publicKey, internalIp,
ips, isIpsAllowlist, dns
);
}
throw new Error('Unsupported platform');
}
Expand Down
2 changes: 1 addition & 1 deletion electron/main/vpn/windows/wireguard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class WireGuardWindows extends WireGuard {
}

async start(): Promise<void> {
this.writeConfig();
this.writeConfig('wireguard.conf');
const exe = getWireGuardClientPath();
const args = ['run', '-config', `${DATA_PATH}/wireguard.conf`, '-log-level', 'info'];
const child = spawn(exe, args);
Expand Down
4 changes: 2 additions & 2 deletions electron/main/vpn/wireguard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export abstract class WireGuard implements VPN {

abstract start(): Promise<void>;

protected writeConfig() {
protected writeConfig(filename: string) {
const conf = this.getConfig();
fs.writeFileSync(`${DATA_PATH}/wireguard.conf`, conf);
fs.writeFileSync(`${DATA_PATH}/${filename}`, conf);
}

protected abstract getConfig(): string;
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"i18n-iso-countries": "^7.6.0",
"ping": "^0.4.2",
"pinia": "^2.1.6",
"sudo-prompt": "^9.2.1",
"svg-pan-zoom-container": "^0.6.1",
"tail": "^2.2.6",
"vue": "^3.2.45",
Expand Down
8 changes: 5 additions & 3 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ export default {
this.$router.push('/');
});

this.currentServer.$patch({
status: await window.preload.isRunning() ? Status.CONNECTED : Status.NOT_RUNNING
});
if (window.electron.process.platform !== 'linux') {
this.currentServer.$patch({
status: await window.preload.isRunning() ? Status.CONNECTED : Status.NOT_RUNNING
});
}
},
beforeUnmount() {
window.preload.removeListener('status-change', this.onStatusChange);
Expand Down