actions.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package main
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "errors"
  6. "fmt"
  7. "math/rand"
  8. "net/http"
  9. "sparrow/pkg/models"
  10. "sparrow/pkg/rpcs"
  11. "sparrow/pkg/server"
  12. "sparrow/pkg/token"
  13. "strings"
  14. "github.com/opentracing/opentracing-go/ext"
  15. "github.com/opentracing/opentracing-go"
  16. "github.com/martini-contrib/render"
  17. )
  18. const (
  19. ErrOK = 0
  20. ErrSystemFault = 10001
  21. ErrDeviceNotFound = 10002
  22. ErrWrongSecret = 10003
  23. ErrProtocolNotSuported = 10004
  24. )
  25. func renderError(code int, err error) Common {
  26. result := Common{}
  27. result.Code = code
  28. result.Message = err.Error()
  29. server.Log.Error(err.Error())
  30. return result
  31. }
  32. // DeviceRegisterArgs device register args
  33. type DeviceRegisterArgs struct {
  34. ProductKey string `json:"product_key" binding:"required"`
  35. DeviceCode string `json:"device_code" binding:"required"`
  36. Version string `json:"version" binding:"required"`
  37. ModuleName string `json:"module" binding:"required"`
  38. }
  39. // DeviceAuthArgs device authentication args
  40. type DeviceAuthArgs struct {
  41. DeviceId int64 `json:"device_id" binding:"required"`
  42. DeviceSecret string `json:"device_secret" binding:"required"`
  43. Protocol string `json:"protocol" binding:"required"`
  44. }
  45. type AccessAuthArgs struct {
  46. UserName string `json:"username"`
  47. Password string `json:"password"`
  48. }
  49. type AccessAuthResp struct {
  50. Result string `json:"result"` // 可选 "allow" | "deny" | "ignore"
  51. IsSuperuser bool `json:"is_superuser"` // false
  52. }
  53. // RegisterDevice 设备激活
  54. func RegisterDevice(args DeviceRegisterArgs, r render.Render) {
  55. server.Log.Printf("ACTION RegisterDevice, args:: %v ", args)
  56. rpcargs := &rpcs.ArgsDeviceRegister{
  57. ProductKey: args.ProductKey,
  58. DeviceCode: args.DeviceCode,
  59. DeviceVersion: args.Version,
  60. ModuleName: args.ModuleName,
  61. }
  62. device := &models.Device{}
  63. err := server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.RegisterDevice", rpcargs, device)
  64. if err != nil {
  65. r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
  66. return
  67. }
  68. server.Log.Infof("register device success: %v", device)
  69. result := DeviceRegisterResponse{}
  70. result.Data = DeviceRegisterData{
  71. DeviceId: int64(device.ID),
  72. DeviceSecret: device.DeviceSecret,
  73. DeviceKey: device.DeviceKey,
  74. DeviceIdentifier: device.DeviceIdentifier,
  75. }
  76. r.JSON(http.StatusOK, result)
  77. return
  78. }
  79. // DeviceAccessAuth emqx 设备接入认证
  80. func DeviceAccessAuth(args AccessAuthArgs, r render.Render) {
  81. server.Log.Printf("ACTION DeviceAccessAuth, args:: %v", args)
  82. result := AccessAuthResp{}
  83. result.Result = "allow"
  84. r.JSON(http.StatusOK, result)
  85. }
  86. // AuthDevice device auth
  87. func AuthDevice(args DeviceAuthArgs, r render.Render) {
  88. device := &models.Device{}
  89. span, ctx := opentracing.StartSpanFromContext(context.Background(), "AuthDevice")
  90. defer span.Finish()
  91. ext.SpanKindRPCClient.Set(span)
  92. arg := uint64(args.DeviceId)
  93. err := server.RPCCallByName(ctx, rpcs.RegistryServerName, "Registry.FindDeviceById", &arg, device)
  94. if err != nil {
  95. r.JSON(http.StatusOK, renderError(ErrDeviceNotFound, err))
  96. return
  97. }
  98. fmt.Printf(fmt.Sprintf("%s, %s", device.DeviceSecret, args.DeviceSecret))
  99. if device.DeviceSecret != args.DeviceSecret {
  100. // device secret is wrong.
  101. r.JSON(http.StatusOK, renderError(ErrWrongSecret, errors.New("wrong device secret.")))
  102. return
  103. }
  104. hepler := token.NewHelper(*confRedisHost, *confRedisPort, *confRedisDb)
  105. token, err := hepler.GenerateToken(device.RecordId)
  106. if err != nil {
  107. r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
  108. return
  109. }
  110. var hosts []string
  111. switch args.Protocol {
  112. case "http":
  113. hosts, err = server.GetServerHosts(strings.ToUpper(args.Protocol)+"Access", "httphost")
  114. case "mqtt":
  115. hosts, err = server.GetServerHosts(strings.ToUpper(args.Protocol)+"Access", "tcphost")
  116. case "coap":
  117. hosts, err = server.GetServerHosts(strings.ToUpper(args.Protocol)+"Access", "udphost")
  118. case "mqttX": // TODO: 增加配置文件支持
  119. hosts = []string{
  120. "114.115.251.196:1883",
  121. }
  122. default:
  123. err = errors.New("unsuported protocol: " + args.Protocol)
  124. }
  125. if err != nil {
  126. r.JSON(http.StatusOK, renderError(ErrProtocolNotSuported, err))
  127. return
  128. }
  129. // just get a random host
  130. host := hosts[rand.Intn(len(hosts))]
  131. result := DeviceAuthResponse{}
  132. result.Data = DeviceAuthData{
  133. AccessToken: hex.EncodeToString(token),
  134. AccessAddr: host,
  135. }
  136. server.Log.Infof("auth device success: %v, token: %x, access: %v", device, token, host)
  137. r.JSON(http.StatusOK, result)
  138. return
  139. }