forked from rileytestut/AltServer-Windows
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathConnectionError.hpp
More file actions
executable file
·194 lines (160 loc) · 6.11 KB
/
Copy pathConnectionError.hpp
File metadata and controls
executable file
·194 lines (160 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// ConnectionError.hpp
// AltServer-Windows
//
// Created by Riley Testut on 6/19/21.
// Copyright © 2021 Riley Testut. All rights reserved.
//
#ifndef ConnectionError_h
#define ConnectionError_h
#include "ServerError.hpp"
#include "Device.hpp"
#include <libimobiledevice/mobile_image_mounter.h>
#include <libimobiledevice/debugserver.h>
#include <libimobiledevice/installation_proxy.h>
#include <sstream>
#include <optional>
extern std::string ConnectionErrorDomain;
enum class ConnectionErrorCode
{
Unknown,
DeviceLocked,
InvalidRequest,
InvalidResponse,
Usbmuxd,
SSL,
TimedOut
};
class ConnectionError : public Error
{
public:
ConnectionError(ConnectionErrorCode code, std::map<std::string, std::any> userInfo = {}) : Error((int)code, userInfo)
{
}
virtual std::string domain() const
{
return ConnectionErrorDomain;
}
virtual std::optional<std::string> localizedFailureReason() const
{
std::string deviceName = "The device";
if (this->userInfo().count(DeviceNameErrorKey) > 0)
{
deviceName = AnyStringValue(this->userInfo()[DeviceNameErrorKey]);
}
std::ostringstream ss;
switch ((ConnectionErrorCode)this->code())
{
case ConnectionErrorCode::Unknown:
{
auto underlyingErrorDomain = AnyStringValue(this->userInfo()[UnderlyingErrorDomainErrorKey]);
auto underlyingErrorCode = AnyStringValue(this->userInfo()[UnderlyingErrorCodeErrorKey]);
if (underlyingErrorDomain.length() != 0 && underlyingErrorCode.length() != 0)
{
ss << underlyingErrorDomain << " error " << underlyingErrorCode << ".";
}
else if (underlyingErrorCode.length() != 0)
{
ss << "Connection error code: " << underlyingErrorCode << ".";
}
else
{
ss << "Unknown connection error.";
}
break;
}
case ConnectionErrorCode::DeviceLocked:
ss << deviceName << " is currently locked.";
break;
case ConnectionErrorCode::InvalidRequest:
ss << deviceName << " received an invalid request from AltServer.";
break;
case ConnectionErrorCode::InvalidResponse:
ss << "AltServer received an invalid response from " << deviceName << ".";
break;
case ConnectionErrorCode::Usbmuxd:
ss << "There was an issue communicating with the usbmuxd daemon.";
break;
case ConnectionErrorCode::SSL:
ss << "AltServer could not establish a secure connection to " << deviceName << ".";
break;
case ConnectionErrorCode::TimedOut:
ss << "AltServer's connection to " << deviceName << " timed out.";
break;
}
return ss.str();
}
virtual std::optional<std::string> localizedRecoverySuggestion() const
{
switch ((ConnectionErrorCode)this->code())
{
case ConnectionErrorCode::DeviceLocked:
return "Please unlock the device with your passcode and try again.";
default: return Error::localizedRecoverySuggestion();
}
}
static std::optional<ConnectionError> errorForMobileImageMounterError(mobile_image_mounter_error_t error, std::shared_ptr<Device> device)
{
std::map<std::string, std::any> userInfo = {
{ UnderlyingErrorDomainErrorKey, "Mobile Image Mounter" },
{ UnderlyingErrorCodeErrorKey, std::to_string((int)error) },
};
if (device != nullptr)
{
userInfo[DeviceNameErrorKey] = device->name();
}
switch (error)
{
case MOBILE_IMAGE_MOUNTER_E_SUCCESS: return std::nullopt;
case MOBILE_IMAGE_MOUNTER_E_INVALID_ARG: return ConnectionError(ConnectionErrorCode::InvalidRequest, userInfo);
case MOBILE_IMAGE_MOUNTER_E_PLIST_ERROR: return ConnectionError(ConnectionErrorCode::InvalidResponse, userInfo);
case MOBILE_IMAGE_MOUNTER_E_CONN_FAILED: return ConnectionError(ConnectionErrorCode::Usbmuxd, userInfo);
case MOBILE_IMAGE_MOUNTER_E_COMMAND_FAILED: return ConnectionError(ConnectionErrorCode::InvalidRequest, userInfo);
case MOBILE_IMAGE_MOUNTER_E_DEVICE_LOCKED: return ConnectionError(ConnectionErrorCode::DeviceLocked, userInfo);
case MOBILE_IMAGE_MOUNTER_E_UNKNOWN_ERROR: return ConnectionError(ConnectionErrorCode::Unknown, userInfo);
}
}
static std::optional<ConnectionError> errorForDebugServerError(debugserver_error_t error, std::shared_ptr<Device> device)
{
std::map<std::string, std::any> userInfo = {
{ UnderlyingErrorDomainErrorKey, "Debug Server" },
{ UnderlyingErrorCodeErrorKey, std::to_string((int)error) },
};
if (device != nullptr)
{
userInfo[DeviceNameErrorKey] = device->name();
}
switch (error)
{
case DEBUGSERVER_E_SUCCESS: return std::nullopt;
case DEBUGSERVER_E_INVALID_ARG: return ConnectionError(ConnectionErrorCode::InvalidRequest, userInfo);
case DEBUGSERVER_E_MUX_ERROR: return ConnectionError(ConnectionErrorCode::Usbmuxd, userInfo);
case DEBUGSERVER_E_SSL_ERROR: return ConnectionError(ConnectionErrorCode::SSL, userInfo);
case DEBUGSERVER_E_RESPONSE_ERROR: return ConnectionError(ConnectionErrorCode::InvalidResponse, userInfo);
case DEBUGSERVER_E_TIMEOUT: return ConnectionError(ConnectionErrorCode::TimedOut, userInfo);
case DEBUGSERVER_E_UNKNOWN_ERROR: return ConnectionError(ConnectionErrorCode::Unknown, userInfo);
}
}
static std::optional<ConnectionError> errorForInstallationProxyError(instproxy_error_t error, std::shared_ptr<Device> device)
{
std::map<std::string, std::any> userInfo = {
{ UnderlyingErrorDomainErrorKey, "Installation Proxy" },
{ UnderlyingErrorCodeErrorKey, std::to_string((int)error) },
};
if (device != nullptr)
{
userInfo[DeviceNameErrorKey] = device->name();
}
switch (error)
{
case INSTPROXY_E_SUCCESS: return std::nullopt;
case INSTPROXY_E_INVALID_ARG: return ConnectionError(ConnectionErrorCode::InvalidRequest, userInfo);
case INSTPROXY_E_PLIST_ERROR: return ConnectionError(ConnectionErrorCode::InvalidResponse, userInfo);
case INSTPROXY_E_CONN_FAILED: return ConnectionError(ConnectionErrorCode::Usbmuxd, userInfo);
case INSTPROXY_E_RECEIVE_TIMEOUT: return ConnectionError(ConnectionErrorCode::TimedOut, userInfo);
// case INSTPROXY_E_DEVICE_OS_VERSION_TOO_LOW: return ConnectionError(ConnectionErrorCode::Unknown, userInfo); // Error message assumes we're installing AltStore
default: return ConnectionError(ConnectionErrorCode::Unknown, userInfo);
}
}
};
#endif /* ConnectionError_h */