Skip to content

Commit

Permalink
added authenticated endpoint to get session details for external rest…
Browse files Browse the repository at this point in the history
…rictions
  • Loading branch information
pk910 committed Aug 13, 2023
1 parent 8730a79 commit 66f5852
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/webserv/FaucetWebApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { IFaucetResultSharingConfig } from "../config/ConfigShared";
import { FaucetError } from "../common/FaucetError";
import { EthClaimInfo, EthClaimManager } from "../eth/EthClaimManager";
import { buildFaucetStatus, buildQueueStatus, buildSessionStatus } from "./api/faucetStatus";
import { sha256 } from "../utils/CryptoUtils";

export interface IFaucetApiUrl {
path: string[];
Expand Down Expand Up @@ -90,7 +91,7 @@ export class FaucetWebApi {
case "getQueueStatus".toLowerCase():
return this.onGetQueueStatus();
case "getFaucetStatus".toLowerCase():
return this.onGetFaucetStatus();
return this.onGetFaucetStatus(apiUrl.query['key'] as string);
default:
let handler: (req: IncomingMessage, url: IFaucetApiUrl, body: Buffer) => Promise<any>;
if((handler = this.apiEndpoints[apiUrl.path[0].toLowerCase()]))
Expand Down Expand Up @@ -340,7 +341,17 @@ export class FaucetWebApi {
return cachedRsp.data;
}

public async onGetFaucetStatus(): Promise<any> {
public async onGetFaucetStatus(key: string): Promise<any> {
if(key) {
if(key !== sha256(faucetConfig.faucetSecret + "-unmasked"))
return new FaucetHttpResponse(403, "Access denied");
return Object.assign(
await buildFaucetStatus(),
buildQueueStatus(true),
await buildSessionStatus(true)
);
}

let now = Math.floor(new Date().getTime() / 1000);
let cachedRsp, cacheKey = "faucet";
if(!(cachedRsp = this.cachedStatusData[cacheKey]) || cachedRsp.time < now - FAUCETSTATUS_CACHE_TIME) {
Expand Down
10 changes: 5 additions & 5 deletions src/webserv/api/faucetStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export async function buildFaucetStatus(): Promise<IClientFaucetStatus> {
return statusRsp;
}

export async function buildSessionStatus(): Promise<IClientSessionsStatus> {
export async function buildSessionStatus(unmasked?: boolean): Promise<IClientSessionsStatus> {
let sessionsRsp: IClientSessionsStatus = {
sessions: null,
};
Expand All @@ -111,10 +111,10 @@ export async function buildSessionStatus(): Promise<IClientSessionsStatus> {
sessionsRsp.sessions = sessions.map((session) => {
let runningSession = sessionManager.getSession(session.sessionId);
return {
id: getHashedSessionId(session.sessionId, faucetConfig.faucetSecret),
id: unmasked ? session.sessionId : getHashedSessionId(session.sessionId, faucetConfig.faucetSecret),
start: session.startTime,
target: session.targetAddr,
ip: getHashedIp(session.remoteIP, faucetConfig.faucetSecret),
ip: unmasked ? session.remoteIP : getHashedIp(session.remoteIP, faucetConfig.faucetSecret),
ipInfo: session.data["ipinfo.data"],
balance: session.dropAmount,
nonce: session.data["pow.lastNonce"],
Expand All @@ -131,12 +131,12 @@ export async function buildSessionStatus(): Promise<IClientSessionsStatus> {
return sessionsRsp;
}

export function buildQueueStatus(): IClientQueueStatus {
export function buildQueueStatus(unmasked?: boolean): IClientQueueStatus {
let claims = ServiceManager.GetService(EthClaimManager).getTransactionQueue();
let rspClaims = claims.map((claimTx) => {
return {
time: claimTx.claim.claimTime,
session: getHashedSessionId(claimTx.session, faucetConfig.faucetSecret),
session: unmasked ? claimTx.session : getHashedSessionId(claimTx.session, faucetConfig.faucetSecret),
target: claimTx.target,
amount: claimTx.amount.toString(),
status: claimTx.claim.claimStatus,
Expand Down

0 comments on commit 66f5852

Please sign in to comment.