getName from type

This commit is contained in:
TheLostTree 2022-07-31 12:46:48 -07:00
parent fb33fbc41f
commit c9763d67a9
3 changed files with 46 additions and 23 deletions

36
proxy.py Normal file
View File

@ -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

View File

@ -18,6 +18,7 @@ class MessageType<T>{
};
var messageTypeMap = new Map<PacketName, MessageType<any>>();
var messageTypeMapReversed = new Map<MessageType<any>, PacketName>();
function send<Class extends MessageType<T>,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<any>;
}
static getName(type: MessageType<any>){
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({

View File

@ -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();