Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TLS record fragmentation #176

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bin/gt.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ const { argv } = yargs
default: true,
})

.option('tls-record-fragmentation', {
type: 'boolean',
describe: 'enable TLS record fragmentation',
default: false
})

.example('$0')
.example('$0 --ip 127.0.0.1 --port 8000')
.example('$0 --dns-server https://doh.securedns.eu/dns-query')
Expand Down Expand Up @@ -128,6 +134,7 @@ async function main() {
port: argv['dns-port']
},
source: 'CLI',
'tlsRecordFragmentation': argv['tls-record-fragmentation']
});

const exitTrap = async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default async function handleHTTPS(clientSocket, firstChunk, proxy) {
// -- clientSocket --

clientSocket.once('data', clientHello => {
const chunks = bufferToChunks(clientHello, proxy.config.clientHelloMTU);
const chunks = bufferToChunks(clientHello, proxy.config.clientHelloMTU, proxy.config.tlsRecordFragmentation);
for (const chunk of chunks) {
logger.debug(`[HTTPS HELLO] ${url.host} (length: ${chunk.length})`);
tryWrite(serverSocket, chunk, close);
Expand Down
26 changes: 25 additions & 1 deletion src/utils/buffer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export function bufferToChunks(buffer, chunkSize) {
export function bufferToChunks(buffer, chunkSize, recordFragmentation) {
if(recordFragmentation) buffer=tlsRecordFragmentation(buffer, chunkSize)
const result = [];
const len = buffer.length;
let i = 0;
Expand All @@ -9,3 +10,26 @@ export function bufferToChunks(buffer, chunkSize) {

return result;
}
/**
* @param {Buffer} buffer
* @param {number} chunkSize
*/
function tlsRecordFragmentation(buffer, chunkSize) {
const list = []
const header = buffer.subarray(0,3)
const fullRecord = buffer.subarray(5)
const len = fullRecord.length
let i = 0

while (i < len) {
const record = fullRecord.subarray(i, i+chunkSize)
const recordLength = record.length
const buf = Buffer.alloc(recordLength+5)
header.copy(buf)
buf.writeUInt16BE(recordLength,3)
record.copy(buf,5)
list.push(buf)
i+=chunkSize
}
return Buffer.concat(list)
}