2023-11-17 20:39:00 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2023-11-22 22:21:11 +03:00
|
|
|
"strings"
|
2023-11-17 20:39:00 +03:00
|
|
|
"sync"
|
2023-11-22 22:21:11 +03:00
|
|
|
"time"
|
2023-11-17 20:39:00 +03:00
|
|
|
|
|
|
|
"github.com/Neur0toxine/sshpoke/internal/config"
|
2023-11-18 16:14:39 +03:00
|
|
|
"github.com/Neur0toxine/sshpoke/internal/docker"
|
2023-11-17 20:39:00 +03:00
|
|
|
"github.com/Neur0toxine/sshpoke/internal/logger"
|
|
|
|
"github.com/Neur0toxine/sshpoke/internal/server/driver"
|
2023-11-18 16:14:39 +03:00
|
|
|
"github.com/Neur0toxine/sshpoke/internal/server/driver/base"
|
2023-11-17 20:39:00 +03:00
|
|
|
"github.com/Neur0toxine/sshpoke/internal/server/driver/plugin"
|
2023-11-18 12:36:17 +03:00
|
|
|
"github.com/Neur0toxine/sshpoke/pkg/dto"
|
2023-11-17 20:39:00 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Manager struct {
|
|
|
|
rw sync.RWMutex
|
2023-11-22 22:21:11 +03:00
|
|
|
forwardsLock sync.Mutex
|
2023-11-18 16:14:39 +03:00
|
|
|
servers map[string]base.Driver
|
2023-11-18 12:36:17 +03:00
|
|
|
plugins map[string]plugin.Plugin
|
2023-11-22 22:21:11 +03:00
|
|
|
statusMap map[string]ServerStatus
|
|
|
|
forwards map[string]bool
|
2023-11-18 16:14:39 +03:00
|
|
|
statusLock sync.RWMutex
|
2023-11-22 22:21:11 +03:00
|
|
|
ctx context.Context
|
2023-11-17 20:39:00 +03:00
|
|
|
defaultServer string
|
|
|
|
}
|
|
|
|
|
2023-11-22 22:21:11 +03:00
|
|
|
type ServerStatus struct {
|
2023-11-18 16:14:39 +03:00
|
|
|
Name string `json:"name"`
|
|
|
|
Connections Connections `json:"connections"`
|
|
|
|
}
|
|
|
|
|
2023-11-17 20:39:00 +03:00
|
|
|
var DefaultManager *Manager
|
|
|
|
var (
|
|
|
|
ErrNoServer = errors.New("server is not specified")
|
|
|
|
ErrNoSuchServer = errors.New("server does not exist")
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewManager(ctx context.Context, servers []config.Server, defaultServer string) *Manager {
|
|
|
|
m := &Manager{
|
2023-11-22 22:21:11 +03:00
|
|
|
ctx: ctx,
|
2023-11-18 16:14:39 +03:00
|
|
|
servers: make(map[string]base.Driver),
|
2023-11-18 12:36:17 +03:00
|
|
|
plugins: make(map[string]plugin.Plugin),
|
2023-11-22 22:21:11 +03:00
|
|
|
statusMap: make(map[string]ServerStatus),
|
|
|
|
forwards: make(map[string]bool),
|
2023-11-17 20:39:00 +03:00
|
|
|
defaultServer: defaultServer,
|
|
|
|
}
|
|
|
|
for _, serverConfig := range servers {
|
|
|
|
server, err := driver.New(ctx, serverConfig.Name, serverConfig.Driver, serverConfig.Params)
|
|
|
|
if err != nil {
|
|
|
|
logger.Sugar.Errorf("cannot initialize server '%s': %s", serverConfig.Name, err)
|
|
|
|
continue
|
|
|
|
}
|
2023-11-18 16:14:39 +03:00
|
|
|
server.SetEventStatusCallback(m.eventStatusCallback(server.Name()))
|
2023-11-18 12:36:17 +03:00
|
|
|
if server.Driver() == config.DriverPlugin {
|
|
|
|
pl := server.(plugin.Plugin)
|
|
|
|
if pl.Token() == "" {
|
|
|
|
logger.Sugar.Warnf("server '%s' will not work because it doesn't have a token", pl.Name())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
existing, found := m.plugins[pl.Token()]
|
|
|
|
if found {
|
|
|
|
logger.Sugar.Fatalw("two plugins cannot have the same token",
|
|
|
|
"plugin1", existing.Name(), "plugin2", pl.Name(), "token", pl.Token())
|
|
|
|
}
|
|
|
|
m.plugins[pl.Token()] = pl
|
|
|
|
}
|
2023-11-17 20:39:00 +03:00
|
|
|
m.servers[serverConfig.Name] = server
|
2023-11-22 22:21:11 +03:00
|
|
|
m.statusMap[serverConfig.Name] = ServerStatus{Name: serverConfig.Name, Connections: make(Connections)}
|
2023-11-17 20:39:00 +03:00
|
|
|
}
|
2023-11-22 22:21:11 +03:00
|
|
|
go m.runMarkAndSweepForwards()
|
2023-11-17 20:39:00 +03:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2023-11-18 12:36:17 +03:00
|
|
|
func (m *Manager) ProcessEvent(event dto.Event) error {
|
2023-11-17 20:39:00 +03:00
|
|
|
serverName := event.Container.Server
|
|
|
|
if serverName == "" {
|
|
|
|
serverName = m.defaultServer
|
|
|
|
}
|
|
|
|
if serverName == "" {
|
|
|
|
return ErrNoServer
|
|
|
|
}
|
|
|
|
defer m.rw.RUnlock()
|
|
|
|
m.rw.RLock()
|
|
|
|
srv, ok := m.servers[event.Container.Server]
|
|
|
|
if !ok {
|
|
|
|
return ErrNoSuchServer
|
|
|
|
}
|
2023-11-22 22:21:11 +03:00
|
|
|
if err := srv.Handle(event); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer m.forwardsLock.Unlock()
|
|
|
|
m.forwardsLock.Lock()
|
|
|
|
switch event.Type {
|
|
|
|
case dto.EventStart:
|
|
|
|
m.forwards[m.forwardID(serverName, event.Container.ID)] = false
|
|
|
|
case dto.EventStop, dto.EventError, dto.EventShutdown:
|
|
|
|
delete(m.forwards, m.forwardID(serverName, event.Container.ID))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) forwardID(serverName, containerID string) string {
|
|
|
|
return serverName + ":" + containerID
|
2023-11-17 20:39:00 +03:00
|
|
|
}
|
|
|
|
|
2023-11-18 16:14:39 +03:00
|
|
|
func (m *Manager) eventStatusCallback(serverName string) base.EventStatusCallback {
|
|
|
|
return func(status dto.EventStatus) {
|
|
|
|
m.processEventStatus(serverName, status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) processEventStatus(serverName string, event dto.EventStatus) {
|
2023-11-19 13:06:38 +03:00
|
|
|
logger.Sugar.Debugw("received EventStatus from server",
|
|
|
|
"serverName", serverName, "eventStatus", event)
|
2023-11-22 22:21:11 +03:00
|
|
|
item, found := docker.Default.GetContainer(event.ID, true)
|
2023-11-18 16:14:39 +03:00
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
2023-11-22 22:21:11 +03:00
|
|
|
defer m.forwardsLock.Unlock()
|
|
|
|
m.forwardsLock.Lock()
|
2023-11-18 16:14:39 +03:00
|
|
|
switch event.Type {
|
|
|
|
case dto.EventStart:
|
2023-11-22 22:21:11 +03:00
|
|
|
defer m.statusLock.Unlock()
|
|
|
|
m.statusLock.Lock()
|
2023-11-18 16:14:39 +03:00
|
|
|
item.Domain = event.Domain
|
2023-11-22 22:21:11 +03:00
|
|
|
m.forwards[m.forwardID(serverName, item.ID)] = false
|
|
|
|
m.statusMap[serverName].Connections[item.ID] = item
|
2023-11-18 16:14:39 +03:00
|
|
|
case dto.EventStop, dto.EventShutdown, dto.EventError:
|
2023-11-22 22:21:11 +03:00
|
|
|
defer m.statusLock.Unlock()
|
|
|
|
m.statusLock.Lock()
|
2023-11-18 16:14:39 +03:00
|
|
|
item.Domain = ""
|
2023-11-22 22:21:11 +03:00
|
|
|
delete(m.forwards, m.forwardID(serverName, item.ID))
|
|
|
|
delete(m.statusMap[serverName].Connections, item.ID)
|
2023-11-18 16:14:39 +03:00
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
2023-11-22 22:21:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) StatusMap() map[string]ServerStatus {
|
|
|
|
defer m.statusLock.RUnlock()
|
|
|
|
m.statusLock.RLock()
|
|
|
|
return m.statusMap
|
|
|
|
}
|
|
|
|
|
|
|
|
// runMarkAndSweepForwards runs mark-and-sweep on the started forwards every 10 seconds.
|
|
|
|
// This job is necessary because Docker sometimes forgets to notify us that containers
|
|
|
|
// were stopped (usually happens when spamming Ctrl+C after `docker compose run`).
|
|
|
|
func (m *Manager) runMarkAndSweepForwards() {
|
|
|
|
ticker := time.NewTicker(time.Second * 10)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-m.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
m.markAndSweepForwards()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// markAndSweepForwards marks stopped containers for removal on the first run and removes them from forwards later.
|
|
|
|
// This job will remove containers from forwards if Docker didn't notify us about stopping containers for some reason.
|
|
|
|
func (m *Manager) markAndSweepForwards() {
|
|
|
|
defer m.forwardsLock.Unlock()
|
|
|
|
m.forwardsLock.Lock()
|
|
|
|
|
|
|
|
for id, state := range m.forwards {
|
|
|
|
forwardIDs := strings.Split(id, ":")
|
|
|
|
serverName, containerID := forwardIDs[0], forwardIDs[1]
|
|
|
|
_, found := docker.Default.GetContainer(containerID, false)
|
|
|
|
if found {
|
|
|
|
m.forwards[id] = false // unmark
|
|
|
|
} else {
|
|
|
|
if state {
|
|
|
|
m.processEventStatus(serverName, dto.EventStatus{
|
|
|
|
Type: dto.EventStop,
|
|
|
|
ID: containerID,
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m.forwards[id] = true // mark
|
|
|
|
}
|
|
|
|
}
|
2023-11-18 16:14:39 +03:00
|
|
|
}
|
|
|
|
|
2023-11-18 12:36:17 +03:00
|
|
|
func (m *Manager) PluginByToken(token string) plugin.Plugin {
|
|
|
|
server, ok := m.plugins[token]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
2023-11-17 20:39:00 +03:00
|
|
|
}
|
2023-11-18 12:36:17 +03:00
|
|
|
return server
|
2023-11-17 20:39:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) WaitForShutdown() {
|
|
|
|
defer m.rw.RUnlock()
|
|
|
|
m.rw.RLock()
|
|
|
|
for _, srv := range m.servers {
|
|
|
|
srv.WaitForShutdown()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|