github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/services/openvpn/service/client_map.go (about)

     1  /*
     2   * Copyright (C) 2018 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package service
    19  
    20  import (
    21  	"sync"
    22  
    23  	"github.com/mysteriumnetwork/node/core/service"
    24  	"github.com/mysteriumnetwork/node/session"
    25  )
    26  
    27  // SessionMap defines map of current sessions
    28  type SessionMap interface {
    29  	Find(session.ID) (*service.Session, bool)
    30  }
    31  
    32  // clientMap extends current sessions with client id metadata from Openvpn.
    33  type clientMap struct {
    34  	sessions SessionMap
    35  	// TODO: use clientID to kill OpenVPN session (client-kill {clientID}) when promise processor instructs so
    36  	sessionClientIDs map[int]session.ID
    37  	sessionMapLock   sync.Mutex
    38  }
    39  
    40  // NewClientMap creates a new instance of client map.
    41  func NewClientMap(sessionMap SessionMap) *clientMap {
    42  	return &clientMap{
    43  		sessions:         sessionMap,
    44  		sessionClientIDs: make(map[int]session.ID),
    45  	}
    46  }
    47  
    48  // Add adds OpenVPN client with used session ID.
    49  func (cm *clientMap) Add(clientID int, sessionID session.ID) {
    50  	cm.sessionMapLock.Lock()
    51  	defer cm.sessionMapLock.Unlock()
    52  
    53  	cm.sessionClientIDs[clientID] = sessionID
    54  }
    55  
    56  // Remove removes given OpenVPN client.
    57  func (cm *clientMap) Remove(clientID int) {
    58  	cm.sessionMapLock.Lock()
    59  	defer cm.sessionMapLock.Unlock()
    60  
    61  	delete(cm.sessionClientIDs, clientID)
    62  }
    63  
    64  // GetSession returns ongoing session instance by given session id.
    65  func (cm *clientMap) GetSession(id session.ID) (*service.Session, bool) {
    66  	return cm.sessions.Find(id)
    67  }
    68  
    69  // GetSessionClients returns Openvpn clients which are using given session.
    70  func (cm *clientMap) GetSessionClients(id session.ID) []int {
    71  	cm.sessionMapLock.Lock()
    72  	defer cm.sessionMapLock.Unlock()
    73  
    74  	res := make([]int, 0)
    75  	for clientID, sessionID := range cm.sessionClientIDs {
    76  		if id == sessionID {
    77  			res = append(res, clientID)
    78  		}
    79  	}
    80  	return res
    81  }
    82  
    83  // GetClientSession returns session for given Openvpn client.
    84  func (cm *clientMap) GetClientSession(clientID int) (session.ID, bool) {
    85  	cm.sessionMapLock.Lock()
    86  	defer cm.sessionMapLock.Unlock()
    87  
    88  	sessionID, exist := cm.sessionClientIDs[clientID]
    89  	return sessionID, exist
    90  }