listSas.go 5.4 KB

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