├── .travis.yml ├── docs.jsig ├── MIGRATION.md ├── .gitignore ├── LICENSE ├── CONTRIBUTION.md ├── tchannel-types.js ├── package.json ├── thrift ├── transport.js ├── decoder.js └── simple-decoder.js ├── README.md ├── tchannel-tracker.js ├── bin └── tcap.js ├── frame-filters.js └── tchannel-session-tracker.js /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | before_install: npm i npm@latest -g 5 | script: npm run travis 6 | -------------------------------------------------------------------------------- /docs.jsig: -------------------------------------------------------------------------------- 1 | /* This is a jsig notation of your interface. 2 | https://github.com/jsigbiz/spec 3 | like tchannel_trace : (arg: Any) => void 4 | TODO 5 | */ 6 | -------------------------------------------------------------------------------- /MIGRATION.md: -------------------------------------------------------------------------------- 1 | # Migration 2 | 3 | Below you will find documentation on how to migrate from older 4 | versions of this module to newer versions. 5 | 6 | This document only describes what breaks and how to update your 7 | code. 8 | 9 | 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled source # 2 | ################### 3 | *.com 4 | *.class 5 | *.dll 6 | *.exe 7 | *.a 8 | *.o 9 | *.so 10 | *.node 11 | 12 | # Node Waf Byproducts # 13 | ####################### 14 | .lock-wscript 15 | build/ 16 | autom4te.cache/ 17 | 18 | # Node Modules # 19 | ################ 20 | # Better to let npm install these from the package.json defintion 21 | # rather than maintain this manually 22 | node_modules/ 23 | 24 | # Packages # 25 | ############ 26 | # it's better to unpack these files and commit the raw source 27 | # git has its own built in compression methods 28 | *.7z 29 | *.dmg 30 | *.gz 31 | *.iso 32 | *.jar 33 | *.rar 34 | *.tar 35 | *.zip 36 | 37 | # Logs and databases # 38 | ###################### 39 | *.log 40 | dump.rdb 41 | *.tap 42 | *.xml 43 | coverage/ 44 | 45 | # OS generated files # 46 | ###################### 47 | .DS_Store? 48 | .DS_Store 49 | ehthumbs.db 50 | Icon? 51 | Thumbs.db 52 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Uber 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /CONTRIBUTION.md: -------------------------------------------------------------------------------- 1 | # Want to contribute? 2 | 3 | Great! That's why this is an open source project. We use this project in our infrastructure at Uber, and we hope that it's useful to others as well. 4 | 5 | Before you get started, here are some suggestions: 6 | 7 | - Check open issues for what you want. 8 | - If there is an open issue, comment on it. Otherwise open an issue describing your bug or feature with use cases. 9 | - Before undertaking a major change, please discuss this on the issue. We'd hate to see you spend a lot of time working on something that conflicts with other goals or requirements that might not be obvious. 10 | - Write code to fix the problem, then open a pull request with tests and documentation. 11 | - The pull requests gets reviewed and then merged assuming there are no problems. 12 | - A new release version gets cut. 13 | 14 | ## Licencing 15 | 16 | - Every file must have a licence block at the top. This is enforced using `uber-licence` 17 | - If you contribute to a file in this project and are not an Uber employee, then you should 18 | add your name to the copyright section of the licence file. 19 | - Work that you contribute must be your own. 20 | 21 | ## Releases 22 | 23 | Declaring formal releases requires peer review. 24 | 25 | - A reviewer of a pull request should recommend a new version number (patch, minor or major). 26 | - Once your change is merged feel free to bump the version as recommended by the reviewer. 27 | - A new version number should not be cut without peer review unless done by the project maintainer. 28 | 29 | ### Cutting a new version 30 | 31 | - Get your branch merged on master 32 | - Run `npm version major` or `npm version minor` or `npm version patch` 33 | - `git push origin master --tags` 34 | - If you are a project owner, then `npm publish` 35 | -------------------------------------------------------------------------------- /tchannel-types.js: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015 Uber Technologies, Inc. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | 21 | 'use strict'; 22 | 23 | var frameNameByType = []; 24 | frameNameByType[0x01] = 'init request'; 25 | frameNameByType[0x02] = 'init response'; 26 | frameNameByType[0x03] = 'call request'; 27 | frameNameByType[0x04] = 'call response'; 28 | frameNameByType[0x13] = 'request continue'; 29 | frameNameByType[0x14] = 'response continue'; 30 | frameNameByType[0xc0] = 'cancel'; 31 | frameNameByType[0xc1] = 'claim'; 32 | frameNameByType[0xd0] = 'ping request'; 33 | frameNameByType[0xd1] = 'ping response'; 34 | frameNameByType[0xff] = 'error'; 35 | module.exports.FrameNameByType = frameNameByType; 36 | 37 | module.exports.ResponseType = { 38 | 'Ok': 0x00, 39 | 'NotOk': 0x01 40 | }; 41 | 42 | var responseNameByType = []; 43 | responseNameByType[0x00] = 'Ok'; 44 | responseNameByType[0x01] = 'NotOk'; 45 | module.exports.ResponseNameByType = responseNameByType; 46 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tcap", 3 | "version": "5.6.1", 4 | "description": "pcap analyzer for tchannel connections", 5 | "keywords": [], 6 | "author": "Kris Kowal ", 7 | "repository": "git://github.com/uber/tcap.git", 8 | "main": "index.js", 9 | "bin": { 10 | "tcap": "./bin/tcap.js" 11 | }, 12 | "homepage": "https://github.com/uber/tcap", 13 | "bugs": { 14 | "url": "https://github.com/uber/tcap/issues", 15 | "email": "kris@uber.com" 16 | }, 17 | "contributors": [ 18 | { 19 | "name": "Kris Kowal" 20 | } 21 | ], 22 | "dependencies": { 23 | "bufrw": "^0.9.20", 24 | "chalk": "^1.0.0", 25 | "commander": "^2.6.0", 26 | "hexer": "^1.4.5", 27 | "pcap": "^2.0.1", 28 | "sprintf-js": "^1.0.2", 29 | "tchannel": "^3.5.19", 30 | "thriftrw": "^3.4.3" 31 | }, 32 | "devDependencies": { 33 | "coveralls": "^2.10.0", 34 | "istanbul": "^0.3.5", 35 | "itape": "^1.5.0", 36 | "lint-trap": "^1.0.0", 37 | "opn": "^1.0.1", 38 | "tape": "^3.4.0", 39 | "uber-licence": "^1.2.0" 40 | }, 41 | "licenses": [ 42 | { 43 | "type": "MIT", 44 | "url": "http://github.com/uber/tcap/raw/master/LICENSE" 45 | } 46 | ], 47 | "scripts": { 48 | "add-licence": "uber-licence", 49 | "check-cover": "istanbul check-coverage --branches=100 --lines=100 --functions=100", 50 | "check-licence": "uber-licence --dry", 51 | "check-ls": "npm ls 1>/dev/null", 52 | "cover": "npm run test-cover -s && npm run check-cover -s", 53 | "lint": "lint-trap .", 54 | "test": "npm run check-ls -s", 55 | "test-cover": "istanbul cover --report html --print detail -- test/index.js", 56 | "trace": "itape test/index.js --trace", 57 | "travis": "npm run cover -s && istanbul report lcov && ((cat coverage/lcov.info | coveralls) || exit 0)", 58 | "view-cover": "opn ./coverage/index.html" 59 | }, 60 | "engines": { 61 | "node": ">= 0.10.x" 62 | }, 63 | "pre-commit": [ 64 | "check-licence", 65 | "test" 66 | ], 67 | "pre-commit.silent": true, 68 | "itape": { 69 | "trace": { 70 | "debuglog": [ 71 | "tcap" 72 | ], 73 | "leakedHandles": { 74 | "timeout": 5001, 75 | "debugSockets": true 76 | }, 77 | "formatStack": true 78 | } 79 | }, 80 | "uber-ngen-version": "5.0.0" 81 | } 82 | -------------------------------------------------------------------------------- /thrift/transport.js: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015 Uber Technologies, Inc. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | 21 | 'use strict'; 22 | 23 | var assert = require('assert'); 24 | var Buffer = require('buffer').Buffer; 25 | 26 | function ReadBuffer(buf) { 27 | assert(buf instanceof Buffer); 28 | this.buf = buf; 29 | this.begin = 0; 30 | } 31 | 32 | ReadBuffer.prototype.ensure = function ensure(size) { 33 | if (this.begin + size > this.buf.length) { 34 | throw new Error('insufficient buffer', { 35 | size: size, 36 | remaining: this.buf.length - this.begin 37 | }); 38 | } 39 | }; 40 | 41 | ReadBuffer.prototype.readByte = function readByte() { 42 | var data = this.buf.readInt8(this.begin); 43 | this.begin++; 44 | return data; 45 | }; 46 | 47 | ReadBuffer.prototype.readI16 = function readI16() { 48 | var data = this.buf.readInt16BE(this.begin); 49 | this.begin += 2; 50 | return data; 51 | }; 52 | 53 | ReadBuffer.prototype.readI32 = function readI32() { 54 | var data = this.buf.readInt32BE(this.begin); 55 | this.begin += 4; 56 | return data; 57 | }; 58 | 59 | ReadBuffer.prototype.readDouble = function readDouble() { 60 | var data = this.buf.readDoubleBE(this.begin); 61 | this.begin += 8; 62 | return data; 63 | }; 64 | 65 | ReadBuffer.prototype.readString = function readString(size) { 66 | assert(typeof size === 'number'); 67 | this.ensure(size); 68 | 69 | var data = this.buf.slice(this.begin, this.begin + size); 70 | this.begin += size; 71 | return data; 72 | }; 73 | 74 | ReadBuffer.prototype.eom = function eom() { 75 | return this.begin === this.buf.length; 76 | }; 77 | 78 | module.exports.ReadBuffer = ReadBuffer; 79 | -------------------------------------------------------------------------------- /thrift/decoder.js: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015 Uber Technologies, Inc. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | 21 | 'use strict'; 22 | var ansi = require('chalk'); 23 | var fs = require('fs'); 24 | var path = require('path'); 25 | var Thrift = require('thriftrw').Thrift; 26 | var TStructRW = require('thriftrw/tstruct').TStructRW; 27 | 28 | var simpleDecoder = require('./simple-decoder'); 29 | 30 | var functions = {}; 31 | 32 | function setup(dir) { 33 | if (!dir) { 34 | return; 35 | } 36 | 37 | var files = fs.readdirSync(dir); 38 | var fileRead = []; 39 | files.forEach(function eachFile(file) { 40 | var match = /([^\/]+)\.thrift$/.exec(file); 41 | if (match) { 42 | var thrift = new Thrift({ 43 | entryPoint: path.join(dir, file), 44 | allowFilesystemAccess: true 45 | }); 46 | fileRead.push(match[0]); 47 | Object.keys(thrift.services).forEach(function each(serviceName) { 48 | var service = thrift.models[serviceName]; 49 | service.functions.forEach(function (f) { 50 | functions[f.fullName] = f; 51 | }); 52 | }); 53 | } 54 | }); 55 | 56 | if (fileRead.length !== 0) { 57 | console.log('Thrift IDLS read: ' + fileRead.join(', ')); 58 | console.log('Thrift procedures found: ' + Object.keys(functions).join(', ')); 59 | } else { 60 | console.log(ansi.red( 61 | ansi.bold('Warning: no thrift IDL file read from ' + dir))); 62 | } 63 | } 64 | 65 | function decode(arg3, arg1, direction) { 66 | if (!arg1 || !direction) { 67 | return simpleDecode(arg3); 68 | } 69 | 70 | var thrift = functions[arg1]; 71 | if (!thrift) { 72 | return simpleDecode(arg3); 73 | } 74 | 75 | var rw = direction === 'outgoing' ? thrift.args.rw : thrift.result.rw; 76 | var result = rw.fromBuffer(arg3); 77 | if (result.err) { 78 | console.log(ansi.red( 79 | ansi.bold('thrift error: could not decode with given IDL: ' + result.err))); 80 | return simpleDecode(arg3); 81 | } 82 | return result.value; 83 | 84 | } 85 | 86 | function simpleDecode(arg3) { 87 | return simpleDecoder.decode(arg3); 88 | // TODO use thriftrw instead and delete the special decoder 89 | } 90 | 91 | module.exports.setup = setup; 92 | module.exports.decode = decode; 93 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tcap 2 | 3 | 8 | 9 | 10 | 11 | Uses pcap to inspect tchannel traffic over a network interface. 12 | 13 | ``` 14 | Usage: tcap [options] 15 | 16 | Options: 17 | 18 | -h, --help output usage information 19 | -V, --version output the version number 20 | -i --interface network interface interfaces (defaults to first with an address) 21 | -p --port a port to track or use "port1-port2" for a range of ports to track between port1 and port2 22 | -f --filter packet filter in pcap-filter(7) syntax (default: all TCP packets on port 4040) 23 | -s --service service name or names to show (default: all services shown), or 24 | use "~service-name" to exclude the service 25 | -t --thrift path of the directory for thrift spec files 26 | -1 --arg1 arg1 method or methods to show (default: all arg1 methods shown), or 27 | use "~arg1-method" to exclude the arg1 28 | --m1 show arg1 name in call responses 29 | -r --response responses to show: O[K], N[otOk], E[rror] (default: all shown) 30 | -b --buffer-size size in MiB to buffer between libpcap and app (default: 10) 31 | -x --hex show hex dumps for all packets 32 | --inspect show JSON dumps for all parsed frames 33 | --color enables colors if not connected to a tty. 34 | --no-color disables colors if connected to a tty. 35 | ``` 36 | 37 | ## Example 38 | 39 | To monitor tchannel traffic on ports 4040 and 4041 over the loopback and first 40 | ethernet interface on a Mac: 41 | 42 | ``` 43 | tcap -i lo0 -i en0 -p 4040 -p 4041 44 | ``` 45 | 46 | Note that the interface names differ on other systems. Use ifconfig. 47 | 48 | 68 | 69 | ## Installation 70 | 71 | On Linux, ensure that the libpcap headers are available to the build toolchain. On Debian or Ubuntu: 72 | 73 | `sudo apt-get install libpcap-dev` 74 | 75 | Install tcap either in your project, or globally as depicted: 76 | 77 | `npm install uber/tcap -g` 78 | 79 | ## Tests 80 | 81 | `npm test` 82 | 83 | ## NPM scripts 84 | 85 | - `npm run add-licence` This will add the licence headers. 86 | - `npm run cover` This runs the tests with code coverage 87 | - `npm run lint` This will run the linter on your code 88 | - `npm test` This will run the tests. 89 | - `npm run trace` This will run your tests in tracing mode. 90 | - `npm run travis` This is run by travis.CI to run your tests 91 | - `npm run view-cover` This will show code coverage in a browser 92 | 93 | ## Contributors 94 | 95 | - kriskowal 96 | - shannili 97 | 98 | ## MIT Licenced 99 | 100 | [build-png]: https://secure.travis-ci.org/uber/tcap.png 101 | [build]: https://travis-ci.org/uber/tcap 102 | [cover-png]: https://coveralls.io/repos/uber/tcap/badge.png 103 | [cover]: https://coveralls.io/r/uber/tcap 104 | [dep-png]: https://david-dm.org/uber/tcap.png 105 | [dep]: https://david-dm.org/uber/tcap 106 | [test-png]: https://ci.testling.com/uber/tcap.png 107 | [tes]: https://ci.testling.com/uber/tcap 108 | [npm-png]: https://nodei.co/npm/tcap.png?stars&downloads 109 | [npm]: https://nodei.co/npm/tcap 110 | -------------------------------------------------------------------------------- /thrift/simple-decoder.js: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015 Uber Technologies, Inc. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | 21 | 'use strict'; 22 | 23 | var ReadBuffer = require('./transport').ReadBuffer; 24 | 25 | var TYPE = { 26 | STOP: 0, 27 | VOID: 1, 28 | BOOL: 2, 29 | BYTE: 3, 30 | DOUBLE: 4, 31 | I16: 6, 32 | I32: 8, 33 | I64: 10, 34 | STRING: 11, 35 | STRUCT: 12, 36 | MAP: 13, 37 | SET: 14, 38 | LIST: 15 39 | }; 40 | 41 | function readString(reader) { 42 | var size = reader.readI32(); 43 | return reader.readString(size).toString('utf8'); 44 | } 45 | 46 | function readMap(reader) { 47 | var result = {}; 48 | var ktypeid = reader.readByte(); 49 | var vtypeid = reader.readByte(); 50 | var size = reader.readI32(); 51 | for (var i = 0; i < size; i++) { 52 | var key = read(reader, ktypeid); 53 | var val = read(reader, vtypeid); 54 | result[key] = val; 55 | } 56 | return result; 57 | } 58 | 59 | function readList(reader) { 60 | var result = []; 61 | var etypeid = reader.readByte(); 62 | var size = reader.readI32(); 63 | for (var i = 0; i < size; i++) { 64 | var ele = read(reader, etypeid); 65 | result.push(ele); 66 | } 67 | return result; 68 | } 69 | 70 | function readStruct(reader) { 71 | /* eslint no-constant-condition:[0] */ 72 | var result = {}; 73 | while (true) { 74 | var ftypeid = reader.readByte(); 75 | if (ftypeid === TYPE.STOP) { 76 | return result; 77 | } 78 | var fid = reader.readI16(); 79 | var field = read(reader, ftypeid); 80 | result[fid] = field; 81 | } 82 | return result; 83 | } 84 | 85 | function read(reader, typeid) { 86 | /* eslint complexity:[2, 20] */ 87 | switch (typeid) { 88 | case TYPE.BOOL: 89 | case TYPE.BYTE: 90 | return reader.readByte(); 91 | case TYPE.I16: 92 | return reader.readI16(); 93 | case TYPE.I32: 94 | return reader.readI32(); 95 | case TYPE.I64: 96 | return reader.readString(8); 97 | case TYPE.DOUBLE: 98 | return reader.readDouble(); 99 | case TYPE.STRING: 100 | return readString(reader); 101 | case TYPE.STRUCT: 102 | return readStruct(reader); 103 | case TYPE.MAP: 104 | return readMap(reader); 105 | case TYPE.LIST: 106 | case TYPE.SET: 107 | return readList(reader); 108 | default: 109 | throw new Error('invalid type', {typeid: typeid}); 110 | } 111 | } 112 | 113 | function decode(buf) { 114 | var reader = new ReadBuffer(buf); 115 | var struct = read(reader, TYPE.STRUCT); 116 | if (!reader.eom()) { 117 | throw new Error('more data after struct stops'); 118 | } 119 | return struct; 120 | } 121 | 122 | module.exports.read = read; 123 | module.exports.decode = decode; 124 | -------------------------------------------------------------------------------- /tchannel-tracker.js: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015 Uber Technologies, Inc. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | /*global console*/ 21 | /*eslint no-console:0 max-statements: [1, 30]*/ 22 | 23 | 'use strict'; 24 | 25 | var pcap = require('pcap'); 26 | var util = require('util'); 27 | var ansi = require('chalk'); 28 | var events = require('events'); 29 | var sprintf = require('sprintf-js').sprintf; 30 | var TChannelSessionTracker = require('./tchannel-session-tracker'); 31 | 32 | module.exports = TChannelTracker; 33 | function TChannelTracker(opts) { 34 | var self = this; 35 | events.EventEmitter.call(self, opts); 36 | 37 | self.pcapFilter = opts.pcapFilter || 'ip proto \\tcp'; 38 | 39 | var ports = opts.ports.slice(); 40 | if (ports.length) { 41 | self.pcapFilter += ' and (' + portFilters(ports) + ')'; 42 | } else if (!/\bport\b/.test(self.pcapFilter)) { 43 | self.pcapFilter += ' and port 4040'; 44 | } 45 | 46 | self.interfaces = opts.interfaces; // e.g., ['en0'] 47 | self.alwaysShowFrameDump = opts.alwaysShowFrameDump; 48 | self.alwaysShowHex = opts.alwaysShowHex; 49 | self.bufferSize = opts.bufferSize; // in bytes 50 | self.nextSessionNumber = 0; 51 | 52 | self.filters = opts.filters; 53 | self.color = opts.color 54 | } 55 | 56 | function portFilters(ports) { 57 | var filter = ''; 58 | for (var i = 0; i < ports.length; i++) { 59 | var port = ports[i]; 60 | if (port.indexOf('-') === -1) { 61 | filter += 'port ' + port; 62 | } else { 63 | filter += 'portrange ' + port; 64 | } 65 | 66 | if (i !== ports.length - 1) { 67 | filter += ' or '; 68 | } 69 | } 70 | 71 | return filter; 72 | } 73 | 74 | util.inherits(TChannelTracker, events.EventEmitter); 75 | 76 | TChannelTracker.prototype.listen = function listen() { 77 | var self = this; 78 | var listeners = {}; 79 | self.interfaces.forEach(listenOnInterface); 80 | function listenOnInterface(iface) { 81 | var tcpTracker = new pcap.TCPTracker(); 82 | var pcapSession = pcap.createSession( 83 | iface, 84 | self.pcapFilter, 85 | self.bufferSize 86 | ); 87 | pcapSession.on('packet', function handleTcpPacket(rawPacket) { 88 | var packet = pcap.decode.packet(rawPacket); 89 | tcpTracker.track_packet(packet); 90 | }); 91 | tcpTracker.on('session', function handleTcpSession(tcpSession) { 92 | self.handleTcpSession(tcpSession, iface); 93 | }); 94 | listeners[iface] = { 95 | tcpTracker: tcpTracker, 96 | pcapSession: pcapSession 97 | }; 98 | console.log( 99 | ansi.cyan('listening on interface %s with filter %s'), 100 | pcapSession.device_name, 101 | self.pcapFilter 102 | ); 103 | } 104 | }; 105 | 106 | TChannelTracker.prototype.handleTcpSession = 107 | function handleTcpSession(tcpSession, iface) { 108 | var self = this; 109 | var sessionNumber = self.nextSessionNumber++; 110 | 111 | console.log(ansi.cyan(sprintf( 112 | 'ts=%10.03f session=%s %s src=%s --> dst=%s on %s', 113 | Date.now() / 1000.0, 114 | sessionNumber, 115 | (tcpSession.missed_syn ? 'in progress' : 'started'), 116 | tcpSession.src, 117 | tcpSession.dst, 118 | iface 119 | ))); 120 | 121 | var filterHandle = {}; 122 | 123 | var incomingSessionTracker = new TChannelSessionTracker({ 124 | sessionNumber: sessionNumber, 125 | direction: 'incoming', 126 | onTrack: !tcpSession.missed_syn, 127 | tcpSession: tcpSession, 128 | alwaysShowFrameDump: self.alwaysShowFrameDump, 129 | alwaysShowHex: self.alwaysShowHex, 130 | filterInstance: {handle: filterHandle, filters: self.filters}, 131 | color: self.color 132 | }); 133 | 134 | var outgoingSessionTracker = new TChannelSessionTracker({ 135 | sessionNumber: sessionNumber, 136 | direction: 'outgoing', 137 | onTrack: !tcpSession.missed_syn, 138 | tcpSession: tcpSession, 139 | alwaysShowFrameDump: self.alwaysShowFrameDump, 140 | alwaysShowHex: self.alwaysShowHex, 141 | filterInstance: {handle: filterHandle, filters: self.filters}, 142 | color: self.color 143 | }); 144 | 145 | tcpSession.on('data send', handleDataSend); 146 | function handleDataSend(session, chunk) { 147 | outgoingSessionTracker.handlePacket(chunk); 148 | } 149 | 150 | tcpSession.on('data recv', handleDataRecv); 151 | function handleDataRecv(session, chunk) { 152 | incomingSessionTracker.handlePacket(chunk); 153 | } 154 | 155 | tcpSession.once('end', handleSessionEnd); 156 | function handleSessionEnd(session) { 157 | console.log(ansi.cyan(sprintf( 158 | 'ts=%10.03f session=%s ended src=%s --> dst=%s on %s', 159 | Date.now() / 1000.0, 160 | sessionNumber, 161 | tcpSession.src, 162 | tcpSession.dst, 163 | iface 164 | ))); 165 | 166 | tcpSession.removeListener('data send', handleDataSend); 167 | tcpSession.removeListener('data recv', handleDataRecv); 168 | incomingSessionTracker.end(); 169 | outgoingSessionTracker.end(); 170 | } 171 | }; 172 | -------------------------------------------------------------------------------- /bin/tcap.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | // Copyright (c) 2015 Uber Technologies, Inc. 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy 5 | // of this software and associated documentation files (the "Software"), to deal 6 | // in the Software without restriction, including without limitation the rights 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | // copies of the Software, and to permit persons to whom the Software is 9 | // furnished to do so, subject to the following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included in 12 | // all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | // THE SOFTWARE. 21 | 'use strict'; 22 | /*global process, console*/ 23 | /*eslint no-console:0*/ 24 | 25 | var commander = require('commander'); 26 | var TChannelTracker = require('../tchannel-tracker.js'); 27 | var ansi = require('chalk'); 28 | var TChannelFrame = require('tchannel/v2/index'); 29 | var FrameTypes = TChannelFrame.Types; 30 | var TchannelTypes = require('../tchannel-types'); 31 | var ResponseType = TchannelTypes.ResponseType; 32 | var FrameFilters = require('../frame-filters'); 33 | var thriftDecoder = require('../thrift/decoder.js'); 34 | 35 | module.exports = main; 36 | 37 | if (require.main === module) { 38 | main(process.argv); 39 | } 40 | 41 | function main(argv) { 42 | 43 | function collect(x, xs) { 44 | xs.push(x); 45 | return xs; 46 | } 47 | 48 | var newline = '\n '; 49 | commander.version(require('../package.json').version) 50 | .option('-i --interface ', 51 | 'network interface interfaces ' + 52 | '(defaults to first with an address)', collect, []) 53 | .option('-p --port ', 54 | 'a port to track or use "port1-port2" for a range of ports to track between port1 and port2', collect, []) 55 | .option('-f --filter ', 56 | 'packet filter in pcap-filter(7) syntax ' + 57 | '(default: all TCP packets on port 4040)') 58 | .option('-s --service ', 59 | 'service name or names to show (default: all services shown), or' + newline + 60 | 'use "~service-name" to exclude the service', collect, []) 61 | .option('-t --thrift ', 62 | 'path of the directory for thrift spec files') 63 | .option('-1 --arg1 ', 64 | 'arg1 method or methods to show (default: all arg1 methods shown), or' + newline + 65 | 'use "~arg1-method" to exclude the arg1', collect, []) 66 | .option('--m1', 67 | 'show arg1 name in call responses') 68 | .option('-r --response ', 69 | 'responses to show: O[K], N[otOk], E[rror] ' + 70 | '(default: all shown)', collect, []) 71 | .option('-b --buffer-size ', 72 | 'size in MiB to buffer between libpcap and app ' + 73 | '(default: 10)') 74 | .option('-x --hex', 75 | 'show hex dumps for all packets') 76 | .option('--inspect', 77 | 'show JSON dumps for all parsed frames') 78 | // handled by chalk module: 79 | .option('--color', 80 | 'enables colors if not connected to a tty.') 81 | .option('--no-color', 82 | 'disables colors if connected to a tty.') 83 | .parse(argv); 84 | 85 | var bufferSizeMb = commander.bufferSize || 10; 86 | 87 | checkUid(); 88 | 89 | thriftDecoder.setup(commander.thrift); 90 | 91 | var tracker = new TChannelTracker({ 92 | interfaces: commander.interface.length ? commander.interface : [''], 93 | ports: commander.port, 94 | pcapFilter: commander.filter, 95 | filters: registerFilters(commander), 96 | alwaysShowFrameDump: commander.inspect, 97 | alwaysShowHex: commander.hex, 98 | bufferSize: bufferSizeMb * 1024 * 1024, 99 | color: commander.color 100 | }); 101 | tracker.listen(); 102 | } 103 | 104 | function checkUid() { 105 | if (process.getuid() !== 0) { 106 | console.log(ansi.red(ansi.bold('Warning: not running with root privs,' + 107 | ' which are usually required for raw packet capture.'))); 108 | console.log(ansi.red('Trying to open anyway...')); 109 | } 110 | } 111 | 112 | function registerFilters(options) { 113 | var filters = new FrameFilters(); 114 | 115 | if (options.thrift) { 116 | // thrift depends on m1 117 | options.m1 = true; 118 | } 119 | 120 | // this order of registration matters 121 | filters.register('serviceName', 122 | options.service.length ? options.service : null); 123 | filters.register('arg1', 124 | options.arg1.length ? options.arg1 : null); 125 | filters.register('response', 126 | checkResponse(options.response)); 127 | filters.register('arg1Matcher', options.m1); 128 | 129 | return filters; 130 | } 131 | 132 | function checkResponse(response) { 133 | response = response.length ? response : null; 134 | if (!response) { 135 | return null; 136 | } 137 | 138 | var res = []; 139 | response.forEach(function createRSTable(name) { 140 | switch (name.toLowerCase()) { 141 | case 'o': 142 | case 'ok': 143 | res[ResponseType.Ok] = 'Ok'; 144 | break; 145 | case 'n': 146 | case 'notok': 147 | res[ResponseType.NotOk] = 'NotOk'; 148 | break; 149 | case 'e': 150 | case 'error': 151 | res[FrameTypes.ErrorResponse] = 'Error'; 152 | break; 153 | default: 154 | console.log(ansi.red( 155 | ansi.bold('Warning: wrong response status ' + 156 | 'in command options: \'-r ' + 157 | name + '\''))); 158 | break; 159 | } 160 | }); 161 | 162 | return res; 163 | } 164 | -------------------------------------------------------------------------------- /frame-filters.js: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015 Uber Technologies, Inc. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | /*global console*/ 21 | /*eslint no-console:0 max-statements: [1, 30]*/ 22 | 23 | 'use strict'; 24 | var assert = require('assert'); 25 | 26 | // 27 | // TODO 28 | // 1. handle the conflicts between filters 29 | // 2. add filter on arg2 and arg3 30 | // 3. add option to allow customizing the sizes of arg2 and arg3 being shown 31 | // 32 | var TChannelFrame = require('tchannel/v2/index'); 33 | var FrameTypes = TChannelFrame.Types; 34 | 35 | 36 | function inclusiveValidate(filters, filterName) { 37 | var inclusive; 38 | var exclusive; 39 | for (var i = 0; i < filters.length; i++) { 40 | var name = filters[i]; 41 | if (name.indexOf('~') === 0) { 42 | exclusive = true; 43 | } else { 44 | inclusive = true; 45 | } 46 | 47 | assert(!exclusive || !inclusive, filterName + ' cannot have a mix of inclusives and exclusives'); 48 | } 49 | 50 | return inclusive; 51 | } 52 | 53 | module.exports = TChannelFrameFilters; 54 | function TChannelFrameFilters() { 55 | var self = this; 56 | 57 | self.filters = {}; 58 | self.filters.serviceName = new ServicerNameFilter(); 59 | self.filters.arg1 = new Arg1Filter(); 60 | self.filters.arg1Matcher = new Arg1Matcher(); 61 | self.filters.response = new ResponseFilter(); 62 | 63 | self.usedFilters = []; 64 | } 65 | 66 | TChannelFrameFilters.prototype.register = function register(name, filter) { 67 | var self = this; 68 | 69 | var handler = self.filters[name]; 70 | if (!handler) { 71 | return; 72 | } 73 | 74 | if (handler.take(filter)) { 75 | self.usedFilters.push(handler); 76 | } 77 | }; 78 | 79 | TChannelFrameFilters.prototype.apply = function apply(handle, frame) { 80 | var self = this; 81 | 82 | if ((!frame || !frame.body || !frame.body.type) && 83 | self.usedFilters.length > 0) { 84 | return false; 85 | } 86 | 87 | return self.usedFilters.every( 88 | function applyFilter(handler) { 89 | return handler.process(handle, frame); 90 | }); 91 | }; 92 | 93 | TChannelFrameFilters.prototype.count = function count() { 94 | var self = this; 95 | return self.usedFilters.length; 96 | }; 97 | 98 | function shouldRemove(frame) { 99 | if ((frame.body.type === FrameTypes.CallResponse) || 100 | (frame.body.type === FrameTypes.CallResponseCont && !frame.flag)) { 101 | return true; 102 | } 103 | 104 | return false; 105 | } 106 | 107 | // 108 | // the filter on service name 109 | // 110 | function ServicerNameFilter() {} 111 | 112 | ServicerNameFilter.prototype.take = function take(filter) { 113 | var self = this; 114 | 115 | if (!filter || !filter.length) { 116 | return false; 117 | } else { 118 | self.serviceNames = filter; 119 | 120 | // validate 121 | self.inclusive = inclusiveValidate(self.serviceNames, 'Service name filters'); 122 | return true; 123 | } 124 | }; 125 | 126 | ServicerNameFilter.prototype.process = function process(handle, frame) { 127 | var self = this; 128 | var inclusive = !!self.inclusive; 129 | var serviceName = frame.body.service; 130 | if (serviceName && !inclusive) { 131 | serviceName = '~' + serviceName; 132 | } 133 | 134 | if (!handle.serviceName) { 135 | handle.serviceName = {}; 136 | } 137 | 138 | var ids = handle.serviceName; 139 | if (!serviceName) { 140 | if (!ids[frame.id]) { 141 | return !inclusive; 142 | } 143 | 144 | if (shouldRemove(frame)) { 145 | delete ids[frame.id]; 146 | } 147 | 148 | return inclusive; 149 | } 150 | 151 | if (self.serviceNames.indexOf(serviceName) < 0) { 152 | return !inclusive; 153 | } 154 | 155 | ids[frame.id] = true; 156 | 157 | return inclusive; 158 | }; 159 | 160 | // 161 | // the filter on arg1 162 | // 163 | function Arg1Filter() {} 164 | 165 | Arg1Filter.prototype.take = function take(filter) { 166 | var self = this; 167 | 168 | if (!filter || !filter.length) { 169 | return false; 170 | } else { 171 | self.arg1Methods = {}; 172 | self.inclusive = inclusiveValidate(filter, 'Arg1 filters'); 173 | filter.forEach(function arr2obj(name) { 174 | self.arg1Methods[name] = true; 175 | }); 176 | 177 | return true; 178 | } 179 | }; 180 | 181 | Arg1Filter.prototype.process = function process(handle, frame) { 182 | var self = this; 183 | var inclusive = !!self.inclusive; 184 | 185 | // arg1 only applies to the following types 186 | if (frame.body.type !== FrameTypes.CallRequest && 187 | frame.body.type !== FrameTypes.CallResponse && 188 | frame.body.type !== FrameTypes.CallRequestCont && 189 | frame.body.type !== FrameTypes.CallResponseCont) { 190 | return !inclusive; 191 | } 192 | 193 | if (!handle.arg1) { 194 | handle.arg1 = {}; 195 | } 196 | 197 | if (!frame.body.args) { 198 | // filter out frames not related 199 | return !inclusive; 200 | } 201 | 202 | var ids = handle.arg1; 203 | if (ids[frame.id]) { 204 | if (shouldRemove(frame)) { 205 | delete ids[frame.id]; 206 | } 207 | 208 | return inclusive; 209 | } 210 | 211 | var name = frame.body.args[0]; 212 | if (!inclusive) { 213 | name = '~' + name; 214 | } 215 | 216 | if (!self.arg1Methods[name]) { 217 | return !inclusive; 218 | } 219 | 220 | ids[frame.id] = true; 221 | 222 | return inclusive; 223 | }; 224 | 225 | // 226 | // the filter on responses 227 | // 228 | function ResponseFilter() {} 229 | 230 | ResponseFilter.prototype.take = function take(filter) { 231 | var self = this; 232 | 233 | if (!filter || !filter.length) { 234 | return false; 235 | } else { 236 | self.responseStatuses = filter; 237 | return true; 238 | } 239 | }; 240 | 241 | ResponseFilter.prototype.process = function process(handle, frame) { 242 | var self = this; 243 | if (!handle.responses) { 244 | handle.responses = []; 245 | } 246 | 247 | // we only track pairs starting with CallRequest for now. 248 | // we may need to consider other requests for error scenarios. 249 | if (frame.body.type === FrameTypes.CallRequest) { 250 | handle.responses[frame.id] = []; 251 | } else if (!handle.responses[frame.id]) { 252 | return false; 253 | } 254 | 255 | // push the frame into the array if it is a request or cont. 256 | if (frame.body.type === FrameTypes.CallRequest || 257 | frame.body.type === FrameTypes.CallRequestCont || 258 | frame.body.flag) { 259 | handle.responses[frame.id].push( 260 | handle.sessionTracker.handleFrameNoFilter(frame)); 261 | 262 | return false; 263 | } 264 | 265 | // if the status is tracked 266 | if (!self.responseStatuses[frame.body.type] && 267 | !(frame.body.code == null || self.responseStatuses[frame.body.code])) { 268 | delete handle.responses[frame.id]; 269 | return false; 270 | } 271 | 272 | // print frames 273 | handle.responses[frame.id] = handle.responses[frame.id] || []; 274 | handle.responses[frame.id].forEach(function log(str) { 275 | console.log(str); 276 | }); 277 | 278 | // return true for the last frame to be printed 279 | return true; 280 | }; 281 | 282 | // 283 | // the pair matcher on arg1 284 | // 285 | function Arg1Matcher() {} 286 | 287 | Arg1Matcher.prototype.take = function take(filter) { 288 | if (!filter) { 289 | return false; 290 | } 291 | 292 | return true; 293 | }; 294 | 295 | // should alway return true, since this isn't a filter 296 | Arg1Matcher.prototype.process = function process(handle, frame) { 297 | // arg1 only applies to the following types 298 | if (frame.body.type !== FrameTypes.CallRequest && 299 | frame.body.type !== FrameTypes.CallResponse && 300 | frame.body.type !== FrameTypes.CallRequestCont && 301 | frame.body.type !== FrameTypes.CallResponseCont) { 302 | return true; 303 | } 304 | 305 | if (!handle.arg1Matcher) { 306 | handle.arg1Matcher = {}; 307 | } 308 | 309 | if (!frame.body.args) { 310 | return true; 311 | } 312 | 313 | var ids = handle.arg1Matcher; 314 | if (ids[frame.id]) { 315 | frame.body.args[0] = ids[frame.id]; 316 | if (shouldRemove(frame)) { 317 | delete ids[frame.id]; 318 | } 319 | 320 | return true; 321 | } 322 | 323 | ids[frame.id] = frame.body.args[0]; 324 | 325 | return true; 326 | }; 327 | -------------------------------------------------------------------------------- /tchannel-session-tracker.js: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015 Uber Technologies, Inc. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | /*global console*/ 21 | /*eslint no-console:0 max-statements: [1, 30]*/ 22 | /*jscs:disable disallowKeywords*/ 23 | 24 | 'use strict'; 25 | 26 | var util = require('util'); 27 | var EOL = require('os').EOL; 28 | var ansi = require('chalk'); 29 | var stream = require('stream'); 30 | var hexer = require('hexer'); 31 | var sprintf = require('sprintf-js').sprintf; 32 | var thriftDecoder = require('./thrift/decoder'); 33 | var v2 = require('tchannel/v2'); 34 | var bufrw = require('bufrw'); 35 | var ChunkReader = require('bufrw/stream/chunk_reader'); 36 | var ErrorResponse = require('tchannel/v2/error_response'); 37 | var CodeErrors = ErrorResponse.CodeErrors; 38 | var TChannelFrame = require('tchannel/v2/index'); 39 | var FrameTypes = TChannelFrame.Types; 40 | var TchannelTypes = require('./tchannel-types'); 41 | var FrameNameByType = TchannelTypes.FrameNameByType; 42 | var ResponseNameByType = TchannelTypes.ResponseNameByType; 43 | 44 | module.exports = TChannelSessionTracker; 45 | 46 | function TChannelSessionTracker(opts) { 47 | var self = this; 48 | self.packetNumber = 0; 49 | self.sessionNumber = opts.sessionNumber; 50 | self.filterInstance = opts.filterInstance; 51 | self.filterInstance.handle.sessionTracker = self; 52 | self.direction = opts.direction; 53 | self.tcpSession = opts.tcpSession; 54 | self.alwaysShowFrameDump = opts.alwaysShowFrameDump; 55 | self.alwaysShowHex = opts.alwaysShowHex; 56 | self.color = opts.color; 57 | self.hexerOptions = opts.hexer || { 58 | prefix: ' ', 59 | gutter: 4, // maximum frame length is 64k so FFFF 60 | colored: self.color, 61 | nullHuman: ansi.black(ansi.bold('empty')), 62 | // headSep is neccessary to overwrite the hexer default 63 | headSep: ansi.cyan(': ') 64 | }; 65 | self.parser = null; 66 | self.speculative = false; 67 | if (opts.onTrack) { 68 | self.startTracking(false); 69 | } 70 | } 71 | 72 | TChannelSessionTracker.prototype.startTracking = 73 | function startTracking(speculative) { 74 | var self = this; 75 | self.buffer = new stream.PassThrough(); 76 | 77 | self.reader = new ChunkReader(bufrw.UInt16BE, v2.Frame.RW); 78 | 79 | self.buffer.pipe(self.reader); 80 | 81 | self.tracking = true; 82 | self.speculative = speculative; 83 | 84 | self.reader.on('data', handleFrame); 85 | function handleFrame(frame) { 86 | self.handleFrame(frame); 87 | } 88 | 89 | self.reader.on('error', handleError); 90 | function handleError(error) { 91 | self.handleError(error); 92 | } 93 | }; 94 | 95 | TChannelSessionTracker.prototype.stopTracking = 96 | function stopTracking() { 97 | var self = this; 98 | if (!self.tracking) { 99 | return; 100 | } 101 | self.buffer.end(); 102 | self.tracking = false; 103 | self.parser = null; 104 | self.buffer = null; 105 | }; 106 | 107 | TChannelSessionTracker.prototype.end = 108 | function end() { 109 | var self = this; 110 | self.stopTracking(); 111 | }; 112 | 113 | TChannelSessionTracker.prototype.handlePacket = 114 | function handlePacket(packet) { 115 | var self = this; 116 | if (self.alwaysShowHex) { 117 | console.log(ansi.cyan(sprintf( 118 | 'ts=%10.03f session=%d %s %s %s packet=%s', 119 | Date.now() / 1000.0, 120 | self.sessionNumber, 121 | self.tcpSession.src, 122 | (self.direction === 'outgoing' ? '-->' : '<--'), 123 | self.tcpSession.dst, 124 | self.packetNumber++ 125 | ))); 126 | console.log(hexer(packet, self.hexerOptions)); 127 | console.log(''); 128 | } 129 | if (!self.tracking) { 130 | self.handleUntrackedPacket(packet); 131 | } 132 | if (self.buffer) { 133 | self.buffer.write(packet); 134 | } 135 | }; 136 | 137 | TChannelSessionTracker.prototype.handleUntrackedPacket = 138 | function handleUntrackedPacket(packet) { 139 | var self = this; 140 | self.startTracking(true); 141 | }; 142 | 143 | TChannelSessionTracker.prototype.handleFrame = 144 | function handleFrame(frame) { 145 | var self = this; 146 | 147 | if (!self.filterInstance.filters.apply(self.filterInstance.handle, frame)) { 148 | // if this frame should be filtered out 149 | return; 150 | } 151 | 152 | console.log(self.handleFrameNoFilter(frame)); 153 | }; 154 | 155 | TChannelSessionTracker.prototype.handleFrameNoFilter = 156 | function handleFrameNoFilter(frame) { 157 | var self = this; 158 | var type = 159 | frame && 160 | frame.body && 161 | frame.body.type; 162 | var parts = []; 163 | parts.push(util.format(ansi.green(sprintf( 164 | 'ts=%10.03f session=%d %s %s %s frame=%d type=0x%02x%s%s', 165 | Date.now() / 1000.0, 166 | self.sessionNumber, 167 | self.tcpSession.src, 168 | (self.direction === 'outgoing' ? '-->' : '<--'), 169 | self.tcpSession.dst, 170 | frame && frame.id, 171 | type, 172 | (frame.body.type !== FrameTypes.CallResponse ? '' : 173 | ResponseNameByType[frame.body.code] ? 174 | ' ' + ResponseNameByType[frame.body.code] : 175 | ''), 176 | (self.speculative ? ansi.yellow(' ???') : '') 177 | )))); 178 | var showJson = self.alwaysShowFrameDump; 179 | parts = parts.concat(self.inspectCommonFrame(frame)); 180 | if (showJson) { 181 | parts.push(util.format(ansi.yellow('frame'))); 182 | parts.push(util.format(util.inspect(frame, {colors: ansi.enabled}))); 183 | } 184 | 185 | parts.push(''); 186 | return parts.join(EOL); 187 | }; 188 | 189 | TChannelSessionTracker.prototype.handleError = 190 | function handleError(error) { 191 | var self = this; 192 | 193 | if (self.filterInstance.filters.count() > 0) { 194 | return self.stopTracking(); 195 | } 196 | 197 | if (self.serviceNames && self.serviceNames.length > 0) { 198 | return self.stopTracking(); 199 | } 200 | 201 | console.log(ansi.red(sprintf( 202 | 'ts=%10.03f session=%d %s %s %s frame parse error', 203 | Date.now() / 1000.0, 204 | self.sessionNumber, 205 | self.tcpSession.src, 206 | (self.direction === 'outgoing' ? '-->' : '<--'), 207 | self.tcpSession.dst 208 | ))); 209 | console.log(bufrw.formatError(error, { 210 | color: true, 211 | markColor: function markColor(str) { 212 | return ansi.red.bold(str); 213 | }, 214 | hexerOptions: self.hexerOptions 215 | })); 216 | 217 | self.stopTracking(); 218 | }; 219 | 220 | TChannelSessionTracker.prototype.inspectCommonFrame = 221 | function inspectCommonFrame(frame) { 222 | var self = this; 223 | var parts = []; 224 | if (!frame.body) { 225 | parts.push(util.format(ansi.yellow(sprintf( 226 | 'unexpected shape' 227 | )))); 228 | // TODO unexpected frame shape 229 | return parts; 230 | } 231 | parts = parts.concat(self.inspectBanner(frame.body, frame)); 232 | parts = parts.concat(self.inspectHeaders(frame.body.headers)); 233 | parts = parts.concat(self.inspectTracing(frame.body.tracing)); 234 | parts = parts.concat(self.inspectMessage(frame.body.message)); 235 | parts = parts.concat(self.inspectBody(frame.body)); 236 | return parts; 237 | }; 238 | 239 | TChannelSessionTracker.prototype.inspectTracing = 240 | function inspectTracing(tracing) { 241 | if (tracing) { 242 | return [sprintf( 243 | 'tracing: spanid=%s parentid=%s traceid=%s flags=0x%02x', 244 | tracing.spanid.toString('hex'), 245 | tracing.parentid.toString('hex'), 246 | tracing.traceid.toString('hex'), 247 | tracing.flags 248 | )]; 249 | } 250 | 251 | return []; 252 | }; 253 | 254 | TChannelSessionTracker.prototype.inspectBanner = 255 | function inspectBanner(body, frame) { 256 | var self = this; 257 | if (!body) { 258 | // TODO 259 | return []; 260 | } 261 | var parts = []; 262 | 263 | // type, e.g., CALL REQUEST 264 | self.addFrameTypeName(parts, body); 265 | 266 | if (typeof frame.id === 'number') { 267 | parts.push(sprintf('id=0x%04x (%d)', frame.id, frame.id)); 268 | } 269 | 270 | // e.g., version=2 271 | if (body.version) { 272 | parts.push(sprintf('version=%d', body.version)); 273 | } 274 | 275 | self.addServiceName(parts, body); 276 | 277 | // e.g., flags=0x01 continued 278 | if (body.flags !== undefined) { 279 | parts.push(sprintf('flags=0x%02x', body.flags)); 280 | } 281 | if (body.flags) { 282 | if (body.flags & 0x01) { 283 | parts.push('continued'); 284 | } 285 | } 286 | 287 | // e.g., ttl=60 288 | if (body.ttl) { 289 | parts.push(sprintf('ttl=0x%04x (%d)', body.ttl, body.ttl)); 290 | } 291 | 292 | if (body.csum) { 293 | self.addCsum(parts, body.csum); 294 | } 295 | 296 | return [parts.join(' ')]; 297 | }; 298 | 299 | TChannelSessionTracker.prototype.addFrameTypeName = 300 | function addFrameTypeName(parts, body) { 301 | if (FrameNameByType[body.type]) { 302 | var suffix = ''; 303 | if (body.type === ErrorResponse.TypeCode) { 304 | if (CodeErrors[body.code]) { 305 | suffix = '[' + CodeErrors[body.code].codeName + ']'; 306 | } else { 307 | suffix = '[Unknown]'; 308 | } 309 | } 310 | parts.push(FrameNameByType[body.type].toUpperCase() + suffix); 311 | } else { 312 | parts.push(ansi.red(sprintf( 313 | 'UNRECOGNIZED FRAME TYPE %d', 314 | body.type 315 | ))); 316 | } 317 | }; 318 | 319 | TChannelSessionTracker.prototype.addServiceName = 320 | function addServiceName(parts, body) { 321 | // e.g., service="teapot" 322 | if (body.service !== undefined) { 323 | var service = JSON.stringify(body.service.toString('utf8')); 324 | if (!body.service.length) { 325 | service = ansi.red(service); 326 | } 327 | parts.push(sprintf('service=%s', service)); 328 | } 329 | }; 330 | 331 | TChannelSessionTracker.prototype.addCsum = 332 | function addCsum(parts, csum) { 333 | if (csum.type === 0x00) { 334 | parts.push(sprintf('csum=none')); 335 | } else if (csum.type === 0x01) { 336 | parts.push(sprintf('csum=0x%04x (crc32)', csum.val)); 337 | } else if (csum.type === 0x02) { 338 | parts.push(sprintf('csum=0x%04x (farmhash)', csum.val)); 339 | } 340 | }; 341 | 342 | TChannelSessionTracker.prototype.inspectHeaders = 343 | function inspectHeaders(headers) { 344 | var parts = []; 345 | if (!headers) { 346 | return parts; 347 | } 348 | var keys = Object.keys(headers); 349 | if (keys.length) { 350 | parts.push(ansi.yellow('headers')); 351 | keys.forEach(function eachKey(key) { 352 | parts.push(util.format(' %s: %s', 353 | ansi.yellow(key), headers[key])); 354 | }); 355 | } 356 | return parts; 357 | }; 358 | 359 | TChannelSessionTracker.prototype.inspectBody = 360 | function inspectBody(body) { 361 | var self = this; 362 | var parts = []; 363 | if (!body) { 364 | return parts; 365 | } 366 | if (body.args) { 367 | for (var i = 0; i < body.args.length; i++) { 368 | parts = parts.concat( 369 | self.inspectArgument('args[' + i + ']', body.args[i])); 370 | } 371 | } 372 | if (body.flags & 0x01) { 373 | parts.push(ansi.yellow('to be continued...')); 374 | } else if (body.args && body.args[2]) { 375 | // TODO argstream accumulate and parse 376 | var as = self.inspectThrift(body.args[2], body.args[0]); 377 | as = as || self.inspectJSON(body.args[2]); 378 | parts = parts.concat(as); 379 | } 380 | 381 | return parts; 382 | }; 383 | 384 | TChannelSessionTracker.prototype.inspectMessage = 385 | function inspectMessage(message) { 386 | var self = this; 387 | if (!message) { 388 | return []; 389 | } 390 | 391 | return self.inspectArgument('message', message); 392 | }; 393 | 394 | TChannelSessionTracker.prototype.inspectArgument = 395 | function inspectArgument(name, argument) { 396 | var self = this; 397 | var parts = []; 398 | if (!argument) { 399 | return parts; 400 | } 401 | parts.push(ansi.yellow(name)); 402 | parts.push(hexer(argument, self.hexerOptions)); 403 | 404 | return parts; 405 | }; 406 | 407 | TChannelSessionTracker.prototype.inspectThrift = 408 | function inspectThrift(buf, arg1) { 409 | var self = this; 410 | var data = thriftDecoder.decode(buf, arg1, self.direction); 411 | var parts = [ansi.yellow('arg3 as thrift')]; 412 | parts.push(util.inspect(data, {colors: self.color, depth: Infinity})); 413 | return parts; 414 | }; 415 | 416 | TChannelSessionTracker.prototype.inspectJSON = 417 | function inspectJSON(buf) { 418 | try { 419 | var data = JSON.parse(buf.toString('utf8')); 420 | var parts = [ansi.yellow('arg3 as json')]; 421 | parts.push(util.inspect(data, {colors: self.color, depth: Infinity})); 422 | return parts; 423 | } catch (e) { 424 | return []; 425 | } 426 | }; 427 | --------------------------------------------------------------------------------