Debian Codegraph @

internal/openvpn/status.go

raw
1 package openvpn
2
3 import (
4 "context"
5 "errors"
6 "fmt"
7 "log/slog"
8 "slices"
9 "strconv"
10 "strings"
11 )
12
13 const (
14 clientKillHaltMessage = "HALT"
15 clientKillMissingResponse = "ERROR: client-kill command failed"
16 )
17
18 func (c *Client) enforceUniqueUser(ctx context.Context, logger *slog.Logger, currentCID uint64, username string) error {
19 if username == "" {
20 return errors.New("cannot enforce a single active session: username is empty")
21 }
22
23 status, err := c.SendCommand(ctx, "status 3", false)
24 if err != nil {
25 return fmt.Errorf("query OpenVPN status: %w", err)
26 }
27
28 clientIDs, err := statusClientIDsByUsername(status, username, currentCID)
29 if err != nil {
30 return fmt.Errorf("parse OpenVPN status: %w", err)
31 }
32
33 for _, clientID := range clientIDs {
34 logger.LogAttrs(
35 ctx, slog.LevelInfo, "terminate existing OpenVPN session",
36 slog.String("user_username", username),
37 slog.Uint64("existing_cid", clientID),
38 )
39
40 response, err := c.SendCommandf(ctx, "client-kill %d %s", clientID, clientKillHaltMessage)
41
42 response = strings.TrimSpace(response)
43 if errors.Is(err, ErrErrorResponse) && response == clientKillMissingResponse {
44 logger.LogAttrs(
45 ctx, slog.LevelDebug, "existing OpenVPN session already terminated",
46 slog.Uint64("existing_cid", clientID),
47 )
48
49 continue
50 }
51
52 if err != nil {
53 return fmt.Errorf("terminate OpenVPN client %d: %w", clientID, err)
54 }
55 }
56
57 return nil
58 }
59
60 func statusClientIDsByUsername(status, username string, currentCID uint64) ([]uint64, error) {
61 usernameIndex, clientIDIndex, err := statusClientListColumns(status)
62 if err != nil {
63 return nil, err
64 }
65
66 clientIDs := make([]uint64, 0)
67 seenClientIDs := make(map[uint64]struct{})
68
69 for line := range strings.SplitSeq(status, "\n") {
70 fields := strings.Split(strings.TrimSuffix(line, "\r"), "\t")
71 if len(fields) == 0 || fields[0] != "CLIENT_LIST" {
72 continue
73 }
74
75 clientID, matches, err := statusClientID(fields, usernameIndex, clientIDIndex, username, currentCID)
76 if err != nil {
77 return nil, err
78 }
79
80 if !matches {
81 continue
82 }
83
84 if _, ok := seenClientIDs[clientID]; ok {
85 continue
86 }
87
88 seenClientIDs[clientID] = struct{}{}
89 clientIDs = append(clientIDs, clientID)
90 }
91
92 return clientIDs, nil
93 }
94
95 func statusClientListColumns(status string) (int, int, error) {
96 for line := range strings.SplitSeq(status, "\n") {
97 fields := strings.Split(strings.TrimSuffix(line, "\r"), "\t")
98 if len(fields) < 2 || fields[0] != "HEADER" || fields[1] != "CLIENT_LIST" {
99 continue
100 }
101
102 var (
103 usernameIndex = slices.Index(fields, "Username") - 1
104 clientIDIndex = slices.Index(fields, "Client ID") - 1
105 )
106 if usernameIndex < 0 || clientIDIndex < 0 {
107 break
108 }
109
110 return usernameIndex, clientIDIndex, nil
111 }
112
113 return 0, 0, errors.New("client list header is missing Username or Client ID")
114 }
115
116 func statusClientID(fields []string, usernameIndex, clientIDIndex int, username string, currentCID uint64) (uint64, bool, error) {
117 if usernameIndex >= len(fields) || clientIDIndex >= len(fields) {
118 return 0, false, fmt.Errorf(
119 "client list record has %d fields, expected at least %d",
120 len(fields),
121 max(usernameIndex, clientIDIndex)+1,
122 )
123 }
124
125 if fields[usernameIndex] != username {
126 return 0, false, nil
127 }
128
129 clientID, err := strconv.ParseUint(fields[clientIDIndex], 10, 64)
130 if err != nil {
131 return 0, false, fmt.Errorf("parse client list client ID %q: %w", fields[clientIDIndex], err)
132 }
133
134 return clientID, clientID != currentCID, nil
135 }