ln.skyspot/session/SessionManager.cs

128 lines
4.3 KiB
C#

// /**
// * File: SessionManager.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using System.Collections.Generic;
using ln.types.net;
using ln.types.btree;
using ln.radius;
using System.Net;
using ln.logging;
using ln.types.odb;
using ln.types.odb.mapped;
using skyspot.radius;
using System.Text;
using skyspot.hotspot;
namespace skyspot.session
{
public class SessionManager
{
public HotspotManager HotspotManager { get; }
public RadiusSecretsStore SecretsStore => HotspotManager.SecretsStore;
RadiusServer accountingRadius;
RadiusServer locatorRadius;
public SessionManager(HotspotManager hotspotManager)
{
HotspotManager = hotspotManager;
InitializeRadius();
}
public void Start()
{
}
private void InitializeRadius()
{
accountingRadius= new RadiusServer(new IPEndPoint(IPAddress.Any,1812));
locatorRadius = new RadiusServer(new IPEndPoint(IPAddress.Any, 1816));
accountingRadius.LookupSecret = (endPoint) => SecretsStore.LookupSecret(endPoint);
locatorRadius.LookupSecret = (endPoint) => SecretsStore.LookupSecret(endPoint);
locatorRadius.MessageReceived = LocatorMessageReceived;
accountingRadius.MessageReceived = AccountingMessageReceived;
}
/**
* Locator Service
*
**/
void LocatorMessageReceived(RadiusServer radiusServer, RadiusMessage radiusMessage)
{
Logging.Log(LogLevel.INFO, "Radius Message received: {0}", radiusMessage);
switch (radiusMessage.Code)
{
case RadiusCode.AccessRequest:
LocatorAccessRequest(radiusServer, radiusMessage);
break;
}
}
void LocatorAccessRequest(RadiusServer radiusServer, RadiusMessage radiusMessage)
{
}
/**
*
* Accounting Service
*
**/
void AccountingMessageReceived(RadiusServer radiusServer, RadiusMessage radiusMessage)
{
Logging.Log(LogLevel.INFO, "Radius Message received: {0}", radiusMessage);
switch (radiusMessage.Code)
{
case RadiusCode.AccessRequest:
AccountingAccessRequest(radiusServer, radiusMessage);
break;
}
}
void AccountingAccessRequest(RadiusServer radiusServer, RadiusMessage radiusMessage)
{
RadiusAttribute.UserName userName = radiusMessage.GetAttribute<RadiusAttribute.UserName>();
RadiusAttribute.UserPassword userPassword = radiusMessage.GetAttribute<RadiusAttribute.UserPassword>();
RadiusAttribute.CalledStationID calledStationID = radiusMessage.GetAttribute<RadiusAttribute.CalledStationID>();
if ((userName == null) || (calledStationID == null))
{
Logging.Log(LogLevel.WARNING, "InvalidRequest: UserName={0} CalledStationID={1}",userName,calledStationID);
return;
}
MAC clientMac = new MAC(userName.AsText);
String networkName = calledStationID.AsText;
HotspotNetworkRuntime hotspotNetworkRuntime = HotspotManager.GetHotspotNetworkRuntime(networkName);
ClientSession clientSession = hotspotNetworkRuntime.GetSession(clientMac);
if (clientSession.CurrentProfile == null)
clientSession.CurrentProfile = hotspotNetworkRuntime.HotspotNetwork.DefaultClientProfile;
Logging.Log(LogLevel.INFO, "Accounting: Session start for client {0} on Network {1}",clientMac, networkName);
RadiusMessage reply = radiusMessage.ConstructReply(RadiusCode.AccessAccept);
reply.AddAttribute(new RadiusAttribute.FramedIPAddress().SetIP(IPv4.Parse("10.123.0.4")));
if (clientSession.CurrentProfile != null)
reply.AddAttribute(new RadiusAttribute.FilterID().SetText(clientSession.CurrentProfile.Name));
radiusServer.Send(reply);
}
}
}