-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
91 lines (84 loc) · 2.13 KB
/
Copy pathutils.go
File metadata and controls
91 lines (84 loc) · 2.13 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package rproxy
import (
"bytes"
cryptorand "crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"math/rand"
"os"
"os/exec"
"path"
"runtime"
"time"
)
// GetCommandStdout 获取命令行输出
func GetCommandStdout(name string, arg ...string) (string, error) {
cmd := exec.Command(name, arg...)
buf := bytes.NewBuffer([]byte{})
cmd.Stdout = buf
err := cmd.Run()
if err != nil {
return "", err
}
return buf.String(), err
}
// GetAppDatadir 获取当前系统的 app 数据目录
func GetAppDatadir() string {
if runtime.GOOS == "windows" {
return path.Join(os.Getenv("APPDATA"), AppDatapath)
} else {
return path.Join(os.Getenv("HOME"), "/Library/Containers", AppDatapath)
}
}
// FileExists 文件是否存在
func FileExists(f string) bool {
_, err := os.Stat(f)
return err == nil || os.IsExist(err)
}
// GenerateCA 生成根证书
func GenerateCA() (ca []byte, key []byte, err error) {
priv, err := rsa.GenerateKey(cryptorand.Reader, 2048)
if err != nil {
return
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(rand.Int63()),
Subject: pkix.Name{
CommonName: "rockrabbit",
Country: []string{"China"},
Organization: []string{"Rproxy"},
Province: []string{"Shandong"},
Locality: []string{"Jinan"},
},
NotBefore: time.Now().AddDate(0, -1, 0),
NotAfter: time.Now().AddDate(20, 0, 0),
BasicConstraintsValid: true,
IsCA: true,
MaxPathLen: 2,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
EmailAddresses: []string{"2896865355@qq.com"},
}
derBytes, err := x509.CreateCertificate(cryptorand.Reader, tmpl, tmpl, &priv.PublicKey, priv)
if err != nil {
return
}
certBlock := &pem.Block{
Type: "CERTIFICATE",
Bytes: derBytes,
}
ca = pem.EncodeToMemory(certBlock)
privBytes := x509.MarshalPKCS1PrivateKey(priv)
if err != nil {
return
}
keyBlock := &pem.Block{
Type: "EC PRIVATE KEY",
Bytes: privBytes,
}
key = pem.EncodeToMemory(keyBlock)
return
}