This repository was archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 264
/
Copy pathMain.as
196 lines (165 loc) · 7.77 KB
/
Main.as
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// easyXDM
// http://easyxdm.net/
// Copyright(c) 2009-2011, Øyvind Sean Kinsey, oyvind@kinsey.no.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
/* Security model:
* SWF's can be loaded from any of the two domains, and messages are only received from these two.
* Alternatively a single SWF can be loaded from a common domain (CDN).
* security.allowXDomain is used to let the page interact with the SWF.
* */
import flash.external.ExternalInterface;
import System.security;
/**
* This class facilitates flash based communication between domains.
* @author Øyvind Sean Kinsey
*/
class Main
{
private static var INITIALIZED:Boolean = false;
// only allow javascript accessors
private static function Validate(input:String):Boolean {
var i = input.length;
while (i--) {
var charCode = input.charCodeAt(i);
if ( (charCode >= 64 && charCode <= 90 /*Uppercase*/) || (charCode >= 97 && charCode <= 122 /*Lowercase*/) ) continue;
if (charCode >= 48 && charCode <= 57 /*Numbers*/) continue;
if (charCode == 95/*_*/ || charCode == 36 /*$*/ || charCode == 45 /*-*/ || charCode == 46 /*.*/ || charCode == 58 /*:*/) continue;
return false;
}
return true;
}
// docs at http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/js/html/wwhelp.htm
public static function main(swfRoot:MovieClip):Void
{
// this is so that main can only be run once - this ensures that the domain passed really is the one
// being used to communicate with the SWF.
if (Main.INITIALIZED) return; else Main.INITIALIZED = true;
// validate the passed arguments
if (!Validate(_root.ns) || !Validate(_root.proto) || !Validate(_root.domain) || !Validate(_root.port) || !Validate(_root.callback)) return;
// LocalConnection has a max length
var maxMessageLength = 40000;
// map of all the senders
var sendMap = { }, connectionMap = { };
// set up the prefix as a string based accessor to remove the risk of XSS
var prefix:String = _root.ns ? _root.ns + "." : "";
// this will be our origin
var origin = _root.proto + "//" + _root.domain + _root.port;
// set up the logger, if any
var log = _root.log == "true" ? function(msg) {
ExternalInterface.call(prefix + "easyXDM.Debug.trace", " swf: " + msg);
} : function() {
};
log("enabling communication with " + _root.domain);
// allow javascript in the page to interact with the SWF
security[_root.proto == "http:" ? "allowInsecureDomain" : "allowDomain"](_root.domain);
// add the postMessage method
ExternalInterface.addCallback("postMessage", { }, function(channel:String, message:String) {
sendMap[channel](message);
});
// add the createChannel method
ExternalInterface.addCallback("createChannel", { }, function(channel:String, secret:String, remoteOrigin:String, isHost:Boolean) {
if (!Main.Validate(channel) || !Main.Validate(secret)) return;
log("creating channel " + channel);
// get the remote domain
var remoteDomain:String = remoteOrigin.substr(remoteOrigin.indexOf("://") + 3);
if (remoteDomain.indexOf(":") != -1) remoteDomain = remoteDomain.substr(0, remoteDomain.indexOf(":"));
// the sending channel has _ prepended so that all allowed domains can use it
var sendingChannelName:String = "_" + channel + "_" + secret + "_" + (isHost ? "consumer" : "provider");
var receivingChannelName:String = "_" + channel + "_" + secret + "_" + (isHost ? "provider" : "consumer");
var incommingFragments = [];
// set up the listening connection
var listeningConnection:LocalConnection = connectionMap[channel] = new LocalConnection();
// set up the sending connection
var sendingConnection:LocalConnection = new LocalConnection();
// domain of the current SWF (for cdn support)
var localSwfDomain:String = listeningConnection.domain();
// allow messages from only the two possible domains
listeningConnection.allowDomain =
listeningConnection.allowInsecureDomain = function(domain) {
var allowed:Boolean = (domain == remoteDomain || domain == _root.domain || domain == localSwfDomain);
log("allowDomain: " + allowed.toString());
return allowed;
};
// set up the onMessage handler - this combines fragmented messages
listeningConnection.onMessage = function(message, fromOrigin, remaining) {
if (fromOrigin !== remoteOrigin) {
log("received message from " + fromOrigin + ", expected from " + remoteOrigin);
return;
}
incommingFragments.push(message);
if (remaining <= 0) {
log("received final fragment");
// escape \\ and pass on
ExternalInterface.call(prefix + "easyXDM.Fn.get(\"flash_" + channel + "_onMessage\")", incommingFragments.join("").split("\\").join("\\\\"), remoteOrigin);
incommingFragments = [];
}else {
log("received fragment, length is " + message.length + " remaining is " + remaining);
}
};
// the host must delay calling channel_init until the other end is ready
listeningConnection.onReady = function() {
log("received ready");
if (isHost) {
log("calling ready");
sendingConnection.send(sendingChannelName, "onReady");
ExternalInterface.call(prefix + "easyXDM.Fn.get(\"flash_" + channel + "_init\")");
}
};
// connect
if (listeningConnection.connect(receivingChannelName)) {
log("listening on " + receivingChannelName);
} else {
log("could not listen on " + receivingChannelName);
}
sendingConnection.onStatus = function(infoObject:Object) {
log("level: " + infoObject.level);
};
sendMap[channel] = function(message:String) {
log("sending to " + sendingChannelName + ", length is " + message.length);
var fragments = [], fragment, length = message.length, pos = 0;
while (pos <= length) {
fragment = message.substr(pos, maxMessageLength);;
pos += maxMessageLength;
log("fragmentlength: " + fragment.length + ", remaining: " + (length - pos))
if (!sendingConnection.send(sendingChannelName, "onMessage", fragment, origin, length - pos)) {
log("sending failed");
}
}
};
// start the channel
if (!isHost) {
log("calling ready");
sendingConnection.send(sendingChannelName, "onReady");
ExternalInterface.call(prefix + "easyXDM.Fn.get(\"flash_" + channel + "_init\")");
}
});
// add the destroyChannel method
ExternalInterface.addCallback("destroyChannel", { }, function(channel:String) {
delete sendMap[channel];
connectionMap[channel].close();
delete connectionMap[channel];
});
// kick things off
log("calling init");
ExternalInterface.call(prefix + "easyXDM.Fn.get(\"" + _root.callback + "\")");
}
}