chore: embed ca-certificates.crt

This commit is contained in:
wwqgtxx 2024-03-28 19:26:41 +08:00
parent 82517e6ba8
commit 06b5121d9e
3 changed files with 22 additions and 2 deletions

View file

@ -141,6 +141,12 @@ jobs:
run: | run: |
go test ./... go test ./...
- name: Update UA
run: |
sudo apt-get install ca-certificates
sudo update-ca-certificates
cp -f /etc/ssl/certs/ca-certificates.crt component/ca/ca-certificates.crt
- name: Build core - name: Build core
env: env:
GOOS: ${{matrix.jobs.goos}} GOOS: ${{matrix.jobs.goos}}

View file

View file

@ -5,10 +5,12 @@ import (
"crypto/sha256" "crypto/sha256"
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
_ "embed"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
"os" "os"
"strconv"
"strings" "strings"
"sync" "sync"
) )
@ -18,6 +20,11 @@ var globalCertPool *x509.CertPool
var mutex sync.RWMutex var mutex sync.RWMutex
var errNotMatch = errors.New("certificate fingerprints do not match") var errNotMatch = errors.New("certificate fingerprints do not match")
//go:embed ca-certificates.crt
var _CaCertificates []byte
var DisableEmbedCa, _ = strconv.ParseBool(os.Getenv("DISABLE_EMBED_CA"))
var DisableSystemCa, _ = strconv.ParseBool(os.Getenv("DISABLE_SYSTEM_CA"))
func AddCertificate(certificate string) error { func AddCertificate(certificate string) error {
mutex.Lock() mutex.Lock()
defer mutex.Unlock() defer mutex.Unlock()
@ -34,13 +41,20 @@ func AddCertificate(certificate string) error {
func initializeCertPool() { func initializeCertPool() {
var err error var err error
globalCertPool, err = x509.SystemCertPool() if DisableSystemCa {
if err != nil {
globalCertPool = x509.NewCertPool() globalCertPool = x509.NewCertPool()
} else {
globalCertPool, err = x509.SystemCertPool()
if err != nil {
globalCertPool = x509.NewCertPool()
}
} }
for _, cert := range trustCerts { for _, cert := range trustCerts {
globalCertPool.AddCert(cert) globalCertPool.AddCert(cert)
} }
if !DisableEmbedCa {
globalCertPool.AppendCertsFromPEM(_CaCertificates)
}
} }
func ResetCertificate() { func ResetCertificate() {