-
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial implement of new data sniffer
- Loading branch information
1 parent
6d10a69
commit 6071309
Showing
7 changed files
with
184 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import EventEmitter from 'events' | ||
import querystring from 'querystring' | ||
import fs from 'fs-extra' | ||
import path from 'path' | ||
import URL from 'url' | ||
|
||
type RequestOrigin = string | undefined | ||
type PathName = string | ||
type Url = string | ||
type RequestInfo = [RequestOrigin, PathName, Url] | ||
|
||
interface KancolleServer { | ||
num?: number | ||
name?: string | ||
ip?: string | ||
} | ||
|
||
interface KancolleServerInfo { | ||
[ip: string]: KancolleServer | ||
} | ||
|
||
class GameAPIBroadcaster extends EventEmitter { | ||
serverList: KancolleServerInfo = fs.readJsonSync(path.join(ROOT, 'assets', 'data', 'server.json')) | ||
|
||
serverInfo: KancolleServer = {} | ||
|
||
sendRequest = (method: string, requestInfo: RequestInfo, rawReqBody: string) => { | ||
this.emit( | ||
'network.on.request', | ||
method, | ||
requestInfo, | ||
JSON.stringify(querystring.parse(rawReqBody || '')), | ||
Date.now(), | ||
) | ||
} | ||
|
||
sendResponse = ( | ||
method: string, | ||
requestInfo: RequestInfo, | ||
rawReqBody: string, | ||
rawResBody: unknown, | ||
resType: XMLHttpRequestResponseType, | ||
statusCode?: number, | ||
) => { | ||
this.updateKanColleServer(requestInfo) | ||
const resBody = this.parseResponseBody(rawResBody, resType) | ||
if (resBody && statusCode === 200) { | ||
this.emit( | ||
'network.on.response', | ||
method, | ||
requestInfo, | ||
resBody, | ||
JSON.stringify(querystring.parse(rawReqBody || '')), | ||
Date.now(), | ||
) | ||
} | ||
} | ||
|
||
sendError = (requestInfo: RequestInfo, statusCode?: number) => { | ||
this.emit('network.error', requestInfo, statusCode) | ||
} | ||
|
||
private parseResponseBody = (rawResBody: unknown, resType: XMLHttpRequestResponseType) => { | ||
switch (resType) { | ||
case 'arraybuffer': | ||
case 'blob': { | ||
// not parseable | ||
return undefined | ||
} | ||
case 'json': { | ||
return JSON.stringify(rawResBody) | ||
} | ||
case 'document': | ||
case 'text': | ||
default: { | ||
try { | ||
const bodyStr = rawResBody as string | ||
const parsed = bodyStr?.startsWith('svdata=') ? bodyStr.substring(7) : bodyStr | ||
JSON.parse(parsed) | ||
return parsed | ||
} catch (e) { | ||
return undefined | ||
} | ||
} | ||
} | ||
} | ||
|
||
private updateKanColleServer = (requestInfo: RequestInfo) => { | ||
const [, pathName, reqUrl] = requestInfo | ||
if (this.isKancolleGameApi(pathName)) { | ||
const { hostname } = URL.parse(reqUrl) | ||
if (hostname) { | ||
if (this.serverList[hostname]) { | ||
this.serverInfo = { | ||
...this.serverList[hostname], | ||
ip: hostname, | ||
} | ||
} else { | ||
this.serverInfo = { | ||
num: -1, | ||
name: '__UNKNOWN', | ||
ip: hostname, | ||
} | ||
} | ||
this.emit('kancolle.server.change', this.serverInfo) | ||
} | ||
} | ||
} | ||
|
||
private isKancolleGameApi = (pathname: PathName = ''): boolean => pathname.startsWith('/kcsapi') | ||
} | ||
|
||
export default new GameAPIBroadcaster() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters