-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
755575b
commit 301cee2
Showing
7 changed files
with
382 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
/* | ||
* This file is part of Cockpit. | ||
* | ||
* Copyright (C) 2023 Red Hat, Inc. | ||
* | ||
* Cockpit is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation; either version 2.1 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Cockpit is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Cockpit; If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import React, { useContext, useEffect, useState } from 'react'; | ||
import cockpit from 'cockpit'; | ||
import { Button } from '@patternfly/react-core/dist/esm/components/Button/index.js'; | ||
import { ClipboardCopy } from '@patternfly/react-core/dist/esm/components/ClipboardCopy/index.js'; | ||
import { EmptyState, EmptyStateBody } from '@patternfly/react-core/dist/esm/components/EmptyState/index.js'; | ||
import { FormGroup, FormFieldGroup, FormFieldGroupHeader } from '@patternfly/react-core/dist/esm/components/Form/index.js'; | ||
import { Grid } from '@patternfly/react-core/dist/esm/layouts/Grid/index.js'; | ||
import { InputGroup } from '@patternfly/react-core/dist/esm/components/InputGroup/index.js'; | ||
import { MinusIcon } from '@patternfly/react-icons'; | ||
import { TextInput } from '@patternfly/react-core/dist/esm/components/TextInput/index.js'; | ||
|
||
import { Name, NetworkModal, dialogSave } from "./dialogs-common"; | ||
import { ModelContext } from './model-context'; | ||
import { useDialogs } from 'dialogs.jsx'; | ||
|
||
import './wireguard.scss'; | ||
|
||
const _ = cockpit.gettext; | ||
|
||
function addressesToString(addresses) { | ||
return addresses.map(address => address[0] + "/" + address[1]).join(","); | ||
} | ||
|
||
function stringToAddresses(str) { | ||
return str.split(",").map(strAddress => { | ||
const [ip, prefix] = strAddress.split("/"); | ||
const defaultPrefix = 32; | ||
// The gateway may be left as 0 if no gateway exists for that subnet. | ||
const gateway = "0.0.0.0"; | ||
return [ip, prefix ?? defaultPrefix, gateway]; | ||
}); | ||
} | ||
|
||
export function WireGuardDialog({ settings, connection, dev }) { | ||
const Dialogs = useDialogs(); | ||
const idPrefix = 'network-wireguard-settings'; | ||
const model = useContext(ModelContext); | ||
|
||
const [iface, setIface] = useState(settings.connection.interface_name); | ||
const [privateKey, setPrivateKey] = useState(settings.wireguard.private_key); | ||
const [publicKey, setPublicKey] = useState(''); | ||
const [listenPort, setListenPort] = useState(settings.wireguard.listen_port); | ||
const [addresses, setAddresses] = useState(addressesToString(settings.ipv4.addresses)); | ||
const [dialogError, setDialogError] = useState(''); | ||
const [peers, setPeers] = useState(settings.wireguard.peers.map(peer => ({ ...peer, allowedIps: peer.allowedIps.join(',') }))); | ||
|
||
useEffect(() => { | ||
async function getPublicKey() { | ||
try { | ||
const key = await cockpit.spawn(["wg", "pubkey"], { err: 'message' }).input(privateKey.trim()); | ||
setPublicKey(key.trim()); | ||
} catch (e) { | ||
console.error(e.message); | ||
setPublicKey(''); | ||
} | ||
} | ||
|
||
getPublicKey(); | ||
}, [privateKey]); | ||
|
||
function onSubmit() { | ||
function createSettingsObj() { | ||
return { | ||
...settings, | ||
connection: { | ||
...settings.connection, | ||
id: `con-${iface}`, | ||
interface_name: iface, | ||
type: 'wireguard' | ||
}, | ||
wireguard: { | ||
private_key: privateKey.trim(), | ||
listen_port: +listenPort, | ||
peers: peers.map(peer => ({ ...peer, allowedIps: [peer.allowedIps] })), | ||
}, | ||
ipv4: { | ||
addresses: stringToAddresses(addresses), | ||
method: 'manual', | ||
dns: [], | ||
dns_search: [] | ||
} | ||
}; | ||
} | ||
|
||
dialogSave({ | ||
connection, | ||
dev, | ||
model, | ||
settings: createSettingsObj(), | ||
onClose: Dialogs.close, | ||
setDialogError | ||
}); | ||
} | ||
|
||
return ( | ||
<NetworkModal title={_("WireGuard settings")} onSubmit={onSubmit} dialogError={dialogError} idPrefix={idPrefix}> | ||
<Name idPrefix={idPrefix} iface={iface} setIface={setIface} /> | ||
<FormGroup label={_("Private key")} fieldId={idPrefix + '-private-key-input'}> | ||
<InputGroup> | ||
<TextInput id={idPrefix + '-private-key-input'} value={privateKey} onChange={(_, val) => setPrivateKey(val)} /> | ||
<Button variant='secondary' | ||
onClick={async () => { | ||
const key = await cockpit.spawn(["wg", "genkey"]); | ||
setPrivateKey(key.trim()); | ||
}} | ||
> | ||
{_("Generate private key")} | ||
</Button> | ||
</InputGroup> | ||
</FormGroup> | ||
<FormGroup label={_("Public key")}> | ||
<ClipboardCopy isReadOnly>{publicKey}</ClipboardCopy> | ||
</FormGroup> | ||
<FormGroup label={_("Listen port")} fieldId={idPrefix + '-listen-port-input'}> | ||
<TextInput id={idPrefix + '-listen-port-input'} placeholder={_("Random port selected when unspecified")} type='number' value={listenPort} onChange={(_, val) => { setListenPort(val) }} /> | ||
</FormGroup> | ||
<FormGroup label={_("Addresses")} fieldId={idPrefix + '-addresses-input'}> | ||
<TextInput id={idPrefix + '-addresses-input'} value={addresses} onChange={(_, val) => { setAddresses(val) }} placeholder={_("E.g. 10.0.0.1/24")} /> | ||
</FormGroup> | ||
<FormFieldGroup | ||
header={ | ||
<FormFieldGroupHeader | ||
titleText={{ text: _("Peers") }} | ||
actions={ | ||
<Button | ||
variant='secondary' | ||
onClick={() => setPeers(peers => [...peers, { publicKey: '', endpoint: '', allowedIps: '' }])} | ||
> | ||
{_("Add peer")} | ||
</Button> | ||
} | ||
/> | ||
} | ||
className='dynamic-form-group' | ||
> | ||
{(peers.length !== 0) | ||
? peers.map((peer, i) => ( | ||
<Grid key={i} hasGutter> | ||
<FormGroup className='pf-m-6-col-on-md' label={_("Public key")} fieldId={idPrefix + '-publickey-peer-' + i}> | ||
<TextInput | ||
value={peer.publicKey} | ||
onChange={(_, val) => { | ||
setPeers(peers => peers.map((peer, index) => i === index ? { ...peer, publicKey: val } : peer)); | ||
}} | ||
id={idPrefix + '-publickey-peer-' + i} | ||
/> | ||
</FormGroup> | ||
<FormGroup className='pf-m-3-col-on-md' label={_("Endpoint")} fieldId={idPrefix + '-endpoint-peer-' + i}> | ||
<TextInput | ||
value={peer.endpoint} | ||
onChange={(_, val) => { | ||
setPeers(peers => peers.map((peer, index) => i === index ? { ...peer, endpoint: val } : peer)); | ||
}} | ||
id={idPrefix + '-endpoint-peer-' + i} | ||
/> | ||
</FormGroup> | ||
<FormGroup className='pf-m-3-col-on-md' label={_("Allowed IPs")} fieldId={idPrefix + '-allowedips-peer-' + i}> | ||
<TextInput | ||
value={peer.allowedIps} | ||
onChange={(_, val) => { | ||
setPeers(peers => peers.map((peer, index) => i === index ? { ...peer, allowedIps: val } : peer)); | ||
}} | ||
id={idPrefix + '-allowedips-peer-' + i} | ||
/> | ||
</FormGroup> | ||
<FormGroup className='pf-m-1-col-on-md remove-button-group'> | ||
<Button | ||
variant='secondary' | ||
onClick={() => { | ||
setPeers(peers => peers.filter((_, index) => i !== index)); | ||
}} | ||
> | ||
<MinusIcon /> | ||
</Button> | ||
</FormGroup> | ||
</Grid> | ||
)) | ||
: <EmptyState> | ||
<EmptyStateBody>{_("No peers added.")}</EmptyStateBody> | ||
</EmptyState> | ||
} | ||
</FormFieldGroup> | ||
</NetworkModal> | ||
); | ||
} | ||
|
||
export function getGhostSettings({ newIfaceName }) { | ||
return { | ||
connection: { | ||
id: `con-${newIfaceName}`, | ||
interface_name: newIfaceName | ||
}, | ||
wireguard: { | ||
listen_port: "", | ||
private_key: "", | ||
peers: [] | ||
}, | ||
ipv4: { | ||
addresses: [] | ||
} | ||
}; | ||
} |
Oops, something went wrong.