Skip to content

Commit

Permalink
ServerIP and Port
Browse files Browse the repository at this point in the history
  • Loading branch information
bragelbytes committed Sep 6, 2023
1 parent d78e407 commit c00f1fe
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Sources/ShadowSwift/SIP008/JsonConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ extension ServerConfig
do
{
let serverPrivateKey = try PrivateKey(type: .P256KeyAgreement, data: serverPrivateKeyData)
return ShadowConfig.ShadowServerConfig(serverAddress: "\(server):\(server_port)", serverPrivateKey: serverPrivateKey, mode: mode)
return try ShadowConfig.ShadowServerConfig(serverAddress: "\(server):\(server_port)", serverPrivateKey: serverPrivateKey, mode: mode)
}
catch
{
Expand Down
79 changes: 74 additions & 5 deletions Sources/ShadowSwift/ShadowConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,33 @@ public class ShadowConfig
{
public static let serverConfigFilename = "ShadowServerConfig.json"
public let serverAddress: String
public let serverIP: String
public let serverPort: UInt16
public let serverPrivateKey: PrivateKey
public let mode: CipherMode
public var transportName = "shadow"

private enum CodingKeys : String, CodingKey
{
case serverAddress, serverPrivateKey, mode = "cipherName", transportName = "transport"
case serverAddress
case serverPrivateKey
case mode = "cipherName"
case transportName = "transport"
}

public init(serverAddress: String, serverPrivateKey: PrivateKey, mode: CipherMode)
public init(serverAddress: String, serverPrivateKey: PrivateKey, mode: CipherMode) throws
{
self.serverAddress = serverAddress

let addressStrings = serverAddress.split(separator: ":")
self.serverIP = String(addressStrings[0])
guard let port = UInt16(addressStrings[1]) else
{
print("Error decoding ShadowServerConfig data: invalid server port \(addressStrings[1])")
throw ShadowConfigError.invalidServerPort(serverAddress: serverAddress)
}

self.serverPort = port
self.serverPrivateKey = serverPrivateKey
self.mode = mode
}
Expand All @@ -38,6 +53,7 @@ public class ShadowConfig
do
{
let decoded = try decoder.decode(ShadowServerConfig.self, from: data)

self = decoded
}
catch
Expand All @@ -63,12 +79,34 @@ public class ShadowConfig
return nil
}
}

public init(from decoder: Decoder) throws
{
let container = try decoder.container(keyedBy: CodingKeys.self)
let address = try container.decode(String.self, forKey: .serverAddress)
let addressStrings = address.split(separator: ":")
let ipAddress = String(addressStrings[0])
guard let port = UInt16(addressStrings[1]) else
{
print("Error decoding ShadowConfig data: invalid server port")
throw ShadowConfigError.invalidJSON
}

self.serverAddress = address
self.serverIP = ipAddress
self.serverPort = port
self.serverPrivateKey = try container.decode(PrivateKey.self, forKey: .serverPrivateKey)
self.mode = try container.decode(CipherMode.self, forKey: .mode)
self.transportName = try container.decode(String.self, forKey: .transportName)
}
}

public struct ShadowClientConfig: Codable
{
public static let clientConfigFilename = "ShadowClientConfig.json"
public let serverAddress: String
public let serverIP: String
public let serverPort: UInt16
public let serverPublicKey: PublicKey
public let mode: CipherMode
public var transportName = "shadow"
Expand All @@ -78,9 +116,20 @@ public class ShadowConfig
case serverAddress, serverPublicKey, mode = "cipherName", transportName = "transport"
}

public init(serverAddress: String, serverPublicKey: PublicKey, mode: CipherMode)
public init(serverAddress: String, serverPublicKey: PublicKey, mode: CipherMode) throws
{
self.serverAddress = serverAddress

let addressStrings = serverAddress.split(separator: ":")
let ipAddress = String(addressStrings[0])
guard let port = UInt16(addressStrings[1]) else
{
print("Error decoding ShadowConfig data: invalid server port")
throw ShadowConfigError.invalidServerPort(serverAddress: serverAddress)
}

self.serverIP = ipAddress
self.serverPort = port
self.serverPublicKey = serverPublicKey
self.mode = mode
}
Expand Down Expand Up @@ -116,15 +165,35 @@ public class ShadowConfig
return nil
}
}

public init(from decoder: Decoder) throws
{
let container = try decoder.container(keyedBy: CodingKeys.self)
let address = try container.decode(String.self, forKey: .serverAddress)
let addressStrings = address.split(separator: ":")
let ipAddress = String(addressStrings[0])
guard let port = UInt16(addressStrings[1]) else
{
print("Error decoding ShadowConfig data: invalid server port")
throw ShadowConfigError.invalidJSON
}

self.serverAddress = address
self.serverIP = ipAddress
self.serverPort = port
self.serverPublicKey = try container.decode(PublicKey.self, forKey: .serverPublicKey)
self.mode = try container.decode(CipherMode.self, forKey: .mode)
self.transportName = try container.decode(String.self, forKey: .transportName)
}
}

public static func generateNewConfigPair(serverAddress: String, cipher: CipherMode) throws -> (serverConfig: ShadowServerConfig, clientConfig: ShadowClientConfig)
{
let privateKey = try PrivateKey(type: .P256KeyAgreement)
let publicKey = privateKey.publicKey

let serverConfig = ShadowServerConfig(serverAddress: serverAddress, serverPrivateKey: privateKey, mode: cipher)
let clientConfig = ShadowClientConfig(serverAddress: serverAddress, serverPublicKey: publicKey, mode: cipher)
let serverConfig = try ShadowServerConfig(serverAddress: serverAddress, serverPrivateKey: privateKey, mode: cipher)
let clientConfig = try ShadowClientConfig(serverAddress: serverAddress, serverPublicKey: publicKey, mode: cipher)

return (serverConfig, clientConfig)
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/ShadowSwift/ShadowErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ enum ShadowConfigError: Error
{
case urlIsNotDirectory
case failedToSaveFile(filePath: String)
case invalidJSON
case invalidServerPort(serverAddress: String)

public var description: String
{
Expand All @@ -28,6 +30,10 @@ enum ShadowConfigError: Error
return "The provided URL is not a directory."
case .failedToSaveFile(let filePath):
return "Failed to save the config file to \(filePath)"
case .invalidJSON:
return "Error decoding JSON data."
case .invalidServerPort(let serverAddress):
return "Error decoding ShadowServerConfig data: invalid server port from address: \(serverAddress)"
}
}
}
36 changes: 25 additions & 11 deletions Tests/ShadowSwiftTests/ShadowSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ShadowSwiftTests: XCTestCase
let publicKey = try PublicKey(type: .P256KeyAgreement, data: publicKeyData)

// TODO: Enter your server IP and Port.
let shadowConfig = ShadowConfig.ShadowClientConfig(serverAddress: "127.0.0.1:1234", serverPublicKey: publicKey, mode: .DARKSTAR)
let shadowConfig = try ShadowConfig.ShadowClientConfig(serverAddress: "127.0.0.1:1234", serverPublicKey: publicKey, mode: .DARKSTAR)
let shadowFactory = ShadowConnectionFactory(config: shadowConfig, logger: self.logger)
let httpRequestData = Data("GET / HTTP/1.0\r\nConnection: close\r\n\r\n")
print(">>>>>> Created a Shadow connection factory.")
Expand Down Expand Up @@ -166,7 +166,7 @@ class ShadowSwiftTests: XCTestCase

let publicKey = try PublicKey(type: .P256KeyAgreement, data: publicKeyData)

let shadowConfig = ShadowConfig.ShadowClientConfig(serverAddress: "127.0.0.1:1234", serverPublicKey: publicKey, mode: .DARKSTAR)
let shadowConfig = try ShadowConfig.ShadowClientConfig(serverAddress: "127.0.0.1:1234", serverPublicKey: publicKey, mode: .DARKSTAR)

let shadowFactory = ShadowConnectionFactory(config: shadowConfig, logger: logger)

Expand Down Expand Up @@ -222,7 +222,7 @@ class ShadowSwiftTests: XCTestCase

let publicKey = try PublicKey(type: .P256KeyAgreement, data: publicKeyData)

let shadowConfig = ShadowConfig.ShadowClientConfig(serverAddress: "127.0.0.1:1234", serverPublicKey: publicKey, mode: .DARKSTAR)
let shadowConfig = try ShadowConfig.ShadowClientConfig(serverAddress: "127.0.0.1:1234", serverPublicKey: publicKey, mode: .DARKSTAR)

let shadowFactory = ShadowConnectionFactory(config: shadowConfig, logger: logger)

Expand Down Expand Up @@ -308,7 +308,7 @@ class ShadowSwiftTests: XCTestCase

let publicKey = try PublicKey(type: .P256KeyAgreement, data: publicKeyData)

let shadowConfig = ShadowConfig.ShadowClientConfig(serverAddress: "127.0.0.1:1234", serverPublicKey: publicKey, mode: .DARKSTAR)
let shadowConfig = try ShadowConfig.ShadowClientConfig(serverAddress: "127.0.0.1:1234", serverPublicKey: publicKey, mode: .DARKSTAR)
let shadowFactory = ShadowConnectionFactory(config: shadowConfig, logger: logger)

guard var shadowConnection = shadowFactory.connect(using: .tcp) else
Expand Down Expand Up @@ -414,7 +414,7 @@ class ShadowSwiftTests: XCTestCase

let publicKey = try PublicKey(type: .P256KeyAgreement, data: publicKeyData)

let shadowConfig = ShadowConfig.ShadowClientConfig(serverAddress: "127.0.0.1:1234", serverPublicKey: publicKey, mode: .DARKSTAR)
let shadowConfig = try ShadowConfig.ShadowClientConfig(serverAddress: "127.0.0.1:1234", serverPublicKey: publicKey, mode: .DARKSTAR)
let shadowFactory = ShadowConnectionFactory(config: shadowConfig, logger: logger)

guard var shadowConnection = shadowFactory.connect(using: .tcp)
Expand Down Expand Up @@ -652,7 +652,7 @@ class ShadowSwiftTests: XCTestCase

let privateKey = try PrivateKey(type: .P256KeyAgreement, data: privateKeyData)

let shadowConfig = ShadowConfig.ShadowServerConfig(serverAddress: "127.0.0.1:1234", serverPrivateKey: privateKey, mode: .DARKSTAR)
let shadowConfig = try ShadowConfig.ShadowServerConfig(serverAddress: "127.0.0.1:1234", serverPrivateKey: privateKey, mode: .DARKSTAR)
let encoder = JSONEncoder()
let json = try? encoder.encode(shadowConfig)

Expand All @@ -677,14 +677,20 @@ class ShadowSwiftTests: XCTestCase
print(publicKeyData.base64EncodedString())
}

func testDarkStarClientAndServer()
func testDarkStarClientAndServer() throws
{
let privateKeyString = "RaHouPFVOazVSqInoMm8BSO9o/7J493y4cUVofmwXAU="
guard let privateKeyBytes = Data(base64: privateKeyString) else {return}
guard let privateKey = try? PrivateKey(type: .P256KeyAgreement, data: privateKeyBytes) else {return}
let publicKey = privateKey.publicKey

guard let server = ShadowServer(host: "127.0.0.1", port: 1234, config: ShadowConfig.ShadowServerConfig(serverAddress: "127.0.0.1:1234", serverPrivateKey: privateKey, mode: .DARKSTAR), logger: self.logger) else {return}
let serverConfig = try ShadowConfig.ShadowServerConfig(serverAddress: "127.0.0.1:1234", serverPrivateKey: privateKey, mode: .DARKSTAR)

guard let server = ShadowServer(host: "127.0.0.1", port: 1234, config: serverConfig, logger: self.logger) else
{
XCTFail()
return
}

let queue = DispatchQueue(label: "Client")
queue.async
Expand All @@ -705,7 +711,9 @@ class ShadowSwiftTests: XCTestCase

}

let factory = ShadowConnectionFactory(config: ShadowConfig.ShadowClientConfig(serverAddress: "127.0.0.1:1234", serverPublicKey: publicKey, mode: .DARKSTAR), logger: self.logger)
let clientConfig = try ShadowConfig.ShadowClientConfig(serverAddress: "127.0.0.1:1234", serverPublicKey: publicKey, mode: .DARKSTAR)

let factory = ShadowConnectionFactory(config: clientConfig, logger: self.logger)
guard var client = factory.connect(using: .tcp) else {return}

client.stateUpdateHandler={
Expand All @@ -724,7 +732,7 @@ class ShadowSwiftTests: XCTestCase
client.start(queue: queue2)
}

func testDarkStarOnlyServer()
func testDarkStarOnlyServer() throws
{
let sent = XCTestExpectation(description: "Sent!")

Expand All @@ -741,7 +749,13 @@ class ShadowSwiftTests: XCTestCase
return
}

guard let server = ShadowServer(host: "127.0.0.1", port: 1234, config: ShadowConfig.ShadowServerConfig(serverAddress: "127.0.0.1:1234", serverPrivateKey: privateKey, mode: .DARKSTAR), logger: self.logger) else {return}
let shadowServerConfig = try ShadowConfig.ShadowServerConfig(serverAddress: "127.0.0.1:1234", serverPrivateKey: privateKey, mode: .DARKSTAR)

guard let server = ShadowServer(host: "127.0.0.1", port: 1234, config: shadowServerConfig, logger: self.logger) else
{
XCTFail()
return
}

do
{
Expand Down

0 comments on commit c00f1fe

Please sign in to comment.