From c9763d67a9765c772e403092b1f5eb8779893aa3 Mon Sep 17 00:00:00 2001 From: TheLostTree <65834918+TheLostTree@users.noreply.github.com> Date: Sun, 31 Jul 2022 12:46:48 -0700 Subject: [PATCH] getName from type --- proxy.py | 36 ++++++++++++++++++++++++++++++++++++ src/ProtoFactory.ts | 31 +++++++++---------------------- src/index.ts | 2 +- 3 files changed, 46 insertions(+), 23 deletions(-) create mode 100644 proxy.py diff --git a/proxy.py b/proxy.py new file mode 100644 index 0000000..ad803c3 --- /dev/null +++ b/proxy.py @@ -0,0 +1,36 @@ +"""MITM script for Star Rail.""" +from mitmproxy import http +from mitmproxy import ctx +from mitmproxy.proxy import layer, layers + + +def load(loader): + # ctx.options.web_open_browser = False + # We change the connection strategy to lazy so that next_layer happens before we actually connect upstream. + ctx.options.connection_strategy = "lazy" + ctx.options.upstream_cert = False + ctx.options.ssl_insecure = True + + +def next_layer(nextlayer: layer.NextLayer): + ctx.log( + f"{nextlayer.context=}\n" + f"{nextlayer.data_client()[:70]=}\n" + ) + sni = nextlayer.context.client.sni + if nextlayer.context.client.tls and sni and (sni.endswith("yuanshen.com") or sni.endswith("mihoyo.com") or sni.endswith("hoyoverse.com") or sni.endswith("starrails.com")): + ctx.log('sni:' + sni) + nextlayer.context.server.address = ("127.0.0.1", 443) + + +def request(flow: http.HTTPFlow) -> None: + # flow.request.scheme = "http" + + # pretty_host takes the "Host" header of the request into account + if flow.request.pretty_url.startswith('http://log-upload-os.mihoyo.com'): + flow.response = http.Response.make( + 404, # (optional) status code + b"404 not found", # (optional) content + {"Content-Type": "text/html"} # (optional) headers + ) + return \ No newline at end of file diff --git a/src/ProtoFactory.ts b/src/ProtoFactory.ts index c754367..61dc307 100644 --- a/src/ProtoFactory.ts +++ b/src/ProtoFactory.ts @@ -18,6 +18,7 @@ class MessageType{ }; var messageTypeMap = new Map>(); +var messageTypeMapReversed = new Map, PacketName>(); function send,T>(type: Class, data: T){ console.log(type.encode(data).finish()) @@ -38,6 +39,10 @@ export default class ProtoFactory{ return messageTypeMap.get(name) as MessageType; } + static getName(type: MessageType){ + return messageTypeMapReversed.get(type) as PacketName; + } + static init() { //iterate over everything in types and check if they are a MessageType for (const key of Object.keys(types)) { @@ -45,6 +50,7 @@ export default class ProtoFactory{ if (isMessageType(value)) { if(Object.values(CmdID).includes(key)){ messageTypeMap.set(key as PacketName, value); + messageTypeMapReversed.set(value, key as PacketName); }else{ // there are some types that are not packets, but are still MessageType // you can figure out what you want to do with them here @@ -52,31 +58,12 @@ export default class ProtoFactory{ } } - c.log("Initialized with " + messageTypeMap.size + " types"); + c.debug("Initialized with " + messageTypeMap.size + " types"); + //c.log(this.getName(types.PlayerLoginScRsp)) return; - // example usages of send function - send(types.PlayerLoginScRsp, { - retcode: 0, - isRelay: false, - basicInfo: { - exp: 0, - level: 1, - hcoin: 0, - mcoin: 0, - nickname: "test", - scoin: 0, - stamina: 100, - worldLevel: 1, - }, - isNewPlayer: true, - stamina: 100, - curTimezone: 1, - loginRandom: 0, - serverTimestampMs: Math.round(new Date().getTime() / 1000), - bsBinVersion: "" - }); + //if you want a partial type send(types.PlayerLoginScRsp, types.PlayerLoginScRsp.fromPartial({ diff --git a/src/index.ts b/src/index.ts index 307e708..444d4ee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ import Interface from "./commands/Interface"; import HttpServer from "./http/HttpServer"; import SRServer from "./server/kcp/SRServer"; import Logger from "./util/Logger"; -import ProtoFactory from "./protoFactory" +import ProtoFactory from "./ProtoFactory" const c = new Logger("CrepeSR"); ProtoFactory.init();