Skip to content

Commit ef07679

Browse files
committed
feat(gm): support GM
1 parent 92ae73a commit ef07679

File tree

169 files changed

+34769
-42
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

169 files changed

+34769
-42
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/spf13/cobra v1.6.1
1515
github.com/spf13/pflag v1.0.5
1616
github.com/stretchr/testify v1.8.0
17+
github.com/tjfoc/gmsm v1.4.2-0.20220114090716-36b992c51540
1718
golang.org/x/net v0.17.0
1819
golang.org/x/sync v0.4.0
1920
golang.org/x/sys v0.14.0
@@ -54,6 +55,7 @@ require (
5455
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
5556
github.com/pmezard/go-difflib v1.0.0 // indirect
5657
github.com/spf13/afero v1.8.0 // indirect
58+
golang.org/x/crypto v0.14.0 // indirect
5759
golang.org/x/oauth2 v0.11.0 // indirect
5860
golang.org/x/term v0.14.0 // indirect
5961
golang.org/x/text v0.14.0 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
319319
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
320320
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
321321
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
322+
github.com/tjfoc/gmsm v1.4.2-0.20220114090716-36b992c51540 h1:Q7nxhP4rDahaXbLofX2fRX1dcEoQRvlJA0Hd2hGgh9k=
323+
github.com/tjfoc/gmsm v1.4.2-0.20220114090716-36b992c51540/go.mod h1:j4INPkHWMrhJb38G+J6W4Tw0AbuN8Thu3PbdVYhVcTE=
322324
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
323325
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
324326
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -340,8 +342,11 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
340342
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
341343
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
342344
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
345+
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
343346
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
344347
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
348+
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
349+
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
345350
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
346351
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
347352
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -405,6 +410,7 @@ golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/
405410
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
406411
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
407412
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
413+
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
408414
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
409415
golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
410416
golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package transport
2+
3+
import (
4+
"context"
5+
"crypto/tls"
6+
"net"
7+
"net/http"
8+
"time"
9+
10+
"github.com/tjfoc/gmsm/gmtls"
11+
)
12+
13+
func NewGMRoundTripper(cfg *gmtls.Config) WrapperFunc {
14+
return func(inner http.RoundTripper) http.RoundTripper {
15+
return &http.Transport{
16+
Proxy: http.ProxyFromEnvironment,
17+
TLSClientConfig: &tls.Config{
18+
InsecureSkipVerify: cfg.InsecureSkipVerify,
19+
},
20+
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
21+
dialer := &net.Dialer{}
22+
conn, err := gmtls.DialWithDialer(dialer, network, addr, cfg)
23+
if err != nil {
24+
return nil, err
25+
}
26+
return conn, nil
27+
},
28+
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
29+
dialer := &net.Dialer{
30+
Timeout: 10 * time.Second,
31+
KeepAlive: 60 * time.Second,
32+
}
33+
conn, err := gmtls.DialWithDialer(dialer, network, addr, cfg)
34+
if err != nil {
35+
return nil, err
36+
}
37+
return conn, nil
38+
},
39+
TLSHandshakeTimeout: 15 * time.Second,
40+
IdleConnTimeout: 30 * time.Second,
41+
}
42+
}
43+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package transport
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/tjfoc/gmsm/gmtls"
8+
"github.com/tjfoc/gmsm/x509"
9+
)
10+
11+
func TestNewGMRoundTripper(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
description string
15+
cfg *gmtls.Config
16+
request *http.Request
17+
expected bool
18+
}{
19+
{
20+
name: "test1",
21+
description: "support both GM and no-GM encryption algorithm",
22+
cfg: &gmtls.Config{
23+
InsecureSkipVerify: true,
24+
},
25+
request: httpNewRequest("GET", "https://sm2test.ovssl.cn", nil, t),
26+
expected: false,
27+
},
28+
{
29+
name: "test2",
30+
description: "support both GM and no-GM encryption algorithm",
31+
cfg: &gmtls.Config{
32+
GMSupport: &gmtls.GMSupport{WorkMode: gmtls.ModeAutoSwitch},
33+
InsecureSkipVerify: true,
34+
},
35+
request: httpNewRequest("GET", "https://sm2test.ovssl.cn", nil, t),
36+
expected: false,
37+
},
38+
{
39+
name: "test3",
40+
description: "support both GM and no-GM encryption algorithm",
41+
cfg: &gmtls.Config{
42+
GMSupport: &gmtls.GMSupport{WorkMode: gmtls.ModeGMSSLOnly},
43+
InsecureSkipVerify: true,
44+
},
45+
request: httpNewRequest("GET", "https://sm2test.ovssl.cn", nil, t),
46+
expected: false,
47+
},
48+
{
49+
name: "test4",
50+
description: "support both GM and no-GM encryption algorithm",
51+
cfg: &gmtls.Config{
52+
GMSupport: &gmtls.GMSupport{WorkMode: gmtls.ModeAutoSwitch},
53+
InsecureSkipVerify: true,
54+
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
55+
for _, v := range rawCerts {
56+
_, err := x509.ParseCertificate(v)
57+
if err != nil {
58+
return err
59+
}
60+
}
61+
return nil
62+
},
63+
},
64+
request: httpNewRequest("GET", "https://sm2test.ovssl.cn", nil, t),
65+
expected: false,
66+
},
67+
{
68+
name: "test5",
69+
description: "support both GM and no-GM encryption algorithm",
70+
cfg: &gmtls.Config{
71+
GMSupport: &gmtls.GMSupport{WorkMode: gmtls.ModeGMSSLOnly},
72+
InsecureSkipVerify: true,
73+
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
74+
for _, v := range rawCerts {
75+
_, err := x509.ParseCertificate(v)
76+
if err != nil {
77+
return err
78+
}
79+
}
80+
return nil
81+
},
82+
},
83+
request: httpNewRequest("GET", "https://sm2test.ovssl.cn", nil, t),
84+
expected: false,
85+
},
86+
87+
{
88+
name: "test6",
89+
description: "don not support GM",
90+
cfg: &gmtls.Config{
91+
InsecureSkipVerify: true,
92+
},
93+
request: httpNewRequest("GET", "https://baidu.com", nil, t),
94+
expected: true,
95+
},
96+
{
97+
name: "test7",
98+
description: "don not support GM",
99+
cfg: &gmtls.Config{
100+
GMSupport: &gmtls.GMSupport{WorkMode: gmtls.ModeAutoSwitch},
101+
InsecureSkipVerify: true,
102+
},
103+
request: httpNewRequest("GET", "https://baidu.com", nil, t),
104+
expected: true,
105+
},
106+
{
107+
name: "test8",
108+
description: "don not support GM",
109+
cfg: &gmtls.Config{
110+
GMSupport: &gmtls.GMSupport{WorkMode: gmtls.ModeGMSSLOnly},
111+
InsecureSkipVerify: true,
112+
},
113+
request: httpNewRequest("GET", "https://baidu.com", nil, t),
114+
expected: true,
115+
},
116+
{
117+
name: "test9",
118+
description: "don not support GM",
119+
cfg: &gmtls.Config{
120+
GMSupport: &gmtls.GMSupport{WorkMode: gmtls.ModeAutoSwitch},
121+
InsecureSkipVerify: true,
122+
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
123+
for _, v := range rawCerts {
124+
_, err := x509.ParseCertificate(v)
125+
if err != nil {
126+
return err
127+
}
128+
}
129+
return nil
130+
},
131+
},
132+
request: httpNewRequest("GET", "https://baidu.com", nil, t),
133+
expected: true,
134+
},
135+
}
136+
137+
for _, testCase := range tests {
138+
t.Run(testCase.name, func(t *testing.T) {
139+
wrapper := NewGMRoundTripper(testCase.cfg)
140+
wrappedTransport := wrapper(http.DefaultTransport)
141+
client := &http.Client{
142+
Transport: wrappedTransport,
143+
}
144+
145+
resp, err := client.Do(testCase.request)
146+
147+
if testCase.expected && err == nil {
148+
t.Errorf("expected error but got none")
149+
}
150+
if !testCase.expected && err != nil {
151+
t.Errorf("expected: %v, but got err: %v", testCase.expected, err)
152+
}
153+
154+
if resp != nil && resp.Body != nil {
155+
resp.Body.Close()
156+
}
157+
})
158+
}
159+
}

pkg/utils/logroundtripper.go renamed to pkg/common/transport/logroundtripper.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package utils
17+
package transport
1818

1919
import (
2020
"bytes"
@@ -37,6 +37,12 @@ var (
3737
serverGRPCEndpointCallCounter uint64
3838
)
3939

40+
func NewLogRoundTripper() WrapperFunc {
41+
return func(rt http.RoundTripper) http.RoundTripper {
42+
return &LogRoundTripper{Rt: rt}
43+
}
44+
}
45+
4046
// LogRoundTripper satisfies the http.RoundTripper interface and is used to
4147
// customize the default http client RoundTripper to allow for logging.
4248
type LogRoundTripper struct {

pkg/utils/logroundtripper_test.go renamed to pkg/common/transport/logroundtripper_test.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package transport
22

33
import (
44
"io"
@@ -20,9 +20,9 @@ func TestRoundTrip(t *testing.T) {
2020
response: &http.Response{
2121
Status: "200 OK",
2222
StatusCode: http.StatusOK,
23-
Proto: "HTTP/1.1",
24-
ProtoMajor: 1,
25-
ProtoMinor: 1,
23+
Proto: "HTTP/2.0",
24+
ProtoMajor: 2,
25+
ProtoMinor: 0,
2626
},
2727
expected: true,
2828
description: "request and response are real entities",
@@ -67,11 +67,11 @@ func TestRoundTrip(t *testing.T) {
6767
name: "test6",
6868
request: httpNewRequest("POST", "https://hub.docker.com/", nil, t),
6969
response: &http.Response{
70-
Status: "404 Not Found",
71-
StatusCode: http.StatusNotFound,
72-
Proto: "HTTP/1.1",
73-
ProtoMajor: 1,
74-
ProtoMinor: 1,
70+
Status: "405 Method Not Allowed",
71+
StatusCode: http.StatusMethodNotAllowed,
72+
Proto: "HTTP/2.0",
73+
ProtoMajor: 2,
74+
ProtoMinor: 0,
7575
},
7676
expected: true,
7777
description: "the method of request is POST and url is real entity",
@@ -80,11 +80,11 @@ func TestRoundTrip(t *testing.T) {
8080
name: "test7",
8181
request: httpNewRequest("PUT", "https://hub.docker.com/", nil, t),
8282
response: &http.Response{
83-
Status: "404 Not Found",
84-
StatusCode: http.StatusNotFound,
85-
Proto: "HTTP/1.1",
86-
ProtoMajor: 1,
87-
ProtoMinor: 1,
83+
Status: "405 Method Not Allowed",
84+
StatusCode: http.StatusMethodNotAllowed,
85+
Proto: "HTTP/2.0",
86+
ProtoMajor: 2,
87+
ProtoMinor: 0,
8888
},
8989
expected: true,
9090
description: "the method of request is PUT and url is real entity",
@@ -93,11 +93,11 @@ func TestRoundTrip(t *testing.T) {
9393
name: "test8",
9494
request: httpNewRequest("DELETE", "https://hub.docker.com/", nil, t),
9595
response: &http.Response{
96-
Status: "404 Not Found",
97-
StatusCode: http.StatusNotFound,
98-
Proto: "HTTP/1.1",
99-
ProtoMajor: 1,
100-
ProtoMinor: 1,
96+
Status: "405 Method Not Allowed",
97+
StatusCode: http.StatusMethodNotAllowed,
98+
Proto: "HTTP/2.0",
99+
ProtoMajor: 2,
100+
ProtoMinor: 0,
101101
},
102102
expected: true,
103103
description: "the method of request is DELETE and url is real entity",
@@ -130,9 +130,9 @@ func TestRoundTrip(t *testing.T) {
130130
response: &http.Response{
131131
Status: "200 OK",
132132
StatusCode: http.StatusOK,
133-
Proto: "HTTP/1.1",
134-
ProtoMajor: 1,
135-
ProtoMinor: 1,
133+
Proto: "HTTP/2.0",
134+
ProtoMajor: 2,
135+
ProtoMinor: 0,
136136
},
137137
expected: true,
138138
description: "the ProtoMajor of customized response is 1",
@@ -143,9 +143,9 @@ func TestRoundTrip(t *testing.T) {
143143
response: &http.Response{
144144
Status: "200 OK",
145145
StatusCode: http.StatusOK,
146-
Proto: "HTTP/1.1",
147-
ProtoMajor: 1,
148-
ProtoMinor: 1,
146+
Proto: "HTTP/2.0",
147+
ProtoMajor: 2,
148+
ProtoMinor: 0,
149149
},
150150
expected: true,
151151
description: "the ProtoMinor of customized response is 1",

pkg/common/transport/transport.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package transport
2+
3+
import (
4+
"net/http"
5+
)
6+
7+
type WrapperFunc func(rt http.RoundTripper) http.RoundTripper
8+
9+
func Wrappers(fns ...WrapperFunc) WrapperFunc {
10+
if len(fns) == 0 {
11+
return nil
12+
}
13+
// optimize the common case of wrapping a possibly nil tr wrapper
14+
// with an additional wrapper
15+
if len(fns) == 2 && fns[0] == nil {
16+
return fns[1]
17+
}
18+
return func(rt http.RoundTripper) http.RoundTripper {
19+
base := rt
20+
for _, fn := range fns {
21+
if fn != nil {
22+
base = fn(base)
23+
}
24+
}
25+
return base
26+
}
27+
}

0 commit comments

Comments
 (0)