V2bX/node/cert.go

117 lines
2.9 KiB
Go
Raw Normal View History

package node
import (
2023-08-16 12:21:15 -04:00
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
2023-08-16 12:21:15 -04:00
"math/big"
"os"
"time"
2023-07-27 21:13:11 -04:00
2023-07-29 07:27:15 -04:00
"github.com/InazumaV/V2bX/common/file"
2023-07-27 21:13:11 -04:00
log "github.com/sirupsen/logrus"
)
2023-07-27 19:06:45 -04:00
func (c *Controller) renewCertTask() error {
2023-08-16 12:21:15 -04:00
l, err := NewLego(c.CertConfig)
2023-06-15 22:04:39 -04:00
if err != nil {
2023-07-27 21:13:11 -04:00
log.WithField("tag", c.tag).Info("new lego error: ", err)
2023-07-27 19:06:45 -04:00
return nil
2023-06-15 22:04:39 -04:00
}
err = l.RenewCert()
if err != nil {
2023-07-27 21:13:11 -04:00
log.WithField("tag", c.tag).Info("renew cert error: ", err)
return nil
2023-06-15 22:04:39 -04:00
}
2023-07-27 19:06:45 -04:00
return nil
2023-06-15 22:04:39 -04:00
}
func (c *Controller) requestCert() error {
switch c.CertConfig.CertMode {
case "reality", "none", "":
2023-07-13 02:16:21 -04:00
case "file":
if c.CertConfig.CertFile == "" || c.CertConfig.KeyFile == "" {
return fmt.Errorf("cert file path or key file path not exist")
}
case "dns", "http":
2023-07-13 02:16:21 -04:00
if c.CertConfig.CertFile == "" || c.CertConfig.KeyFile == "" {
return fmt.Errorf("cert file path or key file path not exist")
}
if file.IsExist(c.CertConfig.CertFile) && file.IsExist(c.CertConfig.KeyFile) {
return nil
}
2023-08-16 12:21:15 -04:00
l, err := NewLego(c.CertConfig)
if err != nil {
return fmt.Errorf("create lego object error: %s", err)
}
err = l.CreateCert()
if err != nil {
2023-08-16 12:21:15 -04:00
return fmt.Errorf("create lego cert error: %s", err)
}
2023-08-16 12:21:15 -04:00
case "self":
if c.CertConfig.CertFile == "" || c.CertConfig.KeyFile == "" {
return fmt.Errorf("cert file path or key file path not exist")
}
if file.IsExist(c.CertConfig.CertFile) && file.IsExist(c.CertConfig.KeyFile) {
return nil
}
err := generateSelfSslCertificate(
c.CertConfig.CertDomain,
c.CertConfig.CertFile,
c.CertConfig.KeyFile)
if err != nil {
return fmt.Errorf("generate self cert error: %s", err)
}
default:
return fmt.Errorf("unsupported certmode: %s", c.CertConfig.CertMode)
}
return nil
}
func generateSelfSslCertificate(domain, certPath, keyPath string) error {
key, _ := rsa.GenerateKey(rand.Reader, 2048)
tmpl := &x509.Certificate{
Version: 3,
SerialNumber: big.NewInt(time.Now().Unix()),
Subject: pkix.Name{
CommonName: domain,
},
DNSNames: []string{domain},
BasicConstraintsValid: true,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(30, 0, 0),
}
cert, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, key.Public(), key)
if err != nil {
return err
}
2023-08-16 12:21:15 -04:00
f, err := os.OpenFile(certPath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return err
}
err = pem.Encode(f, &pem.Block{
Type: "CERTIFICATE",
Bytes: cert,
})
if err != nil {
return err
}
f, err = os.OpenFile(keyPath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return err
}
err = pem.Encode(f, &pem.Block{
Type: "EC PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
})
if err != nil {
return err
}
return nil
}