-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFlashSocketPolicyServerThread.cs
More file actions
148 lines (128 loc) · 5.81 KB
/
FlashSocketPolicyServerThread.cs
File metadata and controls
148 lines (128 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
RMLib: Nonvisual support classes used by multiple R&M Software programs
Copyright (C) Rick Parrish, R&M Software
This file is part of RMLib.
RMLib is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
RMLib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with RMLib. If not, see <http://www.gnu.org/licenses/>.
*/
using System.Globalization;
using System;
namespace RandM.RMLib
{
public class FlashSocketPolicyServerThread : RMThread
{
private string _Address;
private string _AllowedPorts;
private TcpConnection _Connection;
private int _Port;
public event EventHandler BindFailedEvent = null;
public event EventHandler BoundEvent = null;
public event EventHandler<ConnectionAcceptedEventArgs> ConnectionAcceptedEvent = null;
public event EventHandler<StringEventArgs> ErrorMessageEvent = null;
public event EventHandler<StringEventArgs> MessageEvent = null;
public event EventHandler<StringEventArgs> WarningMessageEvent = null;
public FlashSocketPolicyServerThread(string address, int port, string allowedPorts)
{
_Address = address;
_Port = port;
_AllowedPorts = allowedPorts;
}
protected override void Dispose(bool disposing)
{
if (!_Disposed)
{
if (disposing)
{
// dispose managed state (managed objects).
_Connection.Dispose();
}
// free unmanaged resources (unmanaged objects)
// set large fields to null.
// Call the base dispose
base.Dispose(disposing);
}
}
protected override void Execute()
{
_Connection = new TcpConnection();
if (_Connection.Listen(_Address, _Port))
{
RaiseBoundEvent();
while (!_Stop)
{
// Accept a new connection
if (_Connection.CanAccept(500)) // 1/2 of a second
{
TcpConnection NewConnection = _Connection.AcceptTCP();
if (NewConnection != null)
{
// Wait up to 5 seconds for the request string
string Request = NewConnection.ReadLn("\0", 5000);
if (Request.ToLower().Replace(" ", "") == "<policy-file-request/>")
{
ConnectionAcceptedEventArgs.Raise(this, ConnectionAcceptedEvent, _Address, _Port, NewConnection.GetRemoteIP(), NewConnection.GetRemotePort());
RaiseMessageEvent("Answered policy file request from " + NewConnection.GetRemoteIP() + ":" + NewConnection.GetRemotePort().ToString());
NewConnection.WriteLn("<?xml version=\"1.0\"?>");
NewConnection.WriteLn("<cross-domain-policy>");
NewConnection.WriteLn(" <allow-access-from domain=\"*\" to-ports=\"" + _AllowedPorts + "\"/>");
NewConnection.WriteLn(" <site-control permitted-cross-domain-policies=\"all\"/>"); // TODO Maybe add property to determine whether this should be all or master-only
NewConnection.WriteLn("</cross-domain-policy>");
NewConnection.Write("\0");
}
else
{
RaiseErrorMessageEvent("Invalid policy file request from " + NewConnection.GetRemoteIP() + ":" + NewConnection.GetRemotePort().ToString());
}
NewConnection.Close();
}
}
}
_Connection.Close();
}
else
{
RaiseErrorMessageEvent("Flash Socket Policy Thread: Unable to listen on " + _Address + ":" + _Port);
RaiseBindFailedEvent();
}
}
private void RaiseBindFailedEvent()
{
EventHandler Handler = BindFailedEvent;
if (Handler != null) Handler(this, EventArgs.Empty);
}
private void RaiseBoundEvent()
{
EventHandler Handler = BoundEvent;
if (Handler != null) Handler(this, EventArgs.Empty);
}
private void RaiseErrorMessageEvent(string text)
{
EventHandler<StringEventArgs> Handler = ErrorMessageEvent;
if (Handler != null) Handler(this, new StringEventArgs(text));
}
private void RaiseMessageEvent(string text)
{
EventHandler<StringEventArgs> Handler = MessageEvent;
if (Handler != null) Handler(this, new StringEventArgs(text));
}
private void RaiseWarningMessageEvent(string text)
{
EventHandler<StringEventArgs> Handler = WarningMessageEvent;
if (Handler != null) Handler(this, new StringEventArgs(text));
}
public override void Stop()
{
// Close the socket so that any waits on ReadLn(), ReadChar(), etc, will not block
_Connection.Close();
base.Stop();
}
}
}