Skip to content

Commit 9a7409e

Browse files
committed
Add logging
1 parent 45c011f commit 9a7409e

File tree

12 files changed

+87
-105
lines changed

12 files changed

+87
-105
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ else()
4747
endif()
4848
endif()
4949

50-
target_link_libraries(provision PUBLIC plist)
50+
target_link_libraries(provision PUBLIC plist slf4d)
5151

5252
set(RETRIEVE_HEADERS_SOURCE_DIR "retrieve_headers/")
5353
file(GLOB_RECURSE RETRIEVE_HEADERS_D_SOURCES "${RETRIEVE_HEADERS_SOURCE_DIR}*.d")

anisette_server/app.d

+28-35
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import std.math;
1010
import std.net.curl;
1111
import std.parallelism;
1212
import std.path;
13-
import std.stdio;
1413
import std.zip;
1514

15+
import slf4d;
16+
import slf4d.default_provider;
17+
1618
import provision;
1719

1820
import constants;
@@ -34,7 +36,14 @@ __gshared ADI adi;
3436
__gshared Device device;
3537

3638
void main(string[] args) {
37-
writeln(anisetteServerBranding, " v", provisionVersion);
39+
debug {
40+
configureLoggingProvider(new shared DefaultProvider(true, Levels.DEBUG));
41+
} else {
42+
configureLoggingProvider(new shared DefaultProvider(true, Levels.INFO));
43+
}
44+
45+
Logger log = getLogger();
46+
log.infoF!"%s v%s"(anisetteServerBranding, provisionVersion);
3847
auto serverConfig = ServerConfig.defaultValues;
3948
serverConfig.hostname = "0.0.0.0";
4049
serverConfig.port = 6969;
@@ -70,27 +79,9 @@ void main(string[] args) {
7079

7180
if (!(file.exists(coreADIPath) && file.exists(SSCPath)) && apkDownloadAllowed) {
7281
auto http = HTTP();
73-
http.onProgress = (size_t dlTotal, size_t dlNow, size_t ulTotal, size_t ulNow) {
74-
write("Downloading libraries from Apple servers... ");
75-
if (dlTotal != 0) {
76-
write((dlNow * 100)/dlTotal, "% \r");
77-
} else {
78-
// Convert dlNow (in bytes) to a human readable string
79-
float downloadedSize = dlNow;
80-
81-
enum units = ["B", "kB", "MB", "GB", "TB"];
82-
int i = 0;
83-
while (downloadedSize > 1000 && i < units.length - 1) {
84-
downloadedSize = floor(downloadedSize) / 1000;
85-
++i;
86-
}
87-
88-
write(downloadedSize, units[i], " \r");
89-
}
90-
return 0;
91-
};
82+
log.info("Downloading libraries from Apple servers...");
9283
auto apkData = get!(HTTP, ubyte)(nativesUrl, http);
93-
writeln("Downloading libraries from Apple servers... done! \r");
84+
log.info("Done !");
9485
auto apk = new ZipArchive(apkData);
9586
auto dir = apk.directory();
9687

@@ -111,7 +102,7 @@ void main(string[] args) {
111102
adi.provisioningPath = configurationPath;
112103

113104
if (!device.initialized) {
114-
stderr.write("Creating machine... ");
105+
log.info("Creating machine... ");
115106

116107
import std.digest;
117108
import std.random;
@@ -123,27 +114,29 @@ void main(string[] args) {
123114
device.adiIdentifier = (cast(ubyte[]) rndGen.take(2).array()).toHexString().toLower();
124115
device.localUserUUID = (cast(ubyte[]) rndGen.take(8).array()).toHexString().toUpper();
125116

126-
stderr.writeln("done !");
117+
log.info("Machine creation done!");
127118
}
128119

129120
enum dsId = -2;
130121

131122
adi.identifier = device.adiIdentifier;
132123
if (!adi.isMachineProvisioned(dsId)) {
133-
stderr.write("Machine requires provisioning... ");
124+
log.info("Machine requires provisioning... ");
134125

135126
ProvisioningSession provisioningSession = new ProvisioningSession(adi, device);
136127
provisioningSession.provision(dsId);
137-
stderr.writeln("done !");
128+
log.info("Provisioning done!");
138129
}
139130

140131
auto s = new HttpServer((ref ctx) {
132+
Logger log = getLogger();
133+
141134
auto req = ctx.request;
142135
ctx.response.addHeader("Implementation-Version", anisetteServerBranding ~ " " ~ provisionVersion);
143136

144-
writeln("[<<] ", req.method, " ", req.url);
137+
log.infoF!"[<<] %s %s"(req.method, req.url);
145138
if (req.method != "GET") {
146-
writefln("[>>] 405 Method Not Allowed");
139+
log.info("[>>] 405 Method Not Allowed");
147140
ctx.response.setStatus(405).setStatusText("Method Not Allowed");
148141
return;
149142
}
@@ -152,17 +145,17 @@ void main(string[] args) {
152145
if (allowRemoteProvisioning) {
153146
ProvisioningSession provisioningSession = new ProvisioningSession(adi, device);
154147
provisioningSession.provision(dsId);
155-
writeln("[>>] 200 OK");
148+
log.info("[>>] 200 OK");
156149
ctx.response.setStatus(200);
157150
} else {
158-
writeln("[>>] 403 Forbidden");
151+
log.info("[>>] 403 Forbidden");
159152
ctx.response.setStatus(403).setStatusText("Forbidden");
160153
}
161154
return;
162155
}
163156

164157
if (req.url != "") {
165-
writeln("[>>] 404 Not Found");
158+
log.info("[>>] 404 Not Found");
166159
ctx.response.setStatus(404).setStatusText("Not Found");
167160
return;
168161
}
@@ -191,16 +184,16 @@ void main(string[] args) {
191184
"X-Mme-Device-Id": device.uniqueDeviceIdentifier,
192185
];
193186
ctx.response.writeBodyString(response.toString(JSONOptions.doNotEscapeSlashes), "application/json");
194-
writefln!"[>>] 200 OK %s"(response);
187+
log.infoF!"[>>] 200 OK %s"(response);
195188
} catch(Throwable t) {
196189
string exception = t.toString();
197-
writeln("Encountered an error: ", exception);
198-
writeln("[>>] 500 Internal Server Error");
190+
log.errorF!"Encountered an error: %s"(exception);
191+
log.info("[>>] 500 Internal Server Error");
199192
ctx.response.writeBodyString(exception);
200193
ctx.response.setStatus(500).setStatusText("Internal Server Error");
201194
}
202195
}, serverConfig);
203196

204-
writeln("Ready! Serving data.");
197+
log.info("Ready! Serving data.");
205198
s.start();
206199
}

cmake/dependencies.cmake

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# ==============================================================================
22
# Fetching external libraries
33

4+
include(UseDub)
5+
6+
DubProject_Add(slf4d ~2.1.1)
7+
48
if(build_anisetteserver)
5-
include(UseDub)
69
DubProject_Add(handy-httpd ~5.2.1)
710
endif()

dub.sdl

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ targetPath "bin"
99
sourcePaths "lib"
1010
importPaths "lib"
1111

12+
dependency "slf4d" version="~>2.1.1"
13+
1214
configuration "plist-d" {
1315
dependency "plist" version="~master"
1416
}

dub.selections.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"dxml": "0.4.3",
55
"handy-httpd": "5.2.1",
66
"httparsed": "1.2.1",
7-
"plist": "~master"
7+
"plist": "~master",
8+
"slf4d": "2.1.1"
89
}
910
}

lib/provision/adi.d

+10-12
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import std.format;
99
import std.json;
1010
import std.net.curl;
1111
import std.path;
12-
import std.stdio;
1312
import std.string;
1413

14+
import slf4d;
15+
1516
version (LibPlist) {
1617
import provision.plist;
1718
} else {
@@ -46,6 +47,7 @@ public class ADI {
4647
private ADIOTPRequest_t pADIOTPRequest;
4748

4849
private AndroidLibrary storeServicesCore;
50+
private Logger logger;
4951

5052
private string __provisioningPath;
5153
public string provisioningPath() {
@@ -78,21 +80,17 @@ public class ADI {
7880

7981
public this(string libraryPath, AndroidLibrary storeServicesCore) {
8082
this.storeServicesCore = storeServicesCore;
83+
this.logger = getLogger();
8184

8285
// We are loading the symbols from the ELF library from their name.
8386
// Those has been obfuscated but they keep a consistent obfuscated name, like a hash function would.
84-
85-
debug {
86-
stderr.writeln("Loading Android-specific symbols...");
87-
}
87+
logger.debug_("Loading Android-specific symbols...");
8888

8989
pADILoadLibraryWithPath = cast(ADILoadLibraryWithPath_t) storeServicesCore.load("kq56gsgHG6");
9090
pADISetAndroidID = cast(ADISetAndroidID_t) storeServicesCore.load("Sph98paBcz");
9191
pADISetProvisioningPath = cast(ADISetProvisioningPath_t) storeServicesCore.load("nf92ngaK92");
9292

93-
debug {
94-
stderr.writeln("Loading ADI symbols...");
95-
}
93+
logger.debug_("Loading ADI symbols...");
9694

9795
pADIProvisioningErase = cast(ADIProvisioningErase_t) storeServicesCore.load("p435tmhbla");
9896
pADISynchronize = cast(ADISynchronize_t) storeServicesCore.load("tn46gtiuhw");
@@ -103,15 +101,15 @@ public class ADI {
103101
pADIDispose = cast(ADIDispose_t) storeServicesCore.load("jk24uiwqrg");
104102
pADIOTPRequest = cast(ADIOTPRequest_t) storeServicesCore.load("qi864985u0");
105103

106-
debug {
107-
stderr.writeln("First calls...");
108-
}
109-
104+
logger.debug_("Loading libraries…");
110105
loadLibrary(libraryPath);
111106

107+
logger.debug_("Initialization…");
108+
112109
// We are setting those to be sure to have the same value in the class (used in getter) and the real one in ADI.
113110
provisioningPath = "/";
114111
identifier = "0000000000000000";
112+
logger.debug_("Initialization complete !");
115113
}
116114

117115
~this() {

lib/provision/androidlibrary.d

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ import std.mmfile;
1818
import std.path;
1919
import std.random;
2020
import std.range;
21-
import std.stdio;
2221
import std.string;
2322
import std.traits;
2423

24+
import slf4d;
25+
2526
public class AndroidLibrary {
2627
package MmFile elfFile;
2728
package void[] allocation;
@@ -79,9 +80,7 @@ public class AndroidLibrary {
7980
throw new LoaderException("Cannot allocate the memory: " ~ to!string(errno));
8081
}
8182
memoryTable[MemoryBlock(cast(size_t) mmapped_alloc, cast(size_t) mmapped_alloc + allocSize)] = this;
82-
debug {
83-
stderr.writefln("Allocating %x - %x for %s", cast(size_t) mmapped_alloc, cast(size_t) mmapped_alloc + allocSize, libraryName);
84-
}
83+
getLogger().traceF!"Allocating %x - %x for %s"(cast(size_t) mmapped_alloc, cast(size_t) mmapped_alloc + allocSize, libraryName);
8584
allocation = mmapped_alloc[0..allocSize];
8685

8786
size_t fileStart;

lib/provision/package.d

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module provision;
22

3-
enum provisionVersion = "2.0.0";
3+
enum provisionVersion = "2.1.0";
44
public import provision.adi;

lib/provision/plist/c.d

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ version (LibPlistDynamic) {
4343
private __gshared void* libplistHandle;
4444

4545
shared static this() {
46-
import std.stdio;
46+
import slf4d;
4747
static foreach (libplistName; ["libplist.so.3", "libplist-2.0.so.3"]) {
4848
libplistHandle = dlopen(libplistName, RTLD_LAZY);
4949
if (libplistHandle) {
5050
return;
5151
}
5252
}
53-
stderr.writeln("libplist is not available on this machine. ");
53+
getLogger().error("libplist is not available on this machine. ");
5454
abort();
5555
}
5656

lib/provision/symbols.d

+6-13
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import std.algorithm.mutation;
1313
import std.experimental.allocator;
1414
import std.experimental.allocator.mallocator;
1515
import std.random;
16-
import std.stdio : stderr, writeln;
1716
import std.string;
1817
import std.traits : Parameters, ReturnType;
1918

19+
import slf4d;
20+
2021
__gshared:
2122

2223
private extern (C) int __system_property_get_impl(const char* n, char* value) {
@@ -42,7 +43,7 @@ private extern (C) noreturn undefinedSymbol() {
4243

4344
private extern (C) AndroidLibrary dlopenWrapper(const char* name) {
4445
debug {
45-
stderr.writeln("Attempting to load ", name.fromStringz());
46+
getLogger().traceF!"Attempting to load %s"(name.fromStringz());
4647
}
4748
try {
4849
auto caller = rootLibrary();
@@ -56,7 +57,7 @@ private extern (C) AndroidLibrary dlopenWrapper(const char* name) {
5657

5758
private extern (C) void* dlsymWrapper(AndroidLibrary library, const char* symbolName) {
5859
debug {
59-
stderr.writeln("Attempting to load symbol ", symbolName.fromStringz());
60+
getLogger().traceF!"Attempting to load symbol %s"(symbolName.fromStringz());
6061
}
6162
return library.load(cast(string) symbolName.fromStringz());
6263
}
@@ -68,14 +69,6 @@ private extern (C) void dlcloseWrapper(AndroidLibrary library) {
6869
}
6970
}
7071

71-
private extern (C) void* malloc_GC_replacement(size_t sz) {
72-
return GC.malloc(sz);
73-
}
74-
75-
private extern (C) void free_GC_replacement(void* ptr) {
76-
return GC.free(ptr);
77-
}
78-
7972
// gperf generated code:
8073

8174
private enum totalKeywords = 29;
@@ -148,15 +141,15 @@ package void* lookupSymbol(string str) {
148141
{"strncpy", &strncpy}, {"pthread_mutex_lock", &emptyStub},
149142
{"ftruncate", &ftruncate}, {"write", &write},
150143
{"pthread_rwlock_unlock", &emptyStub},
151-
{"pthread_rwlock_destroy", &emptyStub}, {""}, {"free", &free_GC_replacement},
144+
{"pthread_rwlock_destroy", &emptyStub}, {""}, {"free", &free},
152145
{"fstat", &fstat}, {"pthread_rwlock_wrlock", &emptyStub},
153146
{"__errno", &errno}, {""}, {"pthread_rwlock_init", &emptyStub},
154147
{"pthread_mutex_unlock", &emptyStub},
155148
{"pthread_rwlock_rdlock", &emptyStub}, {
156149
"gettimeofday",
157150
&gettimeofday
158151
}, {""}, {"read", &read},
159-
{"mkdir", &mkdir}, {"malloc", &malloc_GC_replacement}, {""}, {""}, {""}, {""},
152+
{"mkdir", &mkdir}, {"malloc", &malloc}, {""}, {""}, {""}, {""},
160153
{"__system_property_get", &__system_property_get_impl}, {""}, {""},
161154
{""}, {"arc4random", &arc4random_impl},
162155
];

0 commit comments

Comments
 (0)