Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Implemented a possibility of obtaining client IP address for telemetr…
Browse files Browse the repository at this point in the history
…y plugin (#624)

* Implement a possibility of obtaining client IP address for telemetry plugin

Signed-off-by: Vitalii Parfonov <[email protected]>
  • Loading branch information
vparfonov authored Feb 14, 2020
1 parent 7967aa2 commit af4ea0a
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import { RPCProtocol } from '@theia/plugin-ext/lib/common/rpc-protocol';
import { CheTelemetryMain, PLUGIN_RPC_CONTEXT, CheTelemetry } from '../common/che-protocol';
import { CheApiService } from '../common/che-protocol';
import { CommandRegistry } from '@theia/core';
import * as axios from 'axios';
import { ClientAddressInfo } from '@eclipse-che/plugin';

export class CheTelemetryMainImpl implements CheTelemetryMain {

private readonly cheApiService: CheApiService;
private ip: string;

constructor(container: interfaces.Container, rpc: RPCProtocol) {
const proxy: CheTelemetry = rpc.getProxy(PLUGIN_RPC_CONTEXT.CHE_TELEMETRY);
Expand All @@ -28,9 +31,10 @@ export class CheTelemetryMainImpl implements CheTelemetryMain {
}

async $event(id: string, ownerId: string, properties: [string, string][]): Promise<void> {
// TODO : get the infos from the browser
const ip = '';

if (!this.ip) {
const client = await this.getClientAddressInfo();
this.ip = client.ip !== undefined ? client.ip : '';
}
let agent = '';
let resolution = '';
const navigator = window.navigator;
Expand All @@ -46,6 +50,23 @@ export class CheTelemetryMainImpl implements CheTelemetryMain {
}
}

return this.cheApiService.submitTelemetryEvent(id, ownerId, ip, agent, resolution, properties);
return this.cheApiService.submitTelemetryEvent(id, ownerId, this.ip, agent, resolution, properties);
}

async $getClientAddressInfo(): Promise<ClientAddressInfo> {
return this.getClientAddressInfo();
}

async getClientAddressInfo(): Promise<ClientAddressInfo> {
const response = await axios.default.get('/che/client-ip');
if (response.status === 200) {
return response.data;
}
console.log('Can`t obtain client adress information. Status: ' + response.status + 'Error message: ' + response.data);
return {
ip: undefined,
ipFamily: undefined,
port: undefined
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export interface CheTelemetry {

export interface CheTelemetryMain {
$event(id: string, ownerId: string, properties: [string, string][]): Promise<void>;
$getClientAddressInfo(): Promise<che.ClientAddressInfo>;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ import { ChePluginServiceImpl } from './che-plugin-service';
import { CheProductServiceImpl } from './che-product-service';
import { PluginApiContributionIntercepted } from './plugin-service';
import { PluginApiContribution } from '@theia/plugin-ext/lib/main/node/plugin-service';
import { CheClientIpServiceContribution } from './che-client-ip-service';

export default new ContainerModule((bind, unbind, isBound, rebind) => {
bind(ChePluginApiProvider).toSelf().inSingletonScope();
bind(Symbol.for(ExtPluginApiProvider)).toService(ChePluginApiProvider);

bind(ChePluginApiContribution).toSelf().inSingletonScope();
bind(CheClientIpServiceContribution).toSelf().inSingletonScope();
bind(BackendApplicationContribution).toService(ChePluginApiContribution);
bind(BackendApplicationContribution).toService(CheClientIpServiceContribution);

rebind(PluginApiContribution).to(PluginApiContributionIntercepted).inSingletonScope();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*********************************************************************
* Copyright (c) 2020 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

import * as express from 'express';
import { BackendApplicationContribution } from '@theia/core/lib/node/backend-application';
import { injectable } from 'inversify';

@injectable()
export class CheClientIpServiceContribution implements BackendApplicationContribution {

configure(app: express.Application): void {
app.get('/che/client-ip', (req, res) => {
res.json({
ip: req.connection.remoteAddress,
ipFamily: req.connection.remoteFamily,
port: req.connection.remotePort
});
});
}
}
3 changes: 3 additions & 0 deletions extensions/eclipse-che-theia-plugin-ext/src/plugin/che-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ export function createAPIFactory(rpc: RPCProtocol): CheApiFactory {
},
addCommandListener(commandId: string, listener: che.TelemetryListener): Promise<void> {
return cheTelemetryImpl.addCommandListener(commandId, listener);
},
getClienAddressInfo(): Promise<che.ClientAddressInfo> {
return cheTelemetryImpl.getClientAddressInfo();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
**********************************************************************/
import { RPCProtocol } from '@theia/plugin-ext/lib/common/rpc-protocol';
import { PLUGIN_RPC_CONTEXT, CheTelemetry, CheTelemetryMain } from '../common/che-protocol';
import { TelemetryListener, TelemetryListenerParam } from '@eclipse-che/plugin';
import { TelemetryListener, TelemetryListenerParam, ClientAddressInfo } from '@eclipse-che/plugin';
export class CheTelemetryImpl implements CheTelemetry {
private readonly telemetryMain: CheTelemetryMain;
private listeners: Map<string, TelemetryListener> = new Map();
Expand All @@ -35,4 +35,8 @@ export class CheTelemetryImpl implements CheTelemetry {
listener(commandId);
}
}

async getClientAddressInfo(): Promise<ClientAddressInfo> {
return this.telemetryMain.$getClientAddressInfo();
}
}
6 changes: 6 additions & 0 deletions extensions/eclipse-che-theia-plugin/src/che-proposed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,16 @@ declare module '@eclipse-che/plugin' {
* Listener for global command invocation
*/
export type TelemetryListener = (commandId: string, param?: TelemetryListenerParam) => void;
export interface ClientAddressInfo {
ip?: string,
port?: string
ipFamily?: number
}
export namespace telemetry {
export function event(id: string, ownerId: string, properties: [string, string][]): Promise<void>;
/** Fires when a command will starts. */
export function addCommandListener(commandId: string, listener: TelemetryListener): Promise<void>;
export function getClienAddressInfo(): Promise<ClientAddressInfo>;
}

/**
Expand Down

0 comments on commit af4ea0a

Please sign in to comment.