stck-nsq-msg.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package stcknsqmsg
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "os"
  7. "stck/stck-nsq-msg/msg"
  8. "github.com/denisbrodbeck/machineid"
  9. "github.com/go-viper/mapstructure/v2"
  10. )
  11. type StckNsqMsg struct {
  12. Type string `mapstructure:"type" json:"type"`
  13. Data StckNsqMsgVariant `mapstructure:"data" json:"data"`
  14. }
  15. type StckNsqMsgRaw struct {
  16. Type string `mapstructure:"type" json:"type"`
  17. Data map[string]interface{} `mapstructure:"data" json:"data"`
  18. }
  19. type StckNsqMsgVariant interface {
  20. MsgType() string
  21. }
  22. func ToStckNsqMsg(msg StckNsqMsgVariant) StckNsqMsg {
  23. return StckNsqMsg{
  24. Type: msg.MsgType(),
  25. Data: msg,
  26. }
  27. }
  28. func ToStckNsqMsgString(msg StckNsqMsgVariant) (string, error) {
  29. m := ToStckNsqMsg(msg)
  30. return m.ToString()
  31. }
  32. func (m *StckNsqMsg) ToString() (string, error) {
  33. var rm StckNsqMsgRaw
  34. rm.Type = m.Type
  35. // Cast to generic map
  36. err := mapstructure.Decode(m.Data, &rm.Data)
  37. if err != nil {
  38. return "", err
  39. }
  40. b, err := json.Marshal(rm)
  41. if err != nil {
  42. return "", err
  43. }
  44. return string(b), nil
  45. }
  46. func FromString(s string) (StckNsqMsg, error) {
  47. var rm StckNsqMsgRaw
  48. var m StckNsqMsg
  49. err := json.Unmarshal([]byte(s), &rm)
  50. if err != nil {
  51. return m, err
  52. }
  53. // Cast to strongly typed struct
  54. m.Type = rm.Type
  55. switch rm.Type {
  56. case msg.DeviceHeartbeatMsgType:
  57. var d msg.DeviceHeartbeatMsg
  58. err = mapstructure.Decode(rm.Data, &d)
  59. if err != nil {
  60. return m, err
  61. }
  62. m.Data = &d
  63. case msg.DeviceActionDoneMsgType:
  64. var d msg.DeviceActionDoneMsg
  65. err = mapstructure.Decode(rm.Data, &d)
  66. if err != nil {
  67. return m, err
  68. }
  69. m.Data = &d
  70. case msg.DevicePongMsgType:
  71. var d msg.DevicePongMsg
  72. err = mapstructure.Decode(rm.Data, &d)
  73. if err != nil {
  74. return m, err
  75. }
  76. m.Data = &d
  77. case msg.PartUploadStatusChangedMsgType:
  78. var d msg.PartUploadStatusChangedMsg
  79. err = mapstructure.Decode(rm.Data, &d)
  80. if err != nil {
  81. return m, err
  82. }
  83. m.Data = &d
  84. case msg.PartInsertToStckStatusChangedMsgType:
  85. var d msg.PartInsertToStckStatusChangedMsg
  86. err = mapstructure.Decode(rm.Data, &d)
  87. if err != nil {
  88. return m, err
  89. }
  90. m.Data = &d
  91. case msg.StreamUploadStatusChangedMsgType:
  92. var d msg.StreamUploadStatusChangedMsg
  93. err = mapstructure.Decode(rm.Data, &d)
  94. if err != nil {
  95. return m, err
  96. }
  97. m.Data = &d
  98. case msg.StreamInsertToStckStatusChangedMsgType:
  99. var d msg.StreamInsertToStckStatusChangedMsg
  100. err = mapstructure.Decode(rm.Data, &d)
  101. if err != nil {
  102. return m, err
  103. }
  104. m.Data = &d
  105. case msg.ForceDeviceRebootMsgType:
  106. var d msg.ForceDeviceRebootMsg
  107. err = mapstructure.Decode(rm.Data, &d)
  108. if err != nil {
  109. return m, err
  110. }
  111. m.Data = &d
  112. case msg.ForceDeviceSendHeartbeatMsgType:
  113. var d msg.ForceDeviceSendHeartbeatMsg
  114. err = mapstructure.Decode(rm.Data, &d)
  115. if err != nil {
  116. return m, err
  117. }
  118. m.Data = &d
  119. case msg.RequestDeviceUpdateMsgType:
  120. var d msg.RequestDeviceUpdateMsg
  121. err = mapstructure.Decode(rm.Data, &d)
  122. if err != nil {
  123. return m, err
  124. }
  125. m.Data = &d
  126. case msg.RequestDeviceUploadLogsToMinioMsgType:
  127. var d msg.RequestDeviceUploadLogsToMinioMsg
  128. err = mapstructure.Decode(rm.Data, &d)
  129. if err != nil {
  130. return m, err
  131. }
  132. m.Data = &d
  133. case msg.DevicePingMsgType:
  134. var d msg.DevicePingMsg
  135. err = mapstructure.Decode(rm.Data, &d)
  136. if err != nil {
  137. return m, err
  138. }
  139. m.Data = &d
  140. case msg.RequestDeviceExecuteShellScriptMsgType:
  141. var d msg.RequestDeviceExecuteShellScriptMsg
  142. err = mapstructure.Decode(rm.Data, &d)
  143. if err != nil {
  144. return m, err
  145. }
  146. m.Data = &d
  147. default:
  148. return m, fmt.Errorf("unknown message type: %s", rm.Type)
  149. }
  150. return m, nil
  151. }
  152. func DeviceIdMatches(id1, id2 string) bool {
  153. return id1 == id2 || id1 == "*" || id2 == "*"
  154. }
  155. func GetLocalIP() (net.IP, error) {
  156. ifaces, err := net.Interfaces()
  157. if err != nil {
  158. return nil, err
  159. }
  160. for _, iface := range ifaces {
  161. addrs, err := iface.Addrs()
  162. if err != nil {
  163. return nil, err
  164. }
  165. for _, addr := range addrs {
  166. var ip net.IP
  167. switch v := addr.(type) {
  168. case *net.IPNet:
  169. ip = v.IP
  170. case *net.IPAddr:
  171. ip = v.IP
  172. }
  173. if ip == nil || ip.IsLoopback() {
  174. continue
  175. }
  176. ip = ip.To4()
  177. if ip == nil {
  178. continue // not an ipv4 address
  179. }
  180. return ip, nil
  181. }
  182. }
  183. return nil, fmt.Errorf("no non-loopback IP address found")
  184. }
  185. // Generate a unique machine identifier based on the local IP and OS machine ID
  186. func MakeUniqueMachineID() string {
  187. ip, err := GetLocalIP()
  188. if err != nil {
  189. fmt.Fprintf(os.Stderr, "get local IP failed: %v\n", err)
  190. }
  191. machineid, err := machineid.ProtectedID("unique")
  192. if err != nil {
  193. fmt.Fprintf(os.Stderr, "get machine ID failed: %v\n", err)
  194. }
  195. if len(machineid) > 48 {
  196. machineid = machineid[:48] // truncate to 48 characters
  197. }
  198. return fmt.Sprintf("%s-%s", ip.String(), machineid)
  199. }