* 合并devcontainer web相关的源文件,简化目录结构 * devcontainer、ssh_key_pair和devcontainer.cloud * fixed bug:创建容器时Host为localhost时创建失败的问题 * 删除了死代码,更新了一些命名(主要是去掉devstar字符串) * 常量名全大写 * devcontainer HOST改为用户设置的域名或IP * 安装时如没有配置devcontainer则默认设置为docker方式 * 直接使用kubernetes和docker简化代码提高可读性 * 去除services/devstar_devcontainer文件夹名中不必要的devstar字符串 * 去除services/devstar_devcontainer文件夹名中不必要的devstar字符串 * 文件名中去掉不必要的devstar字符串 * 变量名中删除不必要的Devstar字符串 * Merge branch 'dev' into feature-websshAndUpdateImage * change pages style * change Structure * fix bug * websshAndUpdateImage
80 lines
2.3 KiB
Go
80 lines
2.3 KiB
Go
package api_service
|
||
|
||
import (
|
||
"crypto/rand"
|
||
"crypto/rsa"
|
||
"crypto/x509"
|
||
"encoding/pem"
|
||
"strconv"
|
||
|
||
"code.gitea.io/gitea/modules/setting"
|
||
"code.gitea.io/gitea/services/devstar_ssh_key_pair/errors"
|
||
"code.gitea.io/gitea/services/devstar_ssh_key_pair/vo"
|
||
"golang.org/x/crypto/ssh"
|
||
)
|
||
|
||
// GenerateNewRSASSHSessionKeyPair 生成 RSA SSH 密钥对
|
||
func GenerateNewRSASSHSessionKeyPair() (error, *vo.GenerateNewRSASSHSessionKeyPairVO) {
|
||
|
||
// 1. 生成 SSH 密钥对 (算法 RSA,长度 setting.SSHKeypair.KeySize)
|
||
privateKey, err := rsa.GenerateKey(rand.Reader, setting.SSHKeypair.KeySize)
|
||
if err != nil {
|
||
return err, nil
|
||
}
|
||
|
||
// 2. 获取 Private Key PEM
|
||
privateKeyDer := x509.MarshalPKCS1PrivateKey(privateKey)
|
||
privateKeyBlock := pem.Block{
|
||
Type: "RSA PRIVATE KEY",
|
||
Headers: nil,
|
||
Bytes: privateKeyDer,
|
||
}
|
||
privateKeyPem := pem.EncodeToMemory(&privateKeyBlock)
|
||
if privateKeyPem == nil {
|
||
return errors.ErrGenerateNewRSASSHSessionKeyPair{
|
||
Action: "Encode Private Key to memory",
|
||
Message: "private key PEM encoded to be nil",
|
||
}, nil
|
||
}
|
||
privateKeyPemStr := string(privateKeyPem)
|
||
|
||
// 3. 获取 Public Key PEM
|
||
publicKey := privateKey.PublicKey
|
||
publicKeyDer, err := x509.MarshalPKIXPublicKey(&publicKey)
|
||
if err != nil {
|
||
return errors.ErrGenerateNewRSASSHSessionKeyPair{
|
||
Action: "Marshal PKIX Public Key",
|
||
Message: err.Error(),
|
||
}, nil
|
||
}
|
||
publicKeyBlock := pem.Block{
|
||
Type: "PUBLIC KEY",
|
||
Headers: nil,
|
||
Bytes: publicKeyDer,
|
||
}
|
||
publicKeyPem := pem.EncodeToMemory(&publicKeyBlock)
|
||
if publicKeyPem == nil {
|
||
return errors.ErrGenerateNewRSASSHSessionKeyPair{
|
||
Action: "Encode Public Key to memory",
|
||
Message: "public key PEM encoded to be nil",
|
||
}, nil
|
||
}
|
||
publicKeyPemStr := string(publicKeyPem)
|
||
|
||
// 3. 计算 SSH Public SSH Key,用于 ~/.ssh/authorized_keys
|
||
sshPublicKey, err := ssh.NewPublicKey(&publicKey)
|
||
if err != nil {
|
||
return errors.ErrGenerateNewRSASSHSessionKeyPair{
|
||
Action: "Calculate SSH Public SSH Key",
|
||
Message: err.Error(),
|
||
}, nil
|
||
}
|
||
sshPublicKeyStr := string(ssh.MarshalAuthorizedKey(sshPublicKey))
|
||
return nil, &vo.GenerateNewRSASSHSessionKeyPairVO{
|
||
PublicKeyPEM: publicKeyPemStr,
|
||
PrivateKeyPEM: privateKeyPemStr,
|
||
PublicKeySsh: sshPublicKeyStr,
|
||
KeySize: strconv.Itoa(setting.SSHKeypair.KeySize),
|
||
}
|
||
}
|