use regexps

This commit is contained in:
Xavier Henner 2019-07-12 18:23:40 +02:00
parent dbdbe8aef0
commit 3d1801ee50
1 changed files with 24 additions and 20 deletions

View File

@ -102,38 +102,42 @@ func (c *vpnSession) ParseSessionId(line string) error {
func (c *vpnSession) ParseEnv(s *OpenVpnMgt, infos *[]string) error { func (c *vpnSession) ParseEnv(s *OpenVpnMgt, infos *[]string) error {
var err error var err error
r := regexp.MustCompile("[^a-zA-Z0-9./_@-]") r := regexp.MustCompile("[^a-zA-Z0-9./_@-]")
renv := regexp.MustCompile("^>CLIENT:ENV,([^=]*)=(.*)$")
for _, line := range *infos { for _, line := range *infos {
p := strings.Split(strings.Replace(line, ">CLIENT:ENV,", "", 1), "=") p := renv.FindStringSubmatch(line)
switch p[0] { if len(p) != 3 {
continue
}
switch p[1] {
case "trusted_port": case "trusted_port":
if c.port, err = strconv.Atoi(r.ReplaceAllString(p[1], "")); err != nil { if c.port, err = strconv.Atoi(r.ReplaceAllString(p[2], "")); err != nil {
return err return err
} }
case "untrusted_port": case "untrusted_port":
if c.port, err = strconv.Atoi(r.ReplaceAllString(p[1], "")); err != nil { if c.port, err = strconv.Atoi(r.ReplaceAllString(p[2], "")); err != nil {
return err return err
} }
case "trusted_ip": case "trusted_ip":
c.IP = r.ReplaceAllString(p[1], "") c.IP = r.ReplaceAllString(p[2], "")
case "untrusted_ip": case "untrusted_ip":
c.IP = r.ReplaceAllString(p[1], "") c.IP = r.ReplaceAllString(p[2], "")
case "ifconfig_pool_remote_ip": case "ifconfig_pool_remote_ip":
c.PrivIP = r.ReplaceAllString(p[1], "") c.PrivIP = r.ReplaceAllString(p[2], "")
case "ifconfig_local": case "ifconfig_local":
c.localIP = r.ReplaceAllString(p[1], "") c.localIP = r.ReplaceAllString(p[2], "")
case "bytes_received": case "bytes_received":
if c.BwWrite, err = strconv.Atoi(p[1]); err != nil { if c.BwWrite, err = strconv.Atoi(p[2]); err != nil {
break break
} }
case "bytes_sent": case "bytes_sent":
if c.BwRead, err = strconv.Atoi(p[1]); err != nil { if c.BwRead, err = strconv.Atoi(p[2]); err != nil {
break break
} }
case "password": case "password":
switch { switch {
case strings.HasPrefix(p[1], "CRV1"): case strings.HasPrefix(p[2], "CRV1"):
split := strings.Split(p[1], ":") split := strings.Split(p[2], ":")
if len(split) != 5 { if len(split) != 5 {
break break
} }
@ -143,8 +147,8 @@ func (c *vpnSession) ParseEnv(s *OpenVpnMgt, infos *[]string) error {
c.otpCode = "***" c.otpCode = "***"
} }
// don't check that password against the ibp database // don't check that password against the ibp database
case strings.HasPrefix(p[1], "SCRV1"): case strings.HasPrefix(p[2], "SCRV1"):
split := strings.Split(p[1], ":") split := strings.Split(p[2], ":")
if len(split) != 3 { if len(split) != 3 {
break break
} }
@ -156,7 +160,7 @@ func (c *vpnSession) ParseEnv(s *OpenVpnMgt, infos *[]string) error {
data, err = base64.StdEncoding.DecodeString(split[2]) data, err = base64.StdEncoding.DecodeString(split[2])
if err != nil { if err != nil {
c.password = p[1] c.password = p[2]
break break
} }
c.otpCode = string(data) c.otpCode = string(data)
@ -169,7 +173,7 @@ func (c *vpnSession) ParseEnv(s *OpenVpnMgt, infos *[]string) error {
go s.CheckPwn(c) go s.CheckPwn(c)
} }
default: default:
c.password = p[1] c.password = p[2]
c.otpCode = "" c.otpCode = ""
// only check if the password is pwned on the first connection // only check if the password is pwned on the first connection
if c.Operation == "log in" { if c.Operation == "log in" {
@ -178,11 +182,11 @@ func (c *vpnSession) ParseEnv(s *OpenVpnMgt, infos *[]string) error {
} }
case "username": case "username":
c.Login = r.ReplaceAllString(p[1], "") c.Login = r.ReplaceAllString(p[2], "")
case "dev": case "dev":
c.dev = r.ReplaceAllString(p[1], "") c.dev = r.ReplaceAllString(p[2], "")
case "ifconfig_netmask": case "ifconfig_netmask":
c.netmask = r.ReplaceAllString(p[1], "") c.netmask = r.ReplaceAllString(p[2], "")
} }
} }
return nil return nil