-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from gptankit/master_tls_management
Improvements to optional tls management
- Loading branch information
Showing
2 changed files
with
64 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/tls" | ||
"model" | ||
"net" | ||
"time" | ||
) | ||
|
||
func getListener(sqp model.ServiceQProperties) (net.Listener, error) { | ||
|
||
transport := "tcp" | ||
addr := ":" + sqp.ListenerPort | ||
certificate := sqp.SSLCertificateFile | ||
key := sqp.SSLPrivateKeyFile | ||
|
||
if !sqp.SSLEnabled { | ||
return newListener(transport, addr) | ||
} else { | ||
return newListener(transport, addr, applyTLS(certificate, key)) | ||
} | ||
} | ||
|
||
func newListener(transport string, addr string, options ...func(*net.Listener) error) (net.Listener, error) { | ||
|
||
listener, err := net.Listen(transport, addr) | ||
if err != nil { | ||
return listener, err | ||
} | ||
|
||
for _, option := range options { | ||
err = option(&listener) | ||
if err != nil { | ||
return listener, err // further options won't be executed | ||
} | ||
} | ||
|
||
return listener, nil | ||
} | ||
|
||
func applyTLS(certificate string, key string) func(*net.Listener) error { | ||
|
||
return func(l *net.Listener) error { | ||
|
||
cert, err := tls.LoadX509KeyPair(certificate, key) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tlsConfig := &tls.Config{ | ||
Certificates: []tls.Certificate{cert}, | ||
ServerName: "serviceq", | ||
NextProtos: []string{"http/1.1", "http/1.0"}, | ||
Time: time.Now, | ||
Rand: rand.Reader, | ||
} | ||
tlsConfig.BuildNameToCertificate() | ||
tlsConfig.PreferServerCipherSuites = true | ||
|
||
*l = tls.NewListener(*l, tlsConfig) | ||
return nil | ||
} | ||
} |
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