yans/internal/server/nntp_server.go

160 lines
3.5 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"
2022-04-13 10:10:11 +00:00
"net/http"
"nhooyr.io/websocket"
"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)},
2022-02-05 18:51:43 +00:00
{Type: protocol.OverCapability, Params: "MSGID"},
{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())
2022-04-13 10:10:11 +00:00
if err := ns.handleConn(ctx, conn, conn.RemoteAddr().String()); err != nil {
log.Println(err)
}
2022-01-17 22:38:56 +00:00
}
}
}
}(ns.ctx)
2022-04-13 10:10:11 +00:00
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{InsecureSkipVerify: true})
if err != nil {
log.Println(err)
return
}
log.Printf("Client %s has connected!", r.RemoteAddr)
if err := ns.handleConn(ns.ctx, websocket.NetConn(ns.ctx, c, websocket.MessageText), r.RemoteAddr); err != nil {
log.Println(err)
}
})
go http.ListenAndServe(fmt.Sprintf("%s:%d", ns.cfg.Address, ns.cfg.WSPort), nil)
return nil
}
func (ns *NNTPServer) handleConn(ctx context.Context, conn net.Conn, remoteAddr string) error {
id, _ := uuid.NewUUID()
closed := make(chan bool)
session, err := NewSession(ctx, conn, remoteAddr, Capabilities, id.String(), closed, NewHandler(ns.backend, ns.cfg.Domain, ns.cfg.UploadPath))
if err != nil {
return err
}
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
return nil
}
func (ns *NNTPServer) Stop() {
ns.cancelFunc()
}