From 6df7d7b3c36e50dc3954a333d6f86622ee31d5c4 Mon Sep 17 00:00:00 2001 From: Djorkaeff Alexandre Date: Fri, 18 Oct 2019 13:20:01 -0300 Subject: [PATCH] [FIX] Set User-Agent (#1318) * Set User-Agent Fetch & Websocket & XHR * Set User-Agent * Custom User Agent on fetch/websocket * Fix names * Use DeviceInfo --- app/lib/methods/getSettings.js | 1 + app/lib/methods/sendFileMessage.js | 2 + app/lib/rocketchat.js | 5 +- app/utils/fetch.js | 12 ++++ patches/@rocket.chat+sdk+1.0.0-alpha.31.patch | 60 +++++++++++++++++++ 5 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 app/utils/fetch.js create mode 100644 patches/@rocket.chat+sdk+1.0.0-alpha.31.patch diff --git a/app/lib/methods/getSettings.js b/app/lib/methods/getSettings.js index 5cd8a04d0..1dd8b7909 100644 --- a/app/lib/methods/getSettings.js +++ b/app/lib/methods/getSettings.js @@ -8,6 +8,7 @@ import settings from '../../constants/settings'; import log from '../../utils/log'; import database from '../database'; import protectedFunction from './helpers/protectedFunction'; +import fetch from '../../utils/fetch'; const serverInfoKeys = ['Site_Name', 'UI_Use_Real_Name', 'FileUpload_MediaTypeWhiteList', 'FileUpload_MaxFileSize']; diff --git a/app/lib/methods/sendFileMessage.js b/app/lib/methods/sendFileMessage.js index dc8674119..511339158 100644 --- a/app/lib/methods/sendFileMessage.js +++ b/app/lib/methods/sendFileMessage.js @@ -2,6 +2,7 @@ import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import database from '../database'; import log from '../../utils/log'; +import { headers } from '../../utils/fetch'; const uploadQueue = {}; @@ -78,6 +79,7 @@ export function sendFileMessage(rid, fileInfo, tmid, server, user) { xhr.setRequestHeader('X-Auth-Token', token); xhr.setRequestHeader('X-User-Id', id); + xhr.setRequestHeader('User-Agent', headers['User-Agent']); xhr.upload.onprogress = async({ total, loaded }) => { try { diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index a9007b71f..1040410c3 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -1,6 +1,6 @@ import { AsyncStorage, InteractionManager } from 'react-native'; import semver from 'semver'; -import { Rocketchat as RocketchatClient } from '@rocket.chat/sdk'; +import { Rocketchat as RocketchatClient, settings as RocketChatSettings } from '@rocket.chat/sdk'; import RNUserDefaults from 'rn-user-defaults'; import { Q } from '@nozbe/watermelondb'; import * as FileSystem from 'expo-file-system'; @@ -12,6 +12,7 @@ import database from './database'; import log from '../utils/log'; import { isIOS, getBundleId } from '../utils/deviceInfo'; import { extractHostname } from '../utils/server'; +import fetch, { headers } from '../utils/fetch'; import { setUser, setLoginServices, loginRequest, loginFailure, logout @@ -57,6 +58,8 @@ const MIN_ROCKETCHAT_VERSION = '0.70.0'; const STATUSES = ['offline', 'online', 'away', 'busy']; +RocketChatSettings.customHeaders = headers; + const RocketChat = { TOKEN_KEY, callJitsi, diff --git a/app/utils/fetch.js b/app/utils/fetch.js new file mode 100644 index 000000000..1e526af2d --- /dev/null +++ b/app/utils/fetch.js @@ -0,0 +1,12 @@ +import { Platform } from 'react-native'; +import DeviceInfo from 'react-native-device-info'; + +export const headers = { 'User-Agent': `RC-RN Mobile/${ DeviceInfo.getVersion() } (build: ${ DeviceInfo.getBuildNumber() }; os: ${ Platform.OS } ${ DeviceInfo.getSystemVersion() })` }; + +export default (url, options = {}) => { + let customOptions = { ...options, headers }; + if (options && options.headers) { + customOptions = { ...customOptions, headers: { ...options.headers, ...headers } }; + } + return fetch(url, customOptions); +}; diff --git a/patches/@rocket.chat+sdk+1.0.0-alpha.31.patch b/patches/@rocket.chat+sdk+1.0.0-alpha.31.patch new file mode 100644 index 000000000..8e73bf1ef --- /dev/null +++ b/patches/@rocket.chat+sdk+1.0.0-alpha.31.patch @@ -0,0 +1,60 @@ +diff --git a/node_modules/@rocket.chat/sdk/lib/api/api.js b/node_modules/@rocket.chat/sdk/lib/api/api.js +index 5b7dc21..49f1af5 100644 +--- a/node_modules/@rocket.chat/sdk/lib/api/api.js ++++ b/node_modules/@rocket.chat/sdk/lib/api/api.js +@@ -62,6 +62,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); + var log_1 = require("../log"); + var message_1 = require("../message"); + var tiny_events_1 = require("tiny-events"); ++var settings = require("../settings"); + var Client = /** @class */ (function () { + function Client(_a) { + var _b = _a.host, host = _b === void 0 ? 'http://localhost:3000' : _b; +@@ -70,7 +71,7 @@ var Client = /** @class */ (function () { + } + Object.defineProperty(Client.prototype, "headers", { + get: function () { +- return __assign({ 'Content-Type': 'application/json' }, this._headers); ++ return __assign({ 'Content-Type': 'application/json', ...settings.customHeaders }, this._headers); + }, + set: function (obj) { + this._headers = obj; +diff --git a/node_modules/@rocket.chat/sdk/lib/drivers/ddp.js b/node_modules/@rocket.chat/sdk/lib/drivers/ddp.js +index e3510c7..e951959 100644 +--- a/node_modules/@rocket.chat/sdk/lib/drivers/ddp.js ++++ b/node_modules/@rocket.chat/sdk/lib/drivers/ddp.js +@@ -110,6 +110,7 @@ tiny_events_1.EventEmitter.prototype.removeAllListeners = function (event) { + var interfaces_1 = require("../../interfaces"); + var util_1 = require("../util"); + var js_sha256_1 = require("js-sha256"); ++var settings = require("../settings"); + /** Websocket handler class, manages connections and subscriptions by DDP */ + var Socket = /** @class */ (function (_super) { + __extends(Socket, _super); +@@ -145,7 +146,7 @@ var Socket = /** @class */ (function (_super) { + return !_this.alive() && _this.reopen(); + }, ms); + try { +- connection = new universal_websocket_client_1.default(this.host); ++ connection = new universal_websocket_client_1.default(this.host, null, { headers: settings.customHeaders }); + connection.onerror = reject; + } + catch (err) { +diff --git a/node_modules/@rocket.chat/sdk/lib/settings.d.ts b/node_modules/@rocket.chat/sdk/lib/settings.d.ts +index 99eb828..8c99307 100644 +--- a/node_modules/@rocket.chat/sdk/lib/settings.d.ts ++++ b/node_modules/@rocket.chat/sdk/lib/settings.d.ts +@@ -17,3 +17,4 @@ export declare let dmCacheMaxAge: number; + export declare let token: string; + export declare let rid: string; + export declare let department: string; ++export declare let customHeaders: object; +diff --git a/node_modules/@rocket.chat/sdk/lib/settings.js b/node_modules/@rocket.chat/sdk/lib/settings.js +index 822c286..15f2688 100644 +--- a/node_modules/@rocket.chat/sdk/lib/settings.js ++++ b/node_modules/@rocket.chat/sdk/lib/settings.js +@@ -30,3 +30,4 @@ exports.token = process.env.LIVECHAT_TOKEN || ''; + exports.rid = process.env.LIVECHAT_ROOM || ''; + exports.department = process.env.LIVECHAT_DEPARTMENT || ''; + //# sourceMappingURL=settings.js.map ++exports.customHeaders = {};