Skip to content

Commit 9698903

Browse files
authored
chore: optimize hijack ca format (#3418)
* chore: optimize hijack ca format Signed-off-by: Jim Ma <majinjing3@gmail.com> * chore: fix cert leaf Signed-off-by: Jim Ma <majinjing3@gmail.com> * fix: unit test Signed-off-by: Jim Ma <majinjing3@gmail.com> --------- Signed-off-by: Jim Ma <majinjing3@gmail.com>
1 parent 89e06a8 commit 9698903

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

client/config/peerhost.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -920,8 +920,8 @@ func (r *Regexp) MarshalYAML() (any, error) {
920920

921921
// HijackConfig represents how dfdaemon hijacks http requests.
922922
type HijackConfig struct {
923-
Cert string `yaml:"cert" mapstructure:"cert"`
924-
Key string `yaml:"key" mapstructure:"key"`
923+
Cert types.PEMContent `yaml:"cert" mapstructure:"cert"`
924+
Key types.PEMContent `yaml:"key" mapstructure:"key"`
925925
Hosts []*HijackHost `yaml:"hosts" mapstructure:"hosts"`
926926
SNI []*TCPListenOption `yaml:"sni" mapstructure:"sni"`
927927
}

client/config/peerhost_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,8 @@ func TestPeerHostOption_Load(t *testing.T) {
470470
},
471471
},
472472
HijackHTTPS: &HijackConfig{
473-
Cert: "./testdata/certs/sca.crt",
474-
Key: "./testdata/certs/sca.key",
473+
Cert: types.PEMContent(_cert),
474+
Key: types.PEMContent(_key),
475475
Hosts: []*HijackHost{
476476
{
477477
Regx: hijackExp,

client/config/testdata/certs/sca.crt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ A5l000dtHekhk+DO2tjQgEKg5+EYMYoki5mEkSbyHkMMY8D6w5A130fpw10ZeN1z
1717
B/v/1PiVkZfu1kbnTZICQDsb4xI/2Sw2x0qKXp1oYzIDt8fZATNJgWhzv47xLLXF
1818
XQM7Yj0HQ3txAi6qOMDw1sYf/TEc1k4VC9J//QJb5/kNnWcAheLPCm3D1+CnAxcD
1919
vL928p4GmUIGbzxm3/WbWfLosSwxq5y4P5bbEd3niM4=
20-
-----END CERTIFICATE-----
20+
-----END CERTIFICATE-----

client/config/testdata/certs/sca.key

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ fbR5XmFsuzmdL0zRIt6+mtDjfqHHYA2avzwvRaBWVprzS8/ISTqJSEs/NWSYuAsP
2424
tjPw2QKBgQCB+sS2lio/sTAQzsYTe/GNmxsL1lKO+yRsTPRRjzcm3ZdOsPgkFDx/
2525
ZCL9Lsp7TqOLOghLGdYj9a45GrXwmEeJo5P9c1y+G9PSzFDMBUyseWmDvrcvYwWo
2626
JMfrfs6pHtZ828AbnT2kfnFv6zok2ns6vE2gme/a9Z/RCjVXyJwF5w==
27-
-----END RSA PRIVATE KEY-----
27+
-----END RSA PRIVATE KEY-----

client/daemon/proxy/proxy_manager.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,20 @@ func NewProxyManager(peerHost *schedulerv1.PeerHost, peerTaskManager peer.TaskMa
9898
if r.Direct {
9999
method = "directly"
100100
}
101-
scheme := ""
101+
prompt := ""
102102
if r.UseHTTPS {
103-
scheme = "and force https"
103+
prompt = " and force https"
104104
}
105-
logger.Infof("[%d] proxy %s %s %s", i+1, r.Regx, method, scheme)
105+
logger.Infof("[%d] proxy %s %s%s", i+1, r.Regx, method, prompt)
106106
}
107107
}
108108

109109
if hijackHTTPS != nil {
110110
options = append(options, WithHTTPSHosts(hijackHTTPS.Hosts...))
111111
if hijackHTTPS.Cert != "" && hijackHTTPS.Key != "" {
112-
cert, err := certFromFile(hijackHTTPS.Cert, hijackHTTPS.Key)
112+
cert, err := certFromFile(string(hijackHTTPS.Cert), string(hijackHTTPS.Key))
113113
if err != nil {
114-
return nil, fmt.Errorf("cert from file: %w", err)
114+
return nil, fmt.Errorf("load cert error: %w", err)
115115
}
116116
if cert.Leaf != nil && cert.Leaf.IsCA {
117117
logger.Debugf("hijack https request with CA <%s>", cert.Leaf.Subject.CommonName)
@@ -174,13 +174,14 @@ func (pm *proxyManager) Watch(opt *config.ProxyOption) {
174174
}
175175
}
176176

177-
func certFromFile(certFile string, keyFile string) (*tls.Certificate, error) {
177+
func certFromFile(certPEM string, keyPEM string) (*tls.Certificate, error) {
178178
// cert.Certificate is a chain of one or more certificates, leaf first.
179-
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
179+
cert, err := tls.X509KeyPair([]byte(certPEM), []byte(keyPEM))
180180
if err != nil {
181181
return nil, fmt.Errorf("load cert: %w", err)
182182
}
183-
logger.Infof("use self-signed certificate (%s, %s) for https hijacking", certFile, keyFile)
183+
184+
logger.Infof("use self-signed certificate for https hijacking")
184185

185186
// leaf is CA cert or server cert
186187
leaf, err := x509.ParseCertificate(cert.Certificate[0])

0 commit comments

Comments
 (0)