Debian Codegraph @

internal/openvpn/connection/client.go

raw
1 package connection
2
3 import (
4 "fmt"
5 "strconv"
6 "strings"
7
8 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config"
9 )
10
11 type Client struct {
12 Reason string
13 IPAddr string
14 IPPort string
15 VPNAddress string
16 CommonName string
17 SessionID string
18 SessionState string
19 IvSSO string
20 KID uint64
21 CID uint64
22 UsernameIsDefined int
23 }
24
25 // NewClient parses a multi-line message from the OpenVPN management interface
26 // and returns the extracted information. The message contains several
27 // `>CLIENT:` records separated by CRLF. Each line is inspected to collect the
28 // connection reason with its CID/KID and to read environment variables such as
29 // untrusted IP address, port and session identifiers. Unsupported or empty
30 // environment lines are ignored. If no valid reason line is found,
31 // ErrParseErrorClientReason is returned.
32 func NewClient(conf *config.Config, message string) (Client, error) { //nolint:cyclop
33 client := Client{}
34
35 var (
36 err error
37 line string
38 )
39
40 ok := true
41 clientMessage := message
42
43 for ok {
44 line, clientMessage, ok = strings.Cut(clientMessage, "\r\n")
45 line = strings.TrimSpace(line)
46
47 switch {
48 case strings.HasPrefix(line, ">CLIENT:ADDRESS"):
49 client.VPNAddress, err = parseClientVPNAddress(line)
50 if err != nil {
51 return Client{}, err
52 }
53 case strings.HasPrefix(line, ">CLIENT:ENV,"):
54 envKey, envValue := parseClientEnv(line)
55 if envKey == "" || envValue == "" {
56 continue
57 }
58
59 switch envKey {
60 case "untrusted_ip", "untrusted_ip6":
61 client.IPAddr = envValue
62 case "untrusted_port":
63 client.IPPort = envValue
64 case conf.OpenVPN.CommonName.EnvironmentVariableName:
65 client.CommonName = envValue
66 if conf.OpenVPN.CommonName.EnvironmentVariableName == "username" {
67 client.UsernameIsDefined = 1
68 }
69 case "IV_SSO":
70 client.IvSSO = envValue
71 case "session_id":
72 client.SessionID = envValue
73 case "session_state":
74 client.SessionState = envValue
75 case "username":
76 client.UsernameIsDefined = 1
77 }
78 case client.Reason == "" && isClientReason(line):
79 client.Reason, client.CID, client.KID, err = parseClientReason(line)
80 if err != nil {
81 return Client{}, err
82 }
83 }
84 }
85
86 if client.Reason == "" {
87 return Client{}, fmt.Errorf("%w: %s", ErrParseErrorClientReason, message)
88 }
89
90 return client, nil
91 }
92
93 func parseClientVPNAddress(line string) (string, error) {
94 vpnIP, found := strings.CutPrefix(line, ">CLIENT:ADDRESS,")
95 if !found {
96 return "", fmt.Errorf("unable to parse line: %s", line)
97 }
98
99 _, vpnIP, found = strings.Cut(vpnIP, ",")
100 if !found {
101 return "", fmt.Errorf("unable to parse line: %s", line)
102 }
103
104 vpnIP, _, found = strings.Cut(vpnIP, ",")
105 if !found {
106 return "", fmt.Errorf("unable to parse line: %s", line)
107 }
108
109 return vpnIP, nil
110 }
111
112 func parseClientEnv(line string) (string, string) {
113 comma := strings.Index(line, ",") + 1
114 key, value, ok := strings.Cut(line[comma:], "=")
115
116 if value == "END" {
117 return "", ""
118 }
119
120 if !ok {
121 return key, ""
122 }
123
124 return key, value
125 }
126
127 func parseClientReason(line string) (string, uint64, uint64, error) {
128 reason, clientIDs, ok := strings.Cut(line, ",")
129 if !ok {
130 return "", 0, 0, fmt.Errorf("unable to parse line '%s': %w", line, ErrInvalidMessage)
131 }
132
133 _, reason, ok = strings.Cut(reason, ":")
134 if !ok || reason == "" {
135 return "", 0, 0, fmt.Errorf("unable to parse client reason: %w", ErrEmptyClientReasons)
136 }
137
138 cidString, kidString, _ := strings.Cut(clientIDs, ",")
139
140 cid, err := strconv.ParseUint(cidString, 10, 64)
141 if err != nil {
142 return "", 0, 0, fmt.Errorf("unable to parse cid: %w", err)
143 }
144
145 var kid uint64
146
147 if reason == "DISCONNECT" || reason == "ESTABLISHED" {
148 return reason, cid, kid, nil
149 }
150
151 // kidString could contain a CR_RESPONSE, cut it again
152 kidString, _, _ = strings.Cut(kidString, ",")
153
154 kid, err = strconv.ParseUint(kidString, 10, 64)
155 if err != nil {
156 return "", 0, 0, fmt.Errorf("unable to parse kid: %w", err)
157 }
158
159 return reason, cid, kid, nil
160 }
161
162 func isClientReason(line string) bool {
163 return strings.HasPrefix(line, ">CLIENT:CONNECT") ||
164 strings.HasPrefix(line, ">CLIENT:REAUTH") ||
165 strings.HasPrefix(line, ">CLIENT:DISCONNECT") ||
166 strings.HasPrefix(line, ">CLIENT:ESTABLISHED") ||
167 strings.HasPrefix(line, ">CLIENT:CR_RESPONSE")
168 }