|
| 1 | +import Button from "@material-ui/core/Button" |
| 2 | +import CircularProgress from "@material-ui/core/CircularProgress" |
| 3 | +import Link from "@material-ui/core/Link" |
| 4 | +import Popover, { PopoverProps } from "@material-ui/core/Popover" |
| 5 | +import { makeStyles } from "@material-ui/core/styles" |
| 6 | +import TextField from "@material-ui/core/TextField" |
| 7 | +import Typography from "@material-ui/core/Typography" |
| 8 | +import OpenInNewIcon from "@material-ui/icons/OpenInNew" |
| 9 | +import Alert from "@material-ui/lab/Alert" |
| 10 | +import React, { useState } from "react" |
| 11 | +import { NetstatPort, NetstatResponse } from "../../api/types" |
| 12 | +import { CodeExample } from "../CodeExample/CodeExample" |
| 13 | +import { Stack } from "../Stack/Stack" |
| 14 | + |
| 15 | +export const Language = { |
| 16 | + title: "Port forward", |
| 17 | + automaticPortText: |
| 18 | + "Here are the applications we detected are listening on ports in this resource. Click to open them in a new tab.", |
| 19 | + manualPortText: |
| 20 | + "You can manually port forward this resource by typing the port and your username in the URL like below.", |
| 21 | + formPortText: "Or you can use the following form to open the port in a new tab.", |
| 22 | + portListing: (port: number, name: string): string => `${port} (${name})`, |
| 23 | + portInputLabel: "Port", |
| 24 | + formButtonText: "Open URL", |
| 25 | +} |
| 26 | + |
| 27 | +export type PortForwardDropdownProps = Pick<PopoverProps, "onClose" | "open" | "anchorEl"> & { |
| 28 | + /** |
| 29 | + * The netstat response to render. Undefined is taken to mean "loading". |
| 30 | + */ |
| 31 | + netstat?: NetstatResponse |
| 32 | + /** |
| 33 | + * Given a port return the URL for accessing that port. |
| 34 | + */ |
| 35 | + urlFormatter: (port: number | string) => string |
| 36 | +} |
| 37 | + |
| 38 | +const portFilter = ({ port }: NetstatPort): boolean => { |
| 39 | + if (port === 443 || port === 80) { |
| 40 | + // These are standard HTTP ports. |
| 41 | + return true |
| 42 | + } else if (port <= 1023) { |
| 43 | + // Assume a privileged port is probably not being used for HTTP. This will |
| 44 | + // catch things like sshd. |
| 45 | + return false |
| 46 | + } |
| 47 | + return true |
| 48 | +} |
| 49 | + |
| 50 | +export const PortForwardDropdown: React.FC<PortForwardDropdownProps> = ({ netstat, open, urlFormatter, ...rest }) => { |
| 51 | + const styles = useStyles() |
| 52 | + const [port, setPort] = useState<number | string>(3000) |
| 53 | + const ports = netstat?.ports?.filter(portFilter) |
| 54 | + |
| 55 | + return ( |
| 56 | + <Popover |
| 57 | + open={!!open} |
| 58 | + transformOrigin={{ |
| 59 | + vertical: "top", |
| 60 | + horizontal: "center", |
| 61 | + }} |
| 62 | + anchorOrigin={{ |
| 63 | + vertical: "bottom", |
| 64 | + horizontal: "center", |
| 65 | + }} |
| 66 | + {...rest} |
| 67 | + > |
| 68 | + <div className={styles.root}> |
| 69 | + <Typography variant="h6" className={styles.title}> |
| 70 | + {Language.title} |
| 71 | + </Typography> |
| 72 | + |
| 73 | + <Typography className={styles.paragraph}>{Language.automaticPortText}</Typography> |
| 74 | + |
| 75 | + {typeof netstat === "undefined" && ( |
| 76 | + <div className={styles.loader}> |
| 77 | + <CircularProgress size="1rem" /> |
| 78 | + </div> |
| 79 | + )} |
| 80 | + |
| 81 | + {netstat?.error && <Alert severity="error">{netstat.error}</Alert>} |
| 82 | + |
| 83 | + {ports && ports.length > 0 && ( |
| 84 | + <div className={styles.ports}> |
| 85 | + {ports.map(({ port, name }) => ( |
| 86 | + <Link className={styles.portLink} key={port} href={urlFormatter(port)} target="_blank"> |
| 87 | + <OpenInNewIcon /> |
| 88 | + {Language.portListing(port, name)} |
| 89 | + </Link> |
| 90 | + ))} |
| 91 | + </div> |
| 92 | + )} |
| 93 | + |
| 94 | + {ports && ports.length === 0 && <Alert severity="info">No HTTP ports were detected.</Alert>} |
| 95 | + |
| 96 | + <Typography className={styles.paragraph}>{Language.manualPortText}</Typography> |
| 97 | + |
| 98 | + <CodeExample code={urlFormatter(port)} /> |
| 99 | + |
| 100 | + <Typography className={styles.paragraph}>{Language.formPortText}</Typography> |
| 101 | + |
| 102 | + <Stack direction="row"> |
| 103 | + <TextField |
| 104 | + className={styles.textField} |
| 105 | + onChange={(event) => setPort(event.target.value)} |
| 106 | + value={port} |
| 107 | + autoFocus |
| 108 | + label={Language.portInputLabel} |
| 109 | + variant="outlined" |
| 110 | + /> |
| 111 | + <Button component={Link} href={urlFormatter(port)} target="_blank" className={styles.linkButton}> |
| 112 | + {Language.formButtonText} |
| 113 | + </Button> |
| 114 | + </Stack> |
| 115 | + </div> |
| 116 | + </Popover> |
| 117 | + ) |
| 118 | +} |
| 119 | + |
| 120 | +const useStyles = makeStyles((theme) => ({ |
| 121 | + root: { |
| 122 | + padding: `${theme.spacing(3)}px`, |
| 123 | + maxWidth: 500, |
| 124 | + }, |
| 125 | + title: { |
| 126 | + fontWeight: 600, |
| 127 | + }, |
| 128 | + ports: { |
| 129 | + margin: `${theme.spacing(2)}px 0`, |
| 130 | + }, |
| 131 | + portLink: { |
| 132 | + alignItems: "center", |
| 133 | + color: theme.palette.text.secondary, |
| 134 | + display: "flex", |
| 135 | + |
| 136 | + "& svg": { |
| 137 | + width: 16, |
| 138 | + height: 16, |
| 139 | + marginRight: theme.spacing(1.5), |
| 140 | + }, |
| 141 | + }, |
| 142 | + loader: { |
| 143 | + margin: `${theme.spacing(2)}px 0`, |
| 144 | + textAlign: "center", |
| 145 | + }, |
| 146 | + paragraph: { |
| 147 | + color: theme.palette.text.secondary, |
| 148 | + margin: `${theme.spacing(2)}px 0`, |
| 149 | + }, |
| 150 | + textField: { |
| 151 | + flex: 1, |
| 152 | + margin: 0, |
| 153 | + }, |
| 154 | + linkButton: { |
| 155 | + color: "inherit", |
| 156 | + flex: 1, |
| 157 | + |
| 158 | + "&:hover": { |
| 159 | + textDecoration: "none", |
| 160 | + }, |
| 161 | + }, |
| 162 | +})) |
0 commit comments