listSas.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package goStrongswanVici
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. //from list-sa event
  7. type IkeSa struct {
  8. Uniqueid string `json:"uniqueid"` //called ike_id in terminate() argument.
  9. Version string `json:"version"`
  10. State string `json:"state"` //had saw: ESTABLISHED
  11. Local_host string `json:"local-host"`
  12. Local_id string `json:"local-id"`
  13. Remote_host string `json:"remote-host"`
  14. Remote_id string `json:"remote-id"`
  15. Remote_xauth_id string `json:"remote-xauth-id"` //client username
  16. Initiator string `json:"initiator"`
  17. Initiator_spi string `json:"initiator-spi"`
  18. Responder_spi string `json:"responder-spi"`
  19. Encr_alg string `json:"encr-alg"`
  20. Encr_keysize string `json:"encr-keysize"`
  21. Integ_alg string `json:"integ-alg"`
  22. Integ_keysize string `json:"integ-keysize"`
  23. Prf_alg string `json:"prf-alg"`
  24. Dh_group string `json:"dh-group"`
  25. Established string `json:"established"`
  26. Rekey_time string `json:"rekey-time"`
  27. Reauth_time string `json:"reauth-time"`
  28. Remote_vips []string `json:"remote-vips"`
  29. Child_sas map[string]Child_sas `json:"child-sas"` //key means child-sa-name(conn name in ipsec.conf)
  30. }
  31. type Child_sas struct {
  32. Reqid string `json:"reqid"`
  33. State string `json:"state"` //had saw: INSTALLED
  34. Mode string `json:"mode"` //had saw: TUNNEL
  35. Protocol string `json:"protocol"`
  36. Encap string `json:"encap"`
  37. Spi_in string `json:"spi-in"`
  38. Spi_out string `json:"spi-out"`
  39. Cpi_in string `json:"cpi-in"`
  40. Cpi_out string `json:"cpi-out"`
  41. Encr_alg string `json:"encr-alg"`
  42. Encr_keysize string `json:"encr-keysize"`
  43. Integ_alg string `json:"integ-alg"`
  44. Integ_keysize string `json:"integ-keysize"`
  45. Prf_alg string `json:"prf-alg"`
  46. Dh_group string `json:"dh-group"`
  47. Esn string `json:"esn"`
  48. Bytes_in string `json:"bytes-in"` //bytes into this machine
  49. Packets_in string `json:"packets-in"`
  50. Use_in string `json:"use-in"`
  51. Bytes_out string `json:"bytes-out"` // bytes out of this machine
  52. Packets_out string `json:"packets-out"`
  53. Use_out string `json:"use-out"`
  54. Rekey_time string `json:"rekey-time"`
  55. Life_time string `json:"life-time"`
  56. Install_time string `json:"install-time"`
  57. Local_ts []string `json:"local-ts"`
  58. Remote_ts []string `json:"remote-ts"`
  59. }
  60. func (s *Child_sas) GetBytesIn() uint64 {
  61. num, err := strconv.ParseUint(s.Bytes_in, 10, 64)
  62. if err != nil {
  63. return 0
  64. }
  65. return num
  66. }
  67. func (s *Child_sas) GetBytesOut() uint64 {
  68. num, err := strconv.ParseUint(s.Bytes_out, 10, 64)
  69. if err != nil {
  70. return 0
  71. }
  72. return num
  73. }
  74. // To be simple, list all clients that are connecting to this server .
  75. // A client is a sa.
  76. // Lists currently active IKE_SAs
  77. func (c *ClientConn) ListSas(ike string, ike_id string) (sas []map[string]IkeSa, err error) {
  78. sas = []map[string]IkeSa{}
  79. var eventErr error
  80. //register event
  81. err = c.RegisterEvent("list-sa", func(response map[string]interface{}) {
  82. sa := &map[string]IkeSa{}
  83. err = ConvertFromGeneral(response, sa)
  84. if err != nil {
  85. fmt.Printf("list-sa event error: %s\n", err)
  86. eventErr = err
  87. return
  88. }
  89. sas = append(sas, *sa)
  90. //fmt.Printf("event %#v\n", response)
  91. })
  92. if err != nil {
  93. return
  94. }
  95. if eventErr != nil {
  96. return
  97. }
  98. inMap := map[string]interface{}{}
  99. if ike != "" {
  100. inMap["ike"] = ike
  101. }
  102. if ike_id != "" {
  103. inMap["ike_id"] = ike_id
  104. }
  105. _, err = c.Request("list-sas", inMap)
  106. if err != nil {
  107. return
  108. }
  109. //fmt.Printf("request finish %#v\n", sas)
  110. err = c.UnregisterEvent("list-sa")
  111. if err != nil {
  112. return
  113. }
  114. return
  115. }
  116. //a vpn conn in the strongswan server
  117. type VpnConnInfo struct {
  118. IkeSa
  119. Child_sas
  120. IkeSaName string //looks like conn name in ipsec.conf, content is same as ChildSaName
  121. ChildSaName string //looks like conn name in ipsec.conf
  122. }
  123. func (c *VpnConnInfo) GuessUserName() string {
  124. if c.Remote_xauth_id != "" {
  125. return c.Remote_xauth_id
  126. }
  127. if c.Remote_id != "" {
  128. return c.Remote_id
  129. }
  130. return ""
  131. }
  132. // a helper method to avoid complex data struct in ListSas
  133. // if it only have one child_sas ,it will put it into info.Child_sas
  134. func (c *ClientConn) ListAllVpnConnInfo() (list []VpnConnInfo, err error) {
  135. sasList, err := c.ListSas("", "")
  136. if err != nil {
  137. return
  138. }
  139. list = make([]VpnConnInfo, len(sasList))
  140. for i, sa := range sasList {
  141. info := VpnConnInfo{}
  142. if len(sa) != 1 {
  143. fmt.Printf("[vici.ListAllVpnConnInfo] warning: len(sa)[%d]!=1\n", len(sa))
  144. }
  145. for ikeSaName, ikeSa := range sa {
  146. info.IkeSaName = ikeSaName
  147. info.IkeSa = ikeSa
  148. //if len(ikeSa.Child_sas) != 1 {
  149. // fmt.Println("[vici.ListAllVpnConnInfo] warning: len(ikeSa.Child_sas)[%d]!=1", len(ikeSa.Child_sas))
  150. //}
  151. for childSaName, childSa := range ikeSa.Child_sas {
  152. info.ChildSaName = childSaName
  153. info.Child_sas = childSa
  154. break
  155. }
  156. break
  157. }
  158. if len(info.IkeSa.Child_sas) == 1 {
  159. info.IkeSa.Child_sas = nil
  160. }
  161. list[i] = info
  162. }
  163. return
  164. }