Created
April 25, 2018 22:50
-
-
Save artburkart/b32c4106a0688f14468d86cf4911287c to your computer and use it in GitHub Desktop.
ipsec-go-vici-example.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/bronze1man/goStrongswanVici" | |
"github.com/davecgh/go-spew/spew" | |
"strings" | |
) | |
type Status map[string]map[string]string | |
func main(){ | |
var status Status | |
status = Status{} | |
connected := 0 | |
// Create a client. | |
client, err := goStrongswanVici.NewClientConnFromDefaultSocket() | |
if err != nil { | |
err = nil | |
return | |
} | |
defer client.Close() | |
// Get all conns info from strongswan | |
conns, err := client.ListAllVpnConnInfo() | |
if err != nil { | |
err = nil | |
return | |
} | |
for _, connInfo := range conns { | |
authState := connInfo.IkeSa.State | |
// Check children connection states | |
if children := connInfo.IkeSa.Child_sas; children != nil { | |
for name, child := range children { | |
nameParts := strings.Split(name, "-") | |
if _, ok := status[nameParts[0]]; !ok { | |
status[nameParts[0]] = map[string]string{} | |
} | |
switch authState { | |
case "ESTABLISHED": | |
if child.State == "INSTALLED" { | |
status[nameParts[0]][nameParts[1]] = "connected" | |
} | |
break | |
case "CONNECTING": | |
status[nameParts[0]][nameParts[1]] = "connecting" | |
default: | |
status[nameParts[0]][nameParts[1]] = "disconnected" | |
} | |
} | |
} else { | |
nameParts := strings.Split(connInfo.ChildSaName, "-") | |
if _, ok := status[nameParts[0]]; !ok { | |
status[nameParts[0]] = map[string]string{} | |
} | |
switch authState { | |
case "ESTABLISHED": | |
if connInfo.Child_sas.State == "INSTALLED" { | |
status[nameParts[0]][nameParts[1]] = "connected" | |
} | |
break | |
case "CONNECTING": | |
status[nameParts[0]][nameParts[1]] = "connecting" | |
default: | |
status[nameParts[0]][nameParts[1]] = "disconnected" | |
} | |
} | |
} | |
for _, stat := range status { | |
for _, conn := range stat { | |
if conn == "connected" { | |
connected += 1 | |
} | |
} | |
} | |
spew.Dump(status) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment