Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Commit

Permalink
Fix shadowsocks adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuhaow committed Aug 13, 2016
1 parent d6a98bf commit 0169b19
Show file tree
Hide file tree
Showing 10 changed files with 1,107 additions and 51 deletions.
192 changes: 191 additions & 1 deletion NEKit.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions NEKitDemo/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Cocoa
import NEKit

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var window: NSWindow!
var proxy: GCDHTTPProxyServer?

func applicationDidFinishLaunching(aNotification: NSNotification) {
let config = Configuration()
let filepath = (NSHomeDirectory() as NSString).stringByAppendingPathComponent(".NEKit_demo.yaml")
// swiftlint:disable force_try
try! config.load(fromConfigFile: filepath)
RuleManager.currentManager = config.ruleManager
proxy = GCDHTTPProxyServer(address: IPv4Address(fromString: "127.0.0.1"), port: Port(port: UInt16(config.proxyPort!)))
// swiftlint:disable force_try
try! proxy!.start()
}

func applicationWillTerminate(aNotification: NSNotification) {
proxy?.stop()
}


}
58 changes: 58 additions & 0 deletions NEKitDemo/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"images" : [
{
"idiom" : "mac",
"size" : "16x16",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "16x16",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "32x32",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "32x32",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "128x128",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "128x128",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "256x256",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "256x256",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "512x512",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "512x512",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
680 changes: 680 additions & 0 deletions NEKitDemo/Base.lproj/MainMenu.xib

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions NEKitDemo/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2016 Zhuhao Wang. All rights reserved.</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>
50 changes: 50 additions & 0 deletions src/Socket/AdapterSocket/AdapterSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,56 @@ class AdapterSocket: NSObject, SocketProtocol, RawTCPSocketDelegate {
/// The current connection status of the socket.
var state: SocketStatus = .Invalid

/// If the socket is disconnected.
var isDisconnected: Bool {
return state == .Closed || state == .Invalid
}

/**
Read data from the socket.
- parameter tag: The tag identifying the data in the callback delegate method.
- warning: This should only be called after the last read is finished, i.e., `delegate?.didReadData()` is called.
*/
func readDataWithTag(tag: Int) {
socket.readDataWithTag(tag)
}

/**
Send data to remote.
- parameter data: Data to send.
- parameter tag: The tag identifying the data in the callback delegate method.
- warning: This should only be called after the last write is finished, i.e., `delegate?.didWriteData()` is called.
*/
func writeData(data: NSData, withTag tag: Int) {
socket.writeData(data, withTag: tag)
}

// func readDataToLength(length: Int, withTag tag: Int) {
// socket.readDataToLength(length, withTag: tag)
// }
//
// func readDataToData(data: NSData, withTag tag: Int) {
// socket.readDataToData(data, withTag: tag)
// }

/**
Disconnect the socket elegantly.
*/
func disconnect() {
state = .Disconnecting
socket.disconnect()
}

/**
Disconnect the socket immediately.
*/
func forceDisconnect() {
state = .Disconnecting
socket.forceDisconnect()
}

// MARK: RawTCPSocketDelegate Protocol Implemention

/**
Expand Down
16 changes: 9 additions & 7 deletions src/Socket/AdapterSocket/ShadowsocksAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,37 +91,39 @@ class ShadowsocksAdapter: AdapterSocket {
super.writeData(data, withTag: tag)
}

func readDataWithTag(tag: Int) {
override func readDataWithTag(tag: Int) {
if readIV == nil && !readingIV {
readingIV = true
socket.readDataToLength(ivLength, withTag: ShadowsocksTag.InitialVector.rawValue)
nextReadTag = tag
socket.readDataToLength(ivLength, withTag: ShadowsocksTag.InitialVector.rawValue)
} else {
super.readDataWithTag(tag)
}
}

func writeData(data: NSData, withTag tag: Int) {
override func writeData(data: NSData, withTag tag: Int) {
writeRawData(encryptData(data), withTag: tag)
}

override func didReadData(data: NSData, withTag tag: Int, from socket: RawTCPSocketProtocol) {
super.didReadData(decryptData(data), withTag: tag, from: socket)
if tag == ShadowsocksTag.InitialVector.rawValue {
readIV = data
readingIV = false
super.didReadData(data, withTag: tag, from: socket)
super.readDataWithTag(nextReadTag)
} else {
delegate?.didReadData(decryptData(data), withTag: tag, from: self)
let readData = decryptData(data)
super.didReadData(readData, withTag: tag, from: socket)
delegate?.didReadData(readData, withTag: tag, from: self)
}
}

override func didWriteData(data: NSData?, withTag tag: Int, from socket: RawTCPSocketProtocol) {
super.didWriteData(data, withTag: tag, from: socket)
super.didWriteData(nil, withTag: tag, from: socket)
if tag == ShadowsocksTag.Connect.rawValue {
delegate?.readyToForward(self)
} else {
delegate?.didWriteData(data, withTag: tag, from: self)
delegate?.didWriteData(nil, withTag: tag, from: self)
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/Socket/AdapterSocket/SpeedAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SpeedAdapter: AdapterSocket, SocketDelegate {
}
}

func disconnect() {
override func disconnect() {
_shouldConnect = false
pendingCount = 0
for (adapter, _) in adapters {
Expand All @@ -40,7 +40,7 @@ class SpeedAdapter: AdapterSocket, SocketDelegate {
}
}

func forceDisconnect() {
override func forceDisconnect() {
_shouldConnect = false
pendingCount = 0
for (adapter, _) in adapters {
Expand Down Expand Up @@ -88,4 +88,5 @@ class SpeedAdapter: AdapterSocket, SocketDelegate {
func didWriteData(data: NSData?, withTag: Int, from: SocketProtocol) {}
func didReadData(data: NSData, withTag: Int, from: SocketProtocol) {}
func updateAdapter(newAdapter: AdapterSocket) {}
func didReceiveRequest(request: ConnectRequest, from: ProxySocket) {}
}
45 changes: 44 additions & 1 deletion src/Socket/ProxySocket/ProxySocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ class ProxySocket: NSObject, SocketProtocol, RawTCPSocketDelegate {
/// Received `ConnectRequest`.
var request: ConnectRequest?

/// If the socket is disconnected.
var isDisconnected: Bool {
return state == .Closed || state == .Invalid
}

/**
Init a `ProxySocket` with a raw TCP socket.
Expand Down Expand Up @@ -36,10 +41,46 @@ class ProxySocket: NSObject, SocketProtocol, RawTCPSocketDelegate {
- parameter tag: The tag identifying the data in the callback delegate method.
- warning: This should only be called after the last read is finished, i.e., `delegate?.didReadData()` is called.
*/
func readDataWithTag(tag: Int = SocketTag.Forward) {
func readDataWithTag(tag: Int) {
socket.readDataWithTag(tag)
}

/**
Send data to remote.
- parameter data: Data to send.
- parameter tag: The tag identifying the data in the callback delegate method.
- warning: This should only be called after the last write is finished, i.e., `delegate?.didWriteData()` is called.
*/
func writeData(data: NSData, withTag tag: Int) {
socket.writeData(data, withTag: tag)
}

// func readDataToLength(length: Int, withTag tag: Int) {
// socket.readDataToLength(length, withTag: tag)
// }
//
// func readDataToData(data: NSData, withTag tag: Int) {
// socket.readDataToData(data, withTag: tag)
// }

/**
Disconnect the socket elegantly.
*/
func disconnect() {
state = .Disconnecting
socket.disconnect()
}

/**
Disconnect the socket immediately.
*/
func forceDisconnect() {
state = .Disconnecting
socket.forceDisconnect()
}


// MARK: SocketProtocol Implemention

/// The underlying TCP socket transmitting data.
Expand Down Expand Up @@ -98,4 +139,6 @@ class ProxySocket: NSObject, SocketProtocol, RawTCPSocketDelegate {
func didConnect(socket: RawTCPSocketProtocol) {

}


}
52 changes: 12 additions & 40 deletions src/Socket/SocketProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,58 +43,36 @@ protocol SocketProtocol: class {

/// The current connection status of the socket.
var state: SocketStatus { get set }
}

extension SocketProtocol {
/// If the socket is disconnected.
var isDisconnected: Bool {
return state == .Closed || state == .Invalid
}
var isDisconnected: Bool { get }

/**
Send data to remote.
Read data from the socket.
- parameter data: Data to send.
- parameter tag: The tag identifying the data in the callback delegate method.
- warning: This should only be called after the last write is finished, i.e., `delegate?.didWriteData()` is called.
- parameter tag: The tag identifying the data in the callback delegate method.
- warning: This should only be called after the last read is finished, i.e., `delegate?.didReadData()` is called.
*/
func writeData(data: NSData, withTag tag: Int = 0) {
socket.writeData(data, withTag: tag)
}

// func readDataToLength(length: Int, withTag tag: Int) {
// socket.readDataToLength(length, withTag: tag)
// }
//
// func readDataToData(data: NSData, withTag tag: Int) {
// socket.readDataToData(data, withTag: tag)
// }
func readDataWithTag(tag: Int)

/**
Read data from the socket.
Send data to remote.
- parameter tag: The tag identifying the data in the callback delegate method.
- warning: This should only be called after the last read is finished, i.e., `delegate?.didReadData()` is called.
- parameter data: Data to send.
- parameter tag: The tag identifying the data in the callback delegate method.
- warning: This should only be called after the last write is finished, i.e., `delegate?.didWriteData()` is called.
*/
func readDataWithTag(tag: Int = SocketTag.Forward) {
socket.readDataWithTag(tag)
}
func writeData(data: NSData, withTag tag: Int)

/**
Disconnect the socket elegantly.
*/
func disconnect() {
state = .Disconnecting
socket.disconnect()
}
func disconnect()

/**
Disconnect the socket immediately.
*/
func forceDisconnect() {
state = .Disconnecting
socket.forceDisconnect()
}
func forceDisconnect()
}

/// The delegate protocol to handle the events from a socket.
Expand Down Expand Up @@ -155,9 +133,3 @@ protocol SocketDelegate : class {
*/
func updateAdapter(newAdapter: AdapterSocket)
}

extension SocketDelegate {
func didReceiveRequest(request: ConnectRequest, from: ProxySocket) {}

func didConnect(adapterSocket: AdapterSocket, withResponse: ConnectResponse) {}
}

0 comments on commit 0169b19

Please sign in to comment.