金慶的專欄
C++博客
::
首頁
::
新隨筆
::
聯系
::
聚合
::
管理
::
423 隨筆 :: 0 文章 :: 454 評論 :: 0 Trackbacks
公告
我的隨筆
我的評論
我參與的隨筆
留言簿
(12)
給我留言
查看公開留言
查看私人留言
隨筆分類
(502)
1. C/C++(166)
(rss)
2. 網游開發(108)
(rss)
3. Golang(20)
(rss)
4. Linux/Unix(30)
(rss)
5. 軟工與管理(44)
(rss)
6. Python(23)
(rss)
7. Erlang(18)
(rss)
8. Rust(16)
(rss)
9. 其它(77)
(rss)
隨筆檔案
(423)
2023年1月 (1)
2022年11月 (1)
2022年10月 (2)
2022年9月 (1)
2022年4月 (6)
2022年1月 (2)
2021年12月 (4)
2021年11月 (6)
2021年10月 (2)
2021年9月 (2)
2021年8月 (7)
2021年7月 (2)
2021年5月 (2)
2021年3月 (1)
2021年2月 (2)
2021年1月 (1)
2020年12月 (1)
2020年10月 (1)
2020年9月 (5)
2020年8月 (1)
2020年7月 (1)
2020年6月 (1)
2020年4月 (2)
2020年3月 (3)
2020年2月 (3)
2020年1月 (1)
2019年12月 (1)
2019年9月 (2)
2019年4月 (2)
2019年1月 (1)
2018年12月 (1)
2018年11月 (3)
2018年10月 (1)
2018年9月 (3)
2018年8月 (3)
2018年7月 (2)
2018年6月 (4)
2018年5月 (4)
2018年4月 (4)
2018年3月 (1)
2018年1月 (2)
2017年12月 (2)
2017年11月 (3)
2017年10月 (3)
2017年8月 (7)
2017年7月 (1)
2017年6月 (1)
2017年5月 (3)
2017年4月 (3)
2017年3月 (3)
2017年2月 (2)
2017年1月 (2)
2016年12月 (5)
2016年11月 (2)
2016年10月 (2)
2016年9月 (1)
2016年8月 (6)
2016年7月 (3)
2016年6月 (2)
2016年5月 (4)
2016年4月 (2)
2016年3月 (2)
2016年1月 (3)
2015年12月 (2)
2015年11月 (2)
2015年10月 (1)
2015年8月 (2)
2015年7月 (1)
2015年6月 (1)
2015年5月 (4)
2015年4月 (3)
2015年3月 (4)
2015年2月 (5)
2015年1月 (4)
2014年12月 (3)
2014年11月 (3)
2014年10月 (2)
2014年9月 (3)
2014年8月 (1)
2014年4月 (4)
2014年3月 (1)
2014年2月 (4)
2014年1月 (5)
2013年12月 (5)
2013年11月 (5)
2013年9月 (2)
2013年8月 (2)
2013年7月 (2)
2013年6月 (2)
2013年5月 (1)
2013年1月 (2)
2012年12月 (1)
2012年11月 (1)
2012年9月 (1)
2012年8月 (3)
2012年7月 (2)
2012年6月 (1)
2012年4月 (3)
2012年3月 (2)
2012年2月 (3)
2012年1月 (2)
2011年11月 (2)
2011年10月 (3)
2011年9月 (2)
2011年8月 (2)
2011年7月 (3)
2011年6月 (2)
2011年5月 (3)
2011年1月 (2)
2010年12月 (1)
2010年11月 (2)
2010年10月 (2)
2010年9月 (3)
2010年8月 (2)
2010年7月 (3)
2010年6月 (1)
2010年5月 (3)
2010年4月 (3)
2010年3月 (5)
2010年2月 (4)
2010年1月 (4)
2009年12月 (2)
2009年11月 (3)
2009年10月 (4)
2009年9月 (3)
2009年8月 (2)
2009年7月 (4)
2009年6月 (1)
2009年5月 (3)
2009年4月 (4)
2009年3月 (2)
2009年2月 (5)
2009年1月 (1)
2008年12月 (7)
2008年11月 (4)
2008年10月 (1)
2008年9月 (3)
2008年8月 (4)
2008年7月 (3)
2008年6月 (4)
2008年5月 (6)
2008年4月 (7)
2008年3月 (6)
2008年1月 (5)
2007年12月 (7)
2007年11月 (4)
2007年10月 (5)
2007年9月 (6)
2007年8月 (8)
2007年7月 (5)
相冊
公告照片
搜索
積分與排名
積分 - 659044
排名 - 25
最新評論
1.?re: boost::asio::spawn 將一統C++網絡庫
asio 成為C++首選網絡庫
--linda
2.?re: log4cxx中文輸出錯誤補丁
評論內容較長,點擊標題查看
--金慶
3.?re: mingw編譯OrzNet
能發送一個mingw編譯好的OrzNet庫給我嗎? liuweiqcxy@163.com
謝謝!
--劉威
4.?re: log4cxx中文輸出錯誤補丁
評論內容較長,點擊標題查看
--bigbad
5.?re: log4cxx中文輸出錯誤補丁
評論內容較長,點擊標題查看
--bigbad
閱讀排行榜
1.?"multiple definition of" 錯誤(11054)
2.?SVN中邪惡的replace(10972)
3.?VS2005編譯libevent(10429)
4.?混音算法的學習與研究(10231)
5.?C調用lua腳本的效率測試(9020)
評論排行榜
1.?VC6正在被拋棄(35)
2.?VS2005編譯libevent(21)
3.?"multiple definition of" 錯誤(18)
4.?C++引用優于指針(17)
5.?ACE與ASIO之間關于Socket編程的比較(16)
grpc加TLS加密和令牌認證
grpc加TLS加密和令牌認證
(金慶的專欄 2018.11)
用 golang 創建 grpc 服務,開啟 TLS 加密,并采用令牌認證。
然后用 C++ 和 golang 分別創建客戶端連接服務器。
參考:
https://segmentfault.com/a/1190000007933303
服務器
import (
...
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
func main() {
listen, err := net.Listen("tcp", ":12345")
if err != nil {
grpclog.Fatalf("failed to listen: %v", err)
}
// TLS認證
creds, err := credentials.NewServerTLSFromFile("keys/server.crt", "keys/server.key")
if err != nil {
grpclog.Fatalf("Failed to generate credentials %v", err)
}
// 實例化grpc Server, 并開啟TLS認證
s := grpc.NewServer(grpc.Creds(creds),
grpc_auth.UnaryServerInterceptor(auth.Authenticate),
grpc_auth.StreamServerInterceptor(auth.Authenticate))
// 注冊HelloService
pb.RegisterHelloServer(s, HelloService)
grpclog.Println("Listen on " + Address + " with TLS")
s.Serve(listen)
}
其中 server.key 是私鑰,server.crt 是自簽名證書,如下生成:
$ openssl genrsa -out server.key 2048
$ openssl req -new -x509 -sha256 -key server.key \
-out server.crt -days 36500 \
-subj /C=CN/ST=Shanghai/L=Songjiang/O=ztgame/OU=tech/CN=mydomain.ztgame.com/emailAddress=myname@ztgame.com
查看證書文件
$ openssl x509 -in server.crt -noout -text
`auth.Authenticate` 如下,作為 interceptor, 對每個請求進行令牌驗證。
package auth
import (
"context"
"sync"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
// from token.yaml file
var tokenToAppName = &sync.Map{}
func init() {
tokenToAppName.Store("test", "test")
}
// XXX load tokenToAppName from file
// Authenticate checks that a token exists and is valid.
// It removes the token from the context and
// stores the app name of the token in the returned context
func Authenticate(ctx context.Context) (context.Context, error) {
token, err := extractHeader(ctx, "authorization-token")
if err != nil {
return ctx, err
}
// Remove token from headers from here on
ctx = purgeHeader(ctx, "authorization-token")
valAppName, ok := tokenToAppName.Load(token)
if !ok {
return ctx, status.Errorf(codes.Unauthenticated, "no app for token '%s'", token)
}
appName := valAppName.(string)
return context.WithValue(ctx, keyAppName{}, appName), nil
}
func extractHeader(ctx context.Context, header string) (string, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return "", status.Error(codes.Unauthenticated, "no headers in request")
}
authHeaders, ok := md[header]
if !ok {
return "", status.Error(codes.Unauthenticated, "no header in request")
}
if len(authHeaders) != 1 {
return "", status.Error(codes.Unauthenticated, "more than 1 header in request")
}
return authHeaders[0], nil
}
func purgeHeader(ctx context.Context, header string) context.Context {
md, _ := metadata.FromIncomingContext(ctx)
mdCopy := md.Copy()
mdCopy[header] = nil
return metadata.NewIncomingContext(ctx, mdCopy)
}
type keyAppName struct{}
// GetAppName can be used to extract app name stored in a context.
func GetAppName(ctx context.Context) string {
// Authenticate()之后必然存在app name
return ctx.Value(keyAppName{}).(string)
}
`tokenToAppName` 是一個map, 將合法的令牌映射為應用名。
每個應用(即用戶)分配一個令牌,根據令牌可查到該用戶是否合法,以及用戶的其他信息。
這里只需要應用名。
每個請求將調用 `Authenticate()`, 該方法將從 http 頭獲取請求的令牌,查找對應的應用名,
ctx 中將刪除令牌,替換成應用名。
`GetAppName()`將從 ctx 中獲取應用名。
服務方法實現如下:
func (s MailServer) Get(ctx context.Context, r *pb.GetRequest) (*pb.GetResponse, error) {
app := auth.GetAppName(ctx)
body, err := db.NewGetter(app).GetMailBody(r.MailIndex)
return &pb.GetResponse{
Result: getResult(err),
Body: body,
}, nil
}
先獲取應用名,然后根據應用名獲取相應的數據返回。
客戶端
golang
// Create the client TLS credentials
creds, err := credentials.NewClientTLSFromFile("key/server.crt", "mydomain.ztgame.com")
if err != nil {
panic(fmt.Errorf("could not load tls cert: %s", err))
}
// We don't need to error here, as this creates a pool and connections
// will happen later
conn, _ := grpc.Dial(
serviceURL,
grpc.WithTransportCredentials(creds),
grpc.WithPerRPCCredentials(auth.TokenAuth{
Token: "test",
}))
cli := pb.NewMailClient(conn)
客戶端只需要 server.crt, 其中包含服務器的公鑰。
NewClientTLSFromFile() 的第2個參數是個域名,是 server.crt 中的域名。
目前測試階段還沒有正式域名設置,所以輸入一個指定域名用于驗證 server.crt 中的域名。
生產環境運行時,應該不需要這個域名,可以直接查詢 DNS 進行驗證。
Dial() 輸入一個 WithPerRPCCredentials 用于令牌驗證。
auth.TokenAuth 需要實現 PerRPCCredentials 接口:
package auth
import (
"context"
)
type TokenAuth struct {
Token string
}
func (t TokenAuth) GetRequestMetadata(ctx context.Context, in ...string) (map[string]string, error) {
return map[string]string{
"authorization-token": t.Token,
}, nil
}
func (TokenAuth) RequireTransportSecurity() bool {
return true
}
"authorization-token" 是客戶端和服務器約定好的http認證頭字符串。
C++
C++ 端的客戶端代碼比golang的稍復雜,因為 grpc C++ 庫沒有 grpc-go 成熟。
代碼參照 grpc 示例 greeter_async_client2.cc:
int main(int argc, char** argv) {
grpc::SslCredentialsOptions ssl_options;
ssl_options.pem_root_certs = SERVER_CRT;
// Create a default SSL ChannelCredentials object.
auto channel_creds = grpc::SslCredentials(ssl_options);
grpc::ChannelArguments cargs;
cargs.SetSslTargetNameOverride("gamemail.ztgame.com"); // 如果加了 DNS 就不用這個了
auto call_creds = grpc::MetadataCredentialsFromPlugin(
std::unique_ptr<grpc::MetadataCredentialsPlugin>(new TokenAuthenticator(TOKEN)));
auto compsited_creds = grpc::CompositeChannelCredentials(channel_creds, call_creds);
// Create a channel using the credentials created in the previous step.
auto channel = grpc::CreateCustomChannel("1.2.3.4:8000", compsited_creds, cargs);
// Instantiate the client.
MailClient tester(channel);
...
return 0;
}
因為 C++ 沒有提供從文件讀取 server.crt 的接口,所以在此直接用了一個常量字符串:
ssl_options.pem_root_certs = SERVER_CRT;
SERVER_CRT 定義如下:
// server.crt 的內容
const char SERVER_CRT[] = R"(
-----BEGIN CERTIFICATE-----
TjERMA8GA1UECAwIU2hhbmdoYWkxEjAQBgNVBAcMCVNvbmdqaWFuZzEPMA0GA1UE
...
E6v50RCQgtWGmna+oy1I2UTVABdjBFnyKPEuz106mBfOhT6cg80hBHVgrV7sLHq8
76QolJm8yzZPL1qpiO4dKHHsCP6R
-----END CERTIFICATE-----
)";
TokenAuthenticator 定義如下,是個自定義認證插件:
// TokenAuthenticator 用來支持令牌認證
// https://grpc.io/docs/guides/auth.html
class TokenAuthenticator : public grpc::MetadataCredentialsPlugin {
public:
TokenAuthenticator(const std::string& token) : token_(token) {}
grpc::Status GetMetadata(
grpc::string_ref service_url, grpc::string_ref method_name,
const grpc::AuthContext& channel_auth_context,
std::multimap<grpc::string, grpc::string>* metadata) override {
metadata->insert(std::make_pair("authorization-token", token_));
return grpc::Status::OK;
}
private:
std::string token_;
};
posted on 2018-11-26 10:39
金慶
閱讀(2330)
評論(0)
編輯
收藏
引用
只有注冊用戶
登錄
后才能發表評論。
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Powered by:
C++博客
Copyright © 金慶
久久国产精品二国产精品
|
亚洲精品无码久久一线
|
久久久艹
|
伊人久久大香线蕉av不卡
|
久久精品国产亚洲精品2020
|
久久精品国产亚洲沈樵
|
欧美日韩精品久久久免费观看
|
国产毛片欧美毛片久久久
|
久久99毛片免费观看不卡
|
久久综合久久伊人
|
99久久精品午夜一区二区
|
中文字幕无码久久人妻
|
99久久成人国产精品免费
|
99久久国产宗和精品1上映
|
久久99国产精品久久久
|
偷窥少妇久久久久久久久
|
国产亚洲美女精品久久久
|
狠狠色婷婷综合天天久久丁香
|
欧美一区二区久久精品
|
久久狠狠色狠狠色综合
|
久久婷婷五月综合色高清
|
亚洲精品NV久久久久久久久久
|
久久国产精品二国产精品
|
99久久亚洲综合精品网站
|
国内精品久久久久久99
|
天天躁日日躁狠狠久久
|
久久精品成人欧美大片
|
久久久久久免费视频
|
亚洲一级Av无码毛片久久精品
|
国产精品熟女福利久久AV
|
一本一道久久精品综合
|
久久久91精品国产一区二区三区
|
无码人妻精品一区二区三区久久
|
国产精品99久久久精品无码
|
亚洲?V乱码久久精品蜜桃
|
亚洲精品国精品久久99热
|
久久青青色综合
|
久久精品国产亚洲av麻豆蜜芽
|
久久婷婷五月综合97色直播
|
精品久久久一二三区
|
亚洲精品无码久久千人斩
|