Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Socks5Component.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
4using System.Xml;
8
10{
16 {
17 private Socks5Server socks5Server;
18 private string hostOverride = null;
19 private readonly int port;
20
24 public const string Socks5Namespace = "http://jabber.org/protocol/bytestreams";
25
34 : base(Server, Subdomain, "SOCKS5 proxy")
35 {
36 this.port = Port;
37 this.socks5Server = new Socks5Server(Port);
38
39 this.RegisterIqGetHandler("query", Socks5Namespace, this.QueryGet, true);
40 this.RegisterIqSetHandler("query", Socks5Namespace, this.QuerySet, false);
41 }
42
46 public override void Dispose()
47 {
48 base.Dispose();
49
50 this.UnregisterIqGetHandler("query", Socks5Namespace, this.QueryGet, true);
51 this.UnregisterIqSetHandler("query", Socks5Namespace, this.QuerySet, false);
52
53 this.socks5Server?.Dispose();
54 this.socks5Server = null;
55 }
56
61 public string HostOverride
62 {
63 get => this.hostOverride;
64 set => this.hostOverride = value;
65 }
66
71 public override bool SupportsAccounts => false;
72
79 protected override Task AppendServiceDiscoveryIdentities(StringBuilder Xml, IqEventArgs e, string Node)
80 {
81 Xml.Append("<identity category='proxy' type='bytestreams' name='SOCKS5 Proxy'/>");
82 return Task.CompletedTask;
83 }
84
85 private async Task QueryGet(object Sender, IqEventArgs e)
86 {
87 if (!this.socks5Server.IsOpen)
88 await e.IqErrorNotAllowed(e.To, "SOCKS5 server not open.", "en");
89 else if (await this.Server.PersistenceLayer.IsPermitted(e.From.BareJid, "SOCKS5"))
90 {
91 StringBuilder Xml = new StringBuilder();
92
93 Xml.Append("<query xmlns='");
94 Xml.Append(Socks5Namespace);
95 Xml.Append("'>");
96 Xml.Append("<streamhost host='");
97
98 if (!string.IsNullOrEmpty(this.hostOverride))
99 Xml.Append(XML.Encode(this.hostOverride));
100 else
101 Xml.Append(XML.Encode(this.Server.Domain));
102
103 Xml.Append("' jid='");
104 Xml.Append(XML.Encode(e.To.Address));
105 Xml.Append("' port='");
106 Xml.Append(this.port.ToString());
107 Xml.Append("'/></query>");
108
109 await e.IqResult(Xml.ToString(), e.To);
110 }
111 else
112 await e.IqErrorForbidden(e.To, "Account blocked from using SOCKS5.", "en");
113 }
114
115 private async Task QuerySet(object Sender, IqEventArgs e)
116 {
117 if (await this.Server.PersistenceLayer.IsPermitted(e.From.BareJid, "SOCKS5"))
118 {
119 string Sid = XML.Attribute(e.Query, "sid");
120 CaseInsensitiveString Jid = null;
121
122 foreach (XmlNode N in e.Query.ChildNodes)
123 {
124 if (N.LocalName == "activate")
125 {
126 Jid = N.InnerText;
127 break;
128 }
129 }
130
132 await e.IqErrorBadRequest(e.To, "Missing JID.", "en");
133 else
134 {
135 string s = Sid + e.From + Jid;
136 string StreamId = Hashes.ComputeSHA1HashString(Encoding.UTF8.GetBytes(s));
137
138 if (this.socks5Server.Activate(StreamId))
139 await e.IqResult(string.Empty, e.To);
140 else
141 await e.IqErrorNotAllowed(e.To, "Stream not active.", "en");
142 }
143 }
144 else
145 await e.IqErrorForbidden(e.To, "Account blocked from using SOCKS5.", "en");
146 }
147
148 }
149}
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:27
Base class for components.
Definition: Component.cs:16
void RegisterIqSetHandler(string LocalName, string Namespace, EventHandlerAsync< IqEventArgs > Handler, bool PublishNamespaceAsFeature)
Registers an IQ-Set handler.
Definition: Component.cs:161
CaseInsensitiveString Subdomain
Subdomain name.
Definition: Component.cs:76
void RegisterIqGetHandler(string LocalName, string Namespace, EventHandlerAsync< IqEventArgs > Handler, bool PublishNamespaceAsFeature)
Registers an IQ-Get handler.
Definition: Component.cs:149
XmppServer Server
XMPP Server.
Definition: Component.cs:96
bool UnregisterIqGetHandler(string LocalName, string Namespace, EventHandlerAsync< IqEventArgs > Handler, bool RemoveNamespaceAsFeature)
Unregisters an IQ-Get handler.
Definition: Component.cs:249
bool UnregisterIqSetHandler(string LocalName, string Namespace, EventHandlerAsync< IqEventArgs > Handler, bool RemoveNamespaceAsFeature)
Unregisters an IQ-Set handler.
Definition: Component.cs:262
Event arguments for IQ queries.
Definition: IqEventArgs.cs:12
XmppAddress From
From address attribute
Definition: IqEventArgs.cs:93
Task IqResult(string Xml, string From)
Returns a response to the current request.
Definition: IqEventArgs.cs:113
Task IqErrorNotAllowed(XmppAddress From, string ErrorText, string Language)
Returns a not-allowed error.
Definition: IqEventArgs.cs:187
XmppAddress To
To address attribute
Definition: IqEventArgs.cs:88
Task IqErrorForbidden(XmppAddress From, string ErrorText, string Language)
Returns a forbidden error.
Definition: IqEventArgs.cs:229
Implements SOCKS5 Byte streams support as an XMPP component: https://xmpp.org/extensions/xep-0065....
string HostOverride
Property that can be used to override the host attribute. If external clients needs another host name...
override bool SupportsAccounts
If the component supports accounts (true), or if the subdomain name is the only valid address.
Socks5Component(XmppServer Server, CaseInsensitiveString Subdomain, int Port)
Implements SOCKS5 Byte streams support as an XMPP component: https://xmpp.org/extensions/xep-0065....
const string Socks5Namespace
http://jabber.org/protocol/bytestreams (XEP-0065)
override Task AppendServiceDiscoveryIdentities(StringBuilder Xml, IqEventArgs e, string Node)
Appends component identities, as defined in XEP-0030, to a discovery response.
override void Dispose()
IDisposable.Dispose
bool IsOpen
If the server is open and accepts incoming connections.
CaseInsensitiveString Address
XMPP Address
Definition: XmppAddress.cs:37
CaseInsensitiveString BareJid
Bare JID
Definition: XmppAddress.cs:45
Represents a case-insensitive string.
static bool IsNullOrEmpty(CaseInsensitiveString value)
Indicates whether the specified string is null or an CaseInsensitiveString.Empty string.
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static string ComputeSHA1HashString(byte[] Data)
Computes the SHA-1 hash of a block of binary data.
Definition: Hashes.cs:274
Task< bool > IsPermitted(CaseInsensitiveString BareJid, string Setting)
Checks if a feature is permitted for a Bare JID.