mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-12-17 12:44:36 +03:00
Some checks are pending
Build docker image / build-image (push) Waiting to run
Build and Release for Windows 7 / build (win7-32, 386, windows) (push) Waiting to run
Build and Release for Windows 7 / build (win7-64, amd64, windows) (push) Waiting to run
Build and Release / build (386, freebsd, ) (push) Waiting to run
Build and Release / build (386, linux, ) (push) Waiting to run
Build and Release / build (386, openbsd, ) (push) Waiting to run
Build and Release / build (386, windows, ) (push) Waiting to run
Build and Release / build (amd64, darwin, ) (push) Waiting to run
Build and Release / build (amd64, freebsd, ) (push) Waiting to run
Build and Release / build (amd64, linux, ) (push) Waiting to run
Build and Release / build (amd64, openbsd, ) (push) Waiting to run
Build and Release / build (amd64, windows, ) (push) Waiting to run
Build and Release / build (arm, 5, linux) (push) Waiting to run
Build and Release / build (arm, 6, linux) (push) Waiting to run
Build and Release / build (arm, 7, freebsd) (push) Waiting to run
Build and Release / build (arm, 7, linux) (push) Waiting to run
Build and Release / build (arm, 7, openbsd) (push) Waiting to run
Build and Release / build (arm, 7, windows) (push) Waiting to run
Build and Release / build (mips, linux) (push) Waiting to run
Build and Release / build (mips64, linux) (push) Waiting to run
Build and Release / build (mips64le, linux) (push) Waiting to run
Build and Release / build (mipsle, linux) (push) Waiting to run
Build and Release / build (ppc64, linux) (push) Waiting to run
Build and Release / build (ppc64le, linux) (push) Waiting to run
Build and Release / build (riscv64, linux) (push) Waiting to run
Build and Release / build (s390x, linux) (push) Waiting to run
Test / test (windows-latest) (push) Waiting to run
Test / test (macos-latest) (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
Build and Release / build (arm64, android) (push) Waiting to run
Build and Release / build (arm64, darwin) (push) Waiting to run
Build and Release / build (arm64, freebsd) (push) Waiting to run
Build and Release / build (arm64, linux) (push) Waiting to run
Build and Release / build (arm64, openbsd) (push) Waiting to run
Build and Release / build (arm64, windows) (push) Waiting to run
Build and Release / build (loong64, linux) (push) Waiting to run
https://github.com/XTLS/Xray-core/issues/4313#issuecomment-2645844058
96 lines
2.1 KiB
Go
96 lines
2.1 KiB
Go
package cert
|
|
|
|
import (
|
|
"context"
|
|
"crypto/x509"
|
|
"encoding/json"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/xtls/xray-core/common"
|
|
"github.com/xtls/xray-core/common/errors"
|
|
"github.com/xtls/xray-core/common/task"
|
|
)
|
|
|
|
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 := "Xray Root CA"
|
|
organization := "Xray 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 errors.New("failed to generate TLS certificate").Base(err)
|
|
}
|
|
|
|
if jsonOutput {
|
|
printJSON(cert)
|
|
}
|
|
|
|
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"`
|
|
}
|
|
|
|
func printJSON(certificate *Certificate) {
|
|
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")
|
|
}
|
|
|
|
func printFile(certificate *Certificate, name string) error {
|
|
certPEM, keyPEM := certificate.ToPEM()
|
|
return task.Run(context.Background(), func() error {
|
|
return writeFile(certPEM, name+".crt")
|
|
}, func() error {
|
|
return writeFile(keyPEM, name+".key")
|
|
})
|
|
}
|
|
|
|
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))
|
|
}
|