@basmilius/apple-companion-link 0.7.2 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +122 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +13 -12
- package/dist/const.d.ts +0 -38
- package/dist/frame.d.ts +0 -26
- package/dist/index.d.ts +0 -6
- package/dist/index.js +0 -651
- package/dist/pairing.d.ts +0 -19
- package/dist/protocol.d.ts +0 -38
- package/dist/stream.d.ts +0 -9
- package/dist/types.d.ts +0 -21
- package/dist/utils.d.ts +0 -2
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { AccessoryCredentials, AccessoryKeys, AccessoryPair, AccessoryVerify, Context, DiscoveryResult, EncryptionAwareConnection } from "@basmilius/apple-common";
|
|
2
|
+
|
|
3
|
+
//#region src/const.d.ts
|
|
4
|
+
declare const HidCommand: {
|
|
5
|
+
readonly Up: 1;
|
|
6
|
+
readonly Down: 2;
|
|
7
|
+
readonly Left: 3;
|
|
8
|
+
readonly Right: 4;
|
|
9
|
+
readonly Menu: 5;
|
|
10
|
+
readonly Select: 6;
|
|
11
|
+
readonly Home: 7;
|
|
12
|
+
readonly VolumeUp: 8;
|
|
13
|
+
readonly VolumeDown: 9;
|
|
14
|
+
readonly Siri: 10;
|
|
15
|
+
readonly Screensaver: 11;
|
|
16
|
+
readonly Sleep: 12;
|
|
17
|
+
readonly Wake: 13;
|
|
18
|
+
readonly PlayPause: 14;
|
|
19
|
+
readonly ChannelIncrement: 15;
|
|
20
|
+
readonly ChannelDecrement: 16;
|
|
21
|
+
readonly Guide: 17;
|
|
22
|
+
readonly PageUp: 18;
|
|
23
|
+
readonly PageDown: 19;
|
|
24
|
+
};
|
|
25
|
+
declare const MediaControlCommand: {
|
|
26
|
+
readonly Play: 1;
|
|
27
|
+
readonly Pause: 2;
|
|
28
|
+
readonly NextTrack: 3;
|
|
29
|
+
readonly PreviousTrack: 4;
|
|
30
|
+
readonly GetVolume: 5;
|
|
31
|
+
readonly SetVolume: 6;
|
|
32
|
+
readonly SkipBy: 7;
|
|
33
|
+
readonly FastForwardBegin: 8;
|
|
34
|
+
readonly FastForwardEnd: 9;
|
|
35
|
+
readonly RewindBegin: 10;
|
|
36
|
+
readonly RewindEnd: 11;
|
|
37
|
+
readonly GetCaptionSettings: 12;
|
|
38
|
+
readonly SetCaptionSettings: 13;
|
|
39
|
+
};
|
|
40
|
+
type HidCommandKey = keyof typeof HidCommand;
|
|
41
|
+
type MediaControlCommandKey = keyof typeof MediaControlCommand;
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/stream.d.ts
|
|
44
|
+
declare class Stream extends EncryptionAwareConnection<Record<string, [unknown]>> {
|
|
45
|
+
#private;
|
|
46
|
+
constructor(context: Context, address: string, port: number);
|
|
47
|
+
disconnect(): Promise<void>;
|
|
48
|
+
exchange(type: number, obj: Record<string, unknown>): Promise<[number, unknown]>;
|
|
49
|
+
send(type: number, payload: Buffer): void;
|
|
50
|
+
sendOPack(type: number, obj: Record<string, unknown>): void;
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/pairing.d.ts
|
|
54
|
+
declare class Pairing {
|
|
55
|
+
#private;
|
|
56
|
+
get internal(): AccessoryPair;
|
|
57
|
+
get stream(): Stream;
|
|
58
|
+
constructor(protocol: Protocol);
|
|
59
|
+
start(): Promise<void>;
|
|
60
|
+
pin(askPin: () => Promise<string>): Promise<AccessoryCredentials>;
|
|
61
|
+
transient(): Promise<AccessoryKeys>;
|
|
62
|
+
}
|
|
63
|
+
declare class Verify {
|
|
64
|
+
#private;
|
|
65
|
+
get internal(): AccessoryVerify;
|
|
66
|
+
get stream(): Stream;
|
|
67
|
+
constructor(protocol: Protocol);
|
|
68
|
+
start(credentials: AccessoryCredentials): Promise<AccessoryKeys>;
|
|
69
|
+
}
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region src/types.d.ts
|
|
72
|
+
type AttentionState = "unknown" | "asleep" | "screensaver" | "awake" | "idle";
|
|
73
|
+
type ButtonPressType = "DoubleTap" | "Hold" | "SingleTap";
|
|
74
|
+
type LaunchableApp = {
|
|
75
|
+
readonly bundleId: string;
|
|
76
|
+
readonly name: string;
|
|
77
|
+
};
|
|
78
|
+
type UserAccount = {
|
|
79
|
+
readonly accountId: string;
|
|
80
|
+
readonly name: string;
|
|
81
|
+
};
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/protocol.d.ts
|
|
84
|
+
declare class Protocol {
|
|
85
|
+
#private;
|
|
86
|
+
get context(): Context;
|
|
87
|
+
get discoveryResult(): DiscoveryResult;
|
|
88
|
+
get pairing(): Pairing;
|
|
89
|
+
get stream(): Stream;
|
|
90
|
+
get verify(): Verify;
|
|
91
|
+
constructor(discoveryResult: DiscoveryResult);
|
|
92
|
+
connect(): Promise<void>;
|
|
93
|
+
destroy(): Promise<void>;
|
|
94
|
+
disconnect(): Promise<void>;
|
|
95
|
+
fetchMediaControlStatus(): Promise<void>;
|
|
96
|
+
fetchNowPlayingInfo(): Promise<void>;
|
|
97
|
+
fetchSupportedActions(): Promise<void>;
|
|
98
|
+
getAttentionState(): Promise<AttentionState>;
|
|
99
|
+
getLaunchableApps(): Promise<LaunchableApp[]>;
|
|
100
|
+
getSiriRemoteInfo(): Promise<any>;
|
|
101
|
+
getUserAccounts(): Promise<UserAccount[]>;
|
|
102
|
+
hidCommand(command: HidCommandKey, down?: boolean): Promise<void>;
|
|
103
|
+
launchApp(bundleId: string): Promise<void>;
|
|
104
|
+
launchUrl(url: string): Promise<void>;
|
|
105
|
+
mediaControlCommand(command: MediaControlCommandKey, content?: object): Promise<object>;
|
|
106
|
+
pressButton(command: HidCommandKey, type?: ButtonPressType, holdDelayMs?: number): Promise<void>;
|
|
107
|
+
switchUserAccount(accountId: string): Promise<void>;
|
|
108
|
+
subscribe(event: string, fn: (data: unknown) => void): Promise<void>;
|
|
109
|
+
unsubscribe(event: string, fn?: (data: unknown) => void): Promise<void>;
|
|
110
|
+
sessionStart(): Promise<object>;
|
|
111
|
+
systemInfo(pairingId: Buffer): Promise<object>;
|
|
112
|
+
tiStart(): Promise<object>;
|
|
113
|
+
touchStart(): Promise<object>;
|
|
114
|
+
tvrcSessionStart(): Promise<object>;
|
|
115
|
+
noOp(): void;
|
|
116
|
+
}
|
|
117
|
+
//#endregion
|
|
118
|
+
//#region src/utils.d.ts
|
|
119
|
+
declare function convertAttentionState(state: number): AttentionState;
|
|
120
|
+
//#endregion
|
|
121
|
+
export { AttentionState, ButtonPressType, HidCommand, HidCommandKey, LaunchableApp, MediaControlCommand, MediaControlCommandKey, Pairing, Protocol, Stream, UserAccount, Verify, convertAttentionState };
|
|
122
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/const.ts","../src/stream.ts","../src/pairing.ts","../src/types.ts","../src/protocol.ts","../src/utils.ts"],"mappings":";;;cAAa,UAAA;EAAA,SACT,EAAA;EAAA,SACA,IAAA;EAAA,SACA,IAAA;EAAA,SACA,KAAA;EAAA,SACA,IAAA;EAAA,SACA,MAAA;EAAA,SACA,IAAA;EAAA,SACA,QAAA;EAAA,SACA,UAAA;EAAA,SACA,IAAA;EAAA,SACA,WAAA;EAAA,SACA,KAAA;EAAA,SACA,IAAA;EAAA,SACA,SAAA;EAAA,SACA,gBAAA;EAAA,SACA,gBAAA;EAAA,SACA,KAAA;EAAA,SACA,MAAA;EAAA,SACA,QAAA;AAAA;AAAA,cAGS,mBAAA;EAAA,SACT,IAAA;EAAA,SACA,KAAA;EAAA,SACA,SAAA;EAAA,SACA,aAAA;EAAA,SACA,SAAA;EAAA,SACA,SAAA;EAAA,SACA,MAAA;EAAA,SACA,gBAAA;EAAA,SACA,cAAA;EAAA,SACA,WAAA;EAAA,SACA,SAAA;EAAA,SACA,kBAAA;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,Context as r,ENCRYPTION as i,EncryptionAwareConnection as a,waitFor as o}from"@basmilius/apple-common";import{OPack as s,Plist as c}from"@basmilius/apple-encoding";import{Chacha20 as l,hkdf as u}from"@basmilius/apple-encryption";const d={Up:1,Down:2,Left:3,Right:4,Menu:5,Select:6,Home:7,VolumeUp:8,VolumeDown:9,Siri:10,Screensaver:11,Sleep:12,Wake:13,PlayPause:14,ChannelIncrement:15,ChannelDecrement:16,Guide:17,PageUp:18,PageDown:19},f={Play:1,Pause:2,NextTrack:3,PreviousTrack:4,GetVolume:5,SetVolume:6,SkipBy:7,FastForwardBegin:8,FastForwardEnd:9,RewindBegin:10,RewindEnd:11,GetCaptionSettings:12,SetCaptionSettings:13},p={Unknown:0,NoOp:1,PairSetupStart:3,PairSetupNext:4,PairVerifyStart:5,PairVerifyNext:6,OPackUnencrypted:7,OPackEncrypted:8,OPackPacked:9,PairingRequest:10,PairingResponse:11,SessionStartRequest:16,SessionStartResponse:17,SessionData:18,FamilyIdentityRequest:32,FamilyIdentityResponse:33,FamilyIdentityUpdate:34},m={Event:1,Request:2,Response:3},h=[p.PairSetupStart,p.PairSetupNext,p.PairVerifyStart,p.PairVerifyNext,p.OPackUnencrypted,p.OPackEncrypted,p.OPackPacked],g=[p.PairSetupStart,p.PairSetupNext,p.PairVerifyStart,p.PairVerifyNext];var _=class{get internal(){return this.#e}get stream(){return this.#t}#e;#t;constructor(e){this.#e=new t(e.context,this.#n.bind(this)),this.#t=e.stream}async start(){await this.#e.start()}async pin(e){return this.#e.pin(e)}async transient(){return this.#e.transient()}async#n(e,t){let n=e===`m1`?p.PairSetupStart:p.PairSetupNext,[,r]=await this.#t.exchange(n,{_pd:t,_pwTy:1});if(typeof r!=`object`||!r)throw Error(`Invalid response from receiver.`);return r._pd}},v=class{get internal(){return this.#e}get stream(){return this.#t}#e;#t;constructor(e){this.#e=new n(e.context,this.#n.bind(this)),this.#t=e.stream}async start(e){let t=await this.#e.start(e);return{accessoryToControllerKey:u({hash:`sha512`,key:t.sharedSecret,length:32,salt:Buffer.alloc(0),info:Buffer.from(`ServerEncrypt-main`)}),controllerToAccessoryKey:u({hash:`sha512`,key:t.sharedSecret,length:32,salt:Buffer.alloc(0),info:Buffer.from(`ClientEncrypt-main`)}),pairingId:t.pairingId,sharedSecret:t.sharedSecret}}async#n(e,t){let n=e===`m1`?p.PairVerifyStart:p.PairVerifyNext,[,r]=await this.#t.exchange(n,{_pd:t,_auTy:4});if(typeof r!=`object`||!r)throw Error(`Invalid response from receiver.`);return r._pd}};function y(e){switch(e){case 1:return`asleep`;case 2:return`screensaver`;case 3:return`awake`;case 4:return`idle`;default:return`unknown`}}var b=class extends a{get#e(){return this[i]}#t=new Map;#n=Buffer.alloc(0);#r;constructor(t,n,r){super(t,n,r),this.debug(!0),this.#r=e(0,2**16),this.on(`close`,this.#a.bind(this)),this.on(`data`,this.#o.bind(this)),this.on(`error`,this.#s.bind(this))}async disconnect(){this.#i(),await super.disconnect()}async exchange(e,t){let n=this.#r;return new Promise((r,i)=>{g.includes(e)?this.#t.set(-1,[r,i]):this.#t.set(n,[r,i]),this.sendOPack(e,t)})}send(e,t){let n=this.isEncrypted&&e!==p.NoOp,r=t.byteLength;n&&(r+=l.CHACHA20_AUTH_TAG_LENGTH);let i=Buffer.allocUnsafe(4);i.writeUint8(e,0),i.writeUintBE(r,1,3);let a;if(n){let e=Buffer.alloc(12);e.writeBigUInt64LE(BigInt(this.#e.writeCount++),0);let n=l.encrypt(this.#e.writeKey,e,i,t);a=Buffer.concat([i,n.ciphertext,n.authTag])}else a=Buffer.concat([i,t]);this.context.logger.raw(`[companion-link]`,`Sending data frame`,this.isEncrypted,e),this.write(a)}sendOPack(e,t){let n=this.#r++;t._x??=s.sizedInteger(n,8),this.context.logger.raw(`[companion-link]`,`Sending opack frame`,e,this.isEncrypted,t),this.send(e,Buffer.from(s.encode(t)))}#i(){this.#n=Buffer.alloc(0);let e=Error(`Stream cleanup`);for(let[,t]of this.#t.values())t(e);this.#t.clear()}#a(){let e=Error(`Connection closed while waiting for response`);for(let[,t]of this.#t.values())t(e);this.#t.clear(),this.#i()}async#o(e){this.#n=Buffer.concat([this.#n,e]);try{for(;this.#n.byteLength>=4;){let t=this.#n.subarray(0,4),n=4+t.readUintBE(1,3);if(this.#n.byteLength<n){this.context.logger.warn(`[companion-link]`,`Data packet is too short needed=${n} available=${this.#n.byteLength} receivedLength=${e.byteLength}`);return}this.context.logger.raw(`[companion-link]`,`Received frame length=${n} availableLength=${this.#n.byteLength} receivedLength=${e.byteLength}`);let r=Buffer.from(this.#n.subarray(0,n));this.#n=this.#n.subarray(n),this.context.logger.raw(`[companion-link]`,`Handle frame, ${this.#n.byteLength} bytes left...`),this.isEncrypted&&(r=this.#c(r));let i=r.subarray(4,n);this.#l(t,i)}}catch(e){this.context.logger.error(`[companion-link]`,`#onData()`,e),this.emit(`error`,e)}}#s(e){for(let[,t]of this.#t.values())t(e);this.#t.clear()}#c(e){let t=e.subarray(0,4),n=t.readUintBE(1,3),r=e.subarray(4,4+n),i=r.subarray(r.byteLength-16),a=r.subarray(0,r.byteLength-16),o=Buffer.alloc(12);o.writeBigUint64LE(BigInt(this.#e.readCount++),0);let s=l.decrypt(this.#e.readKey,o,t,a,i);return Buffer.concat([t,s,i])}#l(e,t){let n=e.readInt8();if(!h.includes(n)){this.context.logger.warn(`[companion-link]`,`Packet not handled, no opack frame.`);return}if(t=s.decode(t),this.context.logger.raw(`[companion-link]`,`Decoded OPACK`,{header:e,payload:t}),`_x`in t){let n=Number(t._x);if(this.#t.has(n)){let[r]=this.#t.get(n);r([e,t]),this.#t.delete(n)}else if(`_i`in t)this.emit(t._i,t._c);else{let e=t._c,n=Object.keys(e).map(e=>e.slice(0,-3));for(let t of n)this.emit(t,e[t])}}else if(this.#t.has(-1)){let[n]=this.#t.get(-1);n([e,t]),this.#t.delete(-1)}else this.context.logger.warn(`[companion-link]`,`No handler for message`,[e,t])}},x=class{get context(){return this.#e}get discoveryResult(){return this.#t}get pairing(){return this.#n}get stream(){return this.#r}get verify(){return this.#i}#e;#t;#n;#r;#i;constructor(e){this.#e=new r(e.id),this.#t=e,this.#r=new b(this.#e,e.address,e.service.port),this.#n=new _(this),this.#i=new v(this)}async connect(){await this.#r.connect()}async destroy(){await this.#r.destroy()}async disconnect(){await this.#r.disconnect()}async fetchMediaControlStatus(){await this.#r.exchange(p.OPackEncrypted,{_i:`FetchMediaControlStatus`,_t:m.Request,_c:{}})}async fetchNowPlayingInfo(){await this.#r.exchange(p.OPackEncrypted,{_i:`FetchCurrentNowPlayingInfoEvent`,_t:m.Request,_c:{}})}async fetchSupportedActions(){await this.#r.exchange(p.OPackEncrypted,{_i:`FetchSupportedActionsEvent`,_t:m.Request,_c:{}})}async getAttentionState(){let[,e]=await this.#r.exchange(p.OPackEncrypted,{_i:`FetchAttentionState`,_t:m.Request,_c:{}}),{_c:t}=S(e);return y(t.state)}async getLaunchableApps(){let[,e]=await this.#r.exchange(p.OPackEncrypted,{_i:`FetchLaunchableApplicationsEvent`,_t:m.Request,_c:{}}),{_c:t}=S(e);return Object.entries(t).map(([e,t])=>({bundleId:e,name:t}))}async getSiriRemoteInfo(){let[,e]=await this.#r.exchange(p.OPackEncrypted,{_i:`FetchSiriRemoteInfo`,_t:m.Request,_c:{}});return c.parse(Buffer.from(e._c.SiriRemoteInfoKey).buffer)}async getUserAccounts(){let[,e]=await this.#r.exchange(p.OPackEncrypted,{_i:`FetchUserAccountsEvent`,_t:m.Request,_c:{}}),{_c:t}=S(e);return Object.entries(t).map(([e,t])=>({accountId:e,name:t}))}async hidCommand(e,t=!1){await this.#r.exchange(p.OPackEncrypted,{_i:`_hidC`,_t:m.Request,_c:{_hBtS:t?1:2,_hidC:d[e]}})}async launchApp(e){await this.#r.exchange(p.OPackEncrypted,{_i:`_launchApp`,_t:m.Request,_c:{_bundleID:e}})}async launchUrl(e){await this.#r.exchange(p.OPackEncrypted,{_i:`_launchApp`,_t:m.Request,_c:{_urlS:e}})}async mediaControlCommand(e,t){let[,n]=await this.#r.exchange(p.OPackEncrypted,{_i:`_mcc`,_t:m.Request,_c:{_mcc:f[e],...t||{}}});return S(n)}async pressButton(e,t=`SingleTap`,n=500){switch(t){case`DoubleTap`:await this.hidCommand(e,!0),await this.hidCommand(e,!1),await this.hidCommand(e,!0),await this.hidCommand(e,!1);break;case`Hold`:await this.hidCommand(e,!0),await o(n),await this.hidCommand(e,!1);break;case`SingleTap`:await this.hidCommand(e,!0),await this.hidCommand(e,!1);break}}async switchUserAccount(e){await this.#r.exchange(p.OPackEncrypted,{_i:`SwitchUserAccountEvent`,_t:m.Request,_c:{SwitchAccountID:e}})}async subscribe(e,t){this.#r.on(e,t),this.#r.sendOPack(p.OPackEncrypted,{_i:`_interest`,_t:m.Event,_c:{_regEvents:[e]}})}async unsubscribe(e,t){this.#r.isConnected&&(t&&this.#r.off(e,t),this.#r.sendOPack(p.OPackEncrypted,{_i:`_interest`,_t:m.Event,_c:{_deregEvents:[e]}}))}async sessionStart(){let[,t]=await this.#r.exchange(p.OPackEncrypted,{_i:`_sessionStart`,_t:m.Request,_btHP:!1,_c:{_srvT:`com.apple.tvremoteservices`,_sid:e(0,2**32-1),_btHP:!1}});return S(t)}async systemInfo(e){let[,t]=await this.#r.exchange(p.OPackEncrypted,{_i:`_systemInfo`,_t:m.Request,_btHP:!1,_c:{_bf:0,_cf:512,_clFl:128,_i:`b561af32aea6`,_idsID:e.toString(),_pubID:`DA:6D:1E:D8:A0:4F`,_sf:1099511628032,_sv:`715.2`,model:`iPhone16,2`,name:`AP Companion Link`,_lP:50402,_dC:`1`,_stA:`com.apple.sharingd.AirDrop,SymptomNetworkDiagnostics,com.apple.photosface.network-service,com.apple.ApplicationService.chrono,com.apple.DDUI-Picker,com.apple.biomesyncd.cascade.rapport,com.apple.SeymourSession,com.apple.workflow.remotewidgets,com.apple.ApplicationService.chrono,SCD.MessageCenter.remoteIntelligence,DeviceSharingDaemonApplicationService,com.apple.biomesyncd.rapport,com.apple.devicediscoveryui.rapportwake,com.apple.healthd.rapport,com.apple.dropin.setup,com.apple.coreduet.sync,com.apple.siri.wakeup,com.apple.wifivelocityd.rapportWake,com.apple.Seymour,CPSRemoteLLM,com.apple.networkrelay.on-demand-setup,com.apple.home.messaging,com.apple.accessibility.axremoted.rapportWake,com.apple.continuitycapture.sideband,com.apple.announce,com.apple.coreidv.coreidvd.handoff`.split(`,`)}});return S(t)}async tiStart(){let[,e]=await this.#r.exchange(p.OPackEncrypted,{_i:`_tiStart`,_t:m.Request,_btHP:!1,_c:{}});return S(e)}async touchStart(){let[,e]=await this.#r.exchange(p.OPackEncrypted,{_i:`_touchStart`,_t:m.Request,_btHP:!1,_c:{_height:s.float(1e3),_tFl:0,_width:s.float(1e3)}});return S(e)}async tvrcSessionStart(){let[,e]=await this.#r.exchange(p.OPackEncrypted,{_i:`TVRCSessionStart`,_t:m.Request,_btHP:!1,_inUseProc:`tvremoted`,_c:{}});return S(e)}noOp(){this.#e.logger.debug(`Sending no-op operation.`),this.#r.send(p.NoOp,Buffer.allocUnsafe(0))}};function S(e){if(typeof e==`object`&&e)return e;throw TypeError(`Expected an object.`)}export{d as HidCommand,f as MediaControlCommand,_ as Pairing,x as Protocol,b as Stream,v as Verify,y as convertAttentionState};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["#internal","#stream","#request","#encryptionState","#queue","#xid","#onClose","#onData","#onError","#cleanup","#buffer","#decrypt","#handle","#context","#discoveryResult","#pairing","#stream","#verify"],"sources":["../src/const.ts","../src/frame.ts","../src/pairing.ts","../src/utils.ts","../src/stream.ts","../src/protocol.ts"],"sourcesContent":["export const HidCommand = {\n Up: 1,\n Down: 2,\n Left: 3,\n Right: 4,\n Menu: 5,\n Select: 6,\n Home: 7,\n VolumeUp: 8,\n VolumeDown: 9,\n Siri: 10,\n Screensaver: 11,\n Sleep: 12,\n Wake: 13,\n PlayPause: 14,\n ChannelIncrement: 15,\n ChannelDecrement: 16,\n Guide: 17,\n PageUp: 18,\n PageDown: 19\n} as const;\n\nexport const MediaControlCommand = {\n Play: 1,\n Pause: 2,\n NextTrack: 3,\n PreviousTrack: 4,\n GetVolume: 5,\n SetVolume: 6,\n SkipBy: 7,\n FastForwardBegin: 8,\n FastForwardEnd: 9,\n RewindBegin: 10,\n RewindEnd: 11,\n GetCaptionSettings: 12,\n SetCaptionSettings: 13\n} as const;\n\nexport type HidCommandKey = keyof typeof HidCommand;\nexport type MediaControlCommandKey = keyof typeof MediaControlCommand;\n","export const FrameType = {\n Unknown: 0,\n NoOp: 1,\n\n PairSetupStart: 3,\n PairSetupNext: 4,\n\n PairVerifyStart: 5,\n PairVerifyNext: 6,\n\n OPackUnencrypted: 7,\n OPackEncrypted: 8,\n OPackPacked: 9,\n\n PairingRequest: 10,\n PairingResponse: 11,\n\n SessionStartRequest: 16,\n SessionStartResponse: 17,\n SessionData: 18,\n\n FamilyIdentityRequest: 32,\n FamilyIdentityResponse: 33,\n FamilyIdentityUpdate: 34\n} as const;\n\nexport const MessageType = {\n Event: 1,\n Request: 2,\n Response: 3\n} as const;\n\nexport const OPackFrameTypes: number[] = [\n FrameType.PairSetupStart,\n FrameType.PairSetupNext,\n FrameType.PairVerifyStart,\n FrameType.PairVerifyNext,\n\n FrameType.OPackUnencrypted,\n FrameType.OPackEncrypted,\n FrameType.OPackPacked\n];\n\nexport const PairingFrameTypes: number[] = [\n FrameType.PairSetupStart,\n FrameType.PairSetupNext,\n FrameType.PairVerifyStart,\n FrameType.PairVerifyNext\n];\n","import { type AccessoryCredentials, type AccessoryKeys, AccessoryPair, AccessoryVerify } from '@basmilius/apple-common';\nimport { hkdf } from '@basmilius/apple-encryption';\nimport { FrameType } from './frame';\nimport type Protocol from './protocol';\nimport type Stream from './stream';\n\nexport class Pairing {\n get internal(): AccessoryPair {\n return this.#internal;\n }\n\n get stream(): Stream {\n return this.#stream;\n }\n\n readonly #internal: AccessoryPair;\n readonly #stream: Stream;\n\n constructor(protocol: Protocol) {\n this.#internal = new AccessoryPair(protocol.context, this.#request.bind(this));\n this.#stream = protocol.stream;\n }\n\n async start(): Promise<void> {\n await this.#internal.start();\n }\n\n async pin(askPin: () => Promise<string>): Promise<AccessoryCredentials> {\n return this.#internal.pin(askPin);\n }\n\n async transient(): Promise<AccessoryKeys> {\n return this.#internal.transient();\n }\n\n async #request(step: 'm1' | 'm3' | 'm5', data: Buffer): Promise<Buffer> {\n const frameType = step === 'm1'\n ? FrameType.PairSetupStart\n : FrameType.PairSetupNext;\n\n const [, response] = await this.#stream.exchange(frameType, {\n _pd: data,\n _pwTy: 1\n });\n\n if (typeof response !== 'object' || response === null) {\n throw new Error('Invalid response from receiver.');\n }\n\n return response['_pd'];\n }\n}\n\nexport class Verify {\n get internal(): AccessoryVerify {\n return this.#internal;\n }\n\n get stream(): Stream {\n return this.#stream;\n }\n\n readonly #internal: AccessoryVerify;\n readonly #stream: Stream;\n\n constructor(protocol: Protocol) {\n this.#internal = new AccessoryVerify(protocol.context, this.#request.bind(this));\n this.#stream = protocol.stream;\n }\n\n async start(credentials: AccessoryCredentials): Promise<AccessoryKeys> {\n const keys = await this.#internal.start(credentials);\n\n const accessoryToControllerKey = hkdf({\n hash: 'sha512',\n key: keys.sharedSecret,\n length: 32,\n salt: Buffer.alloc(0),\n info: Buffer.from('ServerEncrypt-main')\n });\n\n const controllerToAccessoryKey = hkdf({\n hash: 'sha512',\n key: keys.sharedSecret,\n length: 32,\n salt: Buffer.alloc(0),\n info: Buffer.from('ClientEncrypt-main')\n });\n\n return {\n accessoryToControllerKey,\n controllerToAccessoryKey,\n pairingId: keys.pairingId,\n sharedSecret: keys.sharedSecret\n };\n }\n\n async #request(step: 'm1' | 'm3' | 'm5', data: Buffer): Promise<Buffer> {\n const frameType = step === 'm1'\n ? FrameType.PairVerifyStart\n : FrameType.PairVerifyNext;\n\n const [, response] = await this.#stream.exchange(frameType, {\n _pd: data,\n _auTy: 4\n });\n\n if (typeof response !== 'object' || response === null) {\n throw new Error('Invalid response from receiver.');\n }\n\n return response['_pd'];\n }\n}\n","import type { AttentionState } from './types';\n\nexport function convertAttentionState(state: number): AttentionState {\n switch (state) {\n case 0x01:\n return 'asleep';\n\n case 0x02:\n return 'screensaver';\n\n case 0x03:\n return 'awake';\n\n case 0x04:\n return 'idle';\n\n default:\n return 'unknown';\n }\n}\n","import { randomInt } from 'node:crypto';\nimport { type Context, ENCRYPTION, EncryptionAwareConnection, EncryptionState } from '@basmilius/apple-common';\nimport { OPack } from '@basmilius/apple-encoding';\nimport { Chacha20 } from '@basmilius/apple-encryption';\nimport { FrameType, OPackFrameTypes, PairingFrameTypes } from './frame';\n\nconst HEADER_SIZE = 4;\nconst PAIRING_QUEUE_IDENTIFIER = -1;\n\nexport default class Stream extends EncryptionAwareConnection<Record<string, [unknown]>> {\n get #encryptionState(): EncryptionState {\n return this[ENCRYPTION];\n }\n\n readonly #queue: Map<number, [Function, Function]> = new Map();\n #buffer: Buffer = Buffer.alloc(0);\n #xid: number;\n\n constructor(context: Context, address: string, port: number) {\n super(context, address, port);\n\n this.debug(true);\n\n this.#xid = randomInt(0, 2 ** 16);\n\n this.on('close', this.#onClose.bind(this));\n this.on('data', this.#onData.bind(this));\n this.on('error', this.#onError.bind(this));\n }\n\n async disconnect(): Promise<void> {\n this.#cleanup();\n await super.disconnect();\n }\n\n async exchange(type: number, obj: Record<string, unknown>): Promise<[number, unknown]> {\n const _x = this.#xid;\n\n return new Promise<[number, number]>((resolve, reject) => {\n if (PairingFrameTypes.includes(type)) {\n this.#queue.set(PAIRING_QUEUE_IDENTIFIER, [resolve, reject]);\n } else {\n this.#queue.set(_x, [resolve, reject]);\n }\n\n this.sendOPack(type, obj);\n });\n }\n\n send(type: number, payload: Buffer): void {\n const encrypt = this.isEncrypted && type !== FrameType.NoOp;\n let payloadLength = payload.byteLength;\n\n if (encrypt) {\n payloadLength += Chacha20.CHACHA20_AUTH_TAG_LENGTH;\n }\n\n const header = Buffer.allocUnsafe(4);\n header.writeUint8(type, 0);\n header.writeUintBE(payloadLength, 1, 3);\n\n let data: Buffer;\n\n if (encrypt) {\n const nonce = Buffer.alloc(12);\n nonce.writeBigUInt64LE(BigInt(this.#encryptionState.writeCount++), 0);\n\n const encrypted = Chacha20.encrypt(this.#encryptionState.writeKey, nonce, header, payload);\n data = Buffer.concat([header, encrypted.ciphertext, encrypted.authTag]);\n } else {\n data = Buffer.concat([header, payload]);\n }\n\n this.context.logger.raw('[companion-link]', 'Sending data frame', this.isEncrypted, type);\n\n this.write(data);\n }\n\n sendOPack(type: number, obj: Record<string, unknown>): void {\n const _x = this.#xid++;\n obj._x ??= OPack.sizedInteger(_x, 8);\n\n this.context.logger.raw('[companion-link]', 'Sending opack frame', type, this.isEncrypted, obj);\n\n this.send(type, Buffer.from(OPack.encode(obj)));\n }\n\n #cleanup(): void {\n this.#buffer = Buffer.alloc(0);\n\n const error = new Error('Stream cleanup');\n\n for (const [, reject] of this.#queue.values()) {\n reject(error);\n }\n\n this.#queue.clear();\n }\n\n #onClose(): void {\n const error = new Error('Connection closed while waiting for response');\n\n for (const [, reject] of this.#queue.values()) {\n reject(error);\n }\n\n this.#queue.clear();\n this.#cleanup();\n }\n\n async #onData(data: Buffer): Promise<void> {\n this.#buffer = Buffer.concat([this.#buffer, data]);\n\n try {\n while (this.#buffer.byteLength >= HEADER_SIZE) {\n const header = this.#buffer.subarray(0, HEADER_SIZE);\n const payloadLength = header.readUintBE(1, 3);\n const totalLength = HEADER_SIZE + payloadLength;\n\n if (this.#buffer.byteLength < totalLength) {\n this.context.logger.warn('[companion-link]', `Data packet is too short needed=${totalLength} available=${this.#buffer.byteLength} receivedLength=${data.byteLength}`);\n return;\n }\n\n this.context.logger.raw('[companion-link]', `Received frame length=${totalLength} availableLength=${this.#buffer.byteLength} receivedLength=${data.byteLength}`);\n\n let frame: Buffer = Buffer.from(this.#buffer.subarray(0, totalLength));\n this.#buffer = this.#buffer.subarray(totalLength);\n\n this.context.logger.raw('[companion-link]', `Handle frame, ${this.#buffer.byteLength} bytes left...`);\n\n if (this.isEncrypted) {\n frame = this.#decrypt(frame);\n }\n\n const payload = frame.subarray(HEADER_SIZE, totalLength);\n this.#handle(header, payload);\n }\n } catch (err) {\n this.context.logger.error('[companion-link]', '#onData()', err);\n this.emit('error', err);\n }\n }\n\n #onError(err: Error): void {\n for (const [, reject] of this.#queue.values()) {\n reject(err);\n }\n\n this.#queue.clear();\n }\n\n #decrypt(data: Buffer): Buffer {\n const header = data.subarray(0, 4);\n const payloadLength = header.readUintBE(1, 3);\n\n const payload = data.subarray(4, 4 + payloadLength);\n const authTag = payload.subarray(payload.byteLength - 16);\n const ciphertext = payload.subarray(0, payload.byteLength - 16);\n\n const nonce = Buffer.alloc(12);\n nonce.writeBigUint64LE(BigInt(this.#encryptionState.readCount++), 0);\n\n const decrypted = Chacha20.decrypt(this.#encryptionState.readKey, nonce, header, ciphertext, authTag);\n\n return Buffer.concat([header, decrypted, authTag]);\n }\n\n #handle(header: Buffer, payload: Buffer): void {\n const type = header.readInt8();\n\n if (!OPackFrameTypes.includes(type)) {\n this.context.logger.warn('[companion-link]', 'Packet not handled, no opack frame.');\n return;\n }\n\n payload = OPack.decode(payload);\n\n this.context.logger.raw('[companion-link]', 'Decoded OPACK', {header, payload});\n\n if ('_x' in payload) {\n const _x = Number(payload['_x']);\n\n if (this.#queue.has(_x)) {\n const [resolve] = this.#queue.get(_x);\n resolve([header, payload]);\n\n this.#queue.delete(_x);\n } else if ('_i' in payload) {\n this.emit(payload['_i'] as string, payload['_c']);\n } else {\n // probably an event\n const content = payload['_c'];\n const keys = Object.keys(content).map(k => k.slice(0, -3));\n\n for (const key of keys) {\n this.emit(key, content[key]);\n }\n }\n } else if (this.#queue.has(PAIRING_QUEUE_IDENTIFIER)) {\n const [resolve] = this.#queue.get(PAIRING_QUEUE_IDENTIFIER);\n resolve([header, payload]);\n\n this.#queue.delete(PAIRING_QUEUE_IDENTIFIER);\n } else {\n this.context.logger.warn('[companion-link]', 'No handler for message', [header, payload]);\n }\n }\n}\n","import { randomInt } from 'node:crypto';\nimport { Context, type DiscoveryResult, waitFor } from '@basmilius/apple-common';\nimport { OPack, Plist } from '@basmilius/apple-encoding';\nimport { HidCommand, type HidCommandKey, MediaControlCommand, type MediaControlCommandKey } from './const';\nimport { FrameType, MessageType } from './frame';\nimport { Pairing, Verify } from './pairing';\nimport type { AttentionState, ButtonPressType, LaunchableApp, UserAccount } from './types';\nimport { convertAttentionState } from './utils';\nimport Stream from './stream';\n\nexport default class Protocol {\n get context(): Context {\n return this.#context;\n }\n\n get discoveryResult(): DiscoveryResult {\n return this.#discoveryResult;\n }\n\n get pairing(): Pairing {\n return this.#pairing;\n }\n\n get stream(): Stream {\n return this.#stream;\n }\n\n get verify(): Verify {\n return this.#verify;\n }\n\n readonly #context: Context;\n readonly #discoveryResult: DiscoveryResult;\n readonly #pairing: Pairing;\n readonly #stream: Stream;\n readonly #verify: Verify;\n\n constructor(discoveryResult: DiscoveryResult) {\n this.#context = new Context(discoveryResult.id);\n this.#discoveryResult = discoveryResult;\n this.#stream = new Stream(this.#context, discoveryResult.address, discoveryResult.service.port);\n this.#pairing = new Pairing(this);\n this.#verify = new Verify(this);\n }\n\n async connect(): Promise<void> {\n await this.#stream.connect();\n }\n\n async destroy(): Promise<void> {\n await this.#stream.destroy();\n }\n\n async disconnect(): Promise<void> {\n await this.#stream.disconnect();\n }\n\n async fetchMediaControlStatus(): Promise<void> {\n await this.#stream.exchange(FrameType.OPackEncrypted, {\n _i: 'FetchMediaControlStatus',\n _t: MessageType.Request,\n _c: {}\n });\n }\n\n async fetchNowPlayingInfo(): Promise<void> {\n await this.#stream.exchange(FrameType.OPackEncrypted, {\n _i: 'FetchCurrentNowPlayingInfoEvent',\n _t: MessageType.Request,\n _c: {}\n });\n }\n\n async fetchSupportedActions(): Promise<void> {\n await this.#stream.exchange(FrameType.OPackEncrypted, {\n _i: 'FetchSupportedActionsEvent',\n _t: MessageType.Request,\n _c: {}\n });\n }\n\n async getAttentionState(): Promise<AttentionState> {\n const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {\n _i: 'FetchAttentionState',\n _t: MessageType.Request,\n _c: {}\n });\n\n const {_c} = objectOrFail<AttentionStateResponse>(payload);\n\n return convertAttentionState(_c.state);\n }\n\n async getLaunchableApps(): Promise<LaunchableApp[]> {\n const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {\n _i: 'FetchLaunchableApplicationsEvent',\n _t: MessageType.Request,\n _c: {}\n });\n\n const {_c} = objectOrFail<LaunchableAppsResponse>(payload);\n\n return Object.entries(_c).map(([bundleId, name]) => ({\n bundleId,\n name\n }));\n }\n\n async getSiriRemoteInfo(): Promise<any> {\n const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {\n _i: 'FetchSiriRemoteInfo',\n _t: MessageType.Request,\n _c: {}\n });\n\n return Plist.parse(Buffer.from(payload['_c']['SiriRemoteInfoKey']).buffer);\n }\n\n async getUserAccounts(): Promise<UserAccount[]> {\n const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {\n _i: 'FetchUserAccountsEvent',\n _t: MessageType.Request,\n _c: {}\n });\n\n const {_c} = objectOrFail<UserAccountsResponse>(payload);\n\n return Object.entries(_c).map(([accountId, name]) => ({\n accountId,\n name\n }));\n }\n\n async hidCommand(command: HidCommandKey, down = false): Promise<void> {\n await this.#stream.exchange(FrameType.OPackEncrypted, {\n _i: '_hidC',\n _t: MessageType.Request,\n _c: {\n _hBtS: down ? 1 : 2,\n _hidC: HidCommand[command]\n }\n });\n }\n\n async launchApp(bundleId: string): Promise<void> {\n await this.#stream.exchange(FrameType.OPackEncrypted, {\n _i: '_launchApp',\n _t: MessageType.Request,\n _c: {\n _bundleID: bundleId\n }\n });\n }\n\n async launchUrl(url: string): Promise<void> {\n await this.#stream.exchange(FrameType.OPackEncrypted, {\n _i: '_launchApp',\n _t: MessageType.Request,\n _c: {\n _urlS: url\n }\n });\n }\n\n async mediaControlCommand(command: MediaControlCommandKey, content?: object): Promise<object> {\n const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {\n _i: '_mcc',\n _t: MessageType.Request,\n _c: {\n _mcc: MediaControlCommand[command],\n ...(content || {})\n }\n });\n\n return objectOrFail(payload);\n }\n\n async pressButton(command: HidCommandKey, type: ButtonPressType = 'SingleTap', holdDelayMs = 500): Promise<void> {\n switch (type) {\n case 'DoubleTap':\n await this.hidCommand(command, true);\n await this.hidCommand(command, false);\n\n await this.hidCommand(command, true);\n await this.hidCommand(command, false);\n break;\n\n case 'Hold':\n await this.hidCommand(command, true);\n await waitFor(holdDelayMs);\n await this.hidCommand(command, false);\n break;\n\n case 'SingleTap':\n await this.hidCommand(command, true);\n await this.hidCommand(command, false);\n break;\n }\n }\n\n async switchUserAccount(accountId: string): Promise<void> {\n await this.#stream.exchange(FrameType.OPackEncrypted, {\n _i: 'SwitchUserAccountEvent',\n _t: MessageType.Request,\n _c: {\n SwitchAccountID: accountId\n }\n });\n }\n\n async subscribe(event: string, fn: (data: unknown) => void): Promise<void> {\n this.#stream.on(event, fn);\n\n this.#stream.sendOPack(FrameType.OPackEncrypted, {\n _i: '_interest',\n _t: MessageType.Event,\n _c: {\n _regEvents: [event]\n }\n });\n }\n\n async unsubscribe(event: string, fn?: (data: unknown) => void): Promise<void> {\n if (!this.#stream.isConnected) {\n return;\n }\n\n if (fn) {\n this.#stream.off(event, fn);\n }\n\n this.#stream.sendOPack(FrameType.OPackEncrypted, {\n _i: '_interest',\n _t: MessageType.Event,\n _c: {\n _deregEvents: [event]\n }\n });\n }\n\n async sessionStart(): Promise<object> {\n const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {\n _i: '_sessionStart',\n _t: MessageType.Request,\n _btHP: false,\n _c: {\n _srvT: 'com.apple.tvremoteservices',\n _sid: randomInt(0, 2 ** 32 - 1),\n _btHP: false\n }\n });\n\n return objectOrFail(payload);\n }\n\n async systemInfo(pairingId: Buffer): Promise<object> {\n const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {\n _i: '_systemInfo',\n _t: MessageType.Request,\n _btHP: false,\n _c: {\n _bf: 0,\n _cf: 512,\n _clFl: 128,\n _i: 'b561af32aea6',\n _idsID: pairingId.toString(),\n _pubID: 'DA:6D:1E:D8:A0:4F',\n _sf: 1099511628032,\n _sv: '715.2',\n model: 'iPhone16,2',\n name: 'AP Companion Link',\n _lP: 50402,\n _dC: '1',\n _stA: [\n 'com.apple.sharingd.AirDrop',\n 'SymptomNetworkDiagnostics',\n 'com.apple.photosface.network-service',\n 'com.apple.ApplicationService.chrono',\n 'com.apple.DDUI-Picker',\n 'com.apple.biomesyncd.cascade.rapport',\n 'com.apple.SeymourSession',\n 'com.apple.workflow.remotewidgets',\n 'com.apple.ApplicationService.chrono',\n 'SCD.MessageCenter.remoteIntelligence',\n 'DeviceSharingDaemonApplicationService',\n 'com.apple.biomesyncd.rapport',\n 'com.apple.devicediscoveryui.rapportwake',\n 'com.apple.healthd.rapport',\n 'com.apple.dropin.setup',\n 'com.apple.coreduet.sync',\n 'com.apple.siri.wakeup',\n 'com.apple.wifivelocityd.rapportWake',\n 'com.apple.Seymour',\n 'CPSRemoteLLM',\n 'com.apple.networkrelay.on-demand-setup',\n 'com.apple.home.messaging',\n 'com.apple.accessibility.axremoted.rapportWake',\n 'com.apple.continuitycapture.sideband',\n 'com.apple.announce',\n 'com.apple.coreidv.coreidvd.handoff'\n ]\n }\n });\n\n return objectOrFail(payload);\n }\n\n async tiStart(): Promise<object> {\n const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {\n _i: '_tiStart',\n _t: MessageType.Request,\n _btHP: false,\n _c: {}\n });\n\n return objectOrFail(payload);\n }\n\n async touchStart(): Promise<object> {\n const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {\n _i: '_touchStart',\n _t: MessageType.Request,\n _btHP: false,\n _c: {\n _height: OPack.float(1000.0),\n _tFl: 0,\n _width: OPack.float(1000.0)\n }\n });\n\n return objectOrFail(payload);\n }\n\n async tvrcSessionStart(): Promise<object> {\n const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {\n _i: 'TVRCSessionStart',\n _t: MessageType.Request,\n _btHP: false,\n _inUseProc: 'tvremoted',\n _c: {}\n });\n\n return objectOrFail(payload);\n }\n\n noOp(): void {\n this.#context.logger.debug('Sending no-op operation.');\n\n this.#stream.send(FrameType.NoOp, Buffer.allocUnsafe(0));\n }\n}\n\nfunction objectOrFail<T = object>(obj: unknown): T {\n if (obj !== null && typeof obj === 'object') {\n return obj as T;\n }\n\n throw new TypeError('Expected an object.');\n}\n\ntype AttentionStateResponse = {\n readonly _c: {\n readonly state: number;\n };\n};\n\ntype LaunchableAppsResponse = {\n readonly _c: Record<string, string>;\n};\n\ntype UserAccountsResponse = {\n readonly _c: Record<string, string>;\n};\n"],"mappings":"4TAAA,MAAa,EAAa,CACtB,GAAI,EACJ,KAAM,EACN,KAAM,EACN,MAAO,EACP,KAAM,EACN,OAAQ,EACR,KAAM,EACN,SAAU,EACV,WAAY,EACZ,KAAM,GACN,YAAa,GACb,MAAO,GACP,KAAM,GACN,UAAW,GACX,iBAAkB,GAClB,iBAAkB,GAClB,MAAO,GACP,OAAQ,GACR,SAAU,GACb,CAEY,EAAsB,CAC/B,KAAM,EACN,MAAO,EACP,UAAW,EACX,cAAe,EACf,UAAW,EACX,UAAW,EACX,OAAQ,EACR,iBAAkB,EAClB,eAAgB,EAChB,YAAa,GACb,UAAW,GACX,mBAAoB,GACpB,mBAAoB,GACvB,CCpCY,EAAY,CACrB,QAAS,EACT,KAAM,EAEN,eAAgB,EAChB,cAAe,EAEf,gBAAiB,EACjB,eAAgB,EAEhB,iBAAkB,EAClB,eAAgB,EAChB,YAAa,EAEb,eAAgB,GAChB,gBAAiB,GAEjB,oBAAqB,GACrB,qBAAsB,GACtB,YAAa,GAEb,sBAAuB,GACvB,uBAAwB,GACxB,qBAAsB,GACzB,CAEY,EAAc,CACvB,MAAO,EACP,QAAS,EACT,SAAU,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A,EAAa,KAAK,EAAU,KAAM,OAAO,YAAY,EAAE,CAAC,GAIhE,SAAS,EAAyB,EAAiB,CAC/C,GAAoB,OAAO,GAAQ,UAA/B,EACA,OAAO,EAGX,MAAU,UAAU,sBAAsB"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basmilius/apple-companion-link",
|
|
3
3
|
"description": "Implementation of Apple's Companion Link in Node.js.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.8.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": {
|
|
@@ -25,27 +25,28 @@
|
|
|
25
25
|
"provenance": false
|
|
26
26
|
},
|
|
27
27
|
"scripts": {
|
|
28
|
-
"build": "tsgo &&
|
|
28
|
+
"build": "tsgo --noEmit && tsdown",
|
|
29
|
+
"dev": "tsdown --watch",
|
|
29
30
|
"find": "bun find.ts",
|
|
30
31
|
"watch:test": "bun --watch test.ts"
|
|
31
32
|
},
|
|
32
|
-
"main": "./dist/index.
|
|
33
|
-
"types": "./dist/index.d.
|
|
34
|
-
"typings": "./dist/index.d.
|
|
33
|
+
"main": "./dist/index.mjs",
|
|
34
|
+
"types": "./dist/index.d.mts",
|
|
35
|
+
"typings": "./dist/index.d.mts",
|
|
35
36
|
"sideEffects": false,
|
|
36
37
|
"exports": {
|
|
37
38
|
".": {
|
|
38
|
-
"types": "./dist/index.d.
|
|
39
|
-
"default": "./dist/index.
|
|
39
|
+
"types": "./dist/index.d.mts",
|
|
40
|
+
"default": "./dist/index.mjs"
|
|
40
41
|
}
|
|
41
42
|
},
|
|
42
43
|
"dependencies": {
|
|
43
|
-
"@basmilius/apple-common": "0.
|
|
44
|
-
"@basmilius/apple-encoding": "0.
|
|
45
|
-
"@basmilius/apple-encryption": "0.
|
|
44
|
+
"@basmilius/apple-common": "0.8.0",
|
|
45
|
+
"@basmilius/apple-encoding": "0.8.0",
|
|
46
|
+
"@basmilius/apple-encryption": "0.8.0"
|
|
46
47
|
},
|
|
47
48
|
"devDependencies": {
|
|
48
|
-
"@
|
|
49
|
-
"
|
|
49
|
+
"@types/bun": "^1.3.9",
|
|
50
|
+
"tsdown": "^0.21.0-beta.2"
|
|
50
51
|
}
|
|
51
52
|
}
|
package/dist/const.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
export declare const HidCommand: {
|
|
2
|
-
readonly Up: 1;
|
|
3
|
-
readonly Down: 2;
|
|
4
|
-
readonly Left: 3;
|
|
5
|
-
readonly Right: 4;
|
|
6
|
-
readonly Menu: 5;
|
|
7
|
-
readonly Select: 6;
|
|
8
|
-
readonly Home: 7;
|
|
9
|
-
readonly VolumeUp: 8;
|
|
10
|
-
readonly VolumeDown: 9;
|
|
11
|
-
readonly Siri: 10;
|
|
12
|
-
readonly Screensaver: 11;
|
|
13
|
-
readonly Sleep: 12;
|
|
14
|
-
readonly Wake: 13;
|
|
15
|
-
readonly PlayPause: 14;
|
|
16
|
-
readonly ChannelIncrement: 15;
|
|
17
|
-
readonly ChannelDecrement: 16;
|
|
18
|
-
readonly Guide: 17;
|
|
19
|
-
readonly PageUp: 18;
|
|
20
|
-
readonly PageDown: 19;
|
|
21
|
-
};
|
|
22
|
-
export declare const MediaControlCommand: {
|
|
23
|
-
readonly Play: 1;
|
|
24
|
-
readonly Pause: 2;
|
|
25
|
-
readonly NextTrack: 3;
|
|
26
|
-
readonly PreviousTrack: 4;
|
|
27
|
-
readonly GetVolume: 5;
|
|
28
|
-
readonly SetVolume: 6;
|
|
29
|
-
readonly SkipBy: 7;
|
|
30
|
-
readonly FastForwardBegin: 8;
|
|
31
|
-
readonly FastForwardEnd: 9;
|
|
32
|
-
readonly RewindBegin: 10;
|
|
33
|
-
readonly RewindEnd: 11;
|
|
34
|
-
readonly GetCaptionSettings: 12;
|
|
35
|
-
readonly SetCaptionSettings: 13;
|
|
36
|
-
};
|
|
37
|
-
export type HidCommandKey = keyof typeof HidCommand;
|
|
38
|
-
export type MediaControlCommandKey = keyof typeof MediaControlCommand;
|
package/dist/frame.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
export declare const FrameType: {
|
|
2
|
-
readonly Unknown: 0;
|
|
3
|
-
readonly NoOp: 1;
|
|
4
|
-
readonly PairSetupStart: 3;
|
|
5
|
-
readonly PairSetupNext: 4;
|
|
6
|
-
readonly PairVerifyStart: 5;
|
|
7
|
-
readonly PairVerifyNext: 6;
|
|
8
|
-
readonly OPackUnencrypted: 7;
|
|
9
|
-
readonly OPackEncrypted: 8;
|
|
10
|
-
readonly OPackPacked: 9;
|
|
11
|
-
readonly PairingRequest: 10;
|
|
12
|
-
readonly PairingResponse: 11;
|
|
13
|
-
readonly SessionStartRequest: 16;
|
|
14
|
-
readonly SessionStartResponse: 17;
|
|
15
|
-
readonly SessionData: 18;
|
|
16
|
-
readonly FamilyIdentityRequest: 32;
|
|
17
|
-
readonly FamilyIdentityResponse: 33;
|
|
18
|
-
readonly FamilyIdentityUpdate: 34;
|
|
19
|
-
};
|
|
20
|
-
export declare const MessageType: {
|
|
21
|
-
readonly Event: 1;
|
|
22
|
-
readonly Request: 2;
|
|
23
|
-
readonly Response: 3;
|
|
24
|
-
};
|
|
25
|
-
export declare const OPackFrameTypes: number[];
|
|
26
|
-
export declare const PairingFrameTypes: number[];
|
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
|
@@ -1,651 +0,0 @@
|
|
|
1
|
-
// src/protocol.ts
|
|
2
|
-
import { randomInt as randomInt2 } from "node:crypto";
|
|
3
|
-
import { Context, waitFor } from "@basmilius/apple-common";
|
|
4
|
-
import { OPack as OPack2, Plist } from "@basmilius/apple-encoding";
|
|
5
|
-
|
|
6
|
-
// src/const.ts
|
|
7
|
-
var HidCommand = {
|
|
8
|
-
Up: 1,
|
|
9
|
-
Down: 2,
|
|
10
|
-
Left: 3,
|
|
11
|
-
Right: 4,
|
|
12
|
-
Menu: 5,
|
|
13
|
-
Select: 6,
|
|
14
|
-
Home: 7,
|
|
15
|
-
VolumeUp: 8,
|
|
16
|
-
VolumeDown: 9,
|
|
17
|
-
Siri: 10,
|
|
18
|
-
Screensaver: 11,
|
|
19
|
-
Sleep: 12,
|
|
20
|
-
Wake: 13,
|
|
21
|
-
PlayPause: 14,
|
|
22
|
-
ChannelIncrement: 15,
|
|
23
|
-
ChannelDecrement: 16,
|
|
24
|
-
Guide: 17,
|
|
25
|
-
PageUp: 18,
|
|
26
|
-
PageDown: 19
|
|
27
|
-
};
|
|
28
|
-
var MediaControlCommand = {
|
|
29
|
-
Play: 1,
|
|
30
|
-
Pause: 2,
|
|
31
|
-
NextTrack: 3,
|
|
32
|
-
PreviousTrack: 4,
|
|
33
|
-
GetVolume: 5,
|
|
34
|
-
SetVolume: 6,
|
|
35
|
-
SkipBy: 7,
|
|
36
|
-
FastForwardBegin: 8,
|
|
37
|
-
FastForwardEnd: 9,
|
|
38
|
-
RewindBegin: 10,
|
|
39
|
-
RewindEnd: 11,
|
|
40
|
-
GetCaptionSettings: 12,
|
|
41
|
-
SetCaptionSettings: 13
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
// src/frame.ts
|
|
45
|
-
var FrameType = {
|
|
46
|
-
Unknown: 0,
|
|
47
|
-
NoOp: 1,
|
|
48
|
-
PairSetupStart: 3,
|
|
49
|
-
PairSetupNext: 4,
|
|
50
|
-
PairVerifyStart: 5,
|
|
51
|
-
PairVerifyNext: 6,
|
|
52
|
-
OPackUnencrypted: 7,
|
|
53
|
-
OPackEncrypted: 8,
|
|
54
|
-
OPackPacked: 9,
|
|
55
|
-
PairingRequest: 10,
|
|
56
|
-
PairingResponse: 11,
|
|
57
|
-
SessionStartRequest: 16,
|
|
58
|
-
SessionStartResponse: 17,
|
|
59
|
-
SessionData: 18,
|
|
60
|
-
FamilyIdentityRequest: 32,
|
|
61
|
-
FamilyIdentityResponse: 33,
|
|
62
|
-
FamilyIdentityUpdate: 34
|
|
63
|
-
};
|
|
64
|
-
var MessageType = {
|
|
65
|
-
Event: 1,
|
|
66
|
-
Request: 2,
|
|
67
|
-
Response: 3
|
|
68
|
-
};
|
|
69
|
-
var OPackFrameTypes = [
|
|
70
|
-
FrameType.PairSetupStart,
|
|
71
|
-
FrameType.PairSetupNext,
|
|
72
|
-
FrameType.PairVerifyStart,
|
|
73
|
-
FrameType.PairVerifyNext,
|
|
74
|
-
FrameType.OPackUnencrypted,
|
|
75
|
-
FrameType.OPackEncrypted,
|
|
76
|
-
FrameType.OPackPacked
|
|
77
|
-
];
|
|
78
|
-
var PairingFrameTypes = [
|
|
79
|
-
FrameType.PairSetupStart,
|
|
80
|
-
FrameType.PairSetupNext,
|
|
81
|
-
FrameType.PairVerifyStart,
|
|
82
|
-
FrameType.PairVerifyNext
|
|
83
|
-
];
|
|
84
|
-
|
|
85
|
-
// src/pairing.ts
|
|
86
|
-
import { AccessoryPair, AccessoryVerify } from "@basmilius/apple-common";
|
|
87
|
-
import { hkdf } from "@basmilius/apple-encryption";
|
|
88
|
-
class Pairing {
|
|
89
|
-
get internal() {
|
|
90
|
-
return this.#internal;
|
|
91
|
-
}
|
|
92
|
-
get stream() {
|
|
93
|
-
return this.#stream;
|
|
94
|
-
}
|
|
95
|
-
#internal;
|
|
96
|
-
#stream;
|
|
97
|
-
constructor(protocol) {
|
|
98
|
-
this.#internal = new AccessoryPair(protocol.context, this.#request.bind(this));
|
|
99
|
-
this.#stream = protocol.stream;
|
|
100
|
-
}
|
|
101
|
-
async start() {
|
|
102
|
-
await this.#internal.start();
|
|
103
|
-
}
|
|
104
|
-
async pin(askPin) {
|
|
105
|
-
return this.#internal.pin(askPin);
|
|
106
|
-
}
|
|
107
|
-
async transient() {
|
|
108
|
-
return this.#internal.transient();
|
|
109
|
-
}
|
|
110
|
-
async#request(step, data) {
|
|
111
|
-
const frameType = step === "m1" ? FrameType.PairSetupStart : FrameType.PairSetupNext;
|
|
112
|
-
const [, response] = await this.#stream.exchange(frameType, {
|
|
113
|
-
_pd: data,
|
|
114
|
-
_pwTy: 1
|
|
115
|
-
});
|
|
116
|
-
if (typeof response !== "object" || response === null) {
|
|
117
|
-
throw new Error("Invalid response from receiver.");
|
|
118
|
-
}
|
|
119
|
-
return response["_pd"];
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
class Verify {
|
|
124
|
-
get internal() {
|
|
125
|
-
return this.#internal;
|
|
126
|
-
}
|
|
127
|
-
get stream() {
|
|
128
|
-
return this.#stream;
|
|
129
|
-
}
|
|
130
|
-
#internal;
|
|
131
|
-
#stream;
|
|
132
|
-
constructor(protocol) {
|
|
133
|
-
this.#internal = new AccessoryVerify(protocol.context, this.#request.bind(this));
|
|
134
|
-
this.#stream = protocol.stream;
|
|
135
|
-
}
|
|
136
|
-
async start(credentials) {
|
|
137
|
-
const keys = await this.#internal.start(credentials);
|
|
138
|
-
const accessoryToControllerKey = hkdf({
|
|
139
|
-
hash: "sha512",
|
|
140
|
-
key: keys.sharedSecret,
|
|
141
|
-
length: 32,
|
|
142
|
-
salt: Buffer.alloc(0),
|
|
143
|
-
info: Buffer.from("ServerEncrypt-main")
|
|
144
|
-
});
|
|
145
|
-
const controllerToAccessoryKey = hkdf({
|
|
146
|
-
hash: "sha512",
|
|
147
|
-
key: keys.sharedSecret,
|
|
148
|
-
length: 32,
|
|
149
|
-
salt: Buffer.alloc(0),
|
|
150
|
-
info: Buffer.from("ClientEncrypt-main")
|
|
151
|
-
});
|
|
152
|
-
return {
|
|
153
|
-
accessoryToControllerKey,
|
|
154
|
-
controllerToAccessoryKey,
|
|
155
|
-
pairingId: keys.pairingId,
|
|
156
|
-
sharedSecret: keys.sharedSecret
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
async#request(step, data) {
|
|
160
|
-
const frameType = step === "m1" ? FrameType.PairVerifyStart : FrameType.PairVerifyNext;
|
|
161
|
-
const [, response] = await this.#stream.exchange(frameType, {
|
|
162
|
-
_pd: data,
|
|
163
|
-
_auTy: 4
|
|
164
|
-
});
|
|
165
|
-
if (typeof response !== "object" || response === null) {
|
|
166
|
-
throw new Error("Invalid response from receiver.");
|
|
167
|
-
}
|
|
168
|
-
return response["_pd"];
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// src/utils.ts
|
|
173
|
-
function convertAttentionState(state) {
|
|
174
|
-
switch (state) {
|
|
175
|
-
case 1:
|
|
176
|
-
return "asleep";
|
|
177
|
-
case 2:
|
|
178
|
-
return "screensaver";
|
|
179
|
-
case 3:
|
|
180
|
-
return "awake";
|
|
181
|
-
case 4:
|
|
182
|
-
return "idle";
|
|
183
|
-
default:
|
|
184
|
-
return "unknown";
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// src/stream.ts
|
|
189
|
-
import { randomInt } from "node:crypto";
|
|
190
|
-
import { ENCRYPTION, EncryptionAwareConnection } from "@basmilius/apple-common";
|
|
191
|
-
import { OPack } from "@basmilius/apple-encoding";
|
|
192
|
-
import { Chacha20 } from "@basmilius/apple-encryption";
|
|
193
|
-
var HEADER_SIZE = 4;
|
|
194
|
-
var PAIRING_QUEUE_IDENTIFIER = -1;
|
|
195
|
-
|
|
196
|
-
class Stream extends EncryptionAwareConnection {
|
|
197
|
-
get #encryptionState() {
|
|
198
|
-
return this[ENCRYPTION];
|
|
199
|
-
}
|
|
200
|
-
#queue = new Map;
|
|
201
|
-
#buffer = Buffer.alloc(0);
|
|
202
|
-
#xid;
|
|
203
|
-
constructor(context, address, port) {
|
|
204
|
-
super(context, address, port);
|
|
205
|
-
this.debug(true);
|
|
206
|
-
this.#xid = randomInt(0, 2 ** 16);
|
|
207
|
-
this.on("close", this.#onClose.bind(this));
|
|
208
|
-
this.on("data", this.#onData.bind(this));
|
|
209
|
-
this.on("error", this.#onError.bind(this));
|
|
210
|
-
}
|
|
211
|
-
async disconnect() {
|
|
212
|
-
this.#cleanup();
|
|
213
|
-
await super.disconnect();
|
|
214
|
-
}
|
|
215
|
-
async exchange(type, obj) {
|
|
216
|
-
const _x = this.#xid;
|
|
217
|
-
return new Promise((resolve, reject) => {
|
|
218
|
-
if (PairingFrameTypes.includes(type)) {
|
|
219
|
-
this.#queue.set(PAIRING_QUEUE_IDENTIFIER, [resolve, reject]);
|
|
220
|
-
} else {
|
|
221
|
-
this.#queue.set(_x, [resolve, reject]);
|
|
222
|
-
}
|
|
223
|
-
this.sendOPack(type, obj);
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
send(type, payload) {
|
|
227
|
-
const encrypt = this.isEncrypted && type !== FrameType.NoOp;
|
|
228
|
-
let payloadLength = payload.byteLength;
|
|
229
|
-
if (encrypt) {
|
|
230
|
-
payloadLength += Chacha20.CHACHA20_AUTH_TAG_LENGTH;
|
|
231
|
-
}
|
|
232
|
-
const header = Buffer.allocUnsafe(4);
|
|
233
|
-
header.writeUint8(type, 0);
|
|
234
|
-
header.writeUintBE(payloadLength, 1, 3);
|
|
235
|
-
let data;
|
|
236
|
-
if (encrypt) {
|
|
237
|
-
const nonce = Buffer.alloc(12);
|
|
238
|
-
nonce.writeBigUInt64LE(BigInt(this.#encryptionState.writeCount++), 0);
|
|
239
|
-
const encrypted = Chacha20.encrypt(this.#encryptionState.writeKey, nonce, header, payload);
|
|
240
|
-
data = Buffer.concat([header, encrypted.ciphertext, encrypted.authTag]);
|
|
241
|
-
} else {
|
|
242
|
-
data = Buffer.concat([header, payload]);
|
|
243
|
-
}
|
|
244
|
-
this.context.logger.raw("[companion-link]", "Sending data frame", this.isEncrypted, type);
|
|
245
|
-
this.write(data);
|
|
246
|
-
}
|
|
247
|
-
sendOPack(type, obj) {
|
|
248
|
-
const _x = this.#xid++;
|
|
249
|
-
obj._x ??= OPack.sizedInteger(_x, 8);
|
|
250
|
-
this.context.logger.raw("[companion-link]", "Sending opack frame", type, this.isEncrypted, obj);
|
|
251
|
-
this.send(type, Buffer.from(OPack.encode(obj)));
|
|
252
|
-
}
|
|
253
|
-
#cleanup() {
|
|
254
|
-
this.#buffer = Buffer.alloc(0);
|
|
255
|
-
const error = new Error("Stream cleanup");
|
|
256
|
-
for (const [, reject] of this.#queue.values()) {
|
|
257
|
-
reject(error);
|
|
258
|
-
}
|
|
259
|
-
this.#queue.clear();
|
|
260
|
-
}
|
|
261
|
-
#onClose() {
|
|
262
|
-
const error = new Error("Connection closed while waiting for response");
|
|
263
|
-
for (const [, reject] of this.#queue.values()) {
|
|
264
|
-
reject(error);
|
|
265
|
-
}
|
|
266
|
-
this.#queue.clear();
|
|
267
|
-
this.#cleanup();
|
|
268
|
-
}
|
|
269
|
-
async#onData(data) {
|
|
270
|
-
this.#buffer = Buffer.concat([this.#buffer, data]);
|
|
271
|
-
try {
|
|
272
|
-
while (this.#buffer.byteLength >= HEADER_SIZE) {
|
|
273
|
-
const header = this.#buffer.subarray(0, HEADER_SIZE);
|
|
274
|
-
const payloadLength = header.readUintBE(1, 3);
|
|
275
|
-
const totalLength = HEADER_SIZE + payloadLength;
|
|
276
|
-
if (this.#buffer.byteLength < totalLength) {
|
|
277
|
-
this.context.logger.warn("[companion-link]", `Data packet is too short needed=${totalLength} available=${this.#buffer.byteLength} receivedLength=${data.byteLength}`);
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
this.context.logger.raw("[companion-link]", `Received frame length=${totalLength} availableLength=${this.#buffer.byteLength} receivedLength=${data.byteLength}`);
|
|
281
|
-
let frame = Buffer.from(this.#buffer.subarray(0, totalLength));
|
|
282
|
-
this.#buffer = this.#buffer.subarray(totalLength);
|
|
283
|
-
this.context.logger.raw("[companion-link]", `Handle frame, ${this.#buffer.byteLength} bytes left...`);
|
|
284
|
-
if (this.isEncrypted) {
|
|
285
|
-
frame = this.#decrypt(frame);
|
|
286
|
-
}
|
|
287
|
-
const payload = frame.subarray(HEADER_SIZE, totalLength);
|
|
288
|
-
this.#handle(header, payload);
|
|
289
|
-
}
|
|
290
|
-
} catch (err) {
|
|
291
|
-
this.context.logger.error("[companion-link]", "#onData()", err);
|
|
292
|
-
this.emit("error", err);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
#onError(err) {
|
|
296
|
-
for (const [, reject] of this.#queue.values()) {
|
|
297
|
-
reject(err);
|
|
298
|
-
}
|
|
299
|
-
this.#queue.clear();
|
|
300
|
-
}
|
|
301
|
-
#decrypt(data) {
|
|
302
|
-
const header = data.subarray(0, 4);
|
|
303
|
-
const payloadLength = header.readUintBE(1, 3);
|
|
304
|
-
const payload = data.subarray(4, 4 + payloadLength);
|
|
305
|
-
const authTag = payload.subarray(payload.byteLength - 16);
|
|
306
|
-
const ciphertext = payload.subarray(0, payload.byteLength - 16);
|
|
307
|
-
const nonce = Buffer.alloc(12);
|
|
308
|
-
nonce.writeBigUint64LE(BigInt(this.#encryptionState.readCount++), 0);
|
|
309
|
-
const decrypted = Chacha20.decrypt(this.#encryptionState.readKey, nonce, header, ciphertext, authTag);
|
|
310
|
-
return Buffer.concat([header, decrypted, authTag]);
|
|
311
|
-
}
|
|
312
|
-
#handle(header, payload) {
|
|
313
|
-
const type = header.readInt8();
|
|
314
|
-
if (!OPackFrameTypes.includes(type)) {
|
|
315
|
-
this.context.logger.warn("[companion-link]", "Packet not handled, no opack frame.");
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
payload = OPack.decode(payload);
|
|
319
|
-
this.context.logger.raw("[companion-link]", "Decoded OPACK", { header, payload });
|
|
320
|
-
if ("_x" in payload) {
|
|
321
|
-
const _x = Number(payload["_x"]);
|
|
322
|
-
if (this.#queue.has(_x)) {
|
|
323
|
-
const [resolve] = this.#queue.get(_x);
|
|
324
|
-
resolve([header, payload]);
|
|
325
|
-
this.#queue.delete(_x);
|
|
326
|
-
} else if ("_i" in payload) {
|
|
327
|
-
this.emit(payload["_i"], payload["_c"]);
|
|
328
|
-
} else {
|
|
329
|
-
const content = payload["_c"];
|
|
330
|
-
const keys = Object.keys(content).map((k) => k.slice(0, -3));
|
|
331
|
-
for (const key of keys) {
|
|
332
|
-
this.emit(key, content[key]);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
} else if (this.#queue.has(PAIRING_QUEUE_IDENTIFIER)) {
|
|
336
|
-
const [resolve] = this.#queue.get(PAIRING_QUEUE_IDENTIFIER);
|
|
337
|
-
resolve([header, payload]);
|
|
338
|
-
this.#queue.delete(PAIRING_QUEUE_IDENTIFIER);
|
|
339
|
-
} else {
|
|
340
|
-
this.context.logger.warn("[companion-link]", "No handler for message", [header, payload]);
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
// src/protocol.ts
|
|
346
|
-
class Protocol {
|
|
347
|
-
get context() {
|
|
348
|
-
return this.#context;
|
|
349
|
-
}
|
|
350
|
-
get discoveryResult() {
|
|
351
|
-
return this.#discoveryResult;
|
|
352
|
-
}
|
|
353
|
-
get pairing() {
|
|
354
|
-
return this.#pairing;
|
|
355
|
-
}
|
|
356
|
-
get stream() {
|
|
357
|
-
return this.#stream;
|
|
358
|
-
}
|
|
359
|
-
get verify() {
|
|
360
|
-
return this.#verify;
|
|
361
|
-
}
|
|
362
|
-
#context;
|
|
363
|
-
#discoveryResult;
|
|
364
|
-
#pairing;
|
|
365
|
-
#stream;
|
|
366
|
-
#verify;
|
|
367
|
-
constructor(discoveryResult) {
|
|
368
|
-
this.#context = new Context(discoveryResult.id);
|
|
369
|
-
this.#discoveryResult = discoveryResult;
|
|
370
|
-
this.#stream = new Stream(this.#context, discoveryResult.address, discoveryResult.service.port);
|
|
371
|
-
this.#pairing = new Pairing(this);
|
|
372
|
-
this.#verify = new Verify(this);
|
|
373
|
-
}
|
|
374
|
-
async connect() {
|
|
375
|
-
await this.#stream.connect();
|
|
376
|
-
}
|
|
377
|
-
async destroy() {
|
|
378
|
-
await this.#stream.destroy();
|
|
379
|
-
}
|
|
380
|
-
async disconnect() {
|
|
381
|
-
await this.#stream.disconnect();
|
|
382
|
-
}
|
|
383
|
-
async fetchMediaControlStatus() {
|
|
384
|
-
await this.#stream.exchange(FrameType.OPackEncrypted, {
|
|
385
|
-
_i: "FetchMediaControlStatus",
|
|
386
|
-
_t: MessageType.Request,
|
|
387
|
-
_c: {}
|
|
388
|
-
});
|
|
389
|
-
}
|
|
390
|
-
async fetchNowPlayingInfo() {
|
|
391
|
-
await this.#stream.exchange(FrameType.OPackEncrypted, {
|
|
392
|
-
_i: "FetchCurrentNowPlayingInfoEvent",
|
|
393
|
-
_t: MessageType.Request,
|
|
394
|
-
_c: {}
|
|
395
|
-
});
|
|
396
|
-
}
|
|
397
|
-
async fetchSupportedActions() {
|
|
398
|
-
await this.#stream.exchange(FrameType.OPackEncrypted, {
|
|
399
|
-
_i: "FetchSupportedActionsEvent",
|
|
400
|
-
_t: MessageType.Request,
|
|
401
|
-
_c: {}
|
|
402
|
-
});
|
|
403
|
-
}
|
|
404
|
-
async getAttentionState() {
|
|
405
|
-
const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
|
|
406
|
-
_i: "FetchAttentionState",
|
|
407
|
-
_t: MessageType.Request,
|
|
408
|
-
_c: {}
|
|
409
|
-
});
|
|
410
|
-
const { _c } = objectOrFail(payload);
|
|
411
|
-
return convertAttentionState(_c.state);
|
|
412
|
-
}
|
|
413
|
-
async getLaunchableApps() {
|
|
414
|
-
const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
|
|
415
|
-
_i: "FetchLaunchableApplicationsEvent",
|
|
416
|
-
_t: MessageType.Request,
|
|
417
|
-
_c: {}
|
|
418
|
-
});
|
|
419
|
-
const { _c } = objectOrFail(payload);
|
|
420
|
-
return Object.entries(_c).map(([bundleId, name]) => ({
|
|
421
|
-
bundleId,
|
|
422
|
-
name
|
|
423
|
-
}));
|
|
424
|
-
}
|
|
425
|
-
async getSiriRemoteInfo() {
|
|
426
|
-
const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
|
|
427
|
-
_i: "FetchSiriRemoteInfo",
|
|
428
|
-
_t: MessageType.Request,
|
|
429
|
-
_c: {}
|
|
430
|
-
});
|
|
431
|
-
return Plist.parse(Buffer.from(payload["_c"]["SiriRemoteInfoKey"]).buffer);
|
|
432
|
-
}
|
|
433
|
-
async getUserAccounts() {
|
|
434
|
-
const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
|
|
435
|
-
_i: "FetchUserAccountsEvent",
|
|
436
|
-
_t: MessageType.Request,
|
|
437
|
-
_c: {}
|
|
438
|
-
});
|
|
439
|
-
const { _c } = objectOrFail(payload);
|
|
440
|
-
return Object.entries(_c).map(([accountId, name]) => ({
|
|
441
|
-
accountId,
|
|
442
|
-
name
|
|
443
|
-
}));
|
|
444
|
-
}
|
|
445
|
-
async hidCommand(command, down = false) {
|
|
446
|
-
await this.#stream.exchange(FrameType.OPackEncrypted, {
|
|
447
|
-
_i: "_hidC",
|
|
448
|
-
_t: MessageType.Request,
|
|
449
|
-
_c: {
|
|
450
|
-
_hBtS: down ? 1 : 2,
|
|
451
|
-
_hidC: HidCommand[command]
|
|
452
|
-
}
|
|
453
|
-
});
|
|
454
|
-
}
|
|
455
|
-
async launchApp(bundleId) {
|
|
456
|
-
await this.#stream.exchange(FrameType.OPackEncrypted, {
|
|
457
|
-
_i: "_launchApp",
|
|
458
|
-
_t: MessageType.Request,
|
|
459
|
-
_c: {
|
|
460
|
-
_bundleID: bundleId
|
|
461
|
-
}
|
|
462
|
-
});
|
|
463
|
-
}
|
|
464
|
-
async launchUrl(url) {
|
|
465
|
-
await this.#stream.exchange(FrameType.OPackEncrypted, {
|
|
466
|
-
_i: "_launchApp",
|
|
467
|
-
_t: MessageType.Request,
|
|
468
|
-
_c: {
|
|
469
|
-
_urlS: url
|
|
470
|
-
}
|
|
471
|
-
});
|
|
472
|
-
}
|
|
473
|
-
async mediaControlCommand(command, content) {
|
|
474
|
-
const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
|
|
475
|
-
_i: "_mcc",
|
|
476
|
-
_t: MessageType.Request,
|
|
477
|
-
_c: {
|
|
478
|
-
_mcc: MediaControlCommand[command],
|
|
479
|
-
...content || {}
|
|
480
|
-
}
|
|
481
|
-
});
|
|
482
|
-
return objectOrFail(payload);
|
|
483
|
-
}
|
|
484
|
-
async pressButton(command, type = "SingleTap", holdDelayMs = 500) {
|
|
485
|
-
switch (type) {
|
|
486
|
-
case "DoubleTap":
|
|
487
|
-
await this.hidCommand(command, true);
|
|
488
|
-
await this.hidCommand(command, false);
|
|
489
|
-
await this.hidCommand(command, true);
|
|
490
|
-
await this.hidCommand(command, false);
|
|
491
|
-
break;
|
|
492
|
-
case "Hold":
|
|
493
|
-
await this.hidCommand(command, true);
|
|
494
|
-
await waitFor(holdDelayMs);
|
|
495
|
-
await this.hidCommand(command, false);
|
|
496
|
-
break;
|
|
497
|
-
case "SingleTap":
|
|
498
|
-
await this.hidCommand(command, true);
|
|
499
|
-
await this.hidCommand(command, false);
|
|
500
|
-
break;
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
async switchUserAccount(accountId) {
|
|
504
|
-
await this.#stream.exchange(FrameType.OPackEncrypted, {
|
|
505
|
-
_i: "SwitchUserAccountEvent",
|
|
506
|
-
_t: MessageType.Request,
|
|
507
|
-
_c: {
|
|
508
|
-
SwitchAccountID: accountId
|
|
509
|
-
}
|
|
510
|
-
});
|
|
511
|
-
}
|
|
512
|
-
async subscribe(event, fn) {
|
|
513
|
-
this.#stream.on(event, fn);
|
|
514
|
-
this.#stream.sendOPack(FrameType.OPackEncrypted, {
|
|
515
|
-
_i: "_interest",
|
|
516
|
-
_t: MessageType.Event,
|
|
517
|
-
_c: {
|
|
518
|
-
_regEvents: [event]
|
|
519
|
-
}
|
|
520
|
-
});
|
|
521
|
-
}
|
|
522
|
-
async unsubscribe(event, fn) {
|
|
523
|
-
if (!this.#stream.isConnected) {
|
|
524
|
-
return;
|
|
525
|
-
}
|
|
526
|
-
if (fn) {
|
|
527
|
-
this.#stream.off(event, fn);
|
|
528
|
-
}
|
|
529
|
-
this.#stream.sendOPack(FrameType.OPackEncrypted, {
|
|
530
|
-
_i: "_interest",
|
|
531
|
-
_t: MessageType.Event,
|
|
532
|
-
_c: {
|
|
533
|
-
_deregEvents: [event]
|
|
534
|
-
}
|
|
535
|
-
});
|
|
536
|
-
}
|
|
537
|
-
async sessionStart() {
|
|
538
|
-
const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
|
|
539
|
-
_i: "_sessionStart",
|
|
540
|
-
_t: MessageType.Request,
|
|
541
|
-
_btHP: false,
|
|
542
|
-
_c: {
|
|
543
|
-
_srvT: "com.apple.tvremoteservices",
|
|
544
|
-
_sid: randomInt2(0, 2 ** 32 - 1),
|
|
545
|
-
_btHP: false
|
|
546
|
-
}
|
|
547
|
-
});
|
|
548
|
-
return objectOrFail(payload);
|
|
549
|
-
}
|
|
550
|
-
async systemInfo(pairingId) {
|
|
551
|
-
const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
|
|
552
|
-
_i: "_systemInfo",
|
|
553
|
-
_t: MessageType.Request,
|
|
554
|
-
_btHP: false,
|
|
555
|
-
_c: {
|
|
556
|
-
_bf: 0,
|
|
557
|
-
_cf: 512,
|
|
558
|
-
_clFl: 128,
|
|
559
|
-
_i: "b561af32aea6",
|
|
560
|
-
_idsID: pairingId.toString(),
|
|
561
|
-
_pubID: "DA:6D:1E:D8:A0:4F",
|
|
562
|
-
_sf: 1099511628032,
|
|
563
|
-
_sv: "715.2",
|
|
564
|
-
model: "iPhone16,2",
|
|
565
|
-
name: "AP Companion Link",
|
|
566
|
-
_lP: 50402,
|
|
567
|
-
_dC: "1",
|
|
568
|
-
_stA: [
|
|
569
|
-
"com.apple.sharingd.AirDrop",
|
|
570
|
-
"SymptomNetworkDiagnostics",
|
|
571
|
-
"com.apple.photosface.network-service",
|
|
572
|
-
"com.apple.ApplicationService.chrono",
|
|
573
|
-
"com.apple.DDUI-Picker",
|
|
574
|
-
"com.apple.biomesyncd.cascade.rapport",
|
|
575
|
-
"com.apple.SeymourSession",
|
|
576
|
-
"com.apple.workflow.remotewidgets",
|
|
577
|
-
"com.apple.ApplicationService.chrono",
|
|
578
|
-
"SCD.MessageCenter.remoteIntelligence",
|
|
579
|
-
"DeviceSharingDaemonApplicationService",
|
|
580
|
-
"com.apple.biomesyncd.rapport",
|
|
581
|
-
"com.apple.devicediscoveryui.rapportwake",
|
|
582
|
-
"com.apple.healthd.rapport",
|
|
583
|
-
"com.apple.dropin.setup",
|
|
584
|
-
"com.apple.coreduet.sync",
|
|
585
|
-
"com.apple.siri.wakeup",
|
|
586
|
-
"com.apple.wifivelocityd.rapportWake",
|
|
587
|
-
"com.apple.Seymour",
|
|
588
|
-
"CPSRemoteLLM",
|
|
589
|
-
"com.apple.networkrelay.on-demand-setup",
|
|
590
|
-
"com.apple.home.messaging",
|
|
591
|
-
"com.apple.accessibility.axremoted.rapportWake",
|
|
592
|
-
"com.apple.continuitycapture.sideband",
|
|
593
|
-
"com.apple.announce",
|
|
594
|
-
"com.apple.coreidv.coreidvd.handoff"
|
|
595
|
-
]
|
|
596
|
-
}
|
|
597
|
-
});
|
|
598
|
-
return objectOrFail(payload);
|
|
599
|
-
}
|
|
600
|
-
async tiStart() {
|
|
601
|
-
const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
|
|
602
|
-
_i: "_tiStart",
|
|
603
|
-
_t: MessageType.Request,
|
|
604
|
-
_btHP: false,
|
|
605
|
-
_c: {}
|
|
606
|
-
});
|
|
607
|
-
return objectOrFail(payload);
|
|
608
|
-
}
|
|
609
|
-
async touchStart() {
|
|
610
|
-
const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
|
|
611
|
-
_i: "_touchStart",
|
|
612
|
-
_t: MessageType.Request,
|
|
613
|
-
_btHP: false,
|
|
614
|
-
_c: {
|
|
615
|
-
_height: OPack2.float(1000),
|
|
616
|
-
_tFl: 0,
|
|
617
|
-
_width: OPack2.float(1000)
|
|
618
|
-
}
|
|
619
|
-
});
|
|
620
|
-
return objectOrFail(payload);
|
|
621
|
-
}
|
|
622
|
-
async tvrcSessionStart() {
|
|
623
|
-
const [, payload] = await this.#stream.exchange(FrameType.OPackEncrypted, {
|
|
624
|
-
_i: "TVRCSessionStart",
|
|
625
|
-
_t: MessageType.Request,
|
|
626
|
-
_btHP: false,
|
|
627
|
-
_inUseProc: "tvremoted",
|
|
628
|
-
_c: {}
|
|
629
|
-
});
|
|
630
|
-
return objectOrFail(payload);
|
|
631
|
-
}
|
|
632
|
-
noOp() {
|
|
633
|
-
this.#context.logger.debug("Sending no-op operation.");
|
|
634
|
-
this.#stream.send(FrameType.NoOp, Buffer.allocUnsafe(0));
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
function objectOrFail(obj) {
|
|
638
|
-
if (obj !== null && typeof obj === "object") {
|
|
639
|
-
return obj;
|
|
640
|
-
}
|
|
641
|
-
throw new TypeError("Expected an object.");
|
|
642
|
-
}
|
|
643
|
-
export {
|
|
644
|
-
convertAttentionState,
|
|
645
|
-
Verify,
|
|
646
|
-
Stream,
|
|
647
|
-
Protocol,
|
|
648
|
-
Pairing,
|
|
649
|
-
MediaControlCommand,
|
|
650
|
-
HidCommand
|
|
651
|
-
};
|
package/dist/pairing.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { type AccessoryCredentials, type AccessoryKeys, AccessoryPair, AccessoryVerify } from "@basmilius/apple-common";
|
|
2
|
-
import type Protocol from "./protocol";
|
|
3
|
-
import type Stream from "./stream";
|
|
4
|
-
export declare class Pairing {
|
|
5
|
-
#private;
|
|
6
|
-
get internal(): AccessoryPair;
|
|
7
|
-
get stream(): Stream;
|
|
8
|
-
constructor(protocol: Protocol);
|
|
9
|
-
start(): Promise<void>;
|
|
10
|
-
pin(askPin: () => Promise<string>): Promise<AccessoryCredentials>;
|
|
11
|
-
transient(): Promise<AccessoryKeys>;
|
|
12
|
-
}
|
|
13
|
-
export declare class Verify {
|
|
14
|
-
#private;
|
|
15
|
-
get internal(): AccessoryVerify;
|
|
16
|
-
get stream(): Stream;
|
|
17
|
-
constructor(protocol: Protocol);
|
|
18
|
-
start(credentials: AccessoryCredentials): Promise<AccessoryKeys>;
|
|
19
|
-
}
|
package/dist/protocol.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { Context, type DiscoveryResult } from "@basmilius/apple-common";
|
|
2
|
-
import { type HidCommandKey, type MediaControlCommandKey } from "./const";
|
|
3
|
-
import { Pairing, Verify } from "./pairing";
|
|
4
|
-
import type { AttentionState, ButtonPressType, LaunchableApp, UserAccount } from "./types";
|
|
5
|
-
import Stream from "./stream";
|
|
6
|
-
export default class Protocol {
|
|
7
|
-
#private;
|
|
8
|
-
get context(): Context;
|
|
9
|
-
get discoveryResult(): DiscoveryResult;
|
|
10
|
-
get pairing(): Pairing;
|
|
11
|
-
get stream(): Stream;
|
|
12
|
-
get verify(): Verify;
|
|
13
|
-
constructor(discoveryResult: DiscoveryResult);
|
|
14
|
-
connect(): Promise<void>;
|
|
15
|
-
destroy(): Promise<void>;
|
|
16
|
-
disconnect(): Promise<void>;
|
|
17
|
-
fetchMediaControlStatus(): Promise<void>;
|
|
18
|
-
fetchNowPlayingInfo(): Promise<void>;
|
|
19
|
-
fetchSupportedActions(): Promise<void>;
|
|
20
|
-
getAttentionState(): Promise<AttentionState>;
|
|
21
|
-
getLaunchableApps(): Promise<LaunchableApp[]>;
|
|
22
|
-
getSiriRemoteInfo(): Promise<any>;
|
|
23
|
-
getUserAccounts(): Promise<UserAccount[]>;
|
|
24
|
-
hidCommand(command: HidCommandKey, down?: boolean): Promise<void>;
|
|
25
|
-
launchApp(bundleId: string): Promise<void>;
|
|
26
|
-
launchUrl(url: string): Promise<void>;
|
|
27
|
-
mediaControlCommand(command: MediaControlCommandKey, content?: object): Promise<object>;
|
|
28
|
-
pressButton(command: HidCommandKey, type?: ButtonPressType, holdDelayMs?: number): Promise<void>;
|
|
29
|
-
switchUserAccount(accountId: string): Promise<void>;
|
|
30
|
-
subscribe(event: string, fn: (data: unknown) => void): Promise<void>;
|
|
31
|
-
unsubscribe(event: string, fn?: (data: unknown) => void): Promise<void>;
|
|
32
|
-
sessionStart(): Promise<object>;
|
|
33
|
-
systemInfo(pairingId: Buffer): Promise<object>;
|
|
34
|
-
tiStart(): Promise<object>;
|
|
35
|
-
touchStart(): Promise<object>;
|
|
36
|
-
tvrcSessionStart(): Promise<object>;
|
|
37
|
-
noOp(): void;
|
|
38
|
-
}
|
package/dist/stream.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { type Context, EncryptionAwareConnection } from "@basmilius/apple-common";
|
|
2
|
-
export default class Stream extends EncryptionAwareConnection<Record<string, [unknown]>> {
|
|
3
|
-
#private;
|
|
4
|
-
constructor(context: Context, address: string, port: number);
|
|
5
|
-
disconnect(): Promise<void>;
|
|
6
|
-
exchange(type: number, obj: Record<string, unknown>): Promise<[number, unknown]>;
|
|
7
|
-
send(type: number, payload: Buffer): void;
|
|
8
|
-
sendOPack(type: number, obj: Record<string, unknown>): void;
|
|
9
|
-
}
|
package/dist/types.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export type AttentionState =
|
|
2
|
-
| 'unknown'
|
|
3
|
-
| 'asleep'
|
|
4
|
-
| 'screensaver'
|
|
5
|
-
| 'awake'
|
|
6
|
-
| 'idle';
|
|
7
|
-
|
|
8
|
-
export type ButtonPressType =
|
|
9
|
-
| 'DoubleTap'
|
|
10
|
-
| 'Hold'
|
|
11
|
-
| 'SingleTap';
|
|
12
|
-
|
|
13
|
-
export type LaunchableApp = {
|
|
14
|
-
readonly bundleId: string;
|
|
15
|
-
readonly name: string;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export type UserAccount = {
|
|
19
|
-
readonly accountId: string;
|
|
20
|
-
readonly name: string;
|
|
21
|
-
};
|
package/dist/utils.d.ts
DELETED