2020-03-09 02:33:12 -04:00
|
|
|
package cert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2020-10-11 07:22:46 -04:00
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/task"
|
2020-03-09 02:33:12 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenerate(t *testing.T) {
|
|
|
|
err := generate(nil, true, true, "ca")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generate(domainNames []string, isCA bool, jsonOutput bool, fileOutput string) error {
|
|
|
|
commonName := "V2Ray Root CA"
|
|
|
|
organization := "V2Ray Inc"
|
|
|
|
|
|
|
|
expire := time.Hour * 3
|
|
|
|
|
|
|
|
var opts []Option
|
|
|
|
if isCA {
|
|
|
|
opts = append(opts, Authority(isCA))
|
|
|
|
opts = append(opts, KeyUsage(x509.KeyUsageCertSign|x509.KeyUsageKeyEncipherment|x509.KeyUsageDigitalSignature))
|
|
|
|
}
|
|
|
|
|
|
|
|
opts = append(opts, NotAfter(time.Now().Add(expire)))
|
|
|
|
opts = append(opts, CommonName(commonName))
|
|
|
|
if len(domainNames) > 0 {
|
|
|
|
opts = append(opts, DNSNames(domainNames...))
|
|
|
|
}
|
|
|
|
opts = append(opts, Organization(organization))
|
|
|
|
|
|
|
|
cert, err := Generate(nil, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return newError("failed to generate TLS certificate").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if jsonOutput {
|
2020-03-24 08:04:14 -04:00
|
|
|
printJSON(cert)
|
2020-03-09 02:33:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(fileOutput) > 0 {
|
|
|
|
if err := printFile(cert, fileOutput); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonCert struct {
|
|
|
|
Certificate []string `json:"certificate"`
|
|
|
|
Key []string `json:"key"`
|
|
|
|
}
|
|
|
|
|
2020-03-24 08:04:14 -04:00
|
|
|
func printJSON(certificate *Certificate) {
|
2020-03-09 02:33:12 -04:00
|
|
|
certPEM, keyPEM := certificate.ToPEM()
|
|
|
|
jCert := &jsonCert{
|
|
|
|
Certificate: strings.Split(strings.TrimSpace(string(certPEM)), "\n"),
|
|
|
|
Key: strings.Split(strings.TrimSpace(string(keyPEM)), "\n"),
|
|
|
|
}
|
|
|
|
content, err := json.MarshalIndent(jCert, "", " ")
|
|
|
|
common.Must(err)
|
|
|
|
os.Stdout.Write(content)
|
|
|
|
os.Stdout.WriteString("\n")
|
|
|
|
}
|
2021-05-19 17:28:52 -04:00
|
|
|
|
2020-03-09 02:33:12 -04:00
|
|
|
func printFile(certificate *Certificate, name string) error {
|
|
|
|
certPEM, keyPEM := certificate.ToPEM()
|
|
|
|
return task.Run(context.Background(), func() error {
|
|
|
|
return writeFile(certPEM, name+"_cert.pem")
|
|
|
|
}, func() error {
|
|
|
|
return writeFile(keyPEM, name+"_key.pem")
|
|
|
|
})
|
|
|
|
}
|
2021-05-19 17:28:52 -04:00
|
|
|
|
2020-03-09 02:33:12 -04:00
|
|
|
func writeFile(content []byte, name string) error {
|
|
|
|
f, err := os.Create(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return common.Error2(f.Write(content))
|
|
|
|
}
|