From 0eaeaea909b6345a665b60b1f5ba468ab4097e99 Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Fri, 27 Aug 2010 17:31:45 +0000 Subject: [PATCH] IP-based registration implemented --- Builder/IISMainHandler/build.txt | 2 +- Common/Common.csproj | 1 + Common/TableManager.cs | 1 + Common/dataobjects/Account.cs | 8 +++ Core/Core.csproj | 3 + Core/Network/IPv4.cs | 12 ++++ Core/Network/IPv4Address.cs | 52 +++++++++++++++ Core/Network/IPv4Subnet.cs | 29 +++++++++ IISMainHandler/HandlersFactory.cs | 2 + IISMainHandler/IISMainHandler.csproj | 1 + IISMainHandler/WebContext.cs | 6 ++ .../handlers/request/RegisterHandler.cs | 23 +++++++ .../handlers/response/LoginHandler.cs | 5 +- .../handlers/response/RobotsHandler.cs | 3 + templates/Full/Login.xslt | 64 +++++++++++++++++++ 15 files changed, 210 insertions(+), 2 deletions(-) create mode 100644 Core/Network/IPv4.cs create mode 100644 Core/Network/IPv4Address.cs create mode 100644 Core/Network/IPv4Subnet.cs create mode 100644 IISMainHandler/handlers/request/RegisterHandler.cs diff --git a/Builder/IISMainHandler/build.txt b/Builder/IISMainHandler/build.txt index 06c2b90..2b15d82 100644 --- a/Builder/IISMainHandler/build.txt +++ b/Builder/IISMainHandler/build.txt @@ -1 +1 @@ -914 \ No newline at end of file +926 \ No newline at end of file diff --git a/Common/Common.csproj b/Common/Common.csproj index 2d5a531..508f4ca 100644 --- a/Common/Common.csproj +++ b/Common/Common.csproj @@ -90,6 +90,7 @@ + diff --git a/Common/TableManager.cs b/Common/TableManager.cs index 032a88f..a3593a7 100644 --- a/Common/TableManager.cs +++ b/Common/TableManager.cs @@ -14,6 +14,7 @@ namespace FLocal.Common { dataobjects.Board.TableSpec.instance, dataobjects.Category.TableSpec.instance, dataobjects.Invite.TableSpec.instance, + dataobjects.LocalNetwork.TableSpec.instance, dataobjects.PMConversation.TableSpec.instance, dataobjects.PMMessage.TableSpec.instance, dataobjects.Poll.TableSpec.instance, diff --git a/Common/dataobjects/Account.cs b/Common/dataobjects/Account.cs index fad9d29..31162e9 100644 --- a/Common/dataobjects/Account.cs +++ b/Common/dataobjects/Account.cs @@ -265,5 +265,13 @@ namespace FLocal.Common.dataobjects { return account; } + public static Account createAccount(string name, string password, string ip, string registrationEmail) { + var rawChanges = Account.getNewAccountChanges(name, password, ip, registrationEmail); + var accountInsert = rawChanges.Key; + var changes = rawChanges.Value; + ChangeSetUtil.ApplyChanges(changes); + return Account.LoadById(accountInsert.getId().Value); + } + } } diff --git a/Core/Core.csproj b/Core/Core.csproj index 1b0c237..0b3cbcf 100644 --- a/Core/Core.csproj +++ b/Core/Core.csproj @@ -78,6 +78,9 @@ + + + diff --git a/Core/Network/IPv4.cs b/Core/Network/IPv4.cs new file mode 100644 index 0000000..9a90290 --- /dev/null +++ b/Core/Network/IPv4.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace FLocal.Core.Network { + class IPv4 { + + public const ulong UNIT = 256; + + } +} diff --git a/Core/Network/IPv4Address.cs b/Core/Network/IPv4Address.cs new file mode 100644 index 0000000..cd84df7 --- /dev/null +++ b/Core/Network/IPv4Address.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace FLocal.Core.Network { + public class IPv4Address { + + private const ulong MAX = IPv4.UNIT * IPv4.UNIT * IPv4.UNIT * IPv4.UNIT; + + private readonly ulong raw; + + public IPv4Address(string ip) { + string[] parts = ip.Split('.'); + if(parts.Length != 4) throw new ApplicationException("Malformed ip '" + ip + "'"); + ulong result = 0; + for(int i=0; i= IPv4.UNIT) throw new ApplicationException("Malformed ip '" + ip + "'"); + result = (result*IPv4.UNIT) + part; + } + this.raw = result; + } + + public IPv4Address(ulong raw) { + if(raw >= MAX) throw new ApplicationException("Wrong raw representation " + raw); + this.raw = raw; + } + + public override string ToString() { + string[] parts = new string[4]; + ulong temp = this.raw; + for(int i=3; i>=0; i--) { + ulong part = temp % IPv4.UNIT; + parts[i] = part.ToString(); + temp = (temp - part) / IPv4.UNIT; + } + return String.Join(".", parts); + } + + public IEnumerable matchingSubnets { + get { + ulong divisor = 1; + for(int i=32; i>=0; i--) { + yield return new IPv4Subnet(new IPv4Address(this.raw - (this.raw % divisor)), (byte)i); + divisor *= 2; + } + } + } + + } +} diff --git a/Core/Network/IPv4Subnet.cs b/Core/Network/IPv4Subnet.cs new file mode 100644 index 0000000..0af23d3 --- /dev/null +++ b/Core/Network/IPv4Subnet.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace FLocal.Core.Network { + public class IPv4Subnet { + + private readonly IPv4Address prefix; + private readonly byte length; + + public IPv4Subnet(IPv4Address prefix, byte length) { + if(length > 32) throw new CriticalException("Wrong length " + length); + this.prefix = prefix; + this.length = length; + } + + public static IPv4Subnet FromString(string subnet) { + string[] parts = subnet.Split('/'); + if(parts.Length != 2) throw new ApplicationException("Malformed subnet '" + subnet + "'"); + return new IPv4Subnet(new IPv4Address(parts[0]), byte.Parse(parts[1])); + } + + public override string ToString() { + return this.prefix.ToString() + "/" + this.length; + } + + } +} diff --git a/IISMainHandler/HandlersFactory.cs b/IISMainHandler/HandlersFactory.cs index 13e0d31..16126e6 100644 --- a/IISMainHandler/HandlersFactory.cs +++ b/IISMainHandler/HandlersFactory.cs @@ -167,6 +167,8 @@ namespace FLocal.IISHandler { return new handlers.request.LogoutHandler(); case "migrateaccount": return new handlers.request.MigrateAccountHandler(); + case "register": + return new handlers.request.RegisterHandler(); case "registerbyinvite": return new handlers.request.RegisterByInviteHandler(); case "edit": diff --git a/IISMainHandler/IISMainHandler.csproj b/IISMainHandler/IISMainHandler.csproj index bdd15a9..eebe6d0 100644 --- a/IISMainHandler/IISMainHandler.csproj +++ b/IISMainHandler/IISMainHandler.csproj @@ -72,6 +72,7 @@ + diff --git a/IISMainHandler/WebContext.cs b/IISMainHandler/WebContext.cs index 1a8c3b9..d334b5e 100644 --- a/IISMainHandler/WebContext.cs +++ b/IISMainHandler/WebContext.cs @@ -144,5 +144,11 @@ namespace FLocal.IISHandler { return result; } + public Core.Network.IPv4Address remoteHost { + get { + return new Core.Network.IPv4Address(this.httprequest.UserHostAddress); + } + } + } } diff --git a/IISMainHandler/handlers/request/RegisterHandler.cs b/IISMainHandler/handlers/request/RegisterHandler.cs new file mode 100644 index 0000000..00295b0 --- /dev/null +++ b/IISMainHandler/handlers/request/RegisterHandler.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Xml.Linq; +using FLocal.Common.dataobjects; +using FLocal.Importer; +using System.Text.RegularExpressions; +using FLocal.Core; +using FLocal.Common; +using FLocal.Common.actions; + +namespace FLocal.IISHandler.handlers.request { + class RegisterHandler : AbstractNewAccountHandler { + + protected override Account DoCreateAccount(WebContext context) { + if(!LocalNetwork.IsLocalNetwork(context.remoteHost)) throw new FLocalException("IP '" + context.remoteHost.ToString() + "' is not allowed"); + if(context.httprequest.Form["password"] != context.httprequest.Form["password2"]) throw new FLocalException("Passwords mismatch"); + return Account.createAccount(context.httprequest.Form["login"], context.httprequest.Form["password"], context.httprequest.UserHostAddress, context.httprequest.Form["registrationEmail"]); + } + + } +} diff --git a/IISMainHandler/handlers/response/LoginHandler.cs b/IISMainHandler/handlers/response/LoginHandler.cs index ea9d6a2..3b680eb 100644 --- a/IISMainHandler/handlers/response/LoginHandler.cs +++ b/IISMainHandler/handlers/response/LoginHandler.cs @@ -17,7 +17,10 @@ namespace FLocal.IISHandler.handlers.response { } protected override IEnumerable getSpecificData(WebContext context) { - return new XElement[0]; + return new XElement[] { + new XElement("isLocalNetwork", LocalNetwork.IsLocalNetwork(context.remoteHost).ToPlainString()), + new XElement("ip", context.remoteHost.ToString()), + }; } } diff --git a/IISMainHandler/handlers/response/RobotsHandler.cs b/IISMainHandler/handlers/response/RobotsHandler.cs index a3581ab..3c67a11 100644 --- a/IISMainHandler/handlers/response/RobotsHandler.cs +++ b/IISMainHandler/handlers/response/RobotsHandler.cs @@ -17,6 +17,9 @@ namespace FLocal.IISHandler.handlers.response { context.httpresponse.ContentType = "text/plain"; context.httpresponse.WriteLine("User-agent: *"); context.httpresponse.WriteLine("Disallow: /"); + foreach(var subnet in context.remoteHost.matchingSubnets) { + context.httpresponse.WriteLine(subnet.ToString()); + } } } diff --git a/templates/Full/Login.xslt b/templates/Full/Login.xslt index 5e4858c..5629524 100644 --- a/templates/Full/Login.xslt +++ b/templates/Full/Login.xslt @@ -62,6 +62,70 @@ +
+ + + + +
+ + + + + + + + + + + + +
+ Ðåãèñòðàöèÿ +
+ + + Âàø IP + + íå âõîäèò â ñïèñîê ðàçðåø¸ííûõ ïîäñåòåé. +
+ Åñëè âû ñ÷èòàåòå, ÷òî ýòî îøèáêà, ñîîáùèòå àäìèíèñòðàòîðó ñâîé IP-àäðåñ è îïèñàíèå ñåòè (îáùåæèòèå/ó÷åáíûé êîðïóñ, óíèâåðñèòåò è ïðî÷åå). +
+ + Âàø IP + + âõîäèò â ñïèñîê ðàçðåø¸ííûõ ïîäñåòåé. + +
+
+
+ Èìÿ ïîëüçîâàòåëÿ
+
+ Íîâûé ïàðîëü
+
+ Ïîâòîðèòå ïàðîëü
+
+ e-mail äëÿ âîññòàíîâëåíèÿ ïàðîëÿ (íåîáÿçàòåëüíî)
+ +
+ + + îïèñàííàÿ ïî ýòîé ññûëêå êîíñòèòóöèÿ + . + (îáÿçàòåëüíî) +
+ + + (îáÿçàòåëüíî) +
+ + + (îáÿçàòåëüíî) +
+ +
+
+
\ No newline at end of file