PSF-LoginServer/pslogin/src/main/scala/SessionRouter.scala

197 lines
6.7 KiB
Scala
Raw Normal View History

2017-03-07 00:30:45 +00:00
// Copyright (c) 2017 PSForever
2016-02-05 08:19:13 +00:00
import java.net.InetSocketAddress
import akka.actor._
2016-05-02 01:12:42 +00:00
import org.log4s.MDC
2016-02-05 08:19:13 +00:00
import scodec.bits._
import scala.collection.mutable
import akka.actor.SupervisorStrategy.Stop
import net.psforever.packet.PacketCoding
import net.psforever.packet.control.ConnectionClose
import net.psforever.WorldConfig
import services.ServiceManager
import services.ServiceManager.Lookup
import services.account.{IPAddress, StoreIPAddress}
2016-02-05 08:19:13 +00:00
import scala.concurrent.duration._
sealed trait SessionRouterAPI
final case class RawPacket(data : ByteVector) extends SessionRouterAPI
final case class ResponsePacket(data : ByteVector) extends SessionRouterAPI
final case class DropSession(id : Long, reason : String) extends SessionRouterAPI
final case class SessionReaper() extends SessionRouterAPI
2016-02-05 08:19:13 +00:00
case class SessionPipeline(nameTemplate : String, props : Props)
/**
* Login sessions are divided between two actors. The crypto session actor transparently handles all of the cryptographic
* setup of the connection. Once a correct crypto session has been established, all packets, after being decrypted
* will be passed on to the login session actor. This actor has important state that is used to maintain the login
* session.
*
* > PlanetSide Session Pipeline <
*
* read() route decrypt
* UDP Socket -----> [Session Router] -----> [Crypto Actor] -----> [Session Actor]
Persistence (#337) * constructed service actor to handle persistence of player on server beyond the reaches of WSA from one login to the next; created in PSLogin, interfaced with and updated in WSA * for what it's worth, players can be completely logged out of the world after 60s of inactivity, alive Infantry only right now; some code was removed from WSA to make it accessible to other classes but it's incomparable to the new code; broke, fixed, compromised on the code that handles loadouts, server login time, etc. * moved another common vehicle function into global space; persistence object for players in vehicles during log-out or relogging in a vehicle * support for relogging when dead/released/unfound; silenced implant slot setup during character select screen * tested and commented code for managing player avatar persistence * clarificaion of WSA postStop * test fixed * postStop change to account for WSA being cut short during initialization * clarification of SquadService logout * player died during setup; probably a relog * kill the doppelganger WSA; die when you are actually dead * created manifest to assist with vehicle gating; vehicle gating now accomodates the persistence model much better * fixed the test * fixed initial vehicle seat access permissions; moved a subscription to AvatarService to support persistence * bug fixes: safer GridInventory collision checks, plus specific exceptions; SessionRouter waits for the account intermediary before allowing itself to be started; WSA - match error colution, and MAX without arm now creates the arm it expects * adjusted insertion and removal code to make inventory management less prone to partoial insertions of removals; inventory integrity checking code writen, but no plans on implementing it yet
2020-02-14 15:54:52 +00:00
* /|\ | /|\ | /|\ |
* | write() | | encrypt | | response |
* +--------------+ +-----------+ +-----------------+
Persistence (#337) * constructed service actor to handle persistence of player on server beyond the reaches of WSA from one login to the next; created in PSLogin, interfaced with and updated in WSA * for what it's worth, players can be completely logged out of the world after 60s of inactivity, alive Infantry only right now; some code was removed from WSA to make it accessible to other classes but it's incomparable to the new code; broke, fixed, compromised on the code that handles loadouts, server login time, etc. * moved another common vehicle function into global space; persistence object for players in vehicles during log-out or relogging in a vehicle * support for relogging when dead/released/unfound; silenced implant slot setup during character select screen * tested and commented code for managing player avatar persistence * clarificaion of WSA postStop * test fixed * postStop change to account for WSA being cut short during initialization * clarification of SquadService logout * player died during setup; probably a relog * kill the doppelganger WSA; die when you are actually dead * created manifest to assist with vehicle gating; vehicle gating now accomodates the persistence model much better * fixed the test * fixed initial vehicle seat access permissions; moved a subscription to AvatarService to support persistence * bug fixes: safer GridInventory collision checks, plus specific exceptions; SessionRouter waits for the account intermediary before allowing itself to be started; WSA - match error colution, and MAX without arm now creates the arm it expects * adjusted insertion and removal code to make inventory management less prone to partoial insertions of removals; inventory integrity checking code writen, but no plans on implementing it yet
2020-02-14 15:54:52 +00:00
*/
class SessionRouter(role : String, pipeline : List[SessionPipeline]) extends Actor with MDCContextAware {
private[this] val log = org.log4s.getLogger(self.path.name)
import scala.concurrent.ExecutionContext.Implicits.global
val sessionReaper = context.system.scheduler.schedule(10 seconds, 5 seconds, self, SessionReaper())
val idBySocket = mutable.Map[InetSocketAddress, Long]()
val sessionById = mutable.Map[Long, Session]()
val sessionByActor = mutable.Map[ActorRef, Session]()
val closePacket = PacketCoding.EncodePacket(ConnectionClose()).require.bytes
var accountIntermediary : ActorRef = Actor.noSender
var sessionId = 0L // this is a connection session, not an actual logged in session ID
var inputRef : ActorRef = ActorRef.noSender
override def supervisorStrategy = OneForOneStrategy() { case _ => Stop }
override def preStart = {
Persistence (#337) * constructed service actor to handle persistence of player on server beyond the reaches of WSA from one login to the next; created in PSLogin, interfaced with and updated in WSA * for what it's worth, players can be completely logged out of the world after 60s of inactivity, alive Infantry only right now; some code was removed from WSA to make it accessible to other classes but it's incomparable to the new code; broke, fixed, compromised on the code that handles loadouts, server login time, etc. * moved another common vehicle function into global space; persistence object for players in vehicles during log-out or relogging in a vehicle * support for relogging when dead/released/unfound; silenced implant slot setup during character select screen * tested and commented code for managing player avatar persistence * clarificaion of WSA postStop * test fixed * postStop change to account for WSA being cut short during initialization * clarification of SquadService logout * player died during setup; probably a relog * kill the doppelganger WSA; die when you are actually dead * created manifest to assist with vehicle gating; vehicle gating now accomodates the persistence model much better * fixed the test * fixed initial vehicle seat access permissions; moved a subscription to AvatarService to support persistence * bug fixes: safer GridInventory collision checks, plus specific exceptions; SessionRouter waits for the account intermediary before allowing itself to be started; WSA - match error colution, and MAX without arm now creates the arm it expects * adjusted insertion and removal code to make inventory management less prone to partoial insertions of removals; inventory integrity checking code writen, but no plans on implementing it yet
2020-02-14 15:54:52 +00:00
log.info(s"SessionRouter (for ${role}s) initializing ...")
}
def receive = initializing
def initializing : Receive = {
case Hello() =>
inputRef = sender()
ServiceManager.serviceManager ! Lookup("accountIntermediary")
Persistence (#337) * constructed service actor to handle persistence of player on server beyond the reaches of WSA from one login to the next; created in PSLogin, interfaced with and updated in WSA * for what it's worth, players can be completely logged out of the world after 60s of inactivity, alive Infantry only right now; some code was removed from WSA to make it accessible to other classes but it's incomparable to the new code; broke, fixed, compromised on the code that handles loadouts, server login time, etc. * moved another common vehicle function into global space; persistence object for players in vehicles during log-out or relogging in a vehicle * support for relogging when dead/released/unfound; silenced implant slot setup during character select screen * tested and commented code for managing player avatar persistence * clarificaion of WSA postStop * test fixed * postStop change to account for WSA being cut short during initialization * clarification of SquadService logout * player died during setup; probably a relog * kill the doppelganger WSA; die when you are actually dead * created manifest to assist with vehicle gating; vehicle gating now accomodates the persistence model much better * fixed the test * fixed initial vehicle seat access permissions; moved a subscription to AvatarService to support persistence * bug fixes: safer GridInventory collision checks, plus specific exceptions; SessionRouter waits for the account intermediary before allowing itself to be started; WSA - match error colution, and MAX without arm now creates the arm it expects * adjusted insertion and removal code to make inventory management less prone to partoial insertions of removals; inventory integrity checking code writen, but no plans on implementing it yet
2020-02-14 15:54:52 +00:00
case ServiceManager.LookupResult("accountIntermediary", endpoint) =>
accountIntermediary = endpoint
log.info(s"SessionRouter starting; ready for $role sessions")
context.become(started)
case default =>
Persistence (#337) * constructed service actor to handle persistence of player on server beyond the reaches of WSA from one login to the next; created in PSLogin, interfaced with and updated in WSA * for what it's worth, players can be completely logged out of the world after 60s of inactivity, alive Infantry only right now; some code was removed from WSA to make it accessible to other classes but it's incomparable to the new code; broke, fixed, compromised on the code that handles loadouts, server login time, etc. * moved another common vehicle function into global space; persistence object for players in vehicles during log-out or relogging in a vehicle * support for relogging when dead/released/unfound; silenced implant slot setup during character select screen * tested and commented code for managing player avatar persistence * clarificaion of WSA postStop * test fixed * postStop change to account for WSA being cut short during initialization * clarification of SquadService logout * player died during setup; probably a relog * kill the doppelganger WSA; die when you are actually dead * created manifest to assist with vehicle gating; vehicle gating now accomodates the persistence model much better * fixed the test * fixed initial vehicle seat access permissions; moved a subscription to AvatarService to support persistence * bug fixes: safer GridInventory collision checks, plus specific exceptions; SessionRouter waits for the account intermediary before allowing itself to be started; WSA - match error colution, and MAX without arm now creates the arm it expects * adjusted insertion and removal code to make inventory management less prone to partoial insertions of removals; inventory integrity checking code writen, but no plans on implementing it yet
2020-02-14 15:54:52 +00:00
log.error(s"Unknown or unexpected message $default before being properly started. Stopping completely...")
context.stop(self)
}
override def postStop() = {
sessionReaper.cancel()
}
2016-05-02 01:12:42 +00:00
def started : Receive = {
Persistence (#337) * constructed service actor to handle persistence of player on server beyond the reaches of WSA from one login to the next; created in PSLogin, interfaced with and updated in WSA * for what it's worth, players can be completely logged out of the world after 60s of inactivity, alive Infantry only right now; some code was removed from WSA to make it accessible to other classes but it's incomparable to the new code; broke, fixed, compromised on the code that handles loadouts, server login time, etc. * moved another common vehicle function into global space; persistence object for players in vehicles during log-out or relogging in a vehicle * support for relogging when dead/released/unfound; silenced implant slot setup during character select screen * tested and commented code for managing player avatar persistence * clarificaion of WSA postStop * test fixed * postStop change to account for WSA being cut short during initialization * clarification of SquadService logout * player died during setup; probably a relog * kill the doppelganger WSA; die when you are actually dead * created manifest to assist with vehicle gating; vehicle gating now accomodates the persistence model much better * fixed the test * fixed initial vehicle seat access permissions; moved a subscription to AvatarService to support persistence * bug fixes: safer GridInventory collision checks, plus specific exceptions; SessionRouter waits for the account intermediary before allowing itself to be started; WSA - match error colution, and MAX without arm now creates the arm it expects * adjusted insertion and removal code to make inventory management less prone to partoial insertions of removals; inventory integrity checking code writen, but no plans on implementing it yet
2020-02-14 15:54:52 +00:00
case _ @ ReceivedPacket(msg, from) =>
var session : Session = null
2016-05-02 01:12:42 +00:00
if(!idBySocket.contains(from)) {
session = createNewSession(from)
}
else {
val id = idBySocket{from}
session = sessionById{id}
}
2016-05-02 01:12:42 +00:00
if(session.state != Closed()) {
MDC("sessionId") = session.sessionId.toString
Persistence (#337) * constructed service actor to handle persistence of player on server beyond the reaches of WSA from one login to the next; created in PSLogin, interfaced with and updated in WSA * for what it's worth, players can be completely logged out of the world after 60s of inactivity, alive Infantry only right now; some code was removed from WSA to make it accessible to other classes but it's incomparable to the new code; broke, fixed, compromised on the code that handles loadouts, server login time, etc. * moved another common vehicle function into global space; persistence object for players in vehicles during log-out or relogging in a vehicle * support for relogging when dead/released/unfound; silenced implant slot setup during character select screen * tested and commented code for managing player avatar persistence * clarificaion of WSA postStop * test fixed * postStop change to account for WSA being cut short during initialization * clarification of SquadService logout * player died during setup; probably a relog * kill the doppelganger WSA; die when you are actually dead * created manifest to assist with vehicle gating; vehicle gating now accomodates the persistence model much better * fixed the test * fixed initial vehicle seat access permissions; moved a subscription to AvatarService to support persistence * bug fixes: safer GridInventory collision checks, plus specific exceptions; SessionRouter waits for the account intermediary before allowing itself to be started; WSA - match error colution, and MAX without arm now creates the arm it expects * adjusted insertion and removal code to make inventory management less prone to partoial insertions of removals; inventory integrity checking code writen, but no plans on implementing it yet
2020-02-14 15:54:52 +00:00
log.trace(s"RECV: $msg -> ${session.getPipeline.head.path.name}")
session.receive(RawPacket(msg))
2016-05-02 01:12:42 +00:00
MDC.clear()
2016-02-05 08:19:13 +00:00
}
case ResponsePacket(msg) =>
val session = sessionByActor.get(sender())
if(session.isDefined) {
if(session.get.state != Closed()) {
MDC("sessionId") = session.get.sessionId.toString
Persistence (#337) * constructed service actor to handle persistence of player on server beyond the reaches of WSA from one login to the next; created in PSLogin, interfaced with and updated in WSA * for what it's worth, players can be completely logged out of the world after 60s of inactivity, alive Infantry only right now; some code was removed from WSA to make it accessible to other classes but it's incomparable to the new code; broke, fixed, compromised on the code that handles loadouts, server login time, etc. * moved another common vehicle function into global space; persistence object for players in vehicles during log-out or relogging in a vehicle * support for relogging when dead/released/unfound; silenced implant slot setup during character select screen * tested and commented code for managing player avatar persistence * clarificaion of WSA postStop * test fixed * postStop change to account for WSA being cut short during initialization * clarification of SquadService logout * player died during setup; probably a relog * kill the doppelganger WSA; die when you are actually dead * created manifest to assist with vehicle gating; vehicle gating now accomodates the persistence model much better * fixed the test * fixed initial vehicle seat access permissions; moved a subscription to AvatarService to support persistence * bug fixes: safer GridInventory collision checks, plus specific exceptions; SessionRouter waits for the account intermediary before allowing itself to be started; WSA - match error colution, and MAX without arm now creates the arm it expects * adjusted insertion and removal code to make inventory management less prone to partoial insertions of removals; inventory integrity checking code writen, but no plans on implementing it yet
2020-02-14 15:54:52 +00:00
log.trace(s"SEND: $msg -> ${inputRef.path.name}")
session.get.send(msg)
MDC.clear()
}
} else {
log.error("Dropped old response packet from actor " + sender().path.name)
}
case DropSession(id, reason) =>
val session = sessionById.get(id)
if(session.isDefined) {
removeSessionById(id, reason, graceful = true)
} else {
log.error(s"Requested to drop non-existent session ID=$id from ${sender()}")
}
case SessionReaper() =>
val inboundGrace = WorldConfig.Get[Duration]("network.Session.InboundGraceTime").toMillis
val outboundGrace = WorldConfig.Get[Duration]("network.Session.OutboundGraceTime").toMillis
sessionById.foreach { case (id, session) =>
log.trace(session.toString)
if(session.getState == Closed()) {
// clear mappings
session.getPipeline.foreach(sessionByActor remove)
sessionById.remove(id)
idBySocket.remove(session.socketAddress)
log.debug(s"Reaped session ID=$id")
} else if(session.timeSinceLastInboundEvent > inboundGrace) {
removeSessionById(id, "session timed out (inbound)", graceful = false)
} else if(session.timeSinceLastOutboundEvent > outboundGrace) {
removeSessionById(id, "session timed out (outbound)", graceful = true) // tell client to STFU
}
}
case Terminated(actor) =>
val terminatedSession = sessionByActor.get(actor)
2016-05-02 01:12:42 +00:00
if(terminatedSession.isDefined) {
removeSessionById(terminatedSession.get.sessionId, s"${actor.path.name} died",
graceful = true)
} else {
log.error("Received an invalid actor Termination from " + actor.path.name)
}
case default =>
log.error(s"Unknown message $default from " + sender().path)
2016-02-05 08:19:13 +00:00
}
def createNewSession(address : InetSocketAddress) = {
val id = newSessionId
val session = new Session(id, address, inputRef, pipeline)
// establish mappings for easy lookup
idBySocket{address} = id
sessionById{id} = session
session.getPipeline.foreach { actor =>
sessionByActor{actor} = session
}
Persistence (#337) * constructed service actor to handle persistence of player on server beyond the reaches of WSA from one login to the next; created in PSLogin, interfaced with and updated in WSA * for what it's worth, players can be completely logged out of the world after 60s of inactivity, alive Infantry only right now; some code was removed from WSA to make it accessible to other classes but it's incomparable to the new code; broke, fixed, compromised on the code that handles loadouts, server login time, etc. * moved another common vehicle function into global space; persistence object for players in vehicles during log-out or relogging in a vehicle * support for relogging when dead/released/unfound; silenced implant slot setup during character select screen * tested and commented code for managing player avatar persistence * clarificaion of WSA postStop * test fixed * postStop change to account for WSA being cut short during initialization * clarification of SquadService logout * player died during setup; probably a relog * kill the doppelganger WSA; die when you are actually dead * created manifest to assist with vehicle gating; vehicle gating now accomodates the persistence model much better * fixed the test * fixed initial vehicle seat access permissions; moved a subscription to AvatarService to support persistence * bug fixes: safer GridInventory collision checks, plus specific exceptions; SessionRouter waits for the account intermediary before allowing itself to be started; WSA - match error colution, and MAX without arm now creates the arm it expects * adjusted insertion and removal code to make inventory management less prone to partoial insertions of removals; inventory integrity checking code writen, but no plans on implementing it yet
2020-02-14 15:54:52 +00:00
log.info(s"New session ID=$id from " + address.toString)
if(role == "Login") {
accountIntermediary ! StoreIPAddress(id, new IPAddress(address))
}
session
}
def removeSessionById(id : Long, reason : String, graceful : Boolean) : Unit = {
val sessionOption = sessionById.get(id)
if(sessionOption.isEmpty)
return
val session : Session = sessionOption.get
if(graceful) {
Persistence (#337) * constructed service actor to handle persistence of player on server beyond the reaches of WSA from one login to the next; created in PSLogin, interfaced with and updated in WSA * for what it's worth, players can be completely logged out of the world after 60s of inactivity, alive Infantry only right now; some code was removed from WSA to make it accessible to other classes but it's incomparable to the new code; broke, fixed, compromised on the code that handles loadouts, server login time, etc. * moved another common vehicle function into global space; persistence object for players in vehicles during log-out or relogging in a vehicle * support for relogging when dead/released/unfound; silenced implant slot setup during character select screen * tested and commented code for managing player avatar persistence * clarificaion of WSA postStop * test fixed * postStop change to account for WSA being cut short during initialization * clarification of SquadService logout * player died during setup; probably a relog * kill the doppelganger WSA; die when you are actually dead * created manifest to assist with vehicle gating; vehicle gating now accomodates the persistence model much better * fixed the test * fixed initial vehicle seat access permissions; moved a subscription to AvatarService to support persistence * bug fixes: safer GridInventory collision checks, plus specific exceptions; SessionRouter waits for the account intermediary before allowing itself to be started; WSA - match error colution, and MAX without arm now creates the arm it expects * adjusted insertion and removal code to make inventory management less prone to partoial insertions of removals; inventory integrity checking code writen, but no plans on implementing it yet
2020-02-14 15:54:52 +00:00
for(_ <- 0 to 5) {
session.send(closePacket)
}
}
// kill all session specific actors
session.dropSession(graceful)
Persistence (#337) * constructed service actor to handle persistence of player on server beyond the reaches of WSA from one login to the next; created in PSLogin, interfaced with and updated in WSA * for what it's worth, players can be completely logged out of the world after 60s of inactivity, alive Infantry only right now; some code was removed from WSA to make it accessible to other classes but it's incomparable to the new code; broke, fixed, compromised on the code that handles loadouts, server login time, etc. * moved another common vehicle function into global space; persistence object for players in vehicles during log-out or relogging in a vehicle * support for relogging when dead/released/unfound; silenced implant slot setup during character select screen * tested and commented code for managing player avatar persistence * clarificaion of WSA postStop * test fixed * postStop change to account for WSA being cut short during initialization * clarification of SquadService logout * player died during setup; probably a relog * kill the doppelganger WSA; die when you are actually dead * created manifest to assist with vehicle gating; vehicle gating now accomodates the persistence model much better * fixed the test * fixed initial vehicle seat access permissions; moved a subscription to AvatarService to support persistence * bug fixes: safer GridInventory collision checks, plus specific exceptions; SessionRouter waits for the account intermediary before allowing itself to be started; WSA - match error colution, and MAX without arm now creates the arm it expects * adjusted insertion and removal code to make inventory management less prone to partoial insertions of removals; inventory integrity checking code writen, but no plans on implementing it yet
2020-02-14 15:54:52 +00:00
log.info(s"Dropping session ID=$id (reason: $reason)")
}
2016-02-05 08:19:13 +00:00
def newSessionId = {
val oldId = sessionId
sessionId += 1
oldId
}
}