Skip to content

Commit b2c71d1

Browse files
authored
Version mismatch http codes (#16)
* chore: Make releases & testing easier * fix: Check HTTP status codes + version checking * chore: bump release version
1 parent d347637 commit b2c71d1

File tree

6 files changed

+104
-60
lines changed

6 files changed

+104
-60
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ It is assumed you know your way around a command line. Commands are ran on your
7070
{
7171
"api_key": "YOUR_API_KEY",
7272
"device_configuration_manager_url": "https://YOUR_AWESOME_DCM_URL",
73-
"dmon_url": "https://YOUR_URL:PORT/path/", // Url to download update files from
73+
"dmon_url": "https://YOUR_URL:PORT/path/", // Url to download update files from. Leave empty if you don't want to use the update function.
7474
"dmon_username": "username", // Basic Auth username. Leave empty if not used
7575
"dmon_password": "password" // Basic Auth password. Leave empty if not used
7676
}

bin/package

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,12 @@ cd ..
66
# Ugly hack. This should really be excluded by dm.pl but ¯\_(ツ)_/¯
77
/usr/bin/find . -name '.DS_Store' -type f -prune -exec rm -rf '{}' +
88
DEB_VERSION=$(grep -i "^Version:" ./src/DEBIAN/control | cut -d' ' -f2 -)
9-
mkdir releases
9+
mkdir releases 2>/dev/null
1010
./bin/dm.pl src/ releases/com.github.clburlison.dmon-"$DEB_VERSION".deb
11+
12+
# I really hate shell scripts. Should use a real language for this...
13+
HASH1=$(shasum -a256 "releases/com.github.clburlison.dmon-"$DEB_VERSION".deb" | cut -d " " -f1)
14+
HASH2=$(md5 "releases/com.github.clburlison.dmon-"$DEB_VERSION".deb" | cut -d " " -f4)
15+
16+
echo "SHA-256: \`$HASH1\`"
17+
echo "MD5: \`$HASH2\`"

src/DEBIAN/control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ Name: dmon
88
Package: com.github.clburlison.dmon
99
Section: Tweaks
1010
Tag: purpose::extension, role::enduser
11-
Version: 0.0.7
11+
Version: 0.0.8

src/usr/bin/dmon

224 Bytes
Binary file not shown.

tools/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ include $(THEOS_MAKE_PATH)/tool.mk
2424
all::
2525
mkdir -p ../src/usr/bin
2626
/bin/cp .theos/obj/dmon ../src/usr/bin
27+
28+
upload:: all
29+
ssh iphone "rm /usr/bin/dmon" || true
30+
sleep 2
31+
scp .theos/obj/dmon iphone:/usr/bin/dmon

tools/dmon.m

Lines changed: 89 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,70 @@
1717
}
1818

1919
NSString * installedPogoVersion(void) {
20-
NSLog(@"dmon: Finding Pogo version...");
21-
char bundlePath[150]; // This should never be longer than 100 but we added a buffer
20+
char path[1035];
2221
FILE *pogo_cmd = popen("find /var/containers/Bundle/Application/ -type d -name 'PokmonGO.app'", "r");
22+
NSString *bundlePath = nil;
2323
if (pogo_cmd == NULL) {
2424
NSLog(@"dmon: Unable to find PokemonGo on device");
2525
return nil;
2626
}
2727

28-
fgets(bundlePath, sizeof(bundlePath), pogo_cmd);
28+
// Read the output from the command
29+
if (fgets(path, sizeof(path)-1, pogo_cmd) != NULL) {
30+
bundlePath = [NSString stringWithUTF8String:path];
31+
bundlePath = [bundlePath stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
32+
bundlePath = [bundlePath stringByAppendingPathComponent: @"Info.plist"];
33+
}
34+
35+
// Close the file
2936
pclose(pogo_cmd);
30-
bundlePath[strcspn(bundlePath, "\n")] = '\0'; // Remove newline character
31-
sprintf(bundlePath, "%s/Info.plist", bundlePath);
32-
33-
NSString *path = [[NSString alloc] initWithUTF8String:bundlePath];
34-
NSDictionary *infoDict = [[NSDictionary alloc] initWithContentsOfFile:path];
35-
NSString *versionString = [infoDict objectForKey:@"CFBundleShortVersionString"];
36-
NSLog(@"dmon: Installed version of Pokemon Go: %@", versionString);
37-
return versionString;
37+
38+
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:bundlePath];
39+
40+
if (fileHandle) {
41+
// Read the contents of the file
42+
NSData *fileData = [fileHandle readDataToEndOfFile];
43+
// Close the file handle
44+
[fileHandle closeFile];
45+
// Convert the file data to a dictionary
46+
NSDictionary *infoDict = [NSPropertyListSerialization propertyListWithData:fileData options:NSPropertyListImmutable format:NULL error:NULL];
47+
if (infoDict) {
48+
NSString *versionString = [infoDict objectForKey:@"CFBundleShortVersionString"];
49+
NSLog(@"dmon: Version of Pokemon Go: %@", versionString);
50+
return versionString;
51+
}
52+
}
53+
else {
54+
NSLog(@"dmon: Failed to open file for reading: %@", bundlePath);
55+
}
56+
57+
return nil;
3858
}
3959

40-
NSString * installedGCVersion(void) {
41-
// Can't use this really clean method since this library isn't versioned. Big Sad!
42-
// NSBundle *goCheatsBundle = [NSBundle bundleWithPath:@"/Library/MobileSubstrate/DynamicLibraries/libgocheats.dylib"];
43-
// NSLog(@"dmon: bundle info is: %@", goCheatsBundle);
44-
// NSString *goCheatsVersion = [goCheatsBundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
45-
// NSLog(@"dmon: Version: %@", goCheatsVersion);
46-
// return goCheatsVersion;
47-
48-
NSLog(@"dmon: Finding GC version...");
49-
FILE *gc_cmd = popen("apt list 2>/dev/null | grep -i gocheats | cut -d ' ' -f 2", "r");
50-
if (gc_cmd == NULL) {
51-
NSLog(@"dmon: Unable to find GC version");
60+
NSString *getAptList(NSString *packageName) {
61+
FILE *fp;
62+
char path[1035];
63+
NSString *command = [NSString stringWithFormat:@"dpkg-query --showformat='${Version}' --show %@ 2>/dev/null", packageName];
64+
NSString *version = nil;
65+
66+
// Open the command for reading
67+
fp = popen([command UTF8String], "r");
68+
if (fp == NULL) {
69+
NSLog(@"dmon: Failed to run command.");
5270
return nil;
5371
}
5472

55-
char version[256];
56-
int bytesRead = fread(version, sizeof(char), 255, gc_cmd);
57-
version[bytesRead] = '\0';
58-
pclose(gc_cmd);
59-
version[strcspn(version, "\n")] = '\0'; // Remove newline character
73+
// Read the output from the command
74+
if (fgets(path, sizeof(path)-1, fp) != NULL) {
75+
version = [NSString stringWithUTF8String:path];
76+
version = [version stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
77+
}
78+
79+
// Close the file
80+
pclose(fp);
6081

61-
NSLog(@"dmon: Installed version of GC: %s", version);
62-
return [[NSString alloc] initWithUTF8String:version];
82+
NSLog(@"dmon: Version of %@: %@", packageName, version);
83+
return version;
6384
}
6485

6586
NSDictionary * parseConfig(void) {
@@ -128,6 +149,7 @@ int installIpa(NSString *filePath) {
128149
FILE *install_ipa_cmd = popen(command, "r");
129150
fscanf(install_ipa_cmd, "%d", &results);
130151
pclose(install_ipa_cmd);
152+
NSLog(@"dmon: Results for %@ are: %d", filePath, results);
131153
return results;
132154
}
133155

@@ -148,8 +170,8 @@ int installDeb(NSString *filePath) {
148170

149171
int downloadFile(NSString *url, NSString *userpass, NSString *outfile) {
150172
CURL *curl;
151-
CURLcode res = -1;
152173
FILE *fp;
174+
long httpCode = -1;
153175
curl = curl_easy_init();
154176

155177
if (curl) {
@@ -168,58 +190,75 @@ int downloadFile(NSString *url, NSString *userpass, NSString *outfile) {
168190
curl_easy_setopt(curl, CURLOPT_CAINFO, "/usr/lib/ssl/cacert.pem");
169191

170192
// Perform the request
171-
res = curl_easy_perform(curl);
172-
NSLog(@"dmon: Curl return code for %@: %u", url, res);
193+
CURLcode res = curl_easy_perform(curl);
194+
195+
// Get the http status code
196+
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &httpCode);
173197

174198
// Clean up
175199
fclose(fp);
176200
curl_easy_cleanup(curl);
201+
202+
// Check for successful download
203+
if (res == CURLE_OK && httpCode >= 200 && httpCode < 300) {
204+
NSLog(@"dmon: Download was sucessful: %ld", httpCode);
205+
return 0;
206+
} else {
207+
NSLog(@"dmon: Download failed with error code: %ld", httpCode);
208+
// Delete the outfile if it exists
209+
if (remove([outfile UTF8String]) != 0) {
210+
NSLog(@"Failed to delete the file %@", outfile);
211+
}
212+
}
177213
}
178-
return res;
214+
return -1;
179215
}
180216

181217
void update(NSDictionary *config) {
182218
NSString *versionFile = @"version.txt";
183219
NSString *pogo_ipa = @"pogo.ipa";
184220
NSString *gc_deb = @"gc.deb";
185221
NSString *pogoVersion = installedPogoVersion();
186-
NSString *gcVersion = installedGCVersion();
222+
NSString *gcVersion = getAptList(@"com.gocheats.jb");
223+
// getAptList(@"com.github.clburlison.dmon");
187224

188225
// Strip trailing forward slashes to make things consistent for users
189226
NSString *url = [config[@"dmon_url"] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
190227
NSLog(@"dmon: Update URL is: %@", url);
191-
downloadFile(
228+
int versionDownload = downloadFile(
192229
[NSString stringWithFormat:@"%@/%@", url, versionFile],
193230
[NSString stringWithFormat:@"%@:%@", config[@"dmon_username"], config[@"dmon_password"]],
194231
versionFile
195232
);
233+
if (versionDownload != 0) {
234+
NSLog(@"dmon: Unable to process updates as %@ is invalid", versionFile);
235+
return;
236+
}
196237

197238
// Parse the config map style version.txt to NSDictionary
198239
NSMutableDictionary *parsedVersion = parseKeyValueFileAtPath(versionFile);
199240

200241
// Update Pogo if needed
201242
if (![pogoVersion isEqualToString:parsedVersion[@"pogo"]]) {
202-
NSLog(@"dmon: Pogo version mismatch. Have %@. Need %@", pogoVersion, parsedVersion[@"pogo"]);
243+
NSLog(@"dmon: Pogo version mismatch. Have '%@'. Need '%@'", pogoVersion, parsedVersion[@"pogo"]);
203244
int pogoDownload = downloadFile(
204245
[NSString stringWithFormat:@"%@/%@", url, pogo_ipa],
205246
[NSString stringWithFormat:@"%@:%@", config[@"dmon_username"], config[@"dmon_password"]],
206247
pogo_ipa
207248
);
208-
// TODO: Do we need a stricter validation?
209249
if (pogoDownload == 0) {
210250
installIpa(pogo_ipa);
211251
}
212252
}
213253

214254
// Update GC if needed
215255
if (![gcVersion isEqualToString:parsedVersion[@"gc"]]) {
216-
NSLog(@"dmon: GC version mismatch. Have %@. Need %@", gcVersion, parsedVersion[@"gc"]);
256+
NSLog(@"dmon: GC version mismatch. Have '%@'. Need '%@'", gcVersion, parsedVersion[@"gc"]);
217257
int gcDownload = downloadFile(
218258
[NSString stringWithFormat:@"%@/%@", url, gc_deb],
219259
[NSString stringWithFormat:@"%@:%@", config[@"dmon_username"], config[@"dmon_password"]],
220260
gc_deb
221261
);
222-
// TODO: Do we need a stricter validation?
223262
if (gcDownload == 0) {
224263
installDeb(gc_deb);
225264
}
@@ -233,9 +272,17 @@ void monitor(void) {
233272
killall(@"/usr/bin/kernbypass");
234273
NSLog(@"dmon: Force stopping Pogo...");
235274
killall(@"pokemongo");
236-
sleep(2);
237-
NSLog(@"dmon: Pogo not running. Launch it...");
275+
sleep(5);
276+
277+
// Attempt to check versions and update now that Pogo & Kernbypass are not running
278+
NSDictionary *config = parseConfig();
279+
if (config[@"dmon_url"] != nil && [config[@"dmon_url"] isKindOfClass:[NSString class]] && ![config[@"dmon_url"] isEqualToString:@""]) {
280+
// NSLog(@"dmon: Full config: %@", config);
281+
update(config);
282+
}
283+
238284
// Launch Pogo
285+
NSLog(@"dmon: Pogo not running. Launch it...");
239286
void* sbServices = dlopen("/System/Library/PrivateFrameworks/SpringBoardServices.framework/SpringBoardServices", RTLD_LAZY);
240287
int (*SBSLaunchApplicationWithIdentifier)(CFStringRef identifier, Boolean suspended) = dlsym(sbServices, "SBSLaunchApplicationWithIdentifier");
241288
NSString *bundleString=[NSString stringWithUTF8String:"com.nianticlabs.pokemongo"];
@@ -249,23 +296,8 @@ int main(void) {
249296
NSLog(@"dmon: Starting...");
250297

251298
// Start loop
252-
int i = 0;
253299
while (1) {
254-
// Only call at the start of loop
255-
if (i == 0) {
256-
NSDictionary *config = parseConfig();
257-
// NSLog(@"dmon: Full config: %@", config);
258-
update(config);
259-
}
260-
261-
// Call this function every loop
262300
monitor();
263-
264-
// Restart loop on the 30th iteration
265-
if (++i == 30) {
266-
i = 0;
267-
}
268-
269301
sleep(30);
270302
}
271303
return 0;

0 commit comments

Comments
 (0)