File: certdir.go

package info (click to toggle)
golang-github-anacrolix-missinggo 2.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 836 kB
  • sloc: makefile: 4
file content (46 lines) | stat: -rw-r--r-- 909 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package missinggo

import (
	"crypto/tls"
	"log"
	"os"
	"path/filepath"
	"strings"
)

func LoadCertificateDir(dir string) (certs []tls.Certificate, err error) {
	d, err := os.Open(dir)
	if err != nil {
		return
	}
	defer d.Close()
	const defaultPEMFile = "default.pem"
	if p := filepath.Join(dir, defaultPEMFile); FilePathExists(p) {
		cert, err := tls.LoadX509KeyPair(p, p)
		if err == nil {
			certs = append(certs, cert)
		} else {
			log.Printf("error loading default certicate: %s", err)
		}
	}
	files, err := d.Readdir(-1)
	if err != nil {
		return
	}
	for _, f := range files {
		if f.Name() == defaultPEMFile {
			continue
		}
		if !strings.HasSuffix(f.Name(), ".pem") {
			continue
		}
		p := filepath.Join(dir, f.Name())
		cert, err := tls.LoadX509KeyPair(p, p)
		if err != nil {
			log.Printf("error loading key pair from %q: %s", p, err)
			continue
		}
		certs = append(certs, cert)
	}
	return
}