feat: 支持局域网 HTTPS 直连并持久化插件连接配置
This commit is contained in:
1 parent
9eba4556db
commit
2203afde3c
15 files changed
+486
-17
No files matched your search
@@ -0,0 +1,93 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func initCertificate(host, dir string) error {
|
||||
if strings.TrimSpace(host) != host || host == "" || strings.ContainsAny(host, "/\\ :\x00") {
|
||||
if net.ParseIP(host) == nil {
|
||||
return fmt.Errorf("invalid certificate host")
|
||||
}
|
||||
}
|
||||
certFile, keyFile := filepath.Join(dir, "server.crt"), filepath.Join(dir, "server.key")
|
||||
if _, e := os.Stat(certFile); e == nil {
|
||||
pair, e := tls.LoadX509KeyPair(certFile, keyFile)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
cert, e := x509.ParseCertificate(pair.Certificate[0])
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if e = cert.VerifyHostname(host); e != nil {
|
||||
return e
|
||||
}
|
||||
if time.Now().After(cert.NotAfter) {
|
||||
return fmt.Errorf("certificate expired; rotate explicitly")
|
||||
}
|
||||
fmt.Printf("Certificate SHA256: %s\n", digest(pair.Certificate[0]))
|
||||
return nil
|
||||
}
|
||||
if _, e := os.Stat(keyFile); e == nil {
|
||||
return fmt.Errorf("private key already exists without certificate; recover explicitly")
|
||||
}
|
||||
private, e := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
serial, e := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
cert := &x509.Certificate{SerialNumber: serial, Subject: pkix.Name{CommonName: "Remote Compilation"}, NotBefore: time.Now().Add(-5 * time.Minute), NotAfter: time.Now().AddDate(1, 0, 0), KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, DNSNames: []string{"localhost"}, IPAddresses: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("::1")}}
|
||||
if ip := net.ParseIP(host); ip != nil {
|
||||
cert.IPAddresses = append(cert.IPAddresses, ip)
|
||||
} else {
|
||||
cert.DNSNames = append(cert.DNSNames, host)
|
||||
}
|
||||
der, e := x509.CreateCertificate(rand.Reader, cert, cert, &private.PublicKey, private)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
key, e := x509.MarshalPKCS8PrivateKey(private)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if e = os.MkdirAll(dir, 0700); e != nil {
|
||||
return e
|
||||
}
|
||||
write := func(path string, b []byte) error {
|
||||
f, e := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
_, e = f.Write(b)
|
||||
closeErr := f.Close()
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
return closeErr
|
||||
}
|
||||
if e = write(keyFile, pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: key})); e != nil {
|
||||
return e
|
||||
}
|
||||
if e = write(certFile, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})); e != nil {
|
||||
return e
|
||||
}
|
||||
fmt.Printf("Certificate SHA256: %s\n", digest(der))
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user