sshConfigSplitOnCoderSection splits the SSH config into 3 sections. All lines before sshStartToken, the coder section, and all lines after sshEndToken.
(data []byte)
| 730 | // All lines before sshStartToken, the coder section, and all lines after |
| 731 | // sshEndToken. |
| 732 | func sshConfigSplitOnCoderSection(data []byte) (before, section []byte, after []byte, err error) { |
| 733 | startCount := bytes.Count(data, []byte(sshStartToken)) |
| 734 | endCount := bytes.Count(data, []byte(sshEndToken)) |
| 735 | if startCount > 1 || endCount > 1 { |
| 736 | return nil, nil, nil, xerrors.New("Malformed config: ssh config has multiple coder sections, please remove all but one") |
| 737 | } |
| 738 | |
| 739 | startIndex := bytes.Index(data, []byte(sshStartToken)) |
| 740 | endIndex := bytes.Index(data, []byte(sshEndToken)) |
| 741 | if startIndex == -1 && endIndex != -1 { |
| 742 | return nil, nil, nil, xerrors.New("Malformed config: ssh config has end header, but missing start header") |
| 743 | } |
| 744 | if startIndex != -1 && endIndex == -1 { |
| 745 | return nil, nil, nil, xerrors.New("Malformed config: ssh config has start header, but missing end header") |
| 746 | } |
| 747 | if startIndex != -1 && endIndex != -1 { |
| 748 | if startIndex > endIndex { |
| 749 | return nil, nil, nil, xerrors.New("Malformed config: ssh config has coder section, but it is malformed and the END header is before the START header") |
| 750 | } |
| 751 | // We use -1 and +1 here to also include the preceding |
| 752 | // and trailing newline, where applicable. |
| 753 | start := startIndex |
| 754 | if start > 0 { |
| 755 | start-- |
| 756 | } |
| 757 | end := endIndex + len(sshEndToken) |
| 758 | if end < len(data) { |
| 759 | end++ |
| 760 | } |
| 761 | return data[:start], data[start:end], data[end:], nil |
| 762 | } |
| 763 | |
| 764 | return data, nil, nil, nil |
| 765 | } |
| 766 | |
| 767 | // sshConfigProxyCommandEscape prepares the path for use in ProxyCommand. |
| 768 | // It quotes the string if it contains spaces, as per |