(domain string, port int, allowExternalTcp bool)
| 162 | } |
| 163 | |
| 164 | func (m *TunnelManager) addToAuthorizedKeys(domain string, port int, allowExternalTcp bool) (string, error) { |
| 165 | |
| 166 | authKeysPath := fmt.Sprintf("%s/.ssh/authorized_keys", m.user.HomeDir) |
| 167 | |
| 168 | akFile, err := os.OpenFile(authKeysPath, os.O_RDWR|os.O_CREATE, 0600) |
| 169 | if err != nil { |
| 170 | return "", err |
| 171 | } |
| 172 | defer akFile.Close() |
| 173 | |
| 174 | akBytes, err := ioutil.ReadAll(akFile) |
| 175 | if err != nil { |
| 176 | return "", err |
| 177 | } |
| 178 | |
| 179 | akStr := string(akBytes) |
| 180 | |
| 181 | var privKey string |
| 182 | var pubKey string |
| 183 | |
| 184 | pubKey, privKey, err = MakeSSHKeyPair() |
| 185 | if err != nil { |
| 186 | return "", err |
| 187 | } |
| 188 | |
| 189 | pubKey = strings.TrimSpace(pubKey) |
| 190 | |
| 191 | bindAddr := "127.0.0.1" |
| 192 | if allowExternalTcp { |
| 193 | bindAddr = "0.0.0.0" |
| 194 | } |
| 195 | |
| 196 | options := fmt.Sprintf(`command="echo This key permits tunnels only",permitopen="fakehost:1",permitlisten="%s:%d"`, bindAddr, port) |
| 197 | |
| 198 | tunnelId := fmt.Sprintf("boringproxy-%s-%d", domain, port) |
| 199 | |
| 200 | newAk := fmt.Sprintf("%s%s %s %s\n", akStr, options, pubKey, tunnelId) |
| 201 | |
| 202 | // Clear the file |
| 203 | err = akFile.Truncate(0) |
| 204 | if err != nil { |
| 205 | return "", err |
| 206 | } |
| 207 | _, err = akFile.Seek(0, 0) |
| 208 | if err != nil { |
| 209 | return "", err |
| 210 | } |
| 211 | |
| 212 | _, err = akFile.Write([]byte(newAk)) |
| 213 | if err != nil { |
| 214 | return "", err |
| 215 | } |
| 216 | |
| 217 | return privKey, nil |
| 218 | } |
| 219 | |
| 220 | // Adapted from https://stackoverflow.com/a/34347463/943814 |
| 221 | // MakeSSHKeyPair make a pair of public and private keys for SSH access. |
no test coverage detected