monitorSA.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package goStrongswanVici
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "time"
  6. )
  7. const (
  8. EVENT_IKE_UPDOWN = "ike-updown"
  9. EVENT_IKE_REKEY = "ike-rekey"
  10. EVENT_CHILD_UPDOWN = "child-updown"
  11. EVENT_CHILD_REKEY = "child-rekey"
  12. )
  13. type EventIkeSAUpDown struct {
  14. Child_sas map[string]*EventChildSAUpDown `json:"child-sas"`
  15. Dh_group string `json:"dh-group"`
  16. Encr_keysize string `json:"encr-keysize"`
  17. Encr_alg string `json:"encr-alg"`
  18. Established string `json:"established"`
  19. Initiator_spi string `json:"initiator-spi"`
  20. Integ_alg string `json:"integ-alg"`
  21. Local_id string `json:"local-id"`
  22. Local_host string `json:"local-host"`
  23. Local_port string `json:"local-port"`
  24. Nat_any string `json:"nat-any"`
  25. Nat_remote string `json:"nat-remote"`
  26. Prf_alg string `json:"prf-alg"`
  27. Rekey_time string `json:"rekey-time"`
  28. Remote_id string `json:"remote-id"`
  29. Remote_host string `json:"remote-host"`
  30. Remote_port string `json:"remote-port"`
  31. Remote_vips []string `json:"remote-vips"`
  32. Responder_spi string `json:"responder-spi"`
  33. State string `json:"state"`
  34. Task_Active []string `json:"tasks-active"`
  35. Uniqueid string `json:"uniqueid"`
  36. Version string `json:"version"`
  37. Remote_eap_id string `json:"remote-eap-id"` // client user name
  38. }
  39. type EventChildSAUpDown struct {
  40. Bytes_in string `json:"bytes-in"`
  41. Bytes_out string `json:"bytes-out"`
  42. Encap string `json:"encap"`
  43. Encr_alg string `json:"encr-alg"`
  44. Encr_keysize string `json:"encr-keysize"`
  45. Integ_alg string `json:"integ-alg"`
  46. Install_time string `json:"install-time"`
  47. Life_time string `json:"life-time"`
  48. Local_ts []string `json:"local-ts"`
  49. Mode string `json:"mode"`
  50. Name string `json:"name"`
  51. Protocol string `json:"protocol"`
  52. Packets_out string `json:"packets-out"`
  53. Packets_in string `json:"packets-in"`
  54. Rekey_time string `json:"rekey-time"`
  55. Remote_ts []string `json:"remote-ts"`
  56. Reqid string `json:"reqid"`
  57. Spi_in string `json:"spi-in"`
  58. Spi_out string `json:"spi-out"`
  59. State string `json:"state"`
  60. UniqueId string `json:"uniqueid"`
  61. Remote_eap_id string `json:"remote-eap-id"` // client user name
  62. }
  63. type EventIkeRekeyPair struct {
  64. New EventIkeRekeySA `json:"new"`
  65. Old EventIkeRekeySA `json:"old"`
  66. }
  67. type EventIkeRekeySA struct {
  68. Child_sas map[string]*EventChildRekeyPair `json:"child-sas"`
  69. Dh_group string `json:"dh-group"`
  70. Encr_alg string `json:"encr-alg"`
  71. Encr_keysize string `json:"encr-keysize"`
  72. Established string `json:"established"`
  73. Initiator_spi string `json:"initiator-spi"`
  74. Integ_alg string `json:"integ-alg"`
  75. Local_host string `json:"local-host"`
  76. Local_port string `json:"local-port"`
  77. Local_id string `json:"local-id"`
  78. Nat_any string `json:"nat-any"`
  79. Nat_remote string `json:"nat-remote"`
  80. Prf_alg string `json:"prf-alg"`
  81. Rekey_time string `json:"rekey-time"`
  82. Remote_id string `json:"remote-id"`
  83. Remote_host string `json:"remote-host"`
  84. Remote_port string `json:"remote-port"`
  85. Remote_vips []string `json:"remote-vips"`
  86. Responder_spi string `json:"responder-spi"`
  87. State string `json:"state"`
  88. Task_Active []string `json:"tasks-active"`
  89. Task_Passive []string `json:"tasks-passive"`
  90. Uniqueid string `json:"uniqueid"`
  91. Version string `json:"version"`
  92. Remote_eap_id string `json:"remote-eap-id"` // client user name
  93. }
  94. type EventChildRekeyPair struct {
  95. New EventChildRekeySA `json:"new"`
  96. Old EventChildRekeySA `json:"old"`
  97. }
  98. type EventChildRekeySA struct {
  99. Bytes_in string `json:"bytes-in"`
  100. Bytes_out string `json:"bytes-out"`
  101. Encap string `json:"encap"`
  102. Encr_alg string `json:"encr-alg"`
  103. Encr_keysize string `json:"encr-keysize"`
  104. Integ_alg string `json:"integ-alg"`
  105. Install_time string `json:"install-time"`
  106. Life_time string `json:"life-time"`
  107. Local_ts []string `json:"local-ts"`
  108. Mode string `json:"mode"`
  109. Name string `json:"name"`
  110. Packets_in string `json:"packets-in"`
  111. Packets_out string `json:"packets-out"`
  112. Protocol string `json:"protocol"`
  113. Remote_ts []string `json:"remote-ts"`
  114. Rekey_time string `json:"rekey-time"`
  115. Reqid string `json:"reqid"`
  116. Spi_in string `json:"spi-in"`
  117. Spi_out string `json:"spi-out"`
  118. State string `json:"state"`
  119. Use_in string `json:"use-in"`
  120. Use_out string `json:"use-out"`
  121. UniqueId string `json:"uniqueid"`
  122. }
  123. type EventIkeUpDown struct {
  124. Up bool
  125. Ike map[string]*EventIkeSAUpDown
  126. }
  127. type EventIkeRekey struct {
  128. Ike map[string]*EventIkeRekeyPair
  129. }
  130. type EventChildRekey struct {
  131. Ike map[string]*EventIkeRekeySA
  132. }
  133. type EventChildUpDown struct {
  134. Up bool
  135. Ike map[string]*EventIkeSAUpDown
  136. }
  137. type EventIkeSa struct {
  138. IkeSa
  139. TasksActive []string `json:"tasks-active"`
  140. }
  141. type EventInfo struct {
  142. Up bool
  143. Ike map[string]*EventIkeSa
  144. }
  145. func prettyprint(b []byte) string {
  146. var out bytes.Buffer
  147. json.Indent(&out, b, "", " ")
  148. return string(out.Bytes())
  149. }
  150. type monitorCallBack func(event string, info interface{})
  151. func handleIkeUpDown(eventName string, callback monitorCallBack, response map[string]interface{}) {
  152. event := &EventIkeUpDown{}
  153. event.Ike = map[string]*EventIkeSAUpDown{}
  154. //we need to marshall all ikes manual because json uses connections names as key
  155. for name := range response {
  156. value := response[name]
  157. if name == "up" {
  158. event.Up = true
  159. } else {
  160. sa := &EventIkeSAUpDown{}
  161. ConvertFromGeneral(value, sa)
  162. event.Ike[name] = sa
  163. }
  164. }
  165. callback(eventName, event)
  166. }
  167. func handleIkeRekey(eventName string, callback monitorCallBack, response map[string]interface{}) {
  168. event := &EventIkeRekey{}
  169. event.Ike = map[string]*EventIkeRekeyPair{}
  170. //we need to marshall all ikes manual because json uses connections names as key
  171. for name := range response {
  172. value := response[name]
  173. sa := &EventIkeRekeyPair{}
  174. ConvertFromGeneral(value, sa)
  175. event.Ike[name] = sa
  176. }
  177. callback(eventName, event)
  178. }
  179. func handleChildUpDown(eventName string, callback monitorCallBack, response map[string]interface{}) {
  180. event := &EventChildUpDown{}
  181. event.Ike = map[string]*EventIkeSAUpDown{}
  182. //we need to marshall all ikes manual because json uses connections names as key
  183. for name := range response {
  184. value := response[name]
  185. if name == "up" {
  186. event.Up = true
  187. } else {
  188. sa := &EventIkeSAUpDown{}
  189. ConvertFromGeneral(value, sa)
  190. event.Ike[name] = sa
  191. }
  192. }
  193. callback(eventName, event)
  194. }
  195. func handleChildRekey(eventName string, callback monitorCallBack, response map[string]interface{}) {
  196. event := &EventChildRekey{}
  197. event.Ike = map[string]*EventIkeRekeySA{}
  198. //we need to marshall all ikes manual because json uses connections names as key
  199. for name := range response {
  200. value := response[name]
  201. sa := &EventIkeRekeySA{}
  202. ConvertFromGeneral(value, sa)
  203. event.Ike[name] = sa
  204. }
  205. callback(eventName, event)
  206. }
  207. func (c *ClientConn) MonitorSA(callback monitorCallBack, watchdog time.Duration) (err error) {
  208. //register event
  209. c.RegisterEvent(EVENT_CHILD_UPDOWN, func(response map[string]interface{}) {
  210. //dumpResponse(response)
  211. handleChildUpDown(EVENT_CHILD_UPDOWN, callback, response)
  212. })
  213. c.RegisterEvent(EVENT_CHILD_REKEY, func(response map[string]interface{}) {
  214. //dumpResponse(response)
  215. handleChildRekey(EVENT_CHILD_REKEY, callback, response)
  216. })
  217. c.RegisterEvent(EVENT_IKE_UPDOWN, func(response map[string]interface{}) {
  218. //dumpResponse(response)
  219. handleIkeUpDown(EVENT_IKE_UPDOWN, callback, response)
  220. })
  221. c.RegisterEvent(EVENT_IKE_REKEY, func(response map[string]interface{}) {
  222. //dumpResponse(response)
  223. handleIkeRekey(EVENT_IKE_REKEY, callback, response)
  224. })
  225. for {
  226. time.Sleep(watchdog)
  227. //collect some daemon stats to see if connection is alive
  228. if _, err := c.Stats(); err != nil {
  229. return err
  230. }
  231. }
  232. return nil
  233. }