-
Notifications
You must be signed in to change notification settings - Fork 25
/
config.go
107 lines (97 loc) · 2.48 KB
/
config.go
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package utils
import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/pkg/errors"
"go.uber.org/zap"
"io/ioutil"
"os"
"path/filepath"
"github.com/scryinfo/dot/dot"
"github.com/scryinfo/scryg/sutils/sfile"
)
func GetFullPathFile(file string) string {
if filepath.IsAbs(file) {
return file
}
res := ""
for {
ex, err := os.Executable()
if err != nil {
dot.Logger().Errorln("connsImp", zap.Error(err))
res = ""
break
}
ex = filepath.Dir(ex)
temp := filepath.Join(ex, file)
if sfile.ExistFile(temp) {
res = temp
break
} else { //try find file from the current path
temp, err = os.Getwd()
if err != nil {
dot.Logger().Errorln("connsImp", zap.Error(err))
res = ""
break
}
temp = filepath.Join(temp, file)
if sfile.ExistFile(temp) {
res = temp
break
}
}
break
}
return res
}
func GetTlsConfig(conf *TlsConfig) (*tls.Config, error) {
if len(conf.CaPem) > 0 && len(conf.Key) > 0 && len(conf.Pem) > 0 { //both tls
caPemFile := GetFullPathFile(conf.CaPem)
if len(caPemFile) < 1 {
return nil, errors.New("the caPem is not empty, and can not find the file: " + conf.CaPem)
}
keyFile := GetFullPathFile(conf.Key)
if len(keyFile) < 1 {
return nil, errors.New("the Key is not empty, and can not find the file: " + conf.Key)
}
pemFile := GetFullPathFile(conf.Pem)
if len(pemFile) < 1 {
return nil, errors.New("the Pem is not empty, and can not find the file: " + conf.Pem)
}
pool := x509.NewCertPool()
{
caCrt, err := ioutil.ReadFile(caPemFile)
if err != nil {
return nil, errors.WithStack(err)
}
if !pool.AppendCertsFromPEM(caCrt) {
return nil, errors.New("credentials: failed to append certificates")
}
}
cert, err := tls.LoadX509KeyPair(pemFile, keyFile)
if err != nil {
return nil, errors.WithStack(err)
}
return &tls.Config{
ServerName: conf.ServerNameOverride,
Certificates: []tls.Certificate{cert},
RootCAs: pool, //ClientConn, use the RootCAs
}, nil
} else if len(conf.Pem) > 0 { //just server
pemFile := GetFullPathFile(conf.Pem)
if len(pemFile) < 1 {
return nil, errors.New("the Pem is not empty, and can not find the file: " + conf.Pem)
}
b, err := ioutil.ReadFile(pemFile)
if err != nil {
return nil, err
}
cp := x509.NewCertPool()
if !cp.AppendCertsFromPEM(b) {
return nil, fmt.Errorf("credentials: failed to append certificates")
}
return &tls.Config{ServerName: conf.ServerNameOverride, RootCAs: cp}, nil
}
return nil, nil
}