|
| 1 | +package codeserver |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "go.coder.com/sail/internal/dockutil" |
| 9 | + "golang.org/x/xerrors" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + // PortNotFoundError is returned whenever the port isn't found. |
| 14 | + // This can happen if code-server hasn't started it's listener yet |
| 15 | + // or if the code-server process failed for any reason. |
| 16 | + PortNotFoundError = xerrors.New("failed to find port") |
| 17 | +) |
| 18 | + |
| 19 | +// PID returns the pid of code-server running inside of the container. |
| 20 | +func PID(containerName string) (int, error) { |
| 21 | + out, err := dockutil.FmtExec(containerName, "pgrep -P 1 code-server").CombinedOutput() |
| 22 | + if err != nil { |
| 23 | + return 0, xerrors.Errorf("%s: %w", out, err) |
| 24 | + } |
| 25 | + |
| 26 | + return strconv.Atoi(strings.TrimSpace(string(out))) |
| 27 | +} |
| 28 | + |
| 29 | +// Port returns the port that code-server is listening on. |
| 30 | +// To get the port value, it first finds all of the socket |
| 31 | +// inodes the process is using, then reads the entries in |
| 32 | +// `/proc/net/tcp`. It maps the socket inodes to the inodes |
| 33 | +// listed in `/proc/net/tcp` and returns the port that has a |
| 34 | +// remote address of `0` since this is the listener. |
| 35 | +func Port(containerName string) (string, error) { |
| 36 | + inodes, err := codeServerSocketInodes(containerName) |
| 37 | + if err != nil { |
| 38 | + return "", err |
| 39 | + } |
| 40 | + |
| 41 | + stats, err := netTCPStats(containerName) |
| 42 | + if err != nil { |
| 43 | + return "", err |
| 44 | + } |
| 45 | + |
| 46 | + m := make(map[string]*netStat, len(stats)) |
| 47 | + for _, stat := range stats { |
| 48 | + m[stat.inode] = stat |
| 49 | + } |
| 50 | + |
| 51 | + for _, inode := range inodes { |
| 52 | + stat, ok := m[inode] |
| 53 | + if !ok { |
| 54 | + continue |
| 55 | + } |
| 56 | + |
| 57 | + if stat.remotePort == "0" { |
| 58 | + return stat.localPort, nil |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return "", PortNotFoundError |
| 63 | +} |
| 64 | + |
| 65 | +// codeServerSocketInodes returns all of the socket inodes in use by the code-server process. |
| 66 | +// |
| 67 | +// This function reads the code-server processes' `/proc/<pid>/fd` directory to see all of |
| 68 | +// the open file descriptors the process has open. We grep for any file descriptors that |
| 69 | +// are links to sockets and we awk to just parse out the inode field. |
| 70 | +// |
| 71 | +// See: http://man7.org/linux/man-pages/man5/proc.5.html for more information about `/proc/<pid>/fd`. |
| 72 | +func codeServerSocketInodes(containerName string) ([]string, error) { |
| 73 | + pid, err := PID(containerName) |
| 74 | + if err != nil { |
| 75 | + return nil, xerrors.Errorf("failed to find code-server pid: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + // This command parses the output of `find` to access the inode field. |
| 79 | + // For example, this line from `find`: |
| 80 | + // `65595472 0 lrwx------ 1 root root 64 Apr 23 11:30 /proc/1/fd/8 -> socket:[50784188]` |
| 81 | + // |
| 82 | + // would turn into: |
| 83 | + // `50784188` |
| 84 | + out, err := dockutil.FmtExec( |
| 85 | + containerName, |
| 86 | + // find all fd that are links | grep for sockets | get the socket:[inode] | split on `:`, remove the `[]` brackets, and output the inode. |
| 87 | + `find /proc/%d/fd -type l -ls | grep socket | awk '{ print $13 }' | awk -F ":" '{ gsub("\\[|\\]", "", $2); print $2 }'`, |
| 88 | + pid, |
| 89 | + ).CombinedOutput() |
| 90 | + if err != nil { |
| 91 | + return nil, xerrors.Errorf("%s: %w", out, err) |
| 92 | + } |
| 93 | + |
| 94 | + return strings.Split(string(bytes.TrimSpace(out)), "\n"), nil |
| 95 | +} |
| 96 | + |
| 97 | +type netStat struct { |
| 98 | + remotePort string |
| 99 | + localPort string |
| 100 | + inode string |
| 101 | +} |
| 102 | + |
| 103 | +// netTCPStats returns the entries in /proc/net/tcp inside of the container. |
| 104 | +func netTCPStats(containerName string) ([]*netStat, error) { |
| 105 | + // This command reads the entries in `/proc/net/tcp`, removes the header line with `NR > 1`, and gets |
| 106 | + // the local_address, rem_address, and inode fields. See: https://www.kernel.org/doc/Documentation/networking/proc_net_tcp.txt |
| 107 | + // |
| 108 | + // An example of the first two lines in `/proc/net/tcp` before doing any awk transformations: |
| 109 | + // `sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode` |
| 110 | + // `0: 0100007F:BEB3 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 58828878 1 0000000000000000 100 0 0 10 0` |
| 111 | + // |
| 112 | + // After the awk transformation, it would turn into: |
| 113 | + // `0100007F:BEB3 00000000:0000 58828878` |
| 114 | + out, err := dockutil.FmtExec(containerName, `cat /proc/net/tcp | awk 'NR > 1 {print $2, $3, $10 }'`).CombinedOutput() |
| 115 | + if err != nil { |
| 116 | + return nil, xerrors.Errorf("%s: %w", out, err) |
| 117 | + } |
| 118 | + |
| 119 | + return parseNetTCPStats(out) |
| 120 | +} |
| 121 | + |
| 122 | +// parseNetTCPStats parses the fields from the netTCPStats output. |
| 123 | +func parseNetTCPStats(out []byte) ([]*netStat, error) { |
| 124 | + lines := bytes.Split(bytes.TrimSpace(out), []byte("\n")) |
| 125 | + |
| 126 | + var ( |
| 127 | + err error |
| 128 | + stats = make([]*netStat, 0, len(lines)) |
| 129 | + ) |
| 130 | + for _, line := range lines { |
| 131 | + fields := strings.Fields(string(bytes.TrimSpace(line))) |
| 132 | + if len(fields) != 3 { |
| 133 | + return nil, xerrors.Errorf("line formatted incorrectly: %s", line) |
| 134 | + } |
| 135 | + |
| 136 | + var stat netStat |
| 137 | + stat.localPort, err = parseHexPort(fields[0]) |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + |
| 142 | + stat.remotePort, err = parseHexPort(fields[1]) |
| 143 | + if err != nil { |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + |
| 147 | + stat.inode = fields[2] |
| 148 | + |
| 149 | + stats = append(stats, &stat) |
| 150 | + } |
| 151 | + |
| 152 | + return stats, nil |
| 153 | +} |
| 154 | + |
| 155 | +// parseHexPort parses the port field from the ip:port hex combination. |
| 156 | +// This takes in a local_address or rem_address field from `/proc/net/tcp` |
| 157 | +// and parses the hex port into a base 10 port string. |
| 158 | +func parseHexPort(ipPortHex string) (string, error) { |
| 159 | + fields := strings.Split(ipPortHex, ":") |
| 160 | + if len(fields) != 2 { |
| 161 | + return "", xerrors.Errorf("failed to parse port: %s", ipPortHex) |
| 162 | + } |
| 163 | + |
| 164 | + portHex := fields[1] |
| 165 | + |
| 166 | + port, err := strconv.ParseUint(portHex, 16, 16) |
| 167 | + if err != nil { |
| 168 | + return "", err |
| 169 | + } |
| 170 | + |
| 171 | + return strconv.FormatUint(port, 10), nil |
| 172 | +} |
0 commit comments