mirror of
https://github.com/renorris/openfsd
synced 2026-03-22 06:25:35 +08:00
1. Database Enhancements (db/repositories.go): - Added ConfigRepository interface and implementations for PostgreSQL and SQLite - Updated Repositories struct to include ConfigRepository - Modified NewRepositories to initialize both UserRepo and ConfigRepo 2. FSD Server Improvements: - Removed hardcoded jwtSecret, now retrieved from ConfigRepository (fsd/conn.go, web/auth.go) - Added dynamic welcome message retrieval from ConfigRepository (fsd/conn.go) - Optimized METAR buffer size from 4096 to 512 bytes (fsd/metar.go) - Reduced minimum fields for DeleteATC and DeletePilot packets (fsd/packet.go) - Improved Haversine distance calculation with constants (fsd/postoffice.go) - Added thread-safety documentation for sendError (fsd/client.go) 3. Server Configuration (fsd/server.go): - Added NewDefaultServer to initialize server with environment-based config - Implemented automatic database migration and default admin user creation - Added configurable METAR worker count - Improved logging with slog and environment-based debug level 4. Web Interface Enhancements: - Added user and config editor frontend routes (web/frontend.go, web/routes.go) - Improved JWT handling by retrieving secret from ConfigRepository (web/auth.go) - Enhanced user management API endpoints (web/user.go) - Updated dashboard to display CID and conditional admin links (web/templates/dashboard.html) - Embedded templates using go:embed (web/templates.go) 5. Frontend JavaScript Improvements: - Added networkRatingFromInt helper for readable ratings (web/static/js/openfsd/dashboard.js) - Improved API request handling with auth/no-auth variants (web/static/js/openfsd/api.js) 6. Miscellaneous: - Added sethvargo/go-envconfig dependency for environment variable parsing - Fixed parseVisRange to use 64-bit float parsing (fsd/util.go) - Added strPtr utility function (fsd/util.go, web/main.go) - Improved SVG logo rendering in layout (web/templates/layout.html)
117 lines
2.5 KiB
Go
117 lines
2.5 KiB
Go
package fsd
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"go.uber.org/atomic"
|
|
"net"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type Client struct {
|
|
conn net.Conn
|
|
scanner *bufio.Scanner
|
|
ctx context.Context
|
|
cancelCtx func()
|
|
sendChan chan string
|
|
|
|
latLon [2]float64
|
|
visRange float64
|
|
|
|
flightPlan *atomic.String
|
|
beaconCode *atomic.String
|
|
|
|
facilityType int // ATC facility type. This value is only relevant for ATC
|
|
loginData
|
|
|
|
authState vatsimAuthState
|
|
}
|
|
|
|
func newClient(ctx context.Context, conn net.Conn, scanner *bufio.Scanner, loginData loginData) (client *Client) {
|
|
clientCtx, cancel := context.WithCancel(ctx)
|
|
return &Client{
|
|
conn: conn,
|
|
scanner: scanner,
|
|
ctx: clientCtx,
|
|
cancelCtx: cancel,
|
|
sendChan: make(chan string, 32),
|
|
flightPlan: &atomic.String{},
|
|
beaconCode: &atomic.String{},
|
|
loginData: loginData,
|
|
}
|
|
}
|
|
|
|
func (c *Client) senderWorker() {
|
|
defer c.conn.Close()
|
|
defer c.cancelCtx()
|
|
|
|
for {
|
|
select {
|
|
case packet := <-c.sendChan:
|
|
if _, err := c.conn.Write([]byte(packet)); err != nil {
|
|
return
|
|
}
|
|
case <-c.ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// sendError sends an FSD error packet to a Client with the specified code and message.
|
|
// It returns an error if writing to the connection fails.
|
|
//
|
|
// This call is thread-safe
|
|
func (c *Client) sendError(code int, message string) (err error) {
|
|
packet := strings.Builder{}
|
|
packet.Grow(128)
|
|
packet.WriteString("$ERserver:unknown:")
|
|
codeBuf := make([]byte, 0, 8)
|
|
codeBuf = strconv.AppendInt(codeBuf, int64(code), 10)
|
|
packet.Write(codeBuf)
|
|
packet.WriteString("::")
|
|
packet.WriteString(message)
|
|
packet.WriteString("\r\n")
|
|
|
|
return c.send(packet.String())
|
|
}
|
|
|
|
// send sends a packet string to a Client.
|
|
// This call queues the packet in the Client's outbound send channel.
|
|
// This call will block until the packet can be queued in the send channel.
|
|
// Returns a context error if the Client's context has elapsed.
|
|
func (c *Client) send(packet string) (err error) {
|
|
select {
|
|
case c.sendChan <- packet:
|
|
return
|
|
case <-c.ctx.Done():
|
|
return c.ctx.Err()
|
|
}
|
|
}
|
|
|
|
func (s *Server) eventLoop(client *Client) {
|
|
defer client.cancelCtx()
|
|
|
|
go client.senderWorker()
|
|
|
|
for {
|
|
if !client.scanner.Scan() {
|
|
return
|
|
}
|
|
|
|
// Reference the next packet
|
|
packet := client.scanner.Bytes()
|
|
packet = append(packet, '\r', '\n') // Re-append delimiter
|
|
|
|
// Verify packet and obtain type
|
|
packetType, ok := verifyPacket(packet, client)
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
// Run handler
|
|
handler := s.getHandler(packetType)
|
|
handler(client, packet)
|
|
}
|
|
}
|