gonews/networker/connection.go

179 lines
5.1 KiB
Go
Raw Normal View History

2019-09-02 22:24:34 +05:00
package networker
import (
// stdlib
"bufio"
"log"
"net"
"strings"
// local
"sources.dev.pztrn.name/gonews/gonews/eventer"
2019-09-02 22:24:34 +05:00
)
// This structure represents single NNTP client connection.
type connection struct {
// Connection details and handlers.
conn net.Conn
remoteAddr net.Addr
// Read and write buffers
reader *bufio.Reader
writer *bufio.Writer
// Scanner that helps us to read incoming data.
readScanner *bufio.Scanner
// Connection flags.
// Are we in READER or MODE-READER (transit) mode?
// Right now transit mode isn't implemented. Implementation will
// require start using two goroutines for handling connections,
// one for writing and one for reading.
transit bool
2019-10-22 04:38:40 +05:00
// Connection capabilities.
capabilities []string
}
// Initialize initializes necessary things.
func (c *connection) Initialize(conn net.Conn) {
c.conn = conn
c.remoteAddr = c.conn.RemoteAddr()
2019-09-02 22:24:34 +05:00
log.Printf("accepted connection from %v\n", conn.RemoteAddr())
// Create buffers.
c.reader = bufio.NewReader(conn)
c.writer = bufio.NewWriter(conn)
c.readScanner = bufio.NewScanner(c.reader)
// Get capabilities for this connection.
caps, _ := eventer.LaunchEvent("internal/capabilities", nil)
c.capabilities = caps.([]string)
// Set transit mode by default, according to RFC.
c.transit = true
}
// Start starts working with connection. Should be launched in separate
// goroutine.
// It will send greeting and then falls into infinite loop for working
// with connection until the end.
// Right now it implements only READER mode, no transit (which is used
// by server-to-server peering extensively).
func (c *connection) Start() {
2019-09-02 22:24:34 +05:00
defer func() {
err := c.conn.Close()
2019-09-02 22:24:34 +05:00
if err != nil {
log.Println("Failed to close connection from " + c.remoteAddr.String() + ": " + err.Error())
2019-09-02 22:24:34 +05:00
}
log.Println("Connection from " + c.remoteAddr.String() + " closed")
2019-09-02 22:24:34 +05:00
}()
// Send greeting.
greetingData, _ := eventer.LaunchEvent("internal/greeting", nil)
greetingReply := greetingData.(*Reply)
_, err := c.writer.WriteString(greetingReply.Code + " " + greetingReply.Data)
2019-09-02 22:24:34 +05:00
if err != nil {
log.Println("Failed to write greeting for " + c.remoteAddr.String() + ": " + err.Error())
2019-09-02 22:24:34 +05:00
return
}
2019-10-22 04:38:40 +05:00
c.writer.Flush()
2019-09-02 22:24:34 +05:00
// Start reading for commands.
// Every command can be represented as slice where first element
// is actual command and all next - parameters.
// By default we read only one line per iteration.
// ToDo: multiline data parser for posting.
for {
dataAppeared := c.readScanner.Scan()
2019-09-02 22:24:34 +05:00
if !dataAppeared {
log.Println("Failed to read data from " + c.remoteAddr.String() + ": " + c.readScanner.Err().Error())
2019-09-02 22:24:34 +05:00
break
}
log.Println("Got data: " + c.readScanner.Text())
2019-09-02 22:24:34 +05:00
// ToDo: what if we'll upload binary data here?
// Not supported yet.
data := strings.Split(c.readScanner.Text(), " ")
// Separate capabilities worker.
if strings.ToLower(data[0]) == "capabilities" {
dataToWrite := "Capability list:\r\n"
for _, cap := range c.capabilities {
dataToWrite += cap + "\r\n"
}
// We're also mode-switching server (in future), so we should
// also be aware of mode reader things. Writing to client will
// depend on c.transit variable.
// We will announce MODE-READER capability after initial
// connection (because we're in transit mode by default, according
// to RFC), and when client issue "MODE READER" command we will
// stop announcing MODE-READER and will start announce READER
// capability.
if c.transit {
dataToWrite += "MODE-READER\r\n"
} else {
dataToWrite += "READER\r\n"
}
dataToWrite += ".\r\n"
2019-10-22 04:38:40 +05:00
_, _ = c.writer.WriteString(dataToWrite)
c.writer.Flush()
2019-10-22 04:38:40 +05:00
continue
}
// Mode worker. Reader only now.
if strings.ToLower(data[0]) == "mode" && strings.ToLower(data[1]) == "reader" {
c.transit = false
// In any case we'll require user authentication for posting.
2019-10-22 04:38:40 +05:00
_, _ = c.writer.WriteString("201 Posting prohibited\r\n")
_ = c.writer.Flush()
continue
}
// Execute passed command.
replyRaw, err := eventer.LaunchEvent("commands/"+strings.ToLower(data[0]), data[1:])
2019-09-02 22:24:34 +05:00
if err != nil {
// We won't break here as this is just logging of appeared error.
log.Println("Error appeared while processing command '" + data[0] + "' for " + c.remoteAddr.String() + ": " + err.Error())
2019-09-02 22:24:34 +05:00
}
// We might have nil in reply, so we'll assume that passed command
// is unknown to us.
if replyRaw == nil {
_, err := c.writer.WriteString(unknownCommandErrorCode + " " + unknownCommandErrorText + "\r\n")
2019-09-02 22:24:34 +05:00
if err != nil {
log.Println("Failed to write string to socket for " + c.remoteAddr.String() + ": " + err.Error())
2019-09-02 22:24:34 +05:00
break
}
2019-10-22 04:38:40 +05:00
_ = c.writer.Flush()
2019-09-02 22:24:34 +05:00
continue
}
// Every reply will be a reply struct.
reply := replyRaw.(*Reply)
_, err1 := c.writer.WriteString(reply.Code + " " + reply.Data)
2019-09-02 22:24:34 +05:00
if err1 != nil {
log.Println("Failed to write string to socket for " + c.remoteAddr.String() + ": " + err1.Error())
2019-09-02 22:24:34 +05:00
break
}
2019-10-22 04:38:40 +05:00
_ = c.writer.Flush()
2019-09-02 22:24:34 +05:00
// Check for QUIT command.
if strings.ToLower(data[0]) == "quit" {
log.Println("QUIT command received, closing connection to " + c.remoteAddr.String())
2019-09-02 22:24:34 +05:00
break
}
}
}