mirror of
https://github.com/hanzoai/vm.git
synced 2026-10-30 14:55:23 +00:00
- Rename Go module from github.com/casvisor/casvisor to github.com/hanzoai/visor - Update all copyright headers from casbin to Hanzo Industries Inc - Remove all hardcoded passwords and credentials from config files - All credentials now sourced from environment variables / Hanzo KMS - Switch default database from MySQL to PostgreSQL (matching production) - Rename binary output from server/casvisor to visor - Update Dockerfile, build.sh, docker-compose.yml, docker-entrypoint.sh - Replace casdoor* config key references with iam* equivalents - Update CI to use PostgreSQL service and mark CI-only test password - Add visor and casvisor binaries to .gitignore - Add envFrom secretRef for visor-env in K8s deployment - Add ExternalSecret template for Hanzo KMS credential provisioning Co-authored-by: Hanzo Dev <[email protected]>
118 lines
2.3 KiB
Go
118 lines
2.3 KiB
Go
// Copyright 2024 Hanzo Industries Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package guacamole
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
type Session struct {
|
|
Id string
|
|
Protocol string
|
|
Mode string
|
|
WebSocket *websocket.Conn
|
|
GuacdTunnel *Tunnel
|
|
Observer *Manager
|
|
mutex sync.Mutex
|
|
|
|
Uptime int64
|
|
Hostname string
|
|
}
|
|
|
|
func (s *Session) WriteString(str string) error {
|
|
if s.WebSocket == nil {
|
|
return nil
|
|
}
|
|
defer s.mutex.Unlock()
|
|
s.mutex.Lock()
|
|
message := []byte(str)
|
|
return s.WebSocket.WriteMessage(websocket.TextMessage, message)
|
|
}
|
|
|
|
func (s *Session) Close() {
|
|
if s.GuacdTunnel != nil {
|
|
_ = s.GuacdTunnel.Close()
|
|
}
|
|
|
|
if s.WebSocket != nil {
|
|
_ = s.WebSocket.Close()
|
|
}
|
|
}
|
|
|
|
type Manager struct {
|
|
id string
|
|
sessions sync.Map
|
|
}
|
|
|
|
func NewManager() *Manager {
|
|
return &Manager{}
|
|
}
|
|
|
|
func NewObserver(id string) *Manager {
|
|
return &Manager{
|
|
id: id,
|
|
}
|
|
}
|
|
|
|
func (m *Manager) Get(id string) *Session {
|
|
value, ok := m.sessions.Load(id)
|
|
if ok {
|
|
return value.(*Session)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *Manager) Add(s *Session) {
|
|
m.sessions.Store(s.Id, s)
|
|
}
|
|
|
|
func (m *Manager) Delete(id string) {
|
|
session := m.Get(id)
|
|
if session != nil {
|
|
session.Close()
|
|
if session.Observer != nil {
|
|
session.Observer.Clear()
|
|
}
|
|
}
|
|
m.sessions.Delete(id)
|
|
}
|
|
|
|
func (m *Manager) Clear() {
|
|
m.sessions.Range(func(key, value interface{}) bool {
|
|
if session, ok := value.(*Session); ok {
|
|
session.Close()
|
|
}
|
|
m.sessions.Delete(key)
|
|
return true
|
|
})
|
|
}
|
|
|
|
func (m *Manager) Range(f func(key string, value *Session)) {
|
|
m.sessions.Range(func(key, value interface{}) bool {
|
|
if session, ok := value.(*Session); ok {
|
|
f(key.(string), session)
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
|
|
var GlobalSessionManager *Manager
|
|
|
|
func init() {
|
|
GlobalSessionManager = NewManager()
|
|
}
|