mps

morphux package server
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/mps

commit bb223c543b766676031ca2597be9cedac68dac96
parent 0b3795b1b23609df5079f94f25947d000b4d88b7
Author: MrGosti <gostitsog@gmail.com>
Date:   Mon,  1 May 2017 14:01:40 +0200

adding tls support

Diffstat:
Mmain.go | 41++++++++++++++++++++++++++++++++++++-----
1 file changed, 36 insertions(+), 5 deletions(-)

diff --git a/main.go b/main.go @@ -17,6 +17,8 @@ package main import ( + "crypto/rand" + "crypto/tls" "database/sql" "flag" "fmt" @@ -34,6 +36,10 @@ func main() { databasePtr := flag.String("db", "", "a sqlite database") + tlsPtr := flag.Bool("tls", false, "use or not tls") + PublicKeyPtr := flag.String("pub", "", "public key path") + PrivateKeyPtr := flag.String("priv", "", "private key path") + flag.Parse() if *databasePtr == "" || len(flag.Args()) == 0 { @@ -47,17 +53,42 @@ func main() { } defer db.Close() - l, err := net.Listen("tcp", flag.Args()[0]) + var listener net.Listener + + if *tlsPtr == true && *PrivateKeyPtr != "" && *PublicKeyPtr != "" { + cert, err := tls.LoadX509KeyPair(*PrivateKeyPtr, *PublicKeyPtr) + if err != nil { + log.Fatalf("server: loadkeys: %s", err) + } + config := tls.Config{Certificates: []tls.Certificate{cert}} + config.Rand = rand.Reader + + fmt.Println("Successfully load key pair ", *PrivateKeyPtr, *PublicKeyPtr) + + listener, err = tls.Listen("tcp", flag.Args()[0], &config) + } else { + listener, err = net.Listen("tcp", flag.Args()[0]) + } + + if listener == nil { + fmt.Println("Error listening") + os.Exit(1) + } + if err != nil { fmt.Println("Error listening:", err.Error()) os.Exit(1) } - defer l.Close() - fmt.Println("Listening on " + flag.Args()[0]) - for { + defer listener.Close() + if *tlsPtr { + fmt.Println("Listening on "+flag.Args()[0], "with TLS support") + } else { + fmt.Println("Listening on " + flag.Args()[0]) + } - conn, err := l.Accept() + for { + conn, err := listener.Accept() if err != nil { fmt.Println("Error accepting: ", err.Error()) os.Exit(1)