sum7
/
yaja
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
yaja/server/server.go

129 lines
2.8 KiB
Go
Raw Normal View History

package server
import (
"crypto/tls"
2017-12-14 21:30:07 +01:00
"net"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/acme/autocert"
2018-02-07 15:34:18 +01:00
"dev.sum7.eu/genofire/yaja/database"
"dev.sum7.eu/genofire/yaja/server/extension"
"dev.sum7.eu/genofire/yaja/server/toclient"
"dev.sum7.eu/genofire/yaja/server/toserver"
"dev.sum7.eu/genofire/yaja/server/utils"
"dev.sum7.eu/genofire/yaja/xmpp/base"
)
type Server struct {
2017-12-17 17:50:51 +01:00
TLSConfig *tls.Config
TLSManager *autocert.Manager
ClientAddr []string
ServerAddr []string
Database *database.State
LoggingClient log.Level
LoggingServer log.Level
RegisterEnable bool
RegisterDomains []string
ExtensionsClient extension.Extensions
ExtensionsServer extension.Extensions
}
func (srv *Server) Start() {
2017-12-14 21:30:07 +01:00
for _, addr := range srv.ServerAddr {
socket, err := net.Listen("tcp", addr)
if err != nil {
log.Warn("create server socket: ", err.Error())
break
}
go srv.listenServer(socket)
}
2017-12-14 21:30:07 +01:00
for _, addr := range srv.ClientAddr {
socket, err := net.Listen("tcp", addr)
if err != nil {
log.Warn("create client socket: ", err.Error())
break
}
go srv.listenClient(socket)
}
}
func (srv *Server) listenServer(s2s net.Listener) {
for {
conn, err := s2s.Accept()
if err != nil {
log.Warn("accepting server connection: ", err.Error())
break
}
go srv.handleServer(conn)
}
}
func (srv *Server) listenClient(c2s net.Listener) {
for {
conn, err := c2s.Accept()
if err != nil {
log.Warn("accepting client connection: ", err.Error())
break
}
go srv.handleClient(conn)
}
}
func (srv *Server) handleServer(conn net.Conn) {
log.Info("new server connection:", conn.RemoteAddr())
2017-12-17 17:50:51 +01:00
client := utils.NewClient(conn, srv.LoggingClient)
client.Log = client.Log.WithField("c", "s2s")
state := toserver.ConnectionStartup(srv.Database, srv.TLSConfig, srv.TLSManager, srv.ExtensionsServer, client)
for {
state = state.Process()
if state == nil {
client.Log.Info("disconnect")
client.Close()
return
}
// run next state
}
2017-12-14 21:30:07 +01:00
}
func (srv *Server) handleClient(conn net.Conn) {
log.Info("new client connection:", conn.RemoteAddr())
2017-12-17 17:50:51 +01:00
client := utils.NewClient(conn, srv.LoggingServer)
client.Log = client.Log.WithField("c", "c2s")
state := toclient.ConnectionStartup(srv.Database, srv.TLSConfig, srv.TLSManager, srv.DomainRegisterAllowed, srv.ExtensionsClient, client)
2017-12-14 21:30:07 +01:00
for {
2017-12-17 17:50:51 +01:00
state = state.Process()
2017-12-14 21:30:07 +01:00
if state == nil {
2017-12-16 23:20:46 +01:00
client.Log.Info("disconnect")
2017-12-14 21:30:07 +01:00
client.Close()
//s.DisconnectBus <- Disconnect{Jid: client.jid}
return
}
// run next state
}
}
func (srv *Server) DomainRegisterAllowed(jid *xmppbase.JID) bool {
2017-12-16 23:20:46 +01:00
if jid.Domain == "" {
return false
}
for _, domain := range srv.RegisterDomains {
if domain == jid.Domain {
return !srv.RegisterEnable
}
}
return srv.RegisterEnable
}
func (srv *Server) Close() {
}