yans/internal/server/nntp_server.go

131 lines
2.8 KiB
Go
Raw Normal View History

2022-01-17 22:38:56 +00:00
package server
import (
"context"
"fmt"
2022-01-19 19:51:08 +00:00
"github.com/ChronosX88/yans/internal/backend"
"github.com/ChronosX88/yans/internal/backend/sqlite"
"github.com/ChronosX88/yans/internal/common"
2022-01-17 22:38:56 +00:00
"github.com/ChronosX88/yans/internal/config"
"github.com/ChronosX88/yans/internal/protocol"
"github.com/google/uuid"
2022-01-17 22:38:56 +00:00
"log"
"net"
"sync"
2022-01-17 22:38:56 +00:00
)
var (
Capabilities = protocol.Capabilities{
{Type: protocol.VersionCapability, Params: "2"},
{Type: protocol.ImplementationCapability, Params: fmt.Sprintf("%s %s", common.ServerName, common.ServerVersion)},
{Type: protocol.ModeReaderCapability},
}
)
2022-01-17 22:38:56 +00:00
type NNTPServer struct {
ctx context.Context
cancelFunc context.CancelFunc
2022-01-19 19:51:08 +00:00
ln net.Listener
cfg config.Config
2022-01-17 22:38:56 +00:00
2022-01-19 19:51:08 +00:00
backend backend.StorageBackend
sessionPool map[string]*Session
sessionPoolMutex sync.Mutex
2022-01-17 22:38:56 +00:00
}
func NewNNTPServer(cfg config.Config) (*NNTPServer, error) {
2022-01-19 19:51:08 +00:00
b, err := initBackend(cfg)
2022-01-17 22:38:56 +00:00
if err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
ns := &NNTPServer{
ctx: ctx,
cancelFunc: cancel,
2022-01-19 19:51:08 +00:00
cfg: cfg,
backend: b,
sessionPool: map[string]*Session{},
2022-01-17 22:38:56 +00:00
}
return ns, nil
}
2022-01-19 19:51:08 +00:00
func initBackend(cfg config.Config) (backend.StorageBackend, error) {
var sb backend.StorageBackend
switch cfg.BackendType {
case config.SQLiteBackendType:
{
sqliteBackend, err := sqlite.NewSQLiteBackend(cfg.SQLite)
if err != nil {
return nil, err
}
sb = sqliteBackend
}
default:
{
return nil, fmt.Errorf("invalid backend type, supported backends: %s", backend.SupportedBackendList)
}
}
return sb, nil
}
2022-01-17 22:38:56 +00:00
func (ns *NNTPServer) Start() error {
2022-01-19 19:51:08 +00:00
address := fmt.Sprintf("%s:%d", ns.cfg.Address, ns.cfg.Port)
ln, err := net.Listen("tcp", address)
2022-01-17 22:38:56 +00:00
if err != nil {
return err
}
2022-01-19 19:51:08 +00:00
log.Printf("Listening on %s...", address)
2022-01-17 22:38:56 +00:00
go func(ctx context.Context) {
for {
select {
case <-ctx.Done():
break
default:
{
conn, err := ln.Accept()
if err != nil {
log.Println(err)
}
log.Printf("Client %s has connected!", conn.RemoteAddr().String())
id, _ := uuid.NewUUID()
closed := make(chan bool)
2022-01-19 19:51:08 +00:00
session, err := NewSession(ctx, conn, Capabilities, id.String(), closed, NewHandler(ns.backend))
ns.sessionPoolMutex.Lock()
ns.sessionPool[id.String()] = session
ns.sessionPoolMutex.Unlock()
go func(ctx context.Context, id string, closed chan bool) {
for {
select {
case <-ctx.Done():
break
case _, ok := <-closed:
{
if !ok {
ns.sessionPoolMutex.Lock()
delete(ns.sessionPool, id)
ns.sessionPoolMutex.Unlock()
return
}
}
}
}
}(ctx, id.String(), closed)
2022-01-17 22:38:56 +00:00
}
}
}
}(ns.ctx)
return nil
}
func (ns *NNTPServer) Stop() {
ns.cancelFunc()
}