-
-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support iCloud in China #47
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,16 +1,25 @@ | ||||||
import { getBrowserStorageValue, COUNTRY_KEYS } from './storage'; | ||||||
|
||||||
export class UnsuccessfulRequestError extends Error {} | ||||||
|
||||||
type ServiceName = 'premiummailsettings'; | ||||||
|
||||||
type Country = 'default' | 'CN' | ||||||
|
||||||
class ICloudClient { | ||||||
public webservices?: Record<ServiceName, { url: string; status: string }>; | ||||||
private country: Country | ||||||
|
||||||
constructor(webservices?: ICloudClient['webservices']) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
this.webservices = webservices; | ||||||
this.country = 'default' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
static get setupUrl() { | ||||||
return 'https://setup.icloud.com/setup/ws/1'; | ||||||
return { | ||||||
default: 'https://setup.icloud.com/setup/ws/1', | ||||||
CN: 'https://setup.icloud.com.cn/setup/ws/1' | ||||||
} | ||||||
Comment on lines
+19
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's remove this static getter all together and rely on the |
||||||
} | ||||||
|
||||||
public async request( | ||||||
|
@@ -47,6 +56,7 @@ class ICloudClient { | |||||
|
||||||
public async isAuthenticated(): Promise<boolean> { | ||||||
try { | ||||||
this.country = await getBrowserStorageValue<Country>(COUNTRY_KEYS) || 'default' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to keep the |
||||||
await this.validateToken(); | ||||||
return true; | ||||||
} catch { | ||||||
|
@@ -57,7 +67,7 @@ class ICloudClient { | |||||
public async validateToken(): Promise<void> { | ||||||
const { webservices } = (await this.request( | ||||||
'POST', | ||||||
`${ICloudClient.setupUrl}/validate` | ||||||
`${ICloudClient.setupUrl[this.country]}/validate` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
)) as { | ||||||
webservices: ICloudClient['webservices']; | ||||||
}; | ||||||
|
@@ -71,7 +81,7 @@ class ICloudClient { | |||||
options: { trust: boolean } = { trust: false } | ||||||
): Promise<void> { | ||||||
const { trust } = options; | ||||||
await this.request('POST', `${ICloudClient.setupUrl}/logout`, { | ||||||
await this.request('POST', `${ICloudClient.setupUrl[this.country]}/logout`, { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
data: { | ||||||
trustBrowsers: trust, | ||||||
allBrowsers: trust, | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ import { | |||||||||
getBrowserStorageValue, | ||||||||||
POPUP_STATE_STORAGE_KEYS, | ||||||||||
OPTIONS_STORAGE_KEYS, | ||||||||||
COUNTRY_KEYS, | ||||||||||
setBrowserStorageValue, | ||||||||||
} from '../../storage'; | ||||||||||
import ICloudClient, { PremiumMailSettings } from '../../iCloudClient'; | ||||||||||
|
@@ -259,20 +260,27 @@ browser.contextMenus.onClicked.addListener(async (info, tab) => { | |||||||||
// as enabled. | ||||||||||
browser.webRequest.onResponseStarted.addListener( | ||||||||||
async (details: browser.WebRequest.OnResponseStartedDetailsType) => { | ||||||||||
const { statusCode } = details; | ||||||||||
const { statusCode, url } = details; | ||||||||||
if (statusCode < 200 && statusCode > 299) { | ||||||||||
console.debug('Request failed', details); | ||||||||||
return; | ||||||||||
} | ||||||||||
|
||||||||||
const country = Object.entries(ICloudClient.setupUrl).find((entries) => url.startsWith(`${entries[1]}/accountLogin`))?.[0]; | ||||||||||
await setBrowserStorageValue(COUNTRY_KEYS, country); | ||||||||||
Comment on lines
+269
to
+270
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, instead of storing the setupUrl value here, we can do it as part of |
||||||||||
|
||||||||||
const client = new ICloudClient(); | ||||||||||
|
||||||||||
const isAuthenticated = await client.isAuthenticated(); | ||||||||||
if (isAuthenticated) { | ||||||||||
performAuthSideEffects(); | ||||||||||
} | ||||||||||
}, | ||||||||||
{ | ||||||||||
urls: [`${ICloudClient.setupUrl}/accountLogin*`], | ||||||||||
urls: [ | ||||||||||
`${ICloudClient.setupUrl.default}/accountLogin*`, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
`${ICloudClient.setupUrl.CN}/accountLogin*` | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
], | ||||||||||
}, | ||||||||||
[] | ||||||||||
); | ||||||||||
|
@@ -290,7 +298,10 @@ browser.webRequest.onResponseStarted.addListener( | |||||||||
performDeauthSideEffects(); | ||||||||||
}, | ||||||||||
{ | ||||||||||
urls: [`${ICloudClient.setupUrl}/logout*`], | ||||||||||
urls: [ | ||||||||||
`${ICloudClient.setupUrl.default}/logout*`, | ||||||||||
`${ICloudClient.setupUrl.CN}/logout*`, | ||||||||||
], | ||||||||||
}, | ||||||||||
[] | ||||||||||
); | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ import browser from 'webextension-polyfill'; | |||||
|
||||||
export const POPUP_STATE_STORAGE_KEYS = ['iCloudHmePopupState']; | ||||||
export const OPTIONS_STORAGE_KEYS = ['iCloudHmeOptions']; | ||||||
export const COUNTRY_KEYS = ['country']; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
storing an object of type: {setupUrl: string} |
||||||
|
||||||
export async function getBrowserStorageValue<T>( | ||||||
keys: string[] | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,7 +3,7 @@ import ICloudClient from './iCloudClient'; | |||||||||
|
||||||||||
export const setupBlockingWebRequestListeners = () => { | ||||||||||
browser.webRequest.onBeforeSendHeaders.addListener( | ||||||||||
({ requestHeaders, documentUrl, originUrl, initiator }) => { | ||||||||||
({ requestHeaders, documentUrl, originUrl, initiator, url, }) => { | ||||||||||
const initiatedByTheExtension = [documentUrl, originUrl, initiator].some( | ||||||||||
(url) => | ||||||||||
url?.includes(browser.runtime.id) || | ||||||||||
|
@@ -20,19 +20,26 @@ export const setupBlockingWebRequestListeners = () => { | |||||||||
(header) => !['referer', 'origin'].includes(header.name.toLowerCase()) | ||||||||||
) || []; | ||||||||||
|
||||||||||
const suffix = url.match(/icloud.com(\.\w+)/)?.[1] || ''; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
modifiedHeaders.push({ | ||||||||||
name: 'Referer', | ||||||||||
value: 'https://www.icloud.com/', | ||||||||||
value: `https://www.icloud.com${suffix}/`, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
}); | ||||||||||
modifiedHeaders.push({ | ||||||||||
name: 'Origin', | ||||||||||
value: 'https://www.icloud.com', | ||||||||||
value: `https://www.icloud.com${suffix}`, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
}); | ||||||||||
|
||||||||||
return { requestHeaders: modifiedHeaders }; | ||||||||||
}, | ||||||||||
{ | ||||||||||
urls: [`${ICloudClient.setupUrl}/*`, 'https://*.icloud.com/v*/hme/*'], | ||||||||||
urls: [ | ||||||||||
`${ICloudClient.setupUrl.default}/*`, | ||||||||||
`${ICloudClient.setupUrl.CN}/*`, | ||||||||||
'https://*.icloud.com/v*/hme/*', | ||||||||||
'https://*.icloud.com.cn/v*/hme/*', | ||||||||||
], | ||||||||||
}, | ||||||||||
['requestHeaders', 'blocking'] | ||||||||||
); | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.