Skip to content

Commit

Permalink
tiny fix
Browse files Browse the repository at this point in the history
  • Loading branch information
virusdefender committed Aug 16, 2023
1 parent 080e718 commit 9243a66
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
go-version: "1.21.0"

- name: Build
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.crt
*.key
.idea/
certs/
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/virusdefender/cert-copier
module github.com/virusdefender/copy-cert

go 1.17
go 1.21
35 changes: 20 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"slices"
"strings"
"time"
)

type certPair struct {
Expand All @@ -25,7 +27,7 @@ type certPair struct {

func getCertsFromNetwork(addr string) ([]*x509.Certificate, error) {
conf := &tls.Config{
InsecureSkipVerify: true,
InsecureSkipVerify: false,
}
conn, err := tls.Dial("tcp", addr, conf)
if err != nil {
Expand All @@ -35,20 +37,14 @@ func getCertsFromNetwork(addr string) ([]*x509.Certificate, error) {
return conn.ConnectionState().PeerCertificates, nil
}

func reverse(s []*certPair) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}

func makeCerts(originCerts []*x509.Certificate) ([]*certPair, error) {
certs := make([]*certPair, len(originCerts))
// the origin order: website cert, intermediate ca, root ca
for idx, cert := range originCerts {
log.Printf("got cert: %s", cert.Subject.CommonName)
certs[idx] = &certPair{originCert: cert}
}
reverse(certs)
slices.Reverse(certs)

for idx, pair := range certs {
var pub interface{}
Expand Down Expand Up @@ -120,23 +116,31 @@ func main() {
if err != nil {
log.Fatal(err)
}
reverse(newCerts)
slices.Reverse(newCerts)

dir := filepath.Join("certs", time.Now().Local().Format("2006_01_02_15_04_05"))
err = os.MkdirAll(dir, 0o744)
if err != nil {
log.Fatal(err)
}

bundleCert, err := os.OpenFile("bundle.crt", os.O_WRONLY|os.O_CREATE, 0o744)
bundleCert, err := os.OpenFile(filepath.Join(dir, "bundle.crt"), os.O_WRONLY|os.O_CREATE, 0o744)
if err != nil {
log.Fatal(err)
}
defer bundleCert.Close()
bundleKey, err := os.OpenFile("bundle.key", os.O_WRONLY|os.O_CREATE, 0o744)
bundleKey, err := os.OpenFile(filepath.Join(dir, "bundle.key"), os.O_WRONLY|os.O_CREATE, 0o744)
if err != nil {
log.Fatal(err)
}
defer bundleKey.Close()

for _, pair := range newCerts {
log.Printf("going to write new cert and key: %s", pair.newCert.Subject.CommonName)
pathBase := fileNameRegex.ReplaceAllString(pair.newCert.Subject.CommonName, "_")
err = ioutil.WriteFile(pathBase+".crt", pair.newCertPem, 0o744)
// 担心星号在 Windows 上是不合法的文件名(当然我也没测试),但是被替换为下换线又很奇怪,所以替换成 __wildcard__
pathBase := strings.ReplaceAll(pair.newCert.Subject.CommonName, "*", "__wildcard__")
pathBase = fileNameRegex.ReplaceAllString(pathBase, "_")
err = os.WriteFile(filepath.Join(dir, pathBase+".crt"), pair.newCertPem, 0o744)
if err != nil {
log.Fatal(err)
}
Expand All @@ -145,7 +149,7 @@ func main() {
log.Fatal(err)
}

err = ioutil.WriteFile(pathBase+".key", pair.privPem, 0o744)
err = os.WriteFile(filepath.Join(dir, pathBase+".key"), pair.privPem, 0o744)
if err != nil {
log.Fatal(err)
}
Expand All @@ -154,4 +158,5 @@ func main() {
log.Fatal(err)
}
}
log.Printf("certs save to %s", dir)
}

0 comments on commit 9243a66

Please sign in to comment.