-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (47 loc) · 1.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"os"
"github.com/gliderlabs/ssh"
"github.com/joho/godotenv"
"github.com/paastech-cloud/git-ssh-server/config"
"github.com/paastech-cloud/git-ssh-server/handlers"
"github.com/paastech-cloud/git-ssh-server/logger"
"github.com/paastech-cloud/git-ssh-server/utils"
"github.com/rs/zerolog/log"
)
/**
* @description: SSH server with authorization via public key.
*/
func main() {
err := godotenv.Load()
logger.Setup()
if err != nil && os.Getenv("ENV") == "development" {
log.Fatal().Err(err).Msg("error loading .env file")
}
err = config.CheckConfig()
if err != nil {
log.Fatal().Err(err)
}
// check if path to host signer exists
if _, err := os.Stat(config.PathToHostSigner); err != nil {
log.Info().Err(err).Msg("host signer file does not exist, generating a key pair..")
err = utils.GenerateKeyPair(config.PathToHostSigner)
if err != nil {
log.Fatal().Err(err).Msg("Error while generating RSA Key Pair")
}
}
// read host file from host
hostSigner := ssh.HostKeyFile(config.PathToHostSigner)
s := &ssh.Server{
Addr: ":2222",
Handler: handlers.ReceivePackHandler,
PublicKeyHandler: handlers.AuthenticateUser,
}
// add host key to server
err = s.SetOption(hostSigner)
if err != nil {
log.Fatal().Err(err).Msg("error setting host signer")
}
log.Info().Msg("server listening on port 2222")
log.Fatal().Err(s.ListenAndServe())
}