chore: move to sync map
This commit is contained in:
parent
de23b3e815
commit
0333680a2b
51
pkg/maps/map.go
Normal file
51
pkg/maps/map.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package maps
|
||||||
|
|
||||||
|
import (
|
||||||
|
"iter"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Map[K comparable, V any] struct {
|
||||||
|
items map[K]V
|
||||||
|
mu sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func New[K comparable, V any]() *Map[K, V] {
|
||||||
|
return &Map[K, V]{items: make(map[K]V)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Map[K, V]) Get(key K) (V, bool) {
|
||||||
|
m.mu.RLock()
|
||||||
|
defer m.mu.RUnlock()
|
||||||
|
v, ok := m.items[key]
|
||||||
|
return v, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Map[K, V]) Set(key K, value V) {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
m.items[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Map[K, V]) Delete(key K) {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
delete(m.items, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Map[K, V]) HasKey(key K) bool {
|
||||||
|
m.mu.RLock()
|
||||||
|
defer m.mu.RUnlock()
|
||||||
|
_, ok := m.items[key]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Map[K, V]) Entries() iter.Seq2[K, V] {
|
||||||
|
return func(yield func(K, V) bool) {
|
||||||
|
for k, v := range m.items {
|
||||||
|
if !yield(k, v) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -11,12 +11,12 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"reichard.io/conduit/config"
|
"reichard.io/conduit/config"
|
||||||
|
"reichard.io/conduit/pkg/maps"
|
||||||
"reichard.io/conduit/tunnel"
|
"reichard.io/conduit/tunnel"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -33,10 +33,9 @@ type TunnelInfo struct {
|
|||||||
type Server struct {
|
type Server struct {
|
||||||
host string
|
host string
|
||||||
cfg *config.ServerConfig
|
cfg *config.ServerConfig
|
||||||
mu sync.RWMutex
|
|
||||||
|
|
||||||
upgrader websocket.Upgrader
|
upgrader websocket.Upgrader
|
||||||
tunnels map[string]*tunnel.Tunnel
|
tunnels *maps.Map[string, *tunnel.Tunnel]
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(cfg *config.ServerConfig) (*Server, error) {
|
func NewServer(cfg *config.ServerConfig) (*Server, error) {
|
||||||
@ -50,7 +49,7 @@ func NewServer(cfg *config.ServerConfig) (*Server, error) {
|
|||||||
return &Server{
|
return &Server{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
host: serverURL.Host,
|
host: serverURL.Host,
|
||||||
tunnels: make(map[string]*tunnel.Tunnel),
|
tunnels: maps.New[string, *tunnel.Tunnel](),
|
||||||
upgrader: websocket.Upgrader{
|
upgrader: websocket.Upgrader{
|
||||||
CheckOrigin: func(r *http.Request) bool {
|
CheckOrigin: func(r *http.Request) bool {
|
||||||
return true
|
return true
|
||||||
@ -84,14 +83,12 @@ func (s *Server) Start() error {
|
|||||||
func (s *Server) getInfo(w http.ResponseWriter, _ *http.Request) {
|
func (s *Server) getInfo(w http.ResponseWriter, _ *http.Request) {
|
||||||
// Get Tunnels
|
// Get Tunnels
|
||||||
var allTunnels []TunnelInfo
|
var allTunnels []TunnelInfo
|
||||||
s.mu.RLock()
|
for t, c := range s.tunnels.Entries() {
|
||||||
for t, c := range s.tunnels {
|
|
||||||
allTunnels = append(allTunnels, TunnelInfo{
|
allTunnels = append(allTunnels, TunnelInfo{
|
||||||
Name: t,
|
Name: t,
|
||||||
Target: c.Source(),
|
Target: c.Source(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
s.mu.RUnlock()
|
|
||||||
|
|
||||||
// Create Response
|
// Create Response
|
||||||
d, err := json.MarshalIndent(InfoResponse{
|
d, err := json.MarshalIndent(InfoResponse{
|
||||||
@ -152,9 +149,7 @@ func (s *Server) handleRawConnection(conn net.Conn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle Tunnels
|
// Handle Tunnels
|
||||||
s.mu.RLock()
|
conduitTunnel, exists := s.tunnels.Get(subdomain)
|
||||||
conduitTunnel, exists := s.tunnels[subdomain]
|
|
||||||
s.mu.RUnlock()
|
|
||||||
if !exists {
|
if !exists {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
_, _ = fmt.Fprintf(w, "unknown tunnel: %s", subdomain)
|
_, _ = fmt.Fprintf(w, "unknown tunnel: %s", subdomain)
|
||||||
@ -204,7 +199,7 @@ func (s *Server) createTunnel(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Validate Unique
|
// Validate Unique
|
||||||
if _, exists := s.tunnels[tunnelName]; exists {
|
if _, exists := s.tunnels.Get(tunnelName); exists {
|
||||||
w.WriteHeader(http.StatusConflict)
|
w.WriteHeader(http.StatusConflict)
|
||||||
_, _ = w.Write([]byte("Tunnel already registered"))
|
_, _ = w.Write([]byte("Tunnel already registered"))
|
||||||
return
|
return
|
||||||
@ -219,18 +214,14 @@ func (s *Server) createTunnel(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// Create Tunnel
|
// Create Tunnel
|
||||||
conduitTunnel := tunnel.NewServerTunnel(tunnelName, wsConn)
|
conduitTunnel := tunnel.NewServerTunnel(tunnelName, wsConn)
|
||||||
s.mu.Lock()
|
s.tunnels.Set(tunnelName, conduitTunnel)
|
||||||
s.tunnels[tunnelName] = conduitTunnel
|
|
||||||
s.mu.Unlock()
|
|
||||||
log.Infof("tunnel established: %s", tunnelName)
|
log.Infof("tunnel established: %s", tunnelName)
|
||||||
|
|
||||||
// Start Tunnel - This is blocking
|
// Start Tunnel - This is blocking
|
||||||
conduitTunnel.Start()
|
conduitTunnel.Start()
|
||||||
|
|
||||||
// Cleanup Tunnel
|
// Cleanup Tunnel
|
||||||
s.mu.Lock()
|
s.tunnels.Delete(tunnelName)
|
||||||
delete(s.tunnels, tunnelName)
|
|
||||||
s.mu.Unlock()
|
|
||||||
_ = wsConn.Close()
|
_ = wsConn.Close()
|
||||||
log.Infof("tunnel closed: %s", tunnelName)
|
log.Infof("tunnel closed: %s", tunnelName)
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"reichard.io/conduit/pkg/maps"
|
||||||
"reichard.io/conduit/types"
|
"reichard.io/conduit/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -17,8 +18,8 @@ type ConnBuilder func() (conn io.ReadWriteCloser, err error)
|
|||||||
func NewServerTunnel(name string, wsConn *websocket.Conn) *Tunnel {
|
func NewServerTunnel(name string, wsConn *websocket.Conn) *Tunnel {
|
||||||
return &Tunnel{
|
return &Tunnel{
|
||||||
name: name,
|
name: name,
|
||||||
|
streams: maps.New[string, io.ReadWriteCloser](),
|
||||||
wsConn: wsConn,
|
wsConn: wsConn,
|
||||||
streams: make(map[string]io.ReadWriteCloser),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,7 +47,7 @@ func NewClientTunnel(name, target string, wsConn *websocket.Conn) (*Tunnel, erro
|
|||||||
return &Tunnel{
|
return &Tunnel{
|
||||||
name: name,
|
name: name,
|
||||||
wsConn: wsConn,
|
wsConn: wsConn,
|
||||||
streams: make(map[string]io.ReadWriteCloser),
|
streams: maps.New[string, io.ReadWriteCloser](),
|
||||||
connBuilder: connBuilder,
|
connBuilder: connBuilder,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
@ -54,10 +55,10 @@ func NewClientTunnel(name, target string, wsConn *websocket.Conn) (*Tunnel, erro
|
|||||||
type Tunnel struct {
|
type Tunnel struct {
|
||||||
name string
|
name string
|
||||||
wsConn *websocket.Conn
|
wsConn *websocket.Conn
|
||||||
streams map[string]io.ReadWriteCloser
|
streams *maps.Map[string, io.ReadWriteCloser]
|
||||||
connBuilder ConnBuilder
|
connBuilder ConnBuilder
|
||||||
|
|
||||||
wsMu, streamsMu sync.Mutex
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tunnel) Start() {
|
func (t *Tunnel) Start() {
|
||||||
@ -95,7 +96,7 @@ func (t *Tunnel) initStreamConnection(streamID string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, found := t.getStream(streamID); found {
|
if _, found := t.streams.Get(streamID); found {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,19 +114,16 @@ func (t *Tunnel) initStreamConnection(streamID string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tunnel) AddStream(streamID string, conn io.ReadWriteCloser) error {
|
func (t *Tunnel) AddStream(streamID string, conn io.ReadWriteCloser) error {
|
||||||
t.streamsMu.Lock()
|
if t.streams.HasKey(streamID) {
|
||||||
defer t.streamsMu.Unlock()
|
|
||||||
|
|
||||||
if _, found := t.streams[streamID]; found {
|
|
||||||
return fmt.Errorf("stream %s already exists", streamID)
|
return fmt.Errorf("stream %s already exists", streamID)
|
||||||
}
|
}
|
||||||
t.streams[streamID] = conn
|
t.streams.Set(streamID, conn)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tunnel) StartStream(streamID string) error {
|
func (t *Tunnel) StartStream(streamID string) error {
|
||||||
// Get Stream
|
// Get Stream
|
||||||
conn, found := t.getStream(streamID)
|
conn, found := t.streams.Get(streamID)
|
||||||
if !found {
|
if !found {
|
||||||
return fmt.Errorf("stream %s does not exist", streamID)
|
return fmt.Errorf("stream %s does not exist", streamID)
|
||||||
}
|
}
|
||||||
@ -160,7 +158,7 @@ func (t *Tunnel) StartStream(streamID string) error {
|
|||||||
|
|
||||||
func (t *Tunnel) WriteStream(streamID string, data []byte) error {
|
func (t *Tunnel) WriteStream(streamID string, data []byte) error {
|
||||||
// Get Stream
|
// Get Stream
|
||||||
conn, found := t.getStream(streamID)
|
conn, found := t.streams.Get(streamID)
|
||||||
if !found {
|
if !found {
|
||||||
return fmt.Errorf("stream %s does not exist", streamID)
|
return fmt.Errorf("stream %s does not exist", streamID)
|
||||||
}
|
}
|
||||||
@ -170,10 +168,8 @@ func (t *Tunnel) WriteStream(streamID string, data []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tunnel) CloseStream(streamID string) error {
|
func (t *Tunnel) CloseStream(streamID string) error {
|
||||||
t.streamsMu.Lock()
|
if conn, ok := t.streams.Get(streamID); ok {
|
||||||
defer t.streamsMu.Unlock()
|
t.streams.Delete(streamID)
|
||||||
if conn, ok := t.streams[streamID]; ok {
|
|
||||||
delete(t.streams, streamID)
|
|
||||||
return conn.Close()
|
return conn.Close()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -184,17 +180,7 @@ func (t *Tunnel) Source() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tunnel) sendWS(msg *types.Message) error {
|
func (t *Tunnel) sendWS(msg *types.Message) error {
|
||||||
t.wsMu.Lock()
|
t.mu.Lock()
|
||||||
defer t.wsMu.Unlock()
|
defer t.mu.Unlock()
|
||||||
return t.wsConn.WriteJSON(msg)
|
return t.wsConn.WriteJSON(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tunnel) getStream(streamID string) (io.ReadWriteCloser, bool) {
|
|
||||||
t.streamsMu.Lock()
|
|
||||||
defer t.streamsMu.Unlock()
|
|
||||||
|
|
||||||
if conn, ok := t.streams[streamID]; ok {
|
|
||||||
return conn, true
|
|
||||||
}
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user