抓包接口接入iridium前端
This commit is contained in:
1 parent
b1a42730df
commit
b7c2de923a
70 files changed
+9714
-113
No files matched your search
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
Source: https://github.com/pawitp/protobuf-decoder by pawitp
|
||||
Modified to adapt common-js
|
||||
*/
|
||||
const { decodeVarint } = require("./varintUtils");
|
||||
|
||||
class BufferReader {
|
||||
constructor(buffer) {
|
||||
this.buffer = buffer;
|
||||
this.offset = 0;
|
||||
}
|
||||
|
||||
readVarInt() {
|
||||
const result = decodeVarint(this.buffer, this.offset);
|
||||
this.offset += result.length;
|
||||
|
||||
return result.value;
|
||||
}
|
||||
|
||||
readBuffer(length) {
|
||||
this.checkByte(length);
|
||||
const result = this.buffer.slice(this.offset, this.offset + length);
|
||||
this.offset += length;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// gRPC has some additional header - remove it
|
||||
trySkipGrpcHeader() {
|
||||
const backupOffset = this.offset;
|
||||
|
||||
if (this.buffer[this.offset] === 0) {
|
||||
this.offset++;
|
||||
const length = this.buffer.readInt32BE(this.offset);
|
||||
this.offset += 4;
|
||||
|
||||
if (length > this.leftBytes()) {
|
||||
// Something is wrong, revert
|
||||
this.offset = backupOffset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
leftBytes() {
|
||||
return this.buffer.length - this.offset;
|
||||
}
|
||||
|
||||
checkByte(length) {
|
||||
const bytesAvailable = this.leftBytes();
|
||||
if (length > bytesAvailable) {
|
||||
throw new Error(
|
||||
"Not enough bytes left. Requested: " +
|
||||
length +
|
||||
" left: " +
|
||||
bytesAvailable
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
checkpoint() {
|
||||
this.savedOffset = this.offset;
|
||||
}
|
||||
|
||||
resetToCheckpoint() {
|
||||
this.offset = this.savedOffset;
|
||||
}
|
||||
}
|
||||
|
||||
const TYPES = {
|
||||
VARINT: 0,
|
||||
FIXED64: 1,
|
||||
STRING: 2,
|
||||
FIXED32: 5
|
||||
};
|
||||
|
||||
function decodeProto(buffer) {
|
||||
const reader = new BufferReader(buffer);
|
||||
const parts = [];
|
||||
|
||||
reader.trySkipGrpcHeader();
|
||||
|
||||
try {
|
||||
while (reader.leftBytes() > 0) {
|
||||
reader.checkpoint();
|
||||
|
||||
const indexType = parseInt(reader.readVarInt().toString());
|
||||
const type = indexType & 0b111;
|
||||
const index = indexType >> 3;
|
||||
|
||||
let value;
|
||||
if (type === TYPES.VARINT) {
|
||||
value = reader.readVarInt().toString();
|
||||
} else if (type === TYPES.STRING) {
|
||||
const length = parseInt(reader.readVarInt().toString());
|
||||
value = reader.readBuffer(length);
|
||||
} else if (type === TYPES.FIXED32) {
|
||||
value = reader.readBuffer(4);
|
||||
} else if (type === TYPES.FIXED64) {
|
||||
value = reader.readBuffer(8);
|
||||
} else {
|
||||
throw new Error("Unknown type: " + type);
|
||||
}
|
||||
|
||||
parts.push({
|
||||
index,
|
||||
type,
|
||||
value
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
reader.resetToCheckpoint();
|
||||
}
|
||||
|
||||
return {
|
||||
parts,
|
||||
leftOver: reader.readBuffer(reader.leftBytes())
|
||||
};
|
||||
}
|
||||
|
||||
function typeToString(type) {
|
||||
switch (type) {
|
||||
case TYPES.VARINT:
|
||||
return "varint";
|
||||
case TYPES.STRING:
|
||||
return "string";
|
||||
case TYPES.FIXED32:
|
||||
return "fixed32";
|
||||
case TYPES.FIXED64:
|
||||
return "fixed64";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
TYPES, decodeProto, typeToString
|
||||
}
|
||||
Reference in new issue
Block a user