Skip to content

Commit

Permalink
print prettier information
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Dec 10, 2024
1 parent e26c85e commit f0b0d92
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions cmd/grasshopper/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,22 @@ import (
const (
// SALT is used for PBKDF2 key derivation.
SALT = "GRASSHOPPER"

// PBKDF2 iterations.
ITERATIONS = 4096

// PBKDF2 key length.
KEYLEN = 32
)

// Version specifies the current version of the application.
// Injected by the build system.
var Version = "undefined"
var (
// Version specifies the current version of the application.
// Injected by the build system.
Version = "undefined"

// allCryptoMethods lists all supported cryptographic methods.
var allCryptoMethods = []string{"none", "sm4", "tea", "aes", "aes-128", "aes-192", "blowfish", "twofish", "cast5", "3des", "xtea", "salsa20"}
// allCryptoMethods lists all supported cryptographic methods.
allCryptoMethods = []string{"none", "sm4", "tea", "aes", "aes-128", "aes-192", "blowfish", "twofish", "cast5", "3des", "xtea", "salsa20"}
)

// startCmd represents the start command
var startCmd = &cobra.Command{
Expand All @@ -53,15 +61,6 @@ var startCmd = &cobra.Command{
log.Println("Listening on:", config.Listen)
log.Println("Next hops:", config.NextHops)
log.Println("Socket buffer:", config.SockBuf)
log.Println("Incoming crypto:", config.CI)
log.Println("Outgoing crypto:", config.CO)

// Derive cryptographic keys using PBKDF2.
log.Println("Initiating key derivation(IN)")
passIn := pbkdf2.Key([]byte(config.KI), []byte(SALT), 4096, 32, sha1.New)
log.Println("Initiating key derivation(OUT)")
passOut := pbkdf2.Key([]byte(config.KO), []byte(SALT), 4096, 32, sha1.New)
log.Println("Key derivation done")

// Validate cryptographic methods.
if !slices.Contains(allCryptoMethods, config.CI) {
Expand All @@ -72,16 +71,21 @@ var startCmd = &cobra.Command{
log.Fatal("Invalid crypto method:", config.CO)
}

// Initialize cryptographic handlers.
// Derive cryptographic keys using PBKDF2.
log.Printf("Initiating Cryptography (In: %v) <---> (Out: %v)", config.CI, config.CO)
passIn := pbkdf2.Key([]byte(config.KI), []byte(SALT), ITERATIONS, KEYLEN, sha1.New)
crypterIn := newCrypt(passIn, config.CI)
passOut := pbkdf2.Key([]byte(config.KO), []byte(SALT), ITERATIONS, KEYLEN, sha1.New)
crypterOut := newCrypt(passOut, config.CO)
log.Println("Crytography initialized")

// Initialize and start the UDP listener.
listener, err := grasshopper.ListenWithOptions(config.Listen, config.NextHops, config.SockBuf, config.Timeout, crypterIn, crypterOut, nil, nil, log.Default())
if err != nil {
log.Fatal(err)
}

log.Println("Ready")
listener.Start()
},
}
Expand Down

0 comments on commit f0b0d92

Please sign in to comment.