metrics token

This commit is contained in:
boufni95 2023-12-21 20:51:45 +01:00
parent f65b24247f
commit a3bd0211f1
7 changed files with 3445 additions and 3405 deletions

View file

@ -113,6 +113,10 @@ The nostr server will send back a message response, and inside the body there wi
- expected context content - expected context content
- __admin_id__: _string_ - __admin_id__: _string_
- __Metrics__:
- expected context content
- __operator_id__: _string_
- __App__: - __App__:
- expected context content - expected context content
- __app_id__: _string_ - __app_id__: _string_
@ -142,21 +146,21 @@ The nostr server will send back a message response, and inside the body there wi
- output: [AuthApp](#AuthApp) - output: [AuthApp](#AuthApp)
- GetUsageMetrics - GetUsageMetrics
- auth type: __Admin__ - auth type: __Metrics__
- http method: __post__ - http method: __post__
- http route: __/api/admin/metrics/usage__ - http route: __/api/admin/metrics/usage__
- This methods has an __empty__ __request__ body - This methods has an __empty__ __request__ body
- output: [UsageMetrics](#UsageMetrics) - output: [UsageMetrics](#UsageMetrics)
- GetAppsMetrics - GetAppsMetrics
- auth type: __Admin__ - auth type: __Metrics__
- http method: __post__ - http method: __post__
- http route: __/api/admin/metrics/apps__ - http route: __/api/admin/metrics/apps__
- input: [AppsMetricsRequest](#AppsMetricsRequest) - input: [AppsMetricsRequest](#AppsMetricsRequest)
- output: [AppsMetrics](#AppsMetrics) - output: [AppsMetrics](#AppsMetrics)
- GetLndMetrics - GetLndMetrics
- auth type: __Admin__ - auth type: __Metrics__
- http method: __post__ - http method: __post__
- http route: __/api/admin/metrics/lnd__ - http route: __/api/admin/metrics/lnd__
- input: [LndMetricsRequest](#LndMetricsRequest) - input: [LndMetricsRequest](#LndMetricsRequest)
@ -428,37 +432,49 @@ The nostr server will send back a message response, and inside the body there wi
## Messages ## Messages
### The content of requests and response from the methods ### The content of requests and response from the methods
### AuthApp ### AppsMetricsRequest
- __app__: _[Application](#Application)_ - __from_unix__: _number_ *this field is optional
- __auth_token__: _string_ - __to_unix__: _number_ *this field is optional
- __include_operations__: _boolean_ *this field is optional
### PayAddressRequest ### PayAddressResponse
- __address__: _string_ - __txId__: _string_
- __amoutSats__: _number_ - __operation_id__: _string_
- __satsPerVByte__: _number_ - __service_fee__: _number_
- __network_fee__: _number_
### PayInvoiceRequest ### PayInvoiceResponse
- __preimage__: _string_
- __amount_paid__: _number_
- __operation_id__: _string_
- __service_fee__: _number_
- __network_fee__: _number_
### ChainBalanceEvent
- __block_height__: _number_
- __confirmed_balance__: _number_
- __unconfirmed_balance__: _number_
- __total_balance__: _number_
### SetMockInvoiceAsPaidRequest
- __invoice__: _string_ - __invoice__: _string_
- __amount__: _number_ - __amount__: _number_
### GetUserOperationsResponse ### AppUser
- __latestOutgoingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __latestIncomingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __latestOutgoingTxOperations__: _[UserOperations](#UserOperations)_
- __latestIncomingTxOperations__: _[UserOperations](#UserOperations)_
- __latestOutgoingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
- __latestIncomingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
### AddAppUserRequest
- __identifier__: _string_ - __identifier__: _string_
- __fail_if_exists__: _boolean_ - __info__: _[UserInfo](#UserInfo)_
- __balance__: _number_ - __max_withdrawable__: _number_
### Product ### Product
- __id__: _string_ - __id__: _string_
- __name__: _string_ - __name__: _string_
- __price_sats__: _number_ - __price_sats__: _number_
### GetProductBuyLinkResponse
- __link__: _string_
### Empty
### UsageMetric ### UsageMetric
- __processed_at_nano__: _string_ - __processed_at_nano__: _string_
- __parsed_in_nano__: _number_ - __parsed_in_nano__: _number_
@ -470,14 +486,8 @@ The nostr server will send back a message response, and inside the body there wi
- __nostr__: _boolean_ - __nostr__: _boolean_
- __batch_size__: _number_ - __batch_size__: _number_
### LndMetricsRequest ### LndMetrics
- __from_unix__: _number_ *this field is optional - __nodes__: ARRAY of: _[LndNodeMetrics](#LndNodeMetrics)_
- __to_unix__: _number_ *this field is optional
### LndNodeMetrics
- __channels_balance_events__: ARRAY of: _[ChannelBalanceEvent](#ChannelBalanceEvent)_
- __chain_balance_events__: ARRAY of: _[ChainBalanceEvent](#ChainBalanceEvent)_
- __routing_events__: ARRAY of: _[RoutingEvent](#RoutingEvent)_
### UsersInfo ### UsersInfo
- __total__: _number_ - __total__: _number_
@ -487,58 +497,22 @@ The nostr server will send back a message response, and inside the body there wi
- __balance_avg__: _number_ - __balance_avg__: _number_
- __balance_median__: _number_ - __balance_median__: _number_
### NewInvoiceResponse ### NewInvoiceRequest
- __invoice__: _string_ - __amountSats__: _number_
- __memo__: _string_
### PayInvoiceResponse ### GetUserOperationsRequest
- __preimage__: _string_ - __latestIncomingInvoice__: _number_
- __amount_paid__: _number_ - __latestOutgoingInvoice__: _number_
- __operation_id__: _string_ - __latestIncomingTx__: _number_
- __service_fee__: _number_ - __latestOutgoingTx__: _number_
- __network_fee__: _number_ - __latestIncomingUserToUserPayment__: _number_
- __latestOutgoingUserToUserPayment__: _number_
### OpenChannelResponse ### UserOperations
- __channelId__: _string_ - __fromIndex__: _number_
- __toIndex__: _number_
### LnurlLinkResponse - __operations__: ARRAY of: _[UserOperation](#UserOperation)_
- __lnurl__: _string_
- __k1__: _string_
### AddAppRequest
- __name__: _string_
- __allow_user_creation__: _boolean_
### Application
- __name__: _string_
- __id__: _string_
- __balance__: _number_
- __npub__: _string_
### SendAppUserToAppPaymentRequest
- __from_user_identifier__: _string_
- __amount__: _number_
### DecodeInvoiceResponse
- __amount__: _number_
### ClosureMigration
- __closes_at_unix__: _number_
### SetMockAppBalanceRequest
- __amount__: _number_
### HandleLnurlPayResponse
- __pr__: _string_
- __routes__: ARRAY of: _[Empty](#Empty)_
### AppsMetrics
- __apps__: ARRAY of: _[AppMetrics](#AppMetrics)_
### OpenChannelRequest
- __destination__: _string_
- __fundingAmount__: _number_
- __pushAmount__: _number_
- __closeAddress__: _string_
### RelaysMigration ### RelaysMigration
- __relays__: ARRAY of: _string_ - __relays__: ARRAY of: _string_
@ -546,31 +520,28 @@ The nostr server will send back a message response, and inside the body there wi
### GetAppUserRequest ### GetAppUserRequest
- __user_identifier__: _string_ - __user_identifier__: _string_
### PayAppUserInvoiceRequest ### PayAddressRequest
- __address__: _string_
- __amoutSats__: _number_
- __satsPerVByte__: _number_
### AuthApp
- __app__: _[Application](#Application)_
- __auth_token__: _string_
### SetMockAppUserBalanceRequest
- __user_identifier__: _string_ - __user_identifier__: _string_
- __amount__: _number_
### NewInvoiceResponse
- __invoice__: _string_ - __invoice__: _string_
- __amount__: _number_
### SendAppUserToAppUserPaymentRequest ### OpenChannelResponse
- __from_user_identifier__: _string_ - __channelId__: _string_
- __to_user_identifier__: _string_
- __amount__: _number_
### NewAddressRequest ### EncryptionExchangeRequest
- __addressType__: _[AddressType](#AddressType)_ - __publicKey__: _string_
- __deviceId__: _string_
### PayAddressResponse
- __txId__: _string_
- __operation_id__: _string_
- __service_fee__: _number_
- __network_fee__: _number_
### AddProductRequest
- __name__: _string_
- __price_sats__: _number_
### GetProductBuyLinkResponse
- __link__: _string_
### RoutingEvent ### RoutingEvent
- __incoming_channel_id__: _number_ - __incoming_channel_id__: _number_
@ -586,15 +557,41 @@ The nostr server will send back a message response, and inside the body there wi
- __offchain__: _boolean_ - __offchain__: _boolean_
- __forward_fail_event__: _boolean_ - __forward_fail_event__: _boolean_
### AppUser ### OpenChannelRequest
- __identifier__: _string_ - __destination__: _string_
- __info__: _[UserInfo](#UserInfo)_ - __fundingAmount__: _number_
- __max_withdrawable__: _number_ - __pushAmount__: _number_
- __closeAddress__: _string_
### AddAppInvoiceRequest ### LndMetricsRequest
- __payer_identifier__: _string_ - __from_unix__: _number_ *this field is optional
- __http_callback_url__: _string_ - __to_unix__: _number_ *this field is optional
- __invoice_req__: _[NewInvoiceRequest](#NewInvoiceRequest)_
### Application
- __name__: _string_
- __id__: _string_
- __balance__: _number_
- __npub__: _string_
### SendAppUserToAppPaymentRequest
- __from_user_identifier__: _string_
- __amount__: _number_
### LnurlPayInfoResponse
- __tag__: _string_
- __callback__: _string_
- __maxSendable__: _number_
- __minSendable__: _number_
- __metadata__: _string_
- __allowsNostr__: _boolean_
- __nostrPubkey__: _string_
### MigrationUpdate
- __closure__: _[ClosureMigration](#ClosureMigration)_ *this field is optional
- __relays__: _[RelaysMigration](#RelaysMigration)_ *this field is optional
### ClosureMigration
- __closes_at_unix__: _number_
### AddAppUserInvoiceRequest ### AddAppUserInvoiceRequest
- __receiver_identifier__: _string_ - __receiver_identifier__: _string_
@ -605,70 +602,27 @@ The nostr server will send back a message response, and inside the body there wi
### DecodeInvoiceRequest ### DecodeInvoiceRequest
- __invoice__: _string_ - __invoice__: _string_
### UserOperations ### GetUserOperationsResponse
- __fromIndex__: _number_ - __latestOutgoingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __toIndex__: _number_ - __latestIncomingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __operations__: ARRAY of: _[UserOperation](#UserOperation)_ - __latestOutgoingTxOperations__: _[UserOperations](#UserOperations)_
- __latestIncomingTxOperations__: _[UserOperations](#UserOperations)_
- __latestOutgoingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
- __latestIncomingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
### EncryptionExchangeRequest ### LiveUserOperation
- __publicKey__: _string_ - __operation__: _[UserOperation](#UserOperation)_
- __deviceId__: _string_
### GetUserOperationsRequest ### UsageMetrics
- __latestIncomingInvoice__: _number_ - __metrics__: ARRAY of: _[UsageMetric](#UsageMetric)_
- __latestOutgoingInvoice__: _number_
- __latestIncomingTx__: _number_
- __latestOutgoingTx__: _number_
- __latestIncomingUserToUserPayment__: _number_
- __latestOutgoingUserToUserPayment__: _number_
### AppMetrics
- __app__: _[Application](#Application)_
- __users__: _[UsersInfo](#UsersInfo)_
- __total_received__: _number_
- __total_spent__: _number_
- __total_available__: _number_
- __unpaid_invoices__: _number_
- __operations__: ARRAY of: _[UserOperation](#UserOperation)_
### ChainBalanceEvent
- __block_height__: _number_
- __confirmed_balance__: _number_
- __unconfirmed_balance__: _number_
- __total_balance__: _number_
### AuthAppRequest ### AuthAppRequest
- __name__: _string_ - __name__: _string_
- __allow_user_creation__: _boolean_ *this field is optional - __allow_user_creation__: _boolean_ *this field is optional
### SetMockAppUserBalanceRequest ### DecodeInvoiceResponse
- __user_identifier__: _string_
- __amount__: _number_ - __amount__: _number_
### NewAddressResponse
- __address__: _string_
### LiveUserOperation
- __operation__: _[UserOperation](#UserOperation)_
### Empty
### ChannelBalanceEvent
- __block_height__: _number_
- __channel_id__: _string_
- __local_balance_sats__: _number_
- __remote_balance_sats__: _number_
### LndMetrics
- __nodes__: ARRAY of: _[LndNodeMetrics](#LndNodeMetrics)_
### LndGetInfoResponse
- __alias__: _string_
### NewInvoiceRequest
- __amountSats__: _number_
- __memo__: _string_
### LnurlWithdrawInfoResponse ### LnurlWithdrawInfoResponse
- __tag__: _string_ - __tag__: _string_
- __callback__: _string_ - __callback__: _string_
@ -679,27 +633,26 @@ The nostr server will send back a message response, and inside the body there wi
- __balanceCheck__: _string_ - __balanceCheck__: _string_
- __payLink__: _string_ - __payLink__: _string_
### UsageMetrics ### AddAppRequest
- __metrics__: ARRAY of: _[UsageMetric](#UsageMetric)_ - __name__: _string_
- __allow_user_creation__: _boolean_
### AddProductRequest
- __name__: _string_
- __price_sats__: _number_
### NewAddressRequest
- __addressType__: _[AddressType](#AddressType)_
### UserInfo ### UserInfo
- __userId__: _string_ - __userId__: _string_
- __balance__: _number_ - __balance__: _number_
- __max_withdrawable__: _number_ - __max_withdrawable__: _number_
### AppsMetricsRequest ### SendAppUserToAppUserPaymentRequest
- __from_unix__: _number_ *this field is optional - __from_user_identifier__: _string_
- __to_unix__: _number_ *this field is optional - __to_user_identifier__: _string_
- __include_operations__: _boolean_ *this field is optional - __amount__: _number_
### LnurlPayInfoResponse
- __tag__: _string_
- __callback__: _string_
- __maxSendable__: _number_
- __minSendable__: _number_
- __metadata__: _string_
- __allowsNostr__: _boolean_
- __nostrPubkey__: _string_
### UserOperation ### UserOperation
- __paidAtUnix__: _number_ - __paidAtUnix__: _number_
@ -712,20 +665,71 @@ The nostr server will send back a message response, and inside the body there wi
- __network_fee__: _number_ - __network_fee__: _number_
- __confirmed__: _boolean_ - __confirmed__: _boolean_
### MigrationUpdate ### AppMetrics
- __closure__: _[ClosureMigration](#ClosureMigration)_ *this field is optional - __app__: _[Application](#Application)_
- __relays__: _[RelaysMigration](#RelaysMigration)_ *this field is optional - __users__: _[UsersInfo](#UsersInfo)_
- __total_received__: _number_
- __total_spent__: _number_
- __total_available__: _number_
- __unpaid_invoices__: _number_
- __operations__: ARRAY of: _[UserOperation](#UserOperation)_
### AppsMetrics
- __apps__: ARRAY of: _[AppMetrics](#AppMetrics)_
### ChannelBalanceEvent
- __block_height__: _number_
- __channel_id__: _string_
- __local_balance_sats__: _number_
- __remote_balance_sats__: _number_
### AddAppInvoiceRequest
- __payer_identifier__: _string_
- __http_callback_url__: _string_
- __invoice_req__: _[NewInvoiceRequest](#NewInvoiceRequest)_
### PayAppUserInvoiceRequest
- __user_identifier__: _string_
- __invoice__: _string_
- __amount__: _number_
### LndGetInfoRequest ### LndGetInfoRequest
- __nodeId__: _number_ - __nodeId__: _number_
### SetMockInvoiceAsPaidRequest ### AddAppUserRequest
- __invoice__: _string_ - __identifier__: _string_
- __amount__: _number_ - __fail_if_exists__: _boolean_
- __balance__: _number_
### GetAppUserLNURLInfoRequest ### GetAppUserLNURLInfoRequest
- __user_identifier__: _string_ - __user_identifier__: _string_
- __base_url_override__: _string_ - __base_url_override__: _string_
### SetMockAppBalanceRequest
- __amount__: _number_
### NewAddressResponse
- __address__: _string_
### LndNodeMetrics
- __channels_balance_events__: ARRAY of: _[ChannelBalanceEvent](#ChannelBalanceEvent)_
- __chain_balance_events__: ARRAY of: _[ChainBalanceEvent](#ChainBalanceEvent)_
- __routing_events__: ARRAY of: _[RoutingEvent](#RoutingEvent)_
### LndGetInfoResponse
- __alias__: _string_
### PayInvoiceRequest
- __invoice__: _string_
- __amount__: _number_
### LnurlLinkResponse
- __lnurl__: _string_
- __k1__: _string_
### HandleLnurlPayResponse
- __pr__: _string_
- __routes__: ARRAY of: _[Empty](#Empty)_
## Enums ## Enums
### The enumerators used in the messages ### The enumerators used in the messages

File diff suppressed because it is too large Load diff

View file

@ -16,6 +16,7 @@ export type ServerOptions = {
GuestAuthGuard: (authorizationHeader?: string) => Promise<Types.GuestContext> GuestAuthGuard: (authorizationHeader?: string) => Promise<Types.GuestContext>
UserAuthGuard: (authorizationHeader?: string) => Promise<Types.UserContext> UserAuthGuard: (authorizationHeader?: string) => Promise<Types.UserContext>
AdminAuthGuard: (authorizationHeader?: string) => Promise<Types.AdminContext> AdminAuthGuard: (authorizationHeader?: string) => Promise<Types.AdminContext>
MetricsAuthGuard: (authorizationHeader?: string) => Promise<Types.MetricsContext>
AppAuthGuard: (authorizationHeader?: string) => Promise<Types.AppContext> AppAuthGuard: (authorizationHeader?: string) => Promise<Types.AppContext>
} }
declare module 'express-serve-static-core' { interface Request { startTime?: bigint, bodySize?: number } } declare module 'express-serve-static-core' { interface Request { startTime?: bigint, bodySize?: number } }
@ -105,7 +106,7 @@ export default (methods: Types.ServerMethods, opts: ServerOptions) => {
let authCtx: Types.AuthContext = {} let authCtx: Types.AuthContext = {}
try { try {
if (!methods.GetUsageMetrics) throw new Error('method: GetUsageMetrics is not implemented') if (!methods.GetUsageMetrics) throw new Error('method: GetUsageMetrics is not implemented')
const authContext = await opts.AdminAuthGuard(req.headers['authorization']) const authContext = await opts.MetricsAuthGuard(req.headers['authorization'])
authCtx = authContext authCtx = authContext
stats.guard = process.hrtime.bigint() stats.guard = process.hrtime.bigint()
stats.validate = stats.guard stats.validate = stats.guard
@ -124,7 +125,7 @@ export default (methods: Types.ServerMethods, opts: ServerOptions) => {
let authCtx: Types.AuthContext = {} let authCtx: Types.AuthContext = {}
try { try {
if (!methods.GetAppsMetrics) throw new Error('method: GetAppsMetrics is not implemented') if (!methods.GetAppsMetrics) throw new Error('method: GetAppsMetrics is not implemented')
const authContext = await opts.AdminAuthGuard(req.headers['authorization']) const authContext = await opts.MetricsAuthGuard(req.headers['authorization'])
authCtx = authContext authCtx = authContext
stats.guard = process.hrtime.bigint() stats.guard = process.hrtime.bigint()
const request = req.body const request = req.body
@ -146,7 +147,7 @@ export default (methods: Types.ServerMethods, opts: ServerOptions) => {
let authCtx: Types.AuthContext = {} let authCtx: Types.AuthContext = {}
try { try {
if (!methods.GetLndMetrics) throw new Error('method: GetLndMetrics is not implemented') if (!methods.GetLndMetrics) throw new Error('method: GetLndMetrics is not implemented')
const authContext = await opts.AdminAuthGuard(req.headers['authorization']) const authContext = await opts.MetricsAuthGuard(req.headers['authorization'])
authCtx = authContext authCtx = authContext
stats.guard = process.hrtime.bigint() stats.guard = process.hrtime.bigint()
const request = req.body const request = req.body

View file

@ -8,6 +8,7 @@ export type ClientParams = {
retrieveGuestAuth: () => Promise<string | null> retrieveGuestAuth: () => Promise<string | null>
retrieveUserAuth: () => Promise<string | null> retrieveUserAuth: () => Promise<string | null>
retrieveAdminAuth: () => Promise<string | null> retrieveAdminAuth: () => Promise<string | null>
retrieveMetricsAuth: () => Promise<string | null>
retrieveAppAuth: () => Promise<string | null> retrieveAppAuth: () => Promise<string | null>
encryptCallback: (plain: any) => Promise<any> encryptCallback: (plain: any) => Promise<any>
decryptCallback: (encrypted: any) => Promise<any> decryptCallback: (encrypted: any) => Promise<any>
@ -58,8 +59,8 @@ export default (params: ClientParams) => ({
return { status: 'ERROR', reason: 'invalid response' } return { status: 'ERROR', reason: 'invalid response' }
}, },
GetUsageMetrics: async (): Promise<ResultError | ({ status: 'OK' }& Types.UsageMetrics)> => { GetUsageMetrics: async (): Promise<ResultError | ({ status: 'OK' }& Types.UsageMetrics)> => {
const auth = await params.retrieveAdminAuth() const auth = await params.retrieveMetricsAuth()
if (auth === null) throw new Error('retrieveAdminAuth() returned null') if (auth === null) throw new Error('retrieveMetricsAuth() returned null')
let finalRoute = '/api/admin/metrics/usage' let finalRoute = '/api/admin/metrics/usage'
const { data } = await axios.post(params.baseUrl + finalRoute, {}, { headers: { 'authorization': auth } }) const { data } = await axios.post(params.baseUrl + finalRoute, {}, { headers: { 'authorization': auth } })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data if (data.status === 'ERROR' && typeof data.reason === 'string') return data
@ -72,8 +73,8 @@ export default (params: ClientParams) => ({
return { status: 'ERROR', reason: 'invalid response' } return { status: 'ERROR', reason: 'invalid response' }
}, },
GetAppsMetrics: async (request: Types.AppsMetricsRequest): Promise<ResultError | ({ status: 'OK' }& Types.AppsMetrics)> => { GetAppsMetrics: async (request: Types.AppsMetricsRequest): Promise<ResultError | ({ status: 'OK' }& Types.AppsMetrics)> => {
const auth = await params.retrieveAdminAuth() const auth = await params.retrieveMetricsAuth()
if (auth === null) throw new Error('retrieveAdminAuth() returned null') if (auth === null) throw new Error('retrieveMetricsAuth() returned null')
let finalRoute = '/api/admin/metrics/apps' let finalRoute = '/api/admin/metrics/apps'
const { data } = await axios.post(params.baseUrl + finalRoute, request, { headers: { 'authorization': auth } }) const { data } = await axios.post(params.baseUrl + finalRoute, request, { headers: { 'authorization': auth } })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data if (data.status === 'ERROR' && typeof data.reason === 'string') return data
@ -86,8 +87,8 @@ export default (params: ClientParams) => ({
return { status: 'ERROR', reason: 'invalid response' } return { status: 'ERROR', reason: 'invalid response' }
}, },
GetLndMetrics: async (request: Types.LndMetricsRequest): Promise<ResultError | ({ status: 'OK' }& Types.LndMetrics)> => { GetLndMetrics: async (request: Types.LndMetricsRequest): Promise<ResultError | ({ status: 'OK' }& Types.LndMetrics)> => {
const auth = await params.retrieveAdminAuth() const auth = await params.retrieveMetricsAuth()
if (auth === null) throw new Error('retrieveAdminAuth() returned null') if (auth === null) throw new Error('retrieveMetricsAuth() returned null')
let finalRoute = '/api/admin/metrics/lnd' let finalRoute = '/api/admin/metrics/lnd'
const { data } = await axios.post(params.baseUrl + finalRoute, request, { headers: { 'authorization': auth } }) const { data } = await axios.post(params.baseUrl + finalRoute, request, { headers: { 'authorization': auth } })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data if (data.status === 'ERROR' && typeof data.reason === 'string') return data

File diff suppressed because it is too large Load diff

View file

@ -37,6 +37,15 @@ option (file_options) = {
value:"string" value:"string"
} }
}, },
{
id: "metrics",
name: "Metrics",
//encrypted:true,
context:{
key:"operator_id",
value:"string"
}
},
{ {
id:"app", id:"app",
name:"App", name:"App",
@ -98,19 +107,19 @@ service LightningPub {
} }
rpc GetUsageMetrics(structs.Empty) returns (structs.UsageMetrics) { rpc GetUsageMetrics(structs.Empty) returns (structs.UsageMetrics) {
option (auth_type) = "Admin"; option (auth_type) = "Metrics";
option (http_method) = "post"; option (http_method) = "post";
option (http_route) = "/api/admin/metrics/usage"; option (http_route) = "/api/admin/metrics/usage";
} }
rpc GetAppsMetrics(structs.AppsMetricsRequest) returns (structs.AppsMetrics) { rpc GetAppsMetrics(structs.AppsMetricsRequest) returns (structs.AppsMetrics) {
option (auth_type) = "Admin"; option (auth_type) = "Metrics";
option (http_method) = "post"; option (http_method) = "post";
option (http_route) = "/api/admin/metrics/apps"; option (http_route) = "/api/admin/metrics/apps";
} }
rpc GetLndMetrics(structs.LndMetricsRequest) returns (structs.LndMetrics) { rpc GetLndMetrics(structs.LndMetricsRequest) returns (structs.LndMetrics) {
option (auth_type) = "Admin"; option (auth_type) = "Metrics";
option (http_method) = "post"; option (http_method) = "post";
option (http_route) = "/api/admin/metrics/lnd"; option (http_route) = "/api/admin/metrics/lnd";
} }

View file

@ -1,5 +1,5 @@
import { ServerOptions } from "../proto/autogenerated/ts/express_server"; import { ServerOptions } from "../proto/autogenerated/ts/express_server";
import { AdminContext } from "../proto/autogenerated/ts/types"; import { AdminContext, MetricsContext } from "../proto/autogenerated/ts/types";
import Main from './services/main' import Main from './services/main'
import { getLogger } from './services/helpers/logger.js' import { getLogger } from './services/helpers/logger.js'
const serverOptions = (mainHandler: Main): ServerOptions => { const serverOptions = (mainHandler: Main): ServerOptions => {
@ -7,6 +7,7 @@ const serverOptions = (mainHandler: Main): ServerOptions => {
return { return {
logger: { log, error: err => log("ERROR", err) }, logger: { log, error: err => log("ERROR", err) },
AdminAuthGuard: adminAuth, AdminAuthGuard: adminAuth,
MetricsAuthGuard: metricsAuth,
AppAuthGuard: async (authHeader) => { return { app_id: mainHandler.applicationManager.DecodeAppToken(stripBearer(authHeader)) } }, AppAuthGuard: async (authHeader) => { return { app_id: mainHandler.applicationManager.DecodeAppToken(stripBearer(authHeader)) } },
UserAuthGuard: async (authHeader) => { return mainHandler.appUserManager.DecodeUserToken(stripBearer(authHeader)) }, UserAuthGuard: async (authHeader) => { return mainHandler.appUserManager.DecodeUserToken(stripBearer(authHeader)) },
GuestAuthGuard: async (_) => ({}), GuestAuthGuard: async (_) => ({}),
@ -43,4 +44,23 @@ const adminAuth = async (header: string | undefined): Promise<AdminContext> => {
} }
return { admin_id: "admin1" } return { admin_id: "admin1" }
} }
const metricsAuth = async (header: string | undefined): Promise<MetricsContext> => {
const metricsToken = process.env.METRICS_TOKEN || process.env.ADMIN_TOKEN
if (!metricsToken) {
throw new Error("metrics auth disabled")
}
if (!header) {
throw new Error("metrics header not found")
}
let h = header
if (header.startsWith("Bearer ")) {
h = header.substring("Bearer ".length)
}
if (h !== metricsToken) {
throw new Error("metrics token invalid")
}
return { operator_id: "metrics1" }
}
export default serverOptions export default serverOptions