-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add first version of zoom reverse proxy
- Loading branch information
1 parent
1ce2d68
commit 4691e2f
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use strict'; | ||
|
||
const AWS = require('aws-sdk'); | ||
const cachedItem = {}; | ||
const regionsMap = { | ||
'us-west-2': 'oforms.onlyoffice.com', | ||
'eu-central-1': 'blog.onlyoffice.com', | ||
'default': 'onlyoffice.com' | ||
}; | ||
|
||
const ddbRegionsMap = { | ||
ddbRegionsMap_placeholder | ||
}; | ||
|
||
const dynamodbTableName = "dynamodb_table_name_placeholder"; | ||
|
||
const execRegionCode = process.env.AWS_REGION; | ||
var ddbClientRegion = ddbRegionsMap[execRegionCode] || ddbRegionsMap["default"]; | ||
|
||
console.log("DynamoDB client region set to: %s", ddbClientRegion); | ||
|
||
const { DynamoDBClient, GetItemCommand } = require("@aws-sdk/client-dynamodb"); | ||
|
||
async function getTenantRegion(ddbRegion, tenantDomain) { | ||
console.log("Fetching tenant region for domain: %s from DynamoDB region: %s", tenantDomain, ddbRegion); | ||
const ddbClient = new DynamoDBClient({ region: ddbRegion }); | ||
const getItemParams = { | ||
Key: { 'tenant_domain': { S: tenantDomain } }, | ||
ProjectionExpression: "tenant_region", | ||
TableName: dynamodbTableName | ||
}; | ||
|
||
try { | ||
const response = await ddbClient.send(new GetItemCommand(getItemParams)); | ||
if (response.Item) { | ||
const tenantRegion = regionsMap[response.Item["tenant_region"]["S"]]; | ||
console.log("Tenant region fetched: %s", tenantRegion); | ||
return tenantRegion; | ||
} else { | ||
console.warn("No data found for domain: %s", tenantDomain); | ||
return null; | ||
} | ||
} catch (err) { | ||
console.error("Error fetching from DynamoDB: %s", err); | ||
return null; | ||
} | ||
} | ||
|
||
exports.handler = async (event) => { | ||
const request = event.Records[0].cf.request; | ||
const headers = request.headers; | ||
const fullDomain = headers.host[0].value.toLowerCase(); | ||
const domainParts = fullDomain.split('.'); | ||
|
||
if (domainParts.length === 4 && domainParts[1] === 'devops' && domainParts[2] === 'onlyoffice' && domainParts[3] === 'io') { | ||
const tenantDomain = fullDomain; | ||
|
||
// Check the cache first | ||
if (cachedItem[tenantDomain] && cachedItem[tenantDomain].expiresOn > Date.now()) { | ||
request.origin.custom.domainName = cachedItem[tenantDomain].value; | ||
console.log("Origin fetched from cache for domain: %s", tenantDomain); | ||
} else { | ||
// Fetch the region from DynamoDB and update the origin | ||
const tenantRegion = await getTenantRegion(ddbClientRegion, tenantDomain); | ||
if (tenantRegion) { | ||
request.origin.custom.domainName = tenantRegion; | ||
cachedItem[tenantDomain] = { | ||
value: tenantRegion, | ||
expiresOn: Date.now() + 15 * 60 * 1000 // Cache for 15 minutes | ||
}; | ||
console.log("Origin updated to %s for domain: %s", tenantRegion, tenantDomain); | ||
} else { | ||
request.origin.custom.domainName = regionsMap["default"]; | ||
console.log("Default origin used for domain: %s", tenantDomain); | ||
} | ||
} | ||
} | ||
|
||
return request; | ||
}; |