Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6cb1c474e0 | |||
| 7102d31afb | |||
| 0f94bb3d95 | |||
| 28070bf2a4 | |||
| ee0c7f9213 | |||
| a49c7e2119 | |||
| 7fad6b3a82 | |||
| 06baac0fe7 | |||
| 5866676617 | |||
| b074c6c833 | |||
| 8f2af31bd3 | |||
| d3fb769f93 | |||
| f58eb4ccad | |||
| 880b5b4c9e | |||
| 97e378d94a | |||
| ca20b02ca2 | |||
| a1f133d737 | |||
| 13f685f464 | |||
| fae7ef2817 | |||
| b24eaba135 | |||
| e8182190b7 | |||
| bf24825167 | |||
| 9ee57e089d | |||
| f057a8b33d | |||
| b8fe0f64e3 | |||
| 94e54af61c |
+25
-1
@@ -2,7 +2,31 @@
|
||||
All notable changes to this project will be documented in this file.
|
||||
`Starscream` adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
#### [3.0.1](https://github.com/daltoniam/Starscream/tree/3.0.2)
|
||||
#### [3.0.3](https://github.com/daltoniam/Starscream/tree/3.0.3)
|
||||
|
||||
Assorted fixes.
|
||||
|
||||
Pull Requests:
|
||||
[#438](https://github.com/daltoniam/Starscream/pull/438)
|
||||
[#423](https://github.com/daltoniam/Starscream/pull/423)
|
||||
[#420](https://github.com/daltoniam/Starscream/pull/420)
|
||||
[#418](https://github.com/daltoniam/Starscream/pull/418)
|
||||
[#410](https://github.com/daltoniam/Starscream/pull/410)
|
||||
[#405](https://github.com/daltoniam/Starscream/pull/405)
|
||||
[#404](https://github.com/daltoniam/Starscream/pull/404)
|
||||
[#400](https://github.com/daltoniam/Starscream/pull/400)
|
||||
|
||||
Issues:
|
||||
[#435](https://github.com/daltoniam/Starscream/issues/435)
|
||||
[#431](https://github.com/daltoniam/Starscream/issues/431)
|
||||
[#426](https://github.com/daltoniam/Starscream/issues/426)
|
||||
[#409](https://github.com/daltoniam/Starscream/issues/409)
|
||||
[#408](https://github.com/daltoniam/Starscream/issues/408)
|
||||
[#401](https://github.com/daltoniam/Starscream/issues/401)
|
||||
[#399](https://github.com/daltoniam/Starscream/issues/399)
|
||||
[#378](https://github.com/daltoniam/Starscream/issues/378)
|
||||
|
||||
#### [3.0.2](https://github.com/daltoniam/Starscream/tree/3.0.2)
|
||||
|
||||
Small fixes for 3.0.1.
|
||||
|
||||
|
||||
@@ -133,6 +133,25 @@ The writePing method is the same as write, but sends a ping control frame.
|
||||
socket.write(ping: Data()) //example on how to write a ping control frame over the socket!
|
||||
```
|
||||
|
||||
### write a pong frame
|
||||
|
||||
|
||||
the writePong method is the same as writePing, but sends a pong control frame.
|
||||
|
||||
```swift
|
||||
socket.write(pong: Data()) //example on how to write a pong control frame over the socket!
|
||||
```
|
||||
|
||||
Starscream will automatically respond to incoming `ping` control frames so you do not need to manually send `pong`s.
|
||||
|
||||
However if for some reason you need to control this prosses you can turn off the automatic `ping` response by disabling `respondToPingWithPong`.
|
||||
|
||||
```swift
|
||||
socket.respondToPingWithPong = false //Do not automaticaly respond to incoming pings with pongs.
|
||||
```
|
||||
|
||||
In most cases you will not need to do this.
|
||||
|
||||
### disconnect
|
||||
|
||||
The disconnect method does what you would expect and closes the socket.
|
||||
@@ -332,7 +351,7 @@ In most cases you do not need the extra info and should use the normal delegate.
|
||||
|
||||
#### websocketDidReceiveMessage
|
||||
```swift
|
||||
func websocketDidReceiveMessage(socket: WebSocketClient, text: String, response: WebSocket.WSResponse {
|
||||
func websocketDidReceiveMessage(socket: WebSocketClient, text: String, response: WebSocket.WSResponse) {
|
||||
print("got some text: \(text)")
|
||||
print("First frame for this message arrived on \(response.firstFrame)")
|
||||
}
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>FMWK</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>3.0.2</string>
|
||||
<string>3.0.3</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
|
||||
@@ -57,7 +57,8 @@ open class SSLCert {
|
||||
|
||||
open class SSLSecurity : SSLTrustValidator {
|
||||
public var validatedDN = true //should the domain name be validated?
|
||||
|
||||
public var validateEntireChain = true //should the entire cert chain be validated
|
||||
|
||||
var isReady = false //is the key processing done?
|
||||
var certificates: [Data]? //the certificates
|
||||
var pubKeys: [SecKey]? //the public keys
|
||||
@@ -170,6 +171,9 @@ open class SSLSecurity : SSLTrustValidator {
|
||||
var result: SecTrustResultType = .unspecified
|
||||
SecTrustEvaluate(trust,&result)
|
||||
if result == .unspecified || result == .proceed {
|
||||
if !validateEntireChain {
|
||||
return true
|
||||
}
|
||||
var trustedCount = 0
|
||||
for serverCert in serverCerts {
|
||||
for cert in certs {
|
||||
|
||||
+91
-22
@@ -55,6 +55,8 @@ public protocol WebSocketClient: class {
|
||||
var delegate: WebSocketDelegate? {get set }
|
||||
|
||||
var disableSSLCertValidation: Bool { get set }
|
||||
var overrideTrustHostname: Bool { get set }
|
||||
var desiredTrustHostname: String? { get set }
|
||||
#if os(Linux)
|
||||
#else
|
||||
var security: SSLTrustValidator? { get set }
|
||||
@@ -68,6 +70,7 @@ public protocol WebSocketClient: class {
|
||||
func write(string: String, completion: (() -> ())?)
|
||||
func write(data: Data, completion: (() -> ())?)
|
||||
func write(ping: Data, completion: (() -> ())?)
|
||||
func write(pong: Data, completion: (() -> ())?)
|
||||
}
|
||||
|
||||
//implements some of the base behaviors
|
||||
@@ -83,6 +86,10 @@ extension WebSocketClient {
|
||||
public func write(ping: Data) {
|
||||
write(ping: ping, completion: nil)
|
||||
}
|
||||
|
||||
public func write(pong: Data) {
|
||||
write(pong: pong, completion: nil)
|
||||
}
|
||||
|
||||
public func disconnect() {
|
||||
disconnect(forceTimeout: nil, closeCode: CloseCode.normal.rawValue)
|
||||
@@ -93,6 +100,8 @@ extension WebSocketClient {
|
||||
public struct SSLSettings {
|
||||
let useSSL: Bool
|
||||
let disableCertValidation: Bool
|
||||
var overrideTrustHostname: Bool
|
||||
var desiredTrustHostname: String?
|
||||
#if os(Linux)
|
||||
#else
|
||||
let cipherSuites: [SSLCipherSuite]?
|
||||
@@ -123,6 +132,8 @@ open class FoundationStream : NSObject, WSStream, StreamDelegate {
|
||||
private var outputStream: OutputStream?
|
||||
public weak var delegate: WSStreamDelegate?
|
||||
let BUFFER_MAX = 4096
|
||||
|
||||
public var enableSOCKSProxy = false
|
||||
|
||||
public func connect(url: URL, port: Int, timeout: TimeInterval, ssl: SSLSettings, completion: @escaping ((Error?) -> Void)) {
|
||||
var readStream: Unmanaged<CFReadStream>?
|
||||
@@ -131,20 +142,41 @@ open class FoundationStream : NSObject, WSStream, StreamDelegate {
|
||||
CFStreamCreatePairWithSocketToHost(nil, h, UInt32(port), &readStream, &writeStream)
|
||||
inputStream = readStream!.takeRetainedValue()
|
||||
outputStream = writeStream!.takeRetainedValue()
|
||||
|
||||
#if os(watchOS) //watchOS us unfortunately is missing the kCFStream properties to make this work
|
||||
#else
|
||||
if enableSOCKSProxy {
|
||||
let proxyDict = CFNetworkCopySystemProxySettings()
|
||||
let socksConfig = CFDictionaryCreateMutableCopy(nil, 0, proxyDict!.takeRetainedValue())
|
||||
let propertyKey = CFStreamPropertyKey(rawValue: kCFStreamPropertySOCKSProxy)
|
||||
CFWriteStreamSetProperty(outputStream, propertyKey, socksConfig)
|
||||
CFReadStreamSetProperty(inputStream, propertyKey, socksConfig)
|
||||
}
|
||||
#endif
|
||||
|
||||
guard let inStream = inputStream, let outStream = outputStream else { return }
|
||||
inStream.delegate = self
|
||||
outStream.delegate = self
|
||||
if ssl.useSSL {
|
||||
inStream.setProperty(StreamSocketSecurityLevel.negotiatedSSL as AnyObject, forKey: Stream.PropertyKey.socketSecurityLevelKey)
|
||||
outStream.setProperty(StreamSocketSecurityLevel.negotiatedSSL as AnyObject, forKey: Stream.PropertyKey.socketSecurityLevelKey)
|
||||
if ssl.disableCertValidation {
|
||||
#if os(watchOS) //watchOS us unfortunately is missing the kCFStream properties to make this work
|
||||
#else
|
||||
let settings: [NSObject: NSObject] = [kCFStreamSSLValidatesCertificateChain: NSNumber(value: false), kCFStreamSSLPeerName: kCFNull]
|
||||
#if os(watchOS) //watchOS us unfortunately is missing the kCFStream properties to make this work
|
||||
#else
|
||||
var settings = [NSObject: NSObject]()
|
||||
if ssl.disableCertValidation {
|
||||
settings[kCFStreamSSLValidatesCertificateChain] = NSNumber(value: false)
|
||||
}
|
||||
if ssl.overrideTrustHostname {
|
||||
if let hostname = ssl.desiredTrustHostname {
|
||||
settings[kCFStreamSSLPeerName] = hostname as NSString
|
||||
} else {
|
||||
settings[kCFStreamSSLPeerName] = kCFNull
|
||||
}
|
||||
}
|
||||
inStream.setProperty(settings, forKey: kCFStreamPropertySSLSettings as Stream.PropertyKey)
|
||||
outStream.setProperty(settings, forKey: kCFStreamPropertySSLSettings as Stream.PropertyKey)
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#if os(Linux)
|
||||
#else
|
||||
if let cipherSuites = ssl.cipherSuites {
|
||||
@@ -191,15 +223,16 @@ open class FoundationStream : NSObject, WSStream, StreamDelegate {
|
||||
}
|
||||
|
||||
public func write(data: Data) -> Int {
|
||||
guard let outStream = outputStream else {return 0}
|
||||
guard let outStream = outputStream else {return -1}
|
||||
let buffer = UnsafeRawPointer((data as NSData).bytes).assumingMemoryBound(to: UInt8.self)
|
||||
return outStream.write(buffer, maxLength: data.count)
|
||||
}
|
||||
|
||||
public func read() -> Data? {
|
||||
guard let stream = inputStream else {return nil}
|
||||
let buf = NSMutableData(capacity: BUFFER_MAX)
|
||||
let buffer = UnsafeMutableRawPointer(mutating: buf!.bytes).assumingMemoryBound(to: UInt8.self)
|
||||
let length = inputStream!.read(buffer, maxLength: BUFFER_MAX)
|
||||
let length = stream.read(buffer, maxLength: BUFFER_MAX)
|
||||
if length < 1 {
|
||||
return nil
|
||||
}
|
||||
@@ -207,13 +240,13 @@ open class FoundationStream : NSObject, WSStream, StreamDelegate {
|
||||
}
|
||||
|
||||
public func cleanup() {
|
||||
outputStream?.delegate = nil
|
||||
inputStream?.delegate = nil
|
||||
if let stream = inputStream {
|
||||
stream.delegate = nil
|
||||
CFReadStreamSetDispatchQueue(stream, nil)
|
||||
stream.close()
|
||||
}
|
||||
if let stream = outputStream {
|
||||
stream.delegate = nil
|
||||
CFWriteStreamSetDispatchQueue(stream, nil)
|
||||
stream.close()
|
||||
}
|
||||
@@ -225,7 +258,20 @@ open class FoundationStream : NSObject, WSStream, StreamDelegate {
|
||||
#else
|
||||
public func sslTrust() -> (trust: SecTrust?, domain: String?) {
|
||||
let trust = outputStream!.property(forKey: kCFStreamPropertySSLPeerTrust as Stream.PropertyKey) as! SecTrust?
|
||||
let domain = outputStream!.property(forKey: kCFStreamSSLPeerName as Stream.PropertyKey) as? String
|
||||
var domain = outputStream!.property(forKey: kCFStreamSSLPeerName as Stream.PropertyKey) as? String
|
||||
if domain == nil,
|
||||
let sslContextOut = CFWriteStreamCopyProperty(outputStream, CFStreamPropertyKey(rawValue: kCFStreamPropertySSLContext)) as! SSLContext? {
|
||||
var peerNameLen: Int = 0
|
||||
SSLGetPeerDomainNameLength(sslContextOut, &peerNameLen)
|
||||
var peerName = Data(count: peerNameLen)
|
||||
let _ = peerName.withUnsafeMutableBytes { (peerNamePtr: UnsafeMutablePointer<Int8>) in
|
||||
SSLGetPeerDomainName(sslContextOut, peerNamePtr, &peerNameLen)
|
||||
}
|
||||
if let peerDomain = String(bytes: peerName, encoding: .utf8), peerDomain.count > 0 {
|
||||
domain = peerDomain
|
||||
}
|
||||
}
|
||||
|
||||
return (trust, domain)
|
||||
}
|
||||
#endif
|
||||
@@ -351,6 +397,9 @@ open class WebSocket : NSObject, StreamDelegate, WebSocketClient, WSStreamDelega
|
||||
public var onPong: ((Data?) -> Void)?
|
||||
|
||||
public var disableSSLCertValidation = false
|
||||
public var overrideTrustHostname = false
|
||||
public var desiredTrustHostname: String? = nil
|
||||
|
||||
public var enableCompression = true
|
||||
#if os(Linux)
|
||||
#else
|
||||
@@ -364,9 +413,11 @@ open class WebSocket : NSObject, StreamDelegate, WebSocketClient, WSStreamDelega
|
||||
connectedMutex.unlock()
|
||||
return isConnected
|
||||
}
|
||||
|
||||
public var request: URLRequest //this is only public to allow headers, timeout, etc to be modified on reconnect
|
||||
public var currentURL: URL { return request.url! }
|
||||
|
||||
public var respondToPingWithPong: Bool = true
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private struct CompressionState {
|
||||
@@ -379,8 +430,7 @@ open class WebSocket : NSObject, StreamDelegate, WebSocketClient, WSStreamDelega
|
||||
var decompressor:Decompressor? = nil
|
||||
var compressor:Compressor? = nil
|
||||
}
|
||||
|
||||
private var request: URLRequest
|
||||
|
||||
private var stream: WSStream
|
||||
private var connected = false
|
||||
private var isConnecting = false
|
||||
@@ -505,6 +555,15 @@ open class WebSocket : NSObject, StreamDelegate, WebSocketClient, WSStreamDelega
|
||||
dequeueWrite(ping, code: .ping, writeCompletion: completion)
|
||||
}
|
||||
|
||||
/**
|
||||
Write a pong to the websocket. This sends it as a control frame.
|
||||
Respond to a Yodel.
|
||||
*/
|
||||
open func write(pong: Data, completion: (() -> ())? = nil) {
|
||||
guard isConnected else { return }
|
||||
dequeueWrite(pong, code: .pong, writeCompletion: completion)
|
||||
}
|
||||
|
||||
/**
|
||||
Private method that starts the connection.
|
||||
*/
|
||||
@@ -531,12 +590,15 @@ open class WebSocket : NSObject, StreamDelegate, WebSocketClient, WSStreamDelega
|
||||
request.setValue("\(url.host!):\(port!)", forHTTPHeaderField: headerWSHostName)
|
||||
|
||||
var path = url.absoluteString
|
||||
let offset = (url.scheme?.characters.count ?? 2) + 3
|
||||
let offset = (url.scheme?.count ?? 2) + 3
|
||||
path = String(path[path.index(path.startIndex, offsetBy: offset)..<path.endIndex])
|
||||
if let range = path.range(of: "/") {
|
||||
path = String(path[range.lowerBound..<path.endIndex])
|
||||
} else {
|
||||
path = "/"
|
||||
if let query = url.query {
|
||||
path += "?" + query
|
||||
}
|
||||
}
|
||||
|
||||
var httpBody = "\(request.httpMethod ?? "GET") \(path) HTTP/1.1\r\n"
|
||||
@@ -582,10 +644,15 @@ open class WebSocket : NSObject, StreamDelegate, WebSocketClient, WSStreamDelega
|
||||
let useSSL = supportedSSLSchemes.contains(url.scheme!)
|
||||
#if os(Linux)
|
||||
let settings = SSLSettings(useSSL: useSSL,
|
||||
disableCertValidation: disableSSLCertValidation)
|
||||
disableCertValidation: disableSSLCertValidation,
|
||||
overrideTrustHostname: overrideTrustHostname,
|
||||
desiredTrustHostname: desiredTrustHostname)
|
||||
#else
|
||||
let settings = SSLSettings(useSSL: useSSL,
|
||||
disableCertValidation: disableSSLCertValidation, cipherSuites: self.enabledSSLCipherSuites)
|
||||
disableCertValidation: disableSSLCertValidation,
|
||||
overrideTrustHostname: overrideTrustHostname,
|
||||
desiredTrustHostname: desiredTrustHostname,
|
||||
cipherSuites: self.enabledSSLCipherSuites)
|
||||
#endif
|
||||
certValidated = !useSSL
|
||||
let timeout = request.timeoutInterval * 1_000_000
|
||||
@@ -807,8 +874,8 @@ open class WebSocket : NSObject, StreamDelegate, WebSocketClient, WSStreamDelega
|
||||
}
|
||||
|
||||
if let acceptKey = headers[headerWSAcceptName.lowercased()] {
|
||||
if acceptKey.characters.count > 0 {
|
||||
if headerSecKey.characters.count > 0 {
|
||||
if acceptKey.count > 0 {
|
||||
if headerSecKey.count > 0 {
|
||||
let sha = "\(headerSecKey)258EAFA5-E914-47DA-95CA-C5AB0DC85B11".sha1Base64()
|
||||
if sha != acceptKey as String {
|
||||
return -1
|
||||
@@ -1088,8 +1155,10 @@ open class WebSocket : NSObject, StreamDelegate, WebSocketClient, WSStreamDelega
|
||||
private func processResponse(_ response: WSResponse) -> Bool {
|
||||
if response.isFin && response.bytesLeft <= 0 {
|
||||
if response.code == .ping {
|
||||
let data = response.buffer! // local copy so it is perverse for writing
|
||||
dequeueWrite(data as Data, code: .pong)
|
||||
if respondToPingWithPong {
|
||||
let data = response.buffer! // local copy so it is perverse for writing
|
||||
dequeueWrite(data as Data, code: .pong)
|
||||
}
|
||||
} else if response.code == .textFrame {
|
||||
guard let str = String(data: response.buffer! as Data, encoding: .utf8) else {
|
||||
writeError(CloseCode.encoding.rawValue)
|
||||
@@ -1191,7 +1260,7 @@ open class WebSocket : NSObject, StreamDelegate, WebSocketClient, WSStreamDelega
|
||||
let stream = s.stream
|
||||
let writeBuffer = UnsafeRawPointer(frame!.bytes+total).assumingMemoryBound(to: UInt8.self)
|
||||
let len = stream.write(data: Data(bytes: writeBuffer, count: offset-total))
|
||||
if len < 0 {
|
||||
if len <= 0 {
|
||||
var error: Error?
|
||||
let errCode = InternalErrorCode.outputStreamWriteError.rawValue
|
||||
error = s.errorWithDetail("output stream error during write", code: errCode)
|
||||
|
||||
+2
-1
@@ -1,6 +1,6 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = "Starscream"
|
||||
s.version = "3.0.2"
|
||||
s.version = "3.0.3"
|
||||
s.summary = "A conforming WebSocket RFC 6455 client library in Swift."
|
||||
s.homepage = "https://github.com/daltoniam/Starscream"
|
||||
s.license = 'Apache License, Version 2.0'
|
||||
@@ -14,6 +14,7 @@ Pod::Spec.new do |s|
|
||||
s.source_files = 'Sources/*.swift'
|
||||
s.libraries = 'z'
|
||||
s.pod_target_xcconfig = {
|
||||
'SWIFT_VERSION' => '4.0',
|
||||
'SWIFT_INCLUDE_PATHS' => '$(PODS_ROOT)/Starscream/zlib'
|
||||
}
|
||||
s.preserve_paths = 'zlib/*'
|
||||
|
||||
@@ -334,7 +334,7 @@
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
SWIFT_SWIFT3_OBJC_INFERENCE = Off;
|
||||
SWIFT_VERSION = 4.0;
|
||||
TVOS_DEPLOYMENT_TARGET = 10.0;
|
||||
TVOS_DEPLOYMENT_TARGET = 9.0;
|
||||
VALID_ARCHS = "x86_64 i386 arm64 armv7s armv7 armv7k";
|
||||
WATCHOS_DEPLOYMENT_TARGET = 2.0;
|
||||
};
|
||||
@@ -367,7 +367,7 @@
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
|
||||
SWIFT_SWIFT3_OBJC_INFERENCE = Off;
|
||||
SWIFT_VERSION = 4.0;
|
||||
TVOS_DEPLOYMENT_TARGET = 10.0;
|
||||
TVOS_DEPLOYMENT_TARGET = 9.0;
|
||||
VALID_ARCHS = "x86_64 i386 arm64 armv7s armv7 armv7k";
|
||||
WATCHOS_DEPLOYMENT_TARGET = 2.0;
|
||||
};
|
||||
|
||||
BIN
Binary file not shown.
@@ -10,7 +10,7 @@ import UIKit
|
||||
import Starscream
|
||||
|
||||
class ViewController: UIViewController, WebSocketDelegate {
|
||||
var socket = WebSocket(url: URL(string: "ws://localhost:8080/")!, protocols: ["chat", "superchat"])
|
||||
var socket = WebSocket(url: URL(string: "http://echo.websocket.org")!, protocols: ["chat", "superchat"])
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
Reference in New Issue
Block a user