support custom cert/key files to initialize the default certificate

This commit is contained in:
ginuerzh 2018-05-19 12:05:23 +08:00
parent 0695bb5e9a
commit 3271a50bdb
2 changed files with 19 additions and 9 deletions

View File

@ -17,13 +17,18 @@ import (
"github.com/ginuerzh/gost"
)
var (
defaultCertFile = "cert.pem"
defaultKeyFile = "key.pem"
)
// Load the certificate from cert and key files, will use the default certificate if the provided info are invalid.
func tlsConfig(certFile, keyFile string) (*tls.Config, error) {
if certFile == "" {
certFile = "cert.pem"
certFile = defaultCertFile
}
if keyFile == "" {
keyFile = "key.pem"
keyFile = defaultKeyFile
}
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {

View File

@ -57,15 +57,20 @@ func init() {
}
func main() {
// NOTE: as of 2.6, you can use custom cert/key files to initialize the default certificate.
config, err := tlsConfig(defaultCertFile, defaultKeyFile)
if err != nil {
// generate random self-signed certificate.
cert, err := gost.GenCertificate()
if err != nil {
log.Log(err)
os.Exit(1)
}
gost.DefaultTLSConfig = &tls.Config{
config = &tls.Config{
Certificates: []tls.Certificate{cert},
}
}
gost.DefaultTLSConfig = config
for _, route := range routes {
if err := route.serve(); err != nil {