github.com/Laplace-Game-Development/Laplace-Entangled-Environment@v0.0.3/internal/route/secure.go (about) 1 package route 2 3 import ( 4 "crypto/tls" 5 "log" 6 7 "github.com/Laplace-Game-Development/Laplace-Entangled-Environment/internal/policy" 8 ) 9 10 //// Configurables 11 12 // 13 // Encryption Configurables 14 15 // TLS Certificate File Location from root of the project 16 const CrtLocation string = "./tlscert.crt" 17 18 // TLS Key File Location from root of the project 19 const KeyLocation string = "./tlskey.key" 20 21 // 22 // Listener Secure Configurables 23 24 // TLS Configuration for HTTPS Server and SSL with TCP 25 // 26 // This will be assigned on startup then left unchanged 27 var tlsConfig tls.Config = tls.Config{} 28 29 // Set of Commands that need to be done over encrypted connections. 30 // 31 // This Map is a Set! 32 // This should never change during runtime! 33 var secureMap map[policy.ClientCmd]bool = map[policy.ClientCmd]bool{ 34 policy.CmdRegister: true, 35 policy.CmdLogin: true, 36 } 37 38 // ServerTask Startup Function for Encryption. Takes care of initialization. 39 // Loads Certificates and Keys from files and configures TLS. 40 func StartEncryption() (func(), error) { 41 log.Printf("Loading Certificate From: %s \nand Key From: %s\n", CrtLocation, KeyLocation) 42 cert, err := tls.LoadX509KeyPair(CrtLocation, KeyLocation) 43 if err != nil { 44 return nil, err 45 } 46 47 // Instead of setting the certificate we can add a callback to load certificates 48 tlsConfig = tls.Config{ 49 Certificates: []tls.Certificate{cert}, 50 MinVersion: tls.VersionTLS13, 51 } 52 53 return cleanUpEncryption, nil 54 } 55 56 // CleanUp Function returned by Startup function. Doesn't do anything, but here 57 // for consistency. 58 func cleanUpEncryption() { 59 log.Println("Cleaning Up Encryption Logic") 60 } 61 62 // returns if the given command needs an encrypted connection or not 63 // 64 // see "secureMap" 65 func NeedsSecurity(cmd policy.ClientCmd) bool { 66 result, exists := secureMap[cmd] 67 return exists && result 68 }