diff --git a/internal/openvpn/extract/extract.go b/internal/openvpn/extract/extract.go index e0c31217..c25a75f0 100644 --- a/internal/openvpn/extract/extract.go +++ b/internal/openvpn/extract/extract.go @@ -64,6 +64,13 @@ func extractDataFromLine(line string) ( return ip, 0, "", fmt.Errorf("extracting from remote line: %w", err) } return ip, port, protocol, nil + + case strings.HasPrefix(line, "port "): + port, err = extractPort(line) + if err != nil { + return ip, 0, "", fmt.Errorf("extracting from port line: %w", err) + } + return ip, port, "", nil } return ip, 0, "", nil @@ -133,3 +140,25 @@ func extractRemote(line string) (ip netip.Addr, port uint16, return ip, port, protocol, nil } + +var ( + errPostLineFieldsCount = errors.New("post line has not 2 fields as expected") +) + +func extractPort(line string) (port uint16, err error) { + fields := strings.Fields(line) + const expectedFieldsCount = 2 + if len(fields) != expectedFieldsCount { + return 0, fmt.Errorf("%w: %s", errPostLineFieldsCount, line) + } + + portInt, err := strconv.Atoi(fields[1]) + if err != nil { + return 0, fmt.Errorf("%w: %s", errPortNotValid, line) + } else if portInt < 1 || portInt > 65535 { + return 0, fmt.Errorf("%w: %d must be between 1 and 65535", errPortNotValid, portInt) + } + port = uint16(portInt) + + return port, nil +} diff --git a/internal/openvpn/extract/extract_test.go b/internal/openvpn/extract/extract_test.go index 03333f21..b1d424fb 100644 --- a/internal/openvpn/extract/extract_test.go +++ b/internal/openvpn/extract/extract_test.go @@ -118,6 +118,14 @@ func Test_extractDataFromLine(t *testing.T) { port: 1194, protocol: constants.UDP, }, + "extract_port_fail": { + line: "port a", + isErr: errPortNotValid, + }, + "extract_port_success": { + line: "port 1194", + port: 1194, + }, } for name, testCase := range testCases {