Skip to content

Commit c27ba48

Browse files
committed
examples: add TLS transport examples
1 parent 7fc8689 commit c27ba48

20 files changed

Lines changed: 1023 additions & 11 deletions

File tree

examples/features/config/client/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func main() {
3737
// Send request.
3838
rsp, err := clientProxy.SayHello(ctx, req)
3939
if err != nil {
40-
fmt.Println("Say hi err:%v", err)
40+
fmt.Printf("Say hi err:%v\n", err)
4141
return
4242
}
4343
fmt.Printf("Get msg: %s\n", rsp.GetMsg())
@@ -54,7 +54,7 @@ func main() {
5454
// Send request.
5555
rsp, err = clientProxy.SayHello(ctx, req)
5656
if err != nil {
57-
fmt.Println("Say hi err:%v", err)
57+
fmt.Printf("Say hi err:%v\n", err)
5858
return
5959
}
6060
fmt.Printf("Get msg: %s\n", rsp.GetMsg())

examples/features/https/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# HTTPS example
2+
3+
This example demonstrates HTTPS Standard Service in tRPC-Go and provides two
4+
ways to run the client:
5+
6+
- Pure code example (see `client/`).
7+
- YAML-only example (see `clientyaml/`).
8+
9+
The server listens on `9443` with a self-signed certificate (demo only).
10+
11+
## Layout
12+
13+
```
14+
examples/features/https/
15+
├── server/ # Server (9443, https_no_protocol)
16+
├── client/ # Pure code: enable TLS via WithTarget/WithTLS
17+
└── clientyaml/ # YAML-only: enable TLS via YAML
18+
```
19+
20+
## Start the server (9443)
21+
22+
```bash
23+
cd server
24+
go run main.go -conf trpc_go.yaml
25+
```
26+
27+
## Client options
28+
29+
Pick either the pure code client or the YAML-only client.
30+
31+
### Pure code ([client/](./client/))
32+
33+
Explicitly set backend target and TLS in code:
34+
35+
- Case A: `https + ca_cert:none` (insecure).
36+
- Case B: `http + ca_cert:none` (enable TLS via `ca_cert:none`).
37+
38+
Run:
39+
40+
```bash
41+
cd ../client
42+
# Code uses ip://127.0.0.1:9443 + WithTLS(..., "none", ...)
43+
# You will see three logs: A success, B failure (no ca), C success (http+none)
44+
go run main.go
45+
```
46+
47+
Notes (code):
48+
49+
- `protocol: https` alone does not enable TLS; you need a TLS signal:
50+
- `WithTLS("", "", "none", "localhost")`, or
51+
- YAML provides `ca_cert: "none"`/real CA, or
52+
- Port inference (e.g., direct `:9443`).
53+
54+
### YAML-only ([clientyaml/](./clientyaml/))
55+
56+
Three YAML files show common cases:
57+
58+
- `trpc_go.yaml`: `protocol: https` + `ca_cert: "none"`.
59+
- `trpc_go_no_ca.yaml`: `protocol: https`, no `ca_cert`, target port `9443`.
60+
- `trpc_go_http_ca.yaml`: `protocol: http` + `ca_cert: "none"`.
61+
62+
Run:
63+
64+
```bash
65+
cd ../clientyaml
66+
# Case A: protocol:https, no ca_cert (port 9443)
67+
go run main.go -conf trpc_go_no_ca.yaml
68+
# Case B: protocol:http, ca_cert:"none" (port 9443)
69+
go run main.go -conf trpc_go_http_ca.yaml
70+
# Case C: protocol:https, ca_cert:"none" (port 9443)
71+
go run main.go -conf trpc_go.yaml
72+
```
73+
74+
Notes (YAML):
75+
76+
- `dns://host` without port defaults to 80 (no TLS inference).
77+
- Append `:443` (or your TLS port), or
78+
- Provide `ca_cert: "none"`/real CA in YAML.
79+
- Ensure the config is loaded:
80+
- In-service: `trpc.NewServer()` loads automatically.
81+
- Tools: see `clientyaml/main.go` using `LoadGlobalConfig` + `Setup`.
82+
83+
## Expected
84+
85+
- Case A (https, no ca_cert, 9443): success, body `https response body`,
86+
header `reply: https-response-head`.
87+
- Case B (http, ca_cert:none, 9443): success (TLS enabled by `ca_cert:none`).
88+
89+
## FAQ
90+
91+
- Why is `protocol: https` not enough by itself?
92+
- TLS requires a signal (port, CA, or code `WithTLS`).
93+
- Do I have to use 443?
94+
- No. 9443 works in this demo; the key is the target is a TLS service.
95+
- Production tips?
96+
- Provide real `ca_cert` and `tls_server_name` instead of `none`.
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
//
2+
//
3+
// Tencent is pleased to support the open source community by making tRPC available.
4+
//
5+
// Copyright (C) 2023 Tencent.
6+
// All rights reserved.
7+
//
8+
// If you have downloaded a copy of the tRPC source code from Tencent,
9+
// please note that tRPC source code is licensed under the Apache 2.0 License,
10+
// A copy of the Apache 2.0 License is included in this file.
11+
//
12+
//
13+
14+
package main
15+
16+
import (
17+
"context"
18+
"net/http"
19+
20+
"trpc.group/trpc-go/trpc-go/client"
21+
"trpc.group/trpc-go/trpc-go/codec"
22+
thttp "trpc.group/trpc-go/trpc-go/http"
23+
"trpc.group/trpc-go/trpc-go/log"
24+
)
25+
26+
func main() {
27+
// Test 1: protocol: https with ca_cert: "none"
28+
log.Info("=== Test 1: protocol: https with ca_cert: none ===")
29+
httpsWithCaCert()
30+
31+
// Test 2: protocol: https without ca_cert (should work with explicit HTTPS)
32+
log.Info("=== Test 2: protocol: https without ca_cert ===")
33+
httpsWithoutCaCert()
34+
35+
// Test 3: protocol: http with ca_cert: "none" (should also work)
36+
log.Info("=== Test 3: protocol: http with ca_cert: none ===")
37+
httpWithCaCert()
38+
}
39+
40+
// httpsWithCaCert sends an HTTPS POST request using explicit HTTPS protocol with ca_cert: "none".
41+
func httpsWithCaCert() {
42+
// Create a ClientProxy with target and TLS enabled via ca_cert: "none".
43+
httpCli := thttp.NewClientProxy("trpc.app.server.stdhttps",
44+
client.WithSerializationType(codec.SerializationTypeNoop),
45+
client.WithCurrentSerializationType(codec.SerializationTypeNoop),
46+
client.WithTarget("ip://127.0.0.1:9443"),
47+
client.WithTLS("", "", "none", "localhost"),
48+
)
49+
50+
// Create a ClientReqHeader with the specified HTTP method (POST)
51+
reqHeader := &thttp.ClientReqHeader{
52+
Method: http.MethodPost,
53+
}
54+
55+
// Add a custom "request" header to the HTTP request header
56+
reqHeader.AddHeader("request", "https-test-with-ca-cert")
57+
58+
// Create ClientRspHeader to store the response header
59+
rspHead := &thttp.ClientRspHeader{}
60+
61+
// Create a Body containing the request data
62+
req := &codec.Body{Data: []byte("Hello, I am HTTPS client with ca_cert!")}
63+
64+
// Create an empty Body to store the response data
65+
rsp := &codec.Body{}
66+
67+
// Send a HTTPS POST request
68+
if err := httpCli.Post(context.Background(), "/v1/hello", req, rsp,
69+
client.WithReqHead(reqHeader),
70+
client.WithRspHead(rspHead),
71+
); err != nil {
72+
log.Errorf("Error getting HTTPS response with ca_cert: %v", err)
73+
return
74+
}
75+
76+
// Get the "reply" field from the HTTP response header
77+
replyHead := rspHead.Response.Header.Get("reply")
78+
log.Infof("HTTPS with ca_cert - Data: \"%s\", Response head: \"%s\"", string(rsp.Data), replyHead)
79+
}
80+
81+
// httpsWithoutCaCert sends an HTTPS POST request using explicit HTTPS protocol without ca_cert.
82+
// This should work if explicit HTTPS is properly implemented.
83+
func httpsWithoutCaCert() {
84+
// Create a ClientProxy with HTTPS but no ca_cert to test behavior.
85+
httpCli := thttp.NewClientProxy("trpc.app.server.stdhttps-no-ca",
86+
client.WithSerializationType(codec.SerializationTypeNoop),
87+
client.WithCurrentSerializationType(codec.SerializationTypeNoop),
88+
client.WithTarget("ip://127.0.0.1:9443"),
89+
)
90+
91+
// Create a ClientReqHeader with the specified HTTP method (POST)
92+
reqHeader := &thttp.ClientReqHeader{
93+
Method: http.MethodPost,
94+
}
95+
96+
// Add a custom "request" header to the HTTP request header
97+
reqHeader.AddHeader("request", "https-test-without-ca-cert")
98+
99+
// Create ClientRspHeader to store the response header
100+
rspHead := &thttp.ClientRspHeader{}
101+
102+
// Create a Body containing the request data
103+
req := &codec.Body{Data: []byte("Hello, I am HTTPS client without ca_cert!")}
104+
105+
// Create an empty Body to store the response data
106+
rsp := &codec.Body{}
107+
108+
// Send a HTTPS POST request
109+
if err := httpCli.Post(context.Background(), "/v1/hello", req, rsp,
110+
client.WithReqHead(reqHeader),
111+
client.WithRspHead(rspHead),
112+
); err != nil {
113+
log.Errorf("Error getting HTTPS response without ca_cert: %v", err)
114+
return
115+
}
116+
117+
// Get the "reply" field from the HTTP response header
118+
replyHead := rspHead.Response.Header.Get("reply")
119+
log.Infof("HTTPS without ca_cert - Data: \"%s\", Response head: \"%s\"", string(rsp.Data), replyHead)
120+
}
121+
122+
// httpWithCaCert sends an HTTPS POST request using HTTP protocol with ca_cert: "none".
123+
// This should work because ca_cert triggers HTTPS inference.
124+
func httpWithCaCert() {
125+
// Create a ClientProxy with HTTP protocol but TLS enabled via ca_cert: "none".
126+
httpCli := thttp.NewClientProxy("trpc.app.server.stdhttps-http-ca",
127+
client.WithSerializationType(codec.SerializationTypeNoop),
128+
client.WithCurrentSerializationType(codec.SerializationTypeNoop),
129+
client.WithTarget("ip://127.0.0.1:9443"),
130+
client.WithTLS("", "", "none", "localhost"),
131+
)
132+
133+
// Create a ClientReqHeader with the specified HTTP method (POST)
134+
reqHeader := &thttp.ClientReqHeader{
135+
Method: http.MethodPost,
136+
}
137+
138+
// Add a custom "request" header to the HTTP request header
139+
reqHeader.AddHeader("request", "http-test-with-ca-cert")
140+
141+
// Create ClientRspHeader to store the response header
142+
rspHead := &thttp.ClientRspHeader{}
143+
144+
// Create a Body containing the request data
145+
req := &codec.Body{Data: []byte("Hello, I am HTTP client with ca_cert!")}
146+
147+
// Create an empty Body to store the response data
148+
rsp := &codec.Body{}
149+
150+
// Send a HTTPS POST request
151+
if err := httpCli.Post(context.Background(), "/v1/hello", req, rsp,
152+
client.WithReqHead(reqHeader),
153+
client.WithRspHead(rspHead),
154+
); err != nil {
155+
log.Errorf("Error getting HTTP response with ca_cert: %v", err)
156+
return
157+
}
158+
159+
// Get the "reply" field from the HTTP response header
160+
replyHead := rspHead.Response.Header.Get("reply")
161+
log.Infof("HTTP with ca_cert - Data: \"%s\", Response head: \"%s\"", string(rsp.Data), replyHead)
162+
}

0 commit comments

Comments
 (0)