[FIX] Set User-Agent (#1318)

* Set User-Agent Fetch & Websocket & XHR

* Set User-Agent

* Custom User Agent on fetch/websocket

* Fix names

* Use DeviceInfo
This commit is contained in:
Djorkaeff Alexandre 2019-10-18 13:20:01 -03:00 committed by Guilherme Gazzo
parent 05ef9f7d73
commit 6df7d7b3c3
5 changed files with 79 additions and 1 deletions

View File

@ -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'];

View File

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

View File

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

12
app/utils/fetch.js Normal file
View File

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

View File

@ -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 = {};