Skip to content

Commit 1195ec9

Browse files
committed
UI Loading Update and Redis Server Fix
1 parent 1db27c5 commit 1195ec9

8 files changed

Lines changed: 369 additions & 272 deletions

File tree

AdversaryLabSwift.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
468B264D2023C099000A2B5D /* Datable.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 46D8512F201FE4B50052DF49 /* Datable.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
3636
468B26522023D016000A2B5D /* ObservedConnections.swift in Sources */ = {isa = PBXBuildFile; fileRef = 468B26512023D016000A2B5D /* ObservedConnections.swift */; };
3737
468EF6B6207FEDA40007D0DF /* subsequences.so in Resources */ = {isa = PBXBuildFile; fileRef = 468EF6B5207FEDA40007D0DF /* subsequences.so */; };
38+
539110FF2089246800960308 /* CheckRedisServerScript.sh in Resources */ = {isa = PBXBuildFile; fileRef = 539110F72089246800960308 /* CheckRedisServerScript.sh */; };
3839
CA727CD42050A3580051A01B /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = CA727CD32050A3580051A01B /* README.md */; };
3940
CAA6C98320642A2D00EE8DF1 /* KnownProtocols.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAA6C97A20642A2D00EE8DF1 /* KnownProtocols.swift */; };
4041
CAA6C98420642A2D00EE8DF1 /* TLS12.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAA6C97B20642A2D00EE8DF1 /* TLS12.swift */; };
@@ -160,6 +161,7 @@
160161
468B26232023B609000A2B5D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
161162
468B26512023D016000A2B5D /* ObservedConnections.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObservedConnections.swift; sourceTree = "<group>"; };
162163
468EF6B5207FEDA40007D0DF /* subsequences.so */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.bundle"; path = subsequences.so; sourceTree = "<group>"; };
164+
539110F72089246800960308 /* CheckRedisServerScript.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = CheckRedisServerScript.sh; sourceTree = "<group>"; };
163165
CA727CD32050A3580051A01B /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
164166
CAA6C97A20642A2D00EE8DF1 /* KnownProtocols.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KnownProtocols.swift; sourceTree = "<group>"; };
165167
CAA6C97B20642A2D00EE8DF1 /* TLS12.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TLS12.swift; sourceTree = "<group>"; };
@@ -227,6 +229,7 @@
227229
4651A14420083BF700037D31 = {
228230
isa = PBXGroup;
229231
children = (
232+
539110F72089246800960308 /* CheckRedisServerScript.sh */,
230233
460CF7E72076C76D00FB3723 /* ShutdownRedisServerScript.sh */,
231234
460CF7E52076C3D800FB3723 /* LaunchRedisServerScript.sh */,
232235
460CF7E92076EB0800FB3723 /* redis.conf */,
@@ -482,6 +485,7 @@
482485
CA727CD42050A3580051A01B /* README.md in Resources */,
483486
4651A15520083BF700037D31 /* Assets.xcassets in Resources */,
484487
460CF7E62076C3D800FB3723 /* LaunchRedisServerScript.sh in Resources */,
488+
539110FF2089246800960308 /* CheckRedisServerScript.sh in Resources */,
485489
4651A15820083BF700037D31 /* Main.storyboard in Resources */,
486490
460CF7E82076C76D00FB3723 /* ShutdownRedisServerScript.sh in Resources */,
487491
468EF6B6207FEDA40007D0DF /* subsequences.so in Resources */,

AdversaryLabSwift/Controllers/RedisServerController.swift

Lines changed: 93 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,57 @@ class RedisServerController: NSObject
1616

1717
func launchRedisServer()
1818
{
19-
let bundle = Bundle.main
20-
21-
guard let redisConfigPath = bundle.path(forResource: "redis", ofType: "conf")
22-
else
23-
{
24-
print("Unable to launch Redis server: could not find terraform executable.")
25-
return
26-
}
27-
28-
guard let redisPath = bundle.path(forResource: "redis-server", ofType: nil)
29-
else
19+
isRedisServerRunning
3020
{
31-
print("Unable to launch Redis server: could not find terraform executable.")
32-
return
33-
}
34-
35-
guard let redisModulePath = bundle.path(forResource: "subsequences", ofType: "so")
36-
else
37-
{
38-
print("Unable to launch Redis server: could not find the needed module.")
39-
return
40-
}
41-
42-
guard let path = bundle.path(forResource: "LaunchRedisServerScript", ofType: "sh")
21+
(serverIsRunning) in
22+
23+
if serverIsRunning
24+
{
25+
return
26+
}
4327
else
44-
{
45-
print("Unable to launch Redis server. Could not find the script.")
46-
return
28+
{
29+
let bundle = Bundle.main
30+
31+
guard let redisConfigPath = bundle.path(forResource: "redis", ofType: "conf")
32+
else
33+
{
34+
print("Unable to launch Redis server: could not find terraform executable.")
35+
return
36+
}
37+
38+
guard let redisPath = bundle.path(forResource: "redis-server", ofType: nil)
39+
else
40+
{
41+
print("Unable to launch Redis server: could not find terraform executable.")
42+
return
43+
}
44+
45+
guard let redisModulePath = bundle.path(forResource: "subsequences", ofType: "so")
46+
else
47+
{
48+
print("Unable to launch Redis server: could not find the needed module.")
49+
return
50+
}
51+
52+
guard let path = bundle.path(forResource: "LaunchRedisServerScript", ofType: "sh")
53+
else
54+
{
55+
print("Unable to launch Redis server. Could not find the script.")
56+
return
57+
}
58+
59+
print("\n👇👇 Running Script 👇👇:\n")
60+
61+
self.runRedisScript(path: path, arguments: [redisPath, redisConfigPath, redisModulePath])
62+
{
63+
(hasCompleted) in
64+
65+
print("🚀 Launch Redis Server Script Complete 🚀")
66+
}
67+
}
4768
}
4869

49-
print("\n👇👇 Running Script 👇👇:\n")
50-
51-
runRedisScript(path: path, arguments: [redisPath, redisConfigPath, redisModulePath])
52-
{
53-
(hasCompleted) in
54-
55-
print("🚀 Launch Redis Server Script Complete 🚀")
56-
}
5770
}
5871

5972
func shutdownRedisServer()
@@ -90,6 +103,52 @@ class RedisServerController: NSObject
90103
}
91104
}
92105

106+
func isRedisServerRunning(completion:@escaping (_ completion:Bool) -> Void)
107+
{
108+
guard let redisCliPath = Bundle.main.path(forResource: "redis-cli", ofType: nil)
109+
else
110+
{
111+
print("Unable to ping Redis server. Could not find terraform executable.")
112+
completion(false)
113+
return
114+
}
115+
guard let path = Bundle.main.path(forResource: "CheckRedisServerScript", ofType: "sh")
116+
else
117+
{
118+
print("Unable to ping Redis server. Could not find the script.")
119+
completion(false)
120+
return
121+
}
122+
123+
let process = Process()
124+
process.launchPath = path
125+
process.arguments = [redisCliPath]
126+
let pipe = Pipe()
127+
process.standardOutput = pipe
128+
process.terminationHandler =
129+
{
130+
(task) in
131+
// Get the data
132+
let data = pipe.fileHandleForReading.readDataToEndOfFile()
133+
let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
134+
135+
print(output ?? "no output")
136+
137+
if output == "PONG\n"
138+
{
139+
print("\nWe received a pong, server is already running!!")
140+
completion(true)
141+
}
142+
else
143+
{
144+
print("\nNo Pong, launch the server!!")
145+
completion(false)
146+
}
147+
}
148+
process.waitUntilExit()
149+
process.launch()
150+
}
151+
93152
func runRedisScript(path: String, arguments: [String]?, completion:@escaping (_ completion:Bool) -> Void)
94153
{
95154
let processQueue = DispatchQueue.global(qos: .background)

0 commit comments

Comments
 (0)