|
| 1 | +/** |
| 2 | + * Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License-AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +import { useState } from "react"; |
| 8 | +import Modal from "../components/Modal"; |
| 9 | +import Tooltip from "../components/Tooltip"; |
| 10 | +import copy from "../images/copy.svg"; |
| 11 | +import AlertBox from "../components/AlertBox"; |
| 12 | +import InfoBox from "../components/InfoBox"; |
| 13 | + |
| 14 | +function InputWithCopy(props: { value: string; tip?: string; className?: string }) { |
| 15 | + const [copied, setCopied] = useState<boolean>(false); |
| 16 | + const copyToClipboard = (text: string) => { |
| 17 | + const el = document.createElement("textarea"); |
| 18 | + el.value = text; |
| 19 | + document.body.appendChild(el); |
| 20 | + el.select(); |
| 21 | + try { |
| 22 | + document.execCommand("copy"); |
| 23 | + } finally { |
| 24 | + document.body.removeChild(el); |
| 25 | + } |
| 26 | + setCopied(true); |
| 27 | + setTimeout(() => setCopied(false), 2000); |
| 28 | + }; |
| 29 | + const tip = props.tip ?? "Click to copy"; |
| 30 | + return ( |
| 31 | + <div className={`w-full relative ${props.className ?? ""}`}> |
| 32 | + <input |
| 33 | + disabled={true} |
| 34 | + readOnly={true} |
| 35 | + autoFocus |
| 36 | + className="w-full pr-8 overscroll-none" |
| 37 | + type="text" |
| 38 | + defaultValue={props.value} |
| 39 | + /> |
| 40 | + <div className="cursor-pointer" onClick={() => copyToClipboard(props.value)}> |
| 41 | + <div className="absolute top-1/3 right-3"> |
| 42 | + <Tooltip content={copied ? "Copied" : tip}> |
| 43 | + <img src={copy} alt="copy icon" title={tip} /> |
| 44 | + </Tooltip> |
| 45 | + </div> |
| 46 | + </div> |
| 47 | + </div> |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +interface SSHProps { |
| 52 | + workspaceId: string; |
| 53 | + ownerToken: string; |
| 54 | + ideUrl: string; |
| 55 | +} |
| 56 | + |
| 57 | +function SSHView(props: SSHProps) { |
| 58 | + const sshCommand = `ssh ${props.workspaceId}#${props.ownerToken}@${props.ideUrl}`; |
| 59 | + return ( |
| 60 | + <div className="border-t border-b border-gray-200 dark:border-gray-800 mt-2 -mx-6 px-6 py-6"> |
| 61 | + <div className="mt-1 mb-4"> |
| 62 | + <AlertBox> |
| 63 | + <p className="text-red-500 whitespace-normal text-base"> |
| 64 | + <b>Anyone</b> on the internet with this command can access the running workspace. The command |
| 65 | + includes a generated access token that resets on every workspace restart. |
| 66 | + </p> |
| 67 | + </AlertBox> |
| 68 | + <InfoBox className="mt-4"> |
| 69 | + <p className="text-gray-500 whitespace-normal text-base"> |
| 70 | + Before connecting via SSH, make sure you have an existing SSH private key on your machine. You |
| 71 | + can create one using |
| 72 | + <a |
| 73 | + href="https://en.wikipedia.org/wiki/Ssh-keygen" |
| 74 | + target="_blank" |
| 75 | + rel="noopener noreferrer" |
| 76 | + className="gp-link" |
| 77 | + > |
| 78 | + ssh-keygen |
| 79 | + </a> |
| 80 | + . |
| 81 | + </p> |
| 82 | + </InfoBox> |
| 83 | + <p className="mt-4 text-gray-500 whitespace-normal text-base"> |
| 84 | + The following shell command can be used to SSH into this workspace. |
| 85 | + </p> |
| 86 | + </div> |
| 87 | + <InputWithCopy value={sshCommand} tip="Copy SSH Command" /> |
| 88 | + </div> |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +export default function ConnectToSSHModal(props: { |
| 93 | + workspaceId: string; |
| 94 | + ownerToken: string; |
| 95 | + ideUrl: string; |
| 96 | + onClose: () => void; |
| 97 | +}) { |
| 98 | + return ( |
| 99 | + <Modal visible={true} onClose={props.onClose}> |
| 100 | + <h3 className="mb-4">Connect via SSH</h3> |
| 101 | + <SSHView workspaceId={props.workspaceId} ownerToken={props.ownerToken} ideUrl={props.ideUrl} /> |
| 102 | + <div className="flex justify-end mt-6"> |
| 103 | + <button className={"ml-2 secondary"} onClick={() => props.onClose()}> |
| 104 | + Close |
| 105 | + </button> |
| 106 | + </div> |
| 107 | + </Modal> |
| 108 | + ); |
| 109 | +} |
0 commit comments