fix(custom): parse port option line for OpenVPN
This commit is contained in:
@@ -64,6 +64,13 @@ func extractDataFromLine(line string) (
|
|||||||
return ip, 0, "", fmt.Errorf("extracting from remote line: %w", err)
|
return ip, 0, "", fmt.Errorf("extracting from remote line: %w", err)
|
||||||
}
|
}
|
||||||
return ip, port, protocol, nil
|
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
|
return ip, 0, "", nil
|
||||||
@@ -133,3 +140,25 @@ func extractRemote(line string) (ip netip.Addr, port uint16,
|
|||||||
|
|
||||||
return ip, port, protocol, nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -118,6 +118,14 @@ func Test_extractDataFromLine(t *testing.T) {
|
|||||||
port: 1194,
|
port: 1194,
|
||||||
protocol: constants.UDP,
|
protocol: constants.UDP,
|
||||||
},
|
},
|
||||||
|
"extract_port_fail": {
|
||||||
|
line: "port a",
|
||||||
|
isErr: errPortNotValid,
|
||||||
|
},
|
||||||
|
"extract_port_success": {
|
||||||
|
line: "port 1194",
|
||||||
|
port: 1194,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, testCase := range testCases {
|
for name, testCase := range testCases {
|
||||||
|
|||||||
Reference in New Issue
Block a user