Skip to content

Commit

Permalink
Minor change.
Browse files Browse the repository at this point in the history
  • Loading branch information
ortuman committed Mar 26, 2018
1 parent 9000044 commit 0abd860
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
12 changes: 3 additions & 9 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package server

import (
"crypto/tls"
"fmt"
"net"
"net/http"
Expand All @@ -20,6 +19,7 @@ import (
"github.com/ortuman/jackal/log"
"github.com/ortuman/jackal/server/transport"
"github.com/ortuman/jackal/stream/c2s"
"github.com/ortuman/jackal/util"
)

type server struct {
Expand Down Expand Up @@ -125,19 +125,13 @@ func (s *server) listenWebSocketConn(address string) {
log.Fatalf("%v", err)
}
}()

cer, err := tls.LoadX509KeyPair(s.cfg.TLS.CertFile, s.cfg.TLS.PrivKeyFile)
tlsCfg, err := util.LoadCertificate(s.cfg.TLS.PrivKeyFile, s.cfg.TLS.CertFile, c2s.Instance().DefaultLocalDomain())
if err != nil {
log.Fatalf("%v", err)
return
}
cfg := &tls.Config{
ServerName: c2s.Instance().DefaultLocalDomain(),
Certificates: []tls.Certificate{cer},
}
wsSrv := &http.Server{
Addr: address,
TLSConfig: cfg,
TLSConfig: tlsCfg,
}
s.wsUpgrader = &websocket.Upgrader{
ReadBufferSize: s.cfg.Transport.BufferSize,
Expand Down
7 changes: 4 additions & 3 deletions util/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ const localhostDomain = "localhost"

const (
selfSignedCertKeyRSABits = 2048

selfSignedCertFolder = "./.cert/"

selfSignedCertFolder = "./.cert/"
selfSignedCertPrivateKey = selfSignedCertFolder + "key.pem"
selfSignedCertFile = selfSignedCertFolder + "cert.pem"
)

// LoadCertificate loads a certificate given a private key and certificate PEM files.
// Only in case the associated domain is localhost and the files cannot be processed
// a self signed certificate will be automatically generated.
func LoadCertificate(keyFile, certFile, domain string) (*tls.Config, error) {
if len(certFile) == 0 || len(keyFile) == 0 {
switch domain {
Expand Down

0 comments on commit 0abd860

Please sign in to comment.