forked from pion/example-webrtc-applications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.go
59 lines (50 loc) · 1.69 KB
/
web.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"html/template"
"math/big"
"net/http"
"os"
"time"
)
func genPem() {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
checkError(err)
SNLimit := new(big.Int).Lsh(big.NewInt(1), 128)
SN, err := rand.Int(rand.Reader, SNLimit)
checkError(err)
template := 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,
}
template.DNSNames = append(template.DNSNames, "localhost")
template.EmailAddresses = append(template.EmailAddresses, "[email protected]")
certBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey)
checkError(err)
certFile, err := os.Create("cert.pem")
checkError(err)
checkError(pem.Encode(certFile, &pem.Block{Type: "CERTIFICATE", Bytes: certBytes}))
checkError(certFile.Close())
keyFile, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
checkError(err)
// pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv.(*rsa.PrivateKey))})
checkError(pem.Encode(keyFile, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}))
checkError(keyFile.Close())
}
func web(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
t, _ := template.ParseFiles("sfu.html")
checkError(t.Execute(w, nil))
}
}