11import type { Logger } from 'winston' ;
2+ import { IpV4Addr , IpV6Addr } from './ip-addr' ;
3+ import type { IpAddr } from './ip-addr' ;
24import type Processor from './Processor' ;
35import { AudioNotFoundResult , ThrottledUidResult , UnknownUidResult } from './Processor' ;
46import type Stream from './Stream' ;
@@ -7,16 +9,26 @@ export enum Command {
79 checkUid = 0 ,
810 getAudio = 1 ,
911 ping = 2 ,
12+ quit = 3 ,
1013}
1114
12- export enum Response {
15+ export enum AuthResponse {
1316 invalidAuth = 0 ,
1417 validAuth = 1 ,
18+ }
19+
20+ export enum CheckUidResponse {
1521 unknownUid = 0 ,
1622 throttledUid = 2 ,
1723 validUid = 1 ,
24+ }
25+
26+ export enum GetAudioResponse {
1827 audioNotFound = 0 ,
1928 audioFound = 1 ,
29+ }
30+
31+ export enum PingResponse {
2032 pong = 0 ,
2133}
2234
@@ -39,25 +51,39 @@ const handleClient = async (
3951 } , authTimeout ) ;
4052 } ) ,
4153 ( async ( ) => {
42- const credentialsLength = await stream . readUint8 ( ) ;
43- const credentials = ( await stream . readExact ( credentialsLength ) ) . toString ( 'ascii ' ) ;
54+ const clientIdLength = await stream . readUint8 ( ) ;
55+ const clientId = ( await stream . readExact ( clientIdLength ) ) . toString ( 'utf-8 ' ) ;
4456
45- if ( ! credentials . includes ( ':' ) ) {
46- logger ?. info ( 'Malformed credentials' ) ;
47- stream . writeUint8 ( Response . invalidAuth ) ;
48- return null ;
57+ const secretLength = await stream . readUint8 ( ) ;
58+ const secret = ( await stream . readExact ( secretLength ) ) . toString ( 'utf-8' ) ;
59+
60+ const inetVersion = await stream . readUint8 ( ) ;
61+ let localIp : IpAddr ;
62+
63+ switch ( inetVersion ) {
64+ case 4 :
65+ localIp = new IpV4Addr ( await stream . readExact ( 4 ) ) ;
66+ break ;
67+
68+ case 6 :
69+ localIp = new IpV6Addr ( await stream . readExact ( 16 ) ) ;
70+ break ;
71+
72+ default :
73+ logger ?. info ( `Invalid INET version "${ inetVersion } "` ) ;
74+ stream . writeUint8 ( AuthResponse . invalidAuth ) ;
75+ return null ;
4976 }
5077
51- const [ clientId , secret ] = credentials . split ( ':' , 2 ) ;
52- const authResult = await processor . authenticate ( clientId , secret ) ;
78+ const authResult = await processor . authenticate ( clientId , secret , localIp ) ;
5379
5480 if ( ! authResult ) {
5581 logger ?. info ( `Unknown client ID "${ clientId } " or invalid secret` ) ;
56- stream . writeUint8 ( Response . invalidAuth ) ;
82+ stream . writeUint8 ( AuthResponse . invalidAuth ) ;
5783 return null ;
5884 }
5985
60- stream . writeUint8 ( Response . validAuth ) ;
86+ stream . writeUint8 ( AuthResponse . validAuth ) ;
6187 return clientId ;
6288 } ) ( ) ,
6389 ] ) ;
@@ -83,16 +109,16 @@ const handleClient = async (
83109 const response = await processor . checkUid ( clientId , uid ) ;
84110
85111 if ( response instanceof UnknownUidResult ) {
86- stream . writeUint8 ( Response . unknownUid ) ;
112+ stream . writeUint8 ( CheckUidResponse . unknownUid ) ;
87113 break ;
88114 }
89115
90116 if ( response instanceof ThrottledUidResult ) {
91- stream . writeUint8 ( Response . throttledUid ) ;
117+ stream . writeUint8 ( CheckUidResponse . throttledUid ) ;
92118 break ;
93119 }
94120
95- stream . writeUint8 ( Response . validUid ) ;
121+ stream . writeUint8 ( CheckUidResponse . validUid ) ;
96122 stream . writeUint8 ( response . achievementIds . length ) ;
97123
98124 for ( const achievementId of response . achievementIds ) {
@@ -107,20 +133,23 @@ const handleClient = async (
107133 const result = await processor . getAudio ( id ) ;
108134
109135 if ( result instanceof AudioNotFoundResult ) {
110- stream . writeUint8 ( Response . audioNotFound ) ;
136+ stream . writeUint8 ( GetAudioResponse . audioNotFound ) ;
111137 break ;
112138 }
113139
114- stream . writeUint8 ( Response . audioFound ) ;
140+ stream . writeUint8 ( GetAudioResponse . audioFound ) ;
115141 stream . writeUInt32LE ( result . data . length ) ;
116142 stream . writeAll ( result . data ) ;
117143 break ;
118144 }
119145
120146 case Command . ping :
121- stream . writeUint8 ( Response . pong ) ;
147+ stream . writeUint8 ( PingResponse . pong ) ;
122148 break ;
123149
150+ case Command . quit :
151+ return ;
152+
124153 default :
125154 logger ?. info ( `Unknown command: ${ commandCode } ` ) ;
126155 return ;
0 commit comments