[Opensim-dev] [WorldOarModule] - A module to serve up OAR files over http
logicmoo at gmail.com
logicmoo at gmail.com
Sat Mar 7 07:08:46 UTC 2009
Hi,
[WorldOarModule] - A module to serve up OAR files over http
JustinCC wrote a "load oar <fileOrURI>" that can retrieve a OpenSim .OAR file over Http I banged out a light version of a server
below:
So here here are the questions I have:
* Is the password=secrets in the GET request secure enough to make this a module people feel comforatable to run on their servers?
* Might this become a core module for the other end of "load oar" ?
* Is the mime type "application/x-oar" ok? officially, it's a "application/x-tar-gz"
--------------------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.Threading;
using log4net;
using Nini.Config;
using OpenSim.Framework;
using OpenSim.Framework.Servers;
using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.Framework.Scenes;
using System.IO.Compression;
/*
in bin/OpenSim.ini[.example]
[WorldOarModule]
;##
;## It makes it really easy to 'borrow' your data, so use with care.
;##
;## Example :
;## [REGION OAR]: Region OAR location: http://71.197.210.170:9000/index.php?method=regionOar160f328fb2524c5f97f3d982788d4a02
;##
;## To load the region remotely:
;## load oar http://71.197.210.170:9000/index.php?method=regionOar160f328fb2524c5f97f3d982788d4a02&password=secrets
enabled = true
password = secrets
*/
namespace OpenSim.Region.CoreModules.World.Archiver.WorldOar
{
public class WorldOarModule : IRegionModule {
private static readonly ILog m_log =
LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private IConfig m_config;
protected Scene m_scene;
protected bool m_Enabled = false;
private volatile bool oarFileDone = false;
private Exception m_LastException;
private string m_Password;
//private int CacheRegionsDistance = 256;
#region IRegionModule Members
public virtual void Initialise(Scene scene, IConfigSource config)
{
m_scene = scene;
m_config = config.Configs["WorldOarModule"];
if (m_config != null) {
if (m_config.GetBoolean("enabled", false)) {
m_Password = m_config.GetString("password",null);
if (m_Password==null) {
m_log.Error("[WorldOarModule] password=(null)");
return;
}
m_Enabled = true;
}
}
}
public virtual void PostInitialise()
{
if (m_Enabled)
AddHandlers();
}
public virtual void Close()
{
}
public virtual string Name
{
get { return "WorldOarModule";}
}
public bool IsSharedModule
{
get { return false;}
}
#endregion
protected virtual void AddHandlers()
{
string regionimage = "regionOar" + m_scene.RegionInfo.RegionID.ToString();
regionimage = regionimage.Replace("-", "");
m_log.Info("[REGION OAR]: Region OAR location: http://" + m_scene.RegionInfo.ExternalEndPoint.Address.ToString() + ":" +
m_scene.RegionInfo.HttpPort.ToString() + "/index.php?method=" + regionimage);
m_scene.CommsManager.HttpServer.AddHTTPHandler(regionimage, OnHTTPGetOarImage);
m_scene.EventManager.OnOarFileSaved += OnOarFileSaved;
}
#region EventHandlers
public void OnOarFileSaved(string message) {
if (!String.IsNullOrEmpty(message)) {
message = "OarFileSaved: " + message;
m_log.Debug(message);
m_LastException = new ArgumentException(message);
}
oarFileDone = true;
}
#endregion
public void SaveCurrentSceneToArchive(Stream stream)
{
IRegionArchiverModule archiver = m_scene.RequestModuleInterface<IRegionArchiverModule>();
if (archiver != null)
archiver.ArchiveRegion(stream);
}
public static string Compress(byte[] buffer)
{
MemoryStream memory = new MemoryStream();
using (GZipStream compressor = new GZipStream(memory, CompressionMode.Compress, true))
{
compressor.Write(buffer, 0, buffer.Length);
compressor.Close();
}
return Convert.ToBase64String(memory.ToArray());
}
public Hashtable OnHTTPGetOarImage(Hashtable keysvals)
{
if (!keysvals.ContainsKey("password")) {
m_log.Warn("[REGION OAR]: Unknown user requested OAR file for region but supplied no password");
}
string password = keysvals["password"];
if (password!=m_Password) {
m_log.WarnFormat("[REGION OAR]: User {0} requested OAR file and the password was incorrect: {0}",password);
}
m_log.Debug("[REGION OAR]: Sending oar ");
Hashtable reply = new Hashtable();
MemoryStream imgstream = new MemoryStream();
try {
oarFileDone = false;
m_LastException = null;
SaveCurrentSceneToArchive(imgstream);
while (!oarFileDone) {
Thread.Sleep(1000);
}
if (m_LastException!=null) {
throw m_LastException;
}
// Write the stream to a byte array for output
reply["str_response_string"] = Compress(imgstream.ToArray());
reply["int_response_code"] = 200;
reply["content_type"] = "application/x-oar";
} catch (Exception e) {
m_log.Warn("[REGION OAR]: Unable to generate Oar image " + e);
reply["str_response_string"] = "[REGION OAR]: Unable to generate Oar image " + e;
reply["int_response_code"] = 500;
reply["content_type"] = "text/plain";
} finally {
//oarTexture.Dispose();
// image.Dispose();
// Reclaim memory, these are unmanaged resources
imgstream.Close();
imgstream.Dispose();
}
return reply;
}
}
}
More information about the Opensim-dev
mailing list