-
-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: frps support ssh * fix: comments * fix: update pkg * fix: remove useless change --------- Co-authored-by: int7 <[email protected]>
- Loading branch information
Showing
8 changed files
with
909 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package v1 | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
|
||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
const ( | ||
// custom define | ||
SSHClientLoginUserPrefix = "_frpc_ssh_client_" | ||
) | ||
|
||
// encodePrivateKeyToPEM encodes Private Key from RSA to PEM format | ||
func GeneratePrivateKey() ([]byte, error) { | ||
privateKey, err := generatePrivateKey() | ||
if err != nil { | ||
return nil, errors.New("gen private key error") | ||
} | ||
|
||
privBlock := pem.Block{ | ||
Type: "RSA PRIVATE KEY", | ||
Headers: nil, | ||
Bytes: x509.MarshalPKCS1PrivateKey(privateKey), | ||
} | ||
|
||
return pem.EncodeToMemory(&privBlock), nil | ||
} | ||
|
||
// generatePrivateKey creates a RSA Private Key of specified byte size | ||
func generatePrivateKey() (*rsa.PrivateKey, error) { | ||
privateKey, err := rsa.GenerateKey(rand.Reader, 4096) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = privateKey.Validate() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return privateKey, nil | ||
} | ||
|
||
func LoadSSHPublicKeyFilesInDir(dirPath string) (map[string]ssh.PublicKey, error) { | ||
fileMap := make(map[string]ssh.PublicKey) | ||
files, err := os.ReadDir(dirPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, file := range files { | ||
filePath := filepath.Join(dirPath, file.Name()) | ||
content, err := os.ReadFile(filePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
parsedAuthorizedKey, _, _, _, err := ssh.ParseAuthorizedKey(content) | ||
if err != nil { | ||
continue | ||
} | ||
fileMap[ssh.FingerprintSHA256(parsedAuthorizedKey)] = parsedAuthorizedKey | ||
} | ||
|
||
return fileMap, nil | ||
} |
Oops, something went wrong.