-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebSocketClient.scala
166 lines (136 loc) · 4.53 KB
/
WebSocketClient.scala
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
/*
Copyright (c) 2010 ymnk, JCraft,Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.net.Socket
import java.io.{InputStream, OutputStream}
import java.net.URI
import WebSocket._
class WebSocketClient(uri: URI)
(f: PartialFunction[Event, Unit]) extends WebSocket {
import WebSocketClient._
val path = (uri.getPath match{
case "" => "/"
case p => p
}) + Option(uri.getQuery).map("?"+_).getOrElse("")
val default_port = uri.getScheme match {
case "ws" => 80
case "wss" => 443
case _ => error("unknown scheme: "+uri)
}
val port = uri.getPort match{
case -1 => default_port
case p => p
}
val host = {
uri.getHost.toLowerCase + (port match{
case `default_port` => ""
case p => ":"+p
})
}
val origin = "http://"+uri.getHost.toLowerCase
def connect(): Boolean = {
val s = {
if(uri.getScheme == "wss"){
import javax.net.SocketFactory
import javax.net.ssl.SSLSocketFactory
val factory = SSLSocketFactory.getDefault
factory.createSocket(uri.getHost, port)
}
else{
new Socket(uri.getHost, port)
}
}
in = s.getInputStream
out = s.getOutputStream
if(handshake()){
import scala.concurrent.ops.spawn
spawn{
receive(f)
}
}
connected
}
def handshake(): Boolean = {
val key1 = genKey
val key2 = genKey
val key3 = {
val buf = new Array[Byte](8)
scala.util.Random.nextBytes(buf)
buf
}
val request = template.format(path, host, origin, key1, key2)
out.write(request.getBytes("UTF-8"))
out.write(key3)
out.flush
val (head, headers) = readHeader
val reply_digest = new Array[Byte](16)
in.read(reply_digest)
if(headers("Sec-WebSocket-Origin") != origin){
println("warning: "+headers("Sec-WebSocket-Origin")+" != "+origin)
}
//connected = (reply_digest.deep == digest(key1, key2, key3).deep)
connected = java.util.Arrays.equals(reply_digest, digest(key1, key2, key3))
if(!connected){
println("warning: wrong digest")
}
if(connected && f.isDefinedAt(OnOpen)) f(OnOpen)
connected
}
}
object WebSocketClient {
val template =
"GET %s HTTP/1.1\r\n" +
"Upgrade: WebSocket\r\n" +
"Connection: Upgrade\r\n" +
"Host: %s\r\n" +
"Origin: %s\r\n" +
"Sec-WebSocket-Key1: %s\r\n" +
"Sec-WebSocket-Key2: %s\r\n" +
"\r\n"
private val characters = ((0x21 to 0x2F) toList) ::: ((0x3A to 0x7E) toList) map {
_.asInstanceOf[Char]
}
def genKey = {
import scala.util.Random.nextInt
val spaces = nextInt(12)+1
val max = ((BigInt("4294967295") / spaces).toInt) & 0x7FFFFFFF
val number = nextInt(max)
val product = BigInt(number) * spaces
def nextChar = {
characters(nextInt(characters.length))
}
var key = product.toString
key = (0 to nextInt(12)).foldLeft(key) {
case (key, _) =>
key splitAt nextInt(key.length) match {
case (a,b) => a+nextChar+b
}
}
key = (0 to spaces).foldLeft(key) {
case (key, _) =>
key splitAt nextInt(key.length-1)+1 match {
case (a,b) => a+' '+b
}
}
key
}
}