2017-10-10 00:54:14 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2017-12-14 21:30:07 +01:00
|
|
|
"net"
|
2017-10-10 00:54:14 +02:00
|
|
|
|
2017-12-14 21:30:07 +01:00
|
|
|
"github.com/genofire/yaja/database"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/crypto/acme/autocert"
|
2017-10-10 00:54:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
TLSConfig *tls.Config
|
2017-12-14 21:30:07 +01:00
|
|
|
TLSManager *autocert.Manager
|
|
|
|
ClientAddr []string
|
|
|
|
ServerAddr []string
|
|
|
|
Database *database.State
|
2017-10-10 00:54:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-10-10 00:54:14 +02:00
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *Server) handleClient(conn net.Conn) {
|
|
|
|
log.Info("new client connection:", conn.RemoteAddr())
|
|
|
|
client := NewClient(conn, srv)
|
|
|
|
state := ConnectionStartup()
|
|
|
|
|
|
|
|
for {
|
|
|
|
state, client = state.Process(client)
|
|
|
|
if state == nil {
|
|
|
|
client.log.Info("disconnect")
|
|
|
|
client.Close()
|
|
|
|
//s.DisconnectBus <- Disconnect{Jid: client.jid}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// run next state
|
|
|
|
}
|
2017-10-10 00:54:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *Server) Close() {
|
|
|
|
|
|
|
|
}
|