Skip to content

Commit

Permalink
增加安全传输插件
Browse files Browse the repository at this point in the history
  • Loading branch information
osgochina committed Dec 20, 2021
1 parent 8842147 commit f85abb8
Show file tree
Hide file tree
Showing 16 changed files with 607 additions and 3 deletions.
5 changes: 5 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## 更新日志

### v0.2.0
1. 支持`ProtoBuf`协议.
2. 增加安全传输`SecureBodyPlugin`插件.
3. 完善文档,增加框架`logo`.

### v0.1.4
1. MacOS 支持
2. Windows 支持
Expand Down
1 change: 1 addition & 0 deletions docs/drpc/codec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 编解码器
1 change: 1 addition & 0 deletions docs/drpc/plugin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 插件系统
1 change: 1 addition & 0 deletions docs/drpc/plugin_auth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### 权限认证
1 change: 1 addition & 0 deletions docs/drpc/plugin_heartbeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### 心跳
1 change: 1 addition & 0 deletions docs/drpc/plugin_ignorecase.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### 忽略请求方法名的大小写
121 changes: 121 additions & 0 deletions docs/drpc/plugin_securebody.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
### 消息体加密传输

使用该插件,能实现消息内容安全传输,使用的是`aes`算法加密消息内容。

### 使用示例

```go
package securebody_test

import (
"github.com/gogf/gf/test/gtest"
"github.com/osgochina/dmicro/drpc"
"github.com/osgochina/dmicro/drpc/plugin/securebody"
"strconv"
"testing"
"time"
)

type Request struct {
One int
Two int
}

type Response struct {
Three int
}

type math struct{ drpc.CallCtx }

func (m *math) Add(arg *Request) (*Response, *drpc.Status) {
return &Response{Three: arg.One + arg.Two}, nil
}

func newSession(t *gtest.T, port uint16) drpc.Session {
p := securebody.NewSecureBodyPlugin("cipherkey1234567")
srv := drpc.NewEndpoint(drpc.EndpointConfig{
ListenPort: port,
PrintDetail: true,
})
srv.RouteCall(new(math), p)
go srv.ListenAndServe()
time.Sleep(time.Second)

cli := drpc.NewEndpoint(drpc.EndpointConfig{
PrintDetail: true,
}, p)
sess, stat := cli.Dial(":" + strconv.Itoa(int(port)))
if !stat.OK() {
t.Fatal(stat)
}
return sess
}

func TestSecureBodyPlugin(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
sess := newSession(t, 9090)
var result Response
stat := sess.Call(
"/math/add",
&Request{One: 1, Two: 2},
&result,
securebody.WithSecureMeta(),
).Status()
t.Assert(stat.OK(), true)
t.Assert(result.Three, 3)
t.Logf("测试加密:1+2=%d", result.Three)
})
}

func TestReplySecureBodyPlugin(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
sess := newSession(t, 9090)
var result Response
stat := sess.Call(
"/math/add",
&Request{One: 1, Two: 2},
&result,
securebody.WithReplySecureMeta(true),
).Status()
t.Assert(stat.OK(), true)
t.Assert(result.Three, 3)
t.Logf("测试加密:1+2=%d", result.Three)
})
}

```

### 支持的方法

#### 创建`SecureBodyPlugin`插件

`NewSecureBodyPlugin(cipherKey string, statCode ...int32) drpc.Plugin {}`

参数:

* `cipherKey` 自定义加密key
* `statCode` 自定义错误码,该插件中的任何错误,都会返回该错误码


#### 强制要求加密传输

`WithSecureMeta() message.MsgSetting`

#### 强制要求服务端返回的内容加密传输

`WithReplySecureMeta(secure bool) message.MsgSetting `

#### 强制加密消息

`EnforceSecure(output message.Message) `

该方法一般用在服务端响应函数中,如:

```go

func (m *math) Add(arg *Request) (*Response, *drpc.Status) {
//响应消息给客户端的时候,可以使用它强制加密,当然,前提是你已经加载了SecureBodyPlugin插件
securebody.EnforceSecure(m.Output())
return &Response{Three: arg.One + arg.Two}, nil
}
```
1 change: 1 addition & 0 deletions docs/drpc/tfilter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 传输管道过滤器
6 changes: 5 additions & 1 deletion docs/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@
* [Raw协议](drpc/proto_raw.md)
* [JsonRPC协议](drpc/proto_jsonrpc.md)
* [编解码器 - Codec](drpc/codec.md)
* [传输过滤器 - TFilter](drpc/tfilter.md)
* [传输管道过滤器 - TFilter](drpc/tfilter.md)
* [钩子 - Hook](drpc/hook.md)
* [插件 - Plugin](drpc/plugin.md)
* [Auth认证](drpc/plugin_auth.md)
* [心跳](drpc/plugin_heartbeat.md)
* [忽略大小写](drpc/plugin_ignorecase.md)
* [安全传输](drpc/plugin_securebody.md)
* [平滑重启 - Graceful](drpc/graceful.md)

* 组件库
Expand Down
153 changes: 153 additions & 0 deletions drpc/plugin/securebody/secureBody.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions drpc/plugin/securebody/secureBody.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";
option go_package="./;securebody";

package securebody;

message Encrypt {
string cipherVersion = 1;
string ciphertext = 2;
}
Loading

0 comments on commit f85abb8

Please sign in to comment.