forked from soyking/e3w
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e3ch.go
53 lines (48 loc) · 1.25 KB
/
e3ch.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
package e3ch
import (
"crypto/tls"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/pkg/transport"
"github.com/soyking/e3ch"
"github.com/soyking/e3w/conf"
)
func NewE3chClient(config *conf.Config) (*client.EtcdHRCHYClient, error) {
var tlsConfig *tls.Config
var err error
if config.CertFile != "" && config.KeyFile != "" && config.CAFile != "" {
tlsInfo := transport.TLSInfo{
CertFile: config.CertFile,
KeyFile: config.KeyFile,
TrustedCAFile: config.CAFile,
}
tlsConfig, err = tlsInfo.ClientConfig()
if err != nil {
return nil, err
}
}
clt, err := clientv3.New(clientv3.Config{
Endpoints: config.EtcdEndPoints,
Username: config.EtcdUsername,
Password: config.EtcdPassword,
TLS: tlsConfig,
})
if err != nil {
return nil, err
}
client, err := client.New(clt, config.EtcdRootKey, config.DirValue)
if err != nil {
return nil, err
}
return client, client.FormatRootKey()
}
func CloneE3chClient(username, password string, client *client.EtcdHRCHYClient) (*client.EtcdHRCHYClient, error) {
clt, err := clientv3.New(clientv3.Config{
Endpoints: client.EtcdClient().Endpoints(),
Username: username,
Password: password,
})
if err != nil {
return nil, err
}
return client.Clone(clt), nil
}