This commit is contained in:
Jeffrey Paul 2019-08-19 21:15:56 -05:00
parent a3b2ad1703
commit b936973154
6 changed files with 165 additions and 80 deletions

View File

@ -1,6 +1,8 @@
GOPATH := $(shell pwd)
default: run default: run
run: src/*.go run: *.go */*.go
go run src/*.go go run main.go
fmt:
go fmt *.go
go fmt sircd/*.go

12
main.go
View File

@ -1,15 +1,15 @@
package main package main
import "log" import "github.com/sirupsen/logrus"
import "os" import "github.com/sneak/sircd/sircd"
import "sircd"
import "time" import "time"
func main() { func main() {
l := log.New(os.Stdout, "", log.Ldate | log.Lmicroseconds | log.Lshortfile) var log = logrus.New()
l.Println("sircd starting up") log.SetLevel(log.DebugLevel)
log.Println("sircd starting up")
s := sircd.NewSircd() s := sircd.NewSircd()
s.SetLogger(l) s.SetLogger(log)
go s.Start() go s.Start()
for s.Running { for s.Running {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)

View File

@ -1,40 +1,54 @@
package sircd package sircd
import ( import (
"log"
"net"
"bytes" "bytes"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"net"
) )
func newClientIRCConnection(conn net.Conn, log *log.Logger) *clientIRCConnection { func newIrcClient(conn net.Conn, log *logrus.Logger, mc chan *ircMessage) *ircClient {
c := new(clientIRCConnection) c := new(ircClient)
c.Id = uuid.New().String()
c.conn = conn c.conn = conn
c.log = log c.log = log
c.mc = mc
c.inputBytes = new(bytes.Buffer) c.inputBytes = new(bytes.Buffer)
c.outputBytes = new(bytes.Buffer) c.outputBytes = new(bytes.Buffer)
c.ReadSocket()
return c return c
} }
type clientIRCConnection struct { type ircClient struct {
Id uint64 Id string
conn net.Conn conn net.Conn
log *log.Logger log *logrus.Logger
session *ircUserSession
inputBytes *bytes.Buffer inputBytes *bytes.Buffer
outputBytes *bytes.Buffer outputBytes *bytes.Buffer
mc chan *ircMessage
} }
func (c *clientIRCConnection) ReadSocket() { type ircMessage struct {
from *ircClient
//received
//command
}
func (c *ircClient) Close() {
//client cleanup
c.log.Infof("client %d disconnect", c.Id)
}
func (c *ircClient) AppendInputBuffer(input []byte) {
// Read the incoming connection into the buffer. // Read the incoming connection into the buffer.
buf := make([]byte, 1024*1024) c.log.Printf("conn<%s>: got %d bytes from net", c.Id, len(input))
bytesRead, err := c.conn.Read(buf) c.inputBytes.Write(input)
c.log.Printf("conn<%d>: read %d bytes from net", c.Id, bytesRead) c.log.Debugf("conn<%s> buffer now %d bytes total", c.inputBytes.Len())
if err != nil { c.ParseMessages()
c.log.Println("Error reading:", err.Error())
}
c.inputBytes.Write(buf)
} }
func (c *clientIRCConnection) ParseMessages() { func (c *ircClient) ParseMessages() {
c.log.Fatalln("not implemented") c.log.Debugln("my input buffer is %d", c.inputBytes.Len())
c.log.Debugln("my input buffer is: '%s'", c.inputBytes.String())
//c.log.Fatalln("not implemented")
} }

30
sircd/main.go Normal file
View File

@ -0,0 +1,30 @@
package sircd
func (s *sircd) processIRCMessage(m *ircMessage) {
s.log.Info("not implemented")
//for client, _ := range s.ircClients {
//}
}
/*
import "net"
case publish := <-publishes:
for conn, _ := range conns {
go func(conn net.Conn) {
totalWritten := 0
for totalWritten < len(publish) {
writtenThisCall, err := conn.Write(publish[totalWritten:])
if err != nil {
deadConns <- conn
break
}
totalWritten += writtenThisCall
}
}(conn)
}
}
}
listener.Close()
}
*/

View File

@ -1,20 +1,21 @@
package sircd package sircd
import ( import (
"log" "github.com/sirupsen/logrus"
"net" "net"
"os"
"container/list"
) )
type sircd struct { type sircd struct {
Running bool Running bool
log *log.Logger log *logrus.Logger
netName string netName string
ircPort uint16 ircPort uint16
httpPort uint16 httpPort uint16
ircListener net.Listener ircListener net.Listener
ircClients *list.List ircClients map[*ircClient]bool
newClients chan *ircClient
deadClients chan *ircClient
messageQueue chan *ircMessage
} }
const ( const (
@ -26,55 +27,84 @@ const (
func NewSircd() *sircd { func NewSircd() *sircd {
s := new(sircd) s := new(sircd)
s.Running = true s.Running = true
s.ircClients = list.New() s.ircClients = make(map[*ircClient]bool)
return s return s
} }
func (s *sircd) SetLogger(l *log.Logger) { func (s *sircd) SetLogger(l *logrus.Logger) {
s.log = l s.log = l
} }
func (s *sircd) startIRCServer() { func (s *sircd) Start() {
s.newClients = make(chan *ircClient, 128)
s.deadClients = make(chan *ircClient, 128)
s.messageQueue = make(chan *ircMessage, 128)
var err error var err error
s.ircListener, err = net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT) s.ircListener, err = net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
if err != nil { if err != nil {
log.Println("Error listening:", err.Error()) s.log.Fatalln("Error listening:", err.Error())
os.Exit(1)
} }
defer s.ircListener.Close()
s.log.Println("Listening for irc on " + CONN_HOST + ":" + CONN_PORT) s.log.Println("Listening for irc on " + CONN_HOST + ":" + CONN_PORT)
for { go func() {
// Listen for an incoming connection.
conn, err := s.ircListener.Accept() conn, err := s.ircListener.Accept()
if err != nil { if err != nil {
s.log.Fatalf("Error accepting: ", err.Error()) s.log.Panicf("Error accepting: ", err.Error())
} }
// Handle connections in a new goroutine. s.newClients <- newIrcClient(conn, s.log, s.messageQueue)
go s.handleIRCConnection(conn) }()
go s.mainloop()
} }
} func (s *sircd) mainloop() {
func (s *sircd) processIRC() {
for { for {
s.readFromClients() select {
case client := <-s.newClients:
s.ircClients[client] = true
s.log.Println("new irc client: %s", client.Id)
go func() {
buf := make([]byte, 1024*1024)
for {
nbyte, err := client.conn.Read(buf)
if err != nil {
s.deadClients <- client
break
} else {
fragment := make([]byte, nbyte)
copy(fragment, buf[:nbyte])
// this will populate the message channel
client.AppendInputBuffer(fragment)
}
}
}()
case deadClient := <-s.deadClients:
delete(s.ircClients, deadClient)
deadClient.Close()
case message := <-s.messageQueue:
s.processIRCMessage(message)
}
} }
} }
func (s *sircd) readFromClients() { /*
for e := s.ircClients.Front(); e != nil; e = e.Next() { case publish := <-publishes:
client := e.Value.(clientIRCConnection) for conn, _ := range conns {
client.ReadSocket() go func(conn net.Conn) {
client.ParseMessages() totalWritten := 0
for totalWritten < len(publish) {
writtenThisCall, err := conn.Write(publish[totalWritten:])
if err != nil {
deadConns <- conn
break
}
totalWritten += writtenThisCall
}
}(conn)
} }
} }
func (s *sircd) Start() {
go s.startIRCServer()
}
func (s *sircd) handleIRCConnection(conn net.Conn) {
c := newClientIRCConnection(conn, s.log)
s.ircClients.PushBack(c)
} }
listener.Close()
*/

9
sircd/session.go Normal file
View File

@ -0,0 +1,9 @@
package sircd
type ircNick string
type ircRealName string
type ircUserSession struct {
nick ircNick
realname ircRealName
}