vendor/github.com/go-rod/rod/lib/launcher/utils.go

49 lines · 1023 bytes

 1package launcher
 2
 3import (
 4	"crypto"
 5	"crypto/sha256"
 6	"crypto/x509"
 7	"encoding/base64"
 8	"fmt"
 9	"net/url"
10
11	"github.com/go-rod/rod/lib/utils"
12)
13
14var inContainer = utils.InContainer
15
16func toHTTP(u url.URL) *url.URL {
17	newURL := u
18	if newURL.Scheme == "ws" {
19		newURL.Scheme = "http"
20	} else if newURL.Scheme == "wss" {
21		newURL.Scheme = "https"
22	}
23	return &newURL
24}
25
26func toWS(u url.URL) *url.URL {
27	newURL := u
28	if newURL.Scheme == "http" {
29		newURL.Scheme = "ws"
30	} else if newURL.Scheme == "https" {
31		newURL.Scheme = "wss"
32	}
33	return &newURL
34}
35
36// certSPKI generates the SPKI of a certificate public key
37// https://blog.afoolishmanifesto.com/posts/golang-self-signed-and-pinned-certs/
38func certSPKI(pk crypto.PublicKey) ([]byte, error) {
39	pubDER, err := x509.MarshalPKIXPublicKey(pk)
40	if err != nil {
41		return nil, fmt.Errorf("x509.MarshalPKIXPublicKey: %w", err)
42	}
43
44	sum := sha256.Sum256(pubDER)
45	pin := make([]byte, base64.StdEncoding.EncodedLen(len(sum)))
46	base64.StdEncoding.Encode(pin, sum[:])
47
48	return pin, nil
49}