Skip to content

Commit

Permalink
Merge branch 'dev-branch' of https://github.com/mohit810/streamingcdn
Browse files Browse the repository at this point in the history
…into dev-branch
  • Loading branch information
mohit810 committed Nov 22, 2020
2 parents e7b0472 + 9a2e40e commit e492956
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
14 changes: 10 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ import (
"github.com/mohit810/streamingcdn/signal"
)

func init() {
// Generate pem file for https
signal.GenPem()
}

func main() {
port := flag.Int("port", 8080, "http server port")
flag.Parse()
r := httprouter.New()
signal.HTTPSDPServer(r)
err := http.ListenAndServe(":"+strconv.Itoa(*port), r)
if err != nil {
panic(err)
}
//err := http.ListenAndServe(":"+strconv.Itoa(*port), r)
//if err != nil {
// panic(err)
//}
panic(http.ListenAndServeTLS(":"+strconv.Itoa(*port), "cert.pem", "key.pem", r))
}
51 changes: 51 additions & 0 deletions signal/certificate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package signal

import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"github.com/mohit810/streamingcdn/errorHandler"
"math/big"
"os"
"time"
)

func GenPem() {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
errorHandler.CheckError(err)

SNLimit := new(big.Int).Lsh(big.NewInt(1), 128)
SN, err := rand.Int(rand.Reader, SNLimit)
errorHandler.CheckError(err)

tempplate := x509.Certificate{
SerialNumber: SN,
Subject: pkix.Name{
Organization: []string{"test"},
},
NotBefore: time.Now(),
NotAfter: time.Now().Add(365 * 24 * time.Hour),

KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
BasicConstraintsValid: true,
}
tempplate.DNSNames = append(tempplate.DNSNames, "localhost")
tempplate.EmailAddresses = append(tempplate.EmailAddresses, "test@test.com")

certBytes, err := x509.CreateCertificate(rand.Reader, &tempplate, &tempplate, &privateKey.PublicKey, privateKey)
errorHandler.CheckError(err)

certFile, err := os.Create("cert.pem")
errorHandler.CheckError(err)
errorHandler.CheckError(pem.Encode(certFile, &pem.Block{Type: "CERTIFICATE", Bytes: certBytes}))
errorHandler.CheckError(certFile.Close())

keyFile, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
errorHandler.CheckError(err)
// pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv.(*rsa.PrivateKey))})
errorHandler.CheckError(pem.Encode(keyFile, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}))
errorHandler.CheckError(keyFile.Close())
}

0 comments on commit e492956

Please sign in to comment.