zhangzengfei
2023-09-07 55aa27a6ad0e012d62dcea2db37528a1b18836fb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package controllers
 
import (
    "basic.com/valib/bhomeclient.git"
    "basic.com/valib/logger.git"
    "encoding/json"
    "github.com/satori/go.uuid"
    "nanomsg.org/go-mangos"
    "nanomsg.org/go-mangos/protocol/req"
    "nanomsg.org/go-mangos/transport/ipc"
    "nanomsg.org/go-mangos/transport/tcp"
    "strconv"
    "time"
    "vamicro/system-service/models"
)
 
//设备授权
type DevAuthController struct {
 
}
 
//获取设备授权配置
func (dac *DevAuthController) AuthConfig(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply  {
    var config models.AuthConfig
    i, _ := config.Select()
    if i > 0 {
        return &bhomeclient.Reply{ Success: true, Data: config }
    }
    return &bhomeclient.Reply{ Success: true, Data: models.AuthConfig{}}
}
 
//修改设备授权配置
func (dac *DevAuthController) SaveAuthConfig(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply  {
    //一旦修改,如果在集群内,则集群内所有设备的授权配置都将修改
    var reqBody models.AuthConfig
    if err := c.BindJSON(&reqBody);err != nil {
        return &bhomeclient.Reply{ Msg: "参数有误"+err.Error()}
    }
    var config models.AuthConfig
    i, _ := config.Select()
    if i == 0{
        tmp := models.AuthConfig{
            AuthType: reqBody.AuthType,
            Password: reqBody.Password,
        }
        if tmp.Insert() {
            return &bhomeclient.Reply{ Success: true, Msg: "保存成功"}
        } else {
            return &bhomeclient.Reply{ Msg: "保存失败"}
        }
    } else {
        config.AuthType = reqBody.AuthType
        config.Password = reqBody.Password
        if config.Update() {
            return &bhomeclient.Reply{ Success: true, Msg: "保存成功"}
        } else {
            return &bhomeclient.Reply{ Msg: "保存失败"}
        }
    }
}
 
//设备管理发送的申请请求
func (dac *DevAuthController) Apply(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    type applyArg struct {
        Key         string         `json:"key"`
        FromDevId     string         `json:"fromDevId"`
        FromIp         string         `json:"fromIp"`
    }
    var reqBody applyArg
    if err := c.BindJSON(&reqBody); err != nil || reqBody.Key == "" || reqBody.FromIp == "" || reqBody.FromDevId == "" {
        return &bhomeclient.Reply{ Msg: "参数有误"}
    }
    //1.验证请求的key是否匹配
    var config models.AuthConfig
    ic, _ := config.Select()
    if ic > 0 {
        if config.AuthType == models.AuthType_Key && reqBody.Key != config.Password {
            return &bhomeclient.Reply{ Msg: "申请密钥不匹配"}
        }
    }
 
    //2.写入authDevice
    var da models.AuthDevice
    i, _ := da.FindByDevId(reqBody.FromDevId)
    if i == 0 { //未申请过
        tmp := models.AuthDevice{
            Id: uuid.NewV4().String(),
            DevId: reqBody.FromDevId,
            DevIp: reqBody.FromIp,
            ApplyKey: reqBody.Key,
            CreateTime: time.Now().Format("2006-01-02 15:04:05"),
        }
        if tmp.Insert() {
            return &bhomeclient.Reply{ Success:true, Msg: "添加成功,待审核"}
        } else {
            return &bhomeclient.Reply{ Msg: "添加失败"}
        }
    } else { //已申请过
        if da.Status == models.AuthStatus_Agreed {
            return &bhomeclient.Reply{ Success: true, Msg: "已通过,无需重复申请"}
        } else if da.Status == models.AuthStatus_AuthCanceled {
            return &bhomeclient.Reply{ Msg: "已取消授权"}
        } else {
            return &bhomeclient.Reply{ Success:true, Msg:"请等候审核"}
        }
    }
}
 
//显示申请信息
func (dac *DevAuthController) ApplyShow(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    var ad models.AuthDevice
    list := ad.FindByStatus(models.AuthStatus_Waiting)
    if list == nil {
        list = make([]models.AuthDevice, 0)
    }
    return &bhomeclient.Reply{ Success: true, Data: list}
}
 
//通过或拒绝
func (dac *DevAuthController) Approve(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    type approveArg struct {
        Id         string         `json:"id"`
        DevId     string         `json:"devId"`
        DevIp     string         `json:"devIp"`
        Status     int         `json:"status"`
    }
    var reqBody approveArg
    c.BindJSON(&reqBody)
    if reqBody.Status == models.AuthStatus_Agreed || reqBody.Status == models.AuthStatus_Rejected { //通过或拒绝
        //1.通知对方,已通过授权或已拒绝(serf1运行在master节点上,设备管理服务可能运行在集群内任一一台服务器上)
        //根据管理设备的id获取ip(通过设备间bus通信反馈)?
        arg, _ := json.Marshal(reqBody)
        remoteReply,err := approve(arg, reqBody.DevIp)
        if err != nil {
            return &bhomeclient.Reply{ Msg: err.Error()}
        }
        if remoteReply.Success {
            //2.修改本地记录的状态
            //if len(resp) > 0 {
            var ad models.AuthDevice
            if ad.UpdateStatus(reqBody.Id, reqBody.Status) {
                return &bhomeclient.Reply{ Success: true, Msg: "操作成功"}
            } else {
                return &bhomeclient.Reply{ Msg: "操作失败"}
            }
            //} else {
            //    return &bhomeclient.Reply{Msg:"设备管理响应失败"}
            //}
        } else {
            return remoteReply
        }
 
    } else {
        return &bhomeclient.Reply{ Msg: "参数有误"}
    }
}
 
func approve(arg []byte, devIp string) (*bhomeclient.Reply, error) {
    var sock mangos.Socket
    var err error
    var msg []byte
 
    if sock,err = req.NewSocket();err !=nil {
        logger.Debug("comp can't new req socket:%s",err.Error())
        return nil, err
    }
    sock.AddTransport(ipc.NewTransport())
    sock.AddTransport(tcp.NewTransport())
    if err = sock.Dial(devIp+":"+strconv.Itoa(4012));err !=nil {
        logger.Debug("comp can't dial on req socket:%s",err.Error())
        return nil, err
    }
    sock.SetOption(mangos.OptionMaxRecvSize, 1024*1024*100)
    sock.SetOption(mangos.OptionRecvDeadline, time.Second*5)
    if err = sock.Send(arg);err !=nil {
        logger.Debug("comp can't send message on push socket:%s",err.Error())
        return nil, err
    }
    if msg,err = sock.Recv();err !=nil {
        logger.Debug("comp sock.Recv receive err:%s",err.Error())
        return nil, err
    }
    sock.Close()
    var ret bhomeclient.Reply
    retErr := json.Unmarshal(msg, &ret)
    if retErr != nil {
        return nil, retErr
    }
    return &ret, nil
}
 
//获取已授权的设备列表
func (dac *DevAuthController) AuthedList(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    var ad models.AuthDevice
    list := ad.FindByStatus(models.AuthStatus_Agreed)
    if list == nil {
        list = make([]models.AuthDevice, 0)
    }
    return &bhomeclient.Reply{ Success: true, Data: list}
}