diff --git a/src/dashboard/HomegamesDashboard.js b/src/dashboard/HomegamesDashboard.js index 85f0283a..96ef54e7 100644 --- a/src/dashboard/HomegamesDashboard.js +++ b/src/dashboard/HomegamesDashboard.js @@ -4,6 +4,8 @@ const https = require('https'); const path = require('path'); const fs = require('fs'); +const { getConfigValue, getAppDataPath, log } = require('homegames-common'); + const { Asset, Game, ViewableGame, GameNode, Colors, ShapeUtils, Shapes, squish, unsquish, ViewUtils } = require('squish-1009'); const squishMap = require('../common/squish-map'); @@ -13,6 +15,13 @@ const gameModal = require('./game-modal'); const COLORS = Colors.COLORS; +const API_URL = getConfigValue('API_URL', 'https://api.homegames.io:443'); + +console.log("API URL " + API_URL); + +const parsedUrl = new URL(API_URL); +const isSecure = parsedUrl.protocol == 'https:'; + const { ExpiringSet, animations } = require('../common/util'); const gameOption = require('./game-option'); @@ -23,8 +32,6 @@ if (baseDir.endsWith('src')) { baseDir = baseDir.substring(0, baseDir.length - 3); } -const { getConfigValue, getAppDataPath, log } = require('homegames-common'); - const serverPortMin = getConfigValue('GAME_SERVER_PORT_RANGE_MIN', 7002); const serverPortMax = getConfigValue('GAME_SERVER_PORT_RANGE_MAX', 7099); @@ -124,7 +131,7 @@ if (!fs.existsSync(DOWNLOADED_GAME_DIRECTORY)) { const networkHelper = { searchGames: (q) => new Promise((resolve, reject) => { - getUrl('https://api.homegames.io/games?query=' + q).then(response => { + getUrl(`${API_URL}/games?query=${q}`).then(response => { let results; try { results = JSON.parse(response); @@ -135,11 +142,13 @@ const networkHelper = { resolve(results); }).catch(err => { log.error('Error searching games', err); + console.log('huh lol'); + console.log(err); reject(err); }); }), getGameDetails: (gameId) => new Promise((resolve, reject) => { - getUrl('https://api.homegames.io/games/' + gameId).then(response => { + getUrl(`${API_URL}/games/${gameId}`).then(response => { let results; try { results = JSON.parse(response); @@ -154,7 +163,7 @@ const networkHelper = { }); }), getGameVersionDetails: (gameId, versionId) => new Promise((resolve, reject) => { - getUrl('https://api.homegames.io/games/' + gameId + '/version/' + versionId).then(response => { + getUrl(`${API_URL}/games/${gameId}/version/${versionId}`).then(response => { resolve(JSON.parse(response)); }).catch(err => { log.error(err); @@ -237,6 +246,7 @@ const getGameMap = () => { metadata: { name: gameMetadata.name || gameClass.name, thumbnail: gameMetadata.thumbnail, + thumbnailSource: gameMetadata.thumbnailSource, author: gameMetadata.createdBy || 'Unknown author', isTest: gameMetadata.isTest || false }, @@ -301,6 +311,7 @@ const getGameMap = () => { metadata: { name: gameMetadata.name || gameClass.name, thumbnail: gameMetadata.thumbnail, + thumbnailSource: gameMetadata.thumbnailSource, author: gameMetadata.createdBy || 'Unknown author' }, versions: { @@ -366,6 +377,7 @@ class HomegamesDashboard extends ViewableGame { Object.keys(this.localGames).filter(k => this.localGames[k].metadata && this.localGames[k].metadata.thumbnail).forEach(key => { this.assets[key] = new Asset({ 'id': this.localGames[key].metadata && this.localGames[key].metadata.thumbnail, + 'source': this.localGames[key].metadata && this.localGames[key].metadata.thumbnailSource, 'type': 'image' }); }); @@ -764,7 +776,8 @@ class HomegamesDashboard extends ViewableGame { metadata: { name: localGameMetadata.name || key, author: localGameMetadata.author, - thumbnail: localGameMetadata.thumbnail + thumbnail: localGameMetadata.thumbnail, + thumbnailSource: localGameMetadata.thumbnailSource } } } @@ -1027,8 +1040,9 @@ class HomegamesDashboard extends ViewableGame { } downloadGame({ gameDetails, version }) { - const { id: gameId, description, name, createdBy, createdAt } = gameDetails; - const { versionId, location, isReviewed } = version; + const { id: gameId, description, name, developerId: createdBy, created: createdAt } = gameDetails.game; + const { id: versionId, assetId, isReviewed } = version; + const location = `${API_URL}/assets/${assetId}`; const metadataToStore = { version: { @@ -1069,6 +1083,7 @@ class HomegamesDashboard extends ViewableGame { Object.keys(this.localGames).filter(k => this.localGames[k].metadata && this.localGames[k].metadata.thumbnail).forEach(key => { this.assets[key] = new Asset({ 'id': this.localGames[key].metadata && this.localGames[key].metadata.thumbnail, + 'source': this.localGames[key].metadata && this.localGames[key].metadata.thumbnailSource, 'type': 'image' }); }); @@ -1076,7 +1091,7 @@ class HomegamesDashboard extends ViewableGame { }); }); - https.get(location, (res) => { + (API_URL.startsWith('https') ? https : http).get(location, (res) => { res.pipe(zipWriteStream); zipWriteStream.on('finish', () => { zipWriteStream.close(); diff --git a/src/dashboard/game-modal.js b/src/dashboard/game-modal.js index 1f98d732..4af49522 100644 --- a/src/dashboard/game-modal.js +++ b/src/dashboard/game-modal.js @@ -2,7 +2,7 @@ const { fork } = require('child_process'); const http = require('http'); const https = require('https'); const path = require('path'); -const { Game, ViewableGame, GameNode, Colors, ShapeUtils, Shapes, squish, unsquish, ViewUtils } = require('squish-1006'); +const { Game, ViewableGame, GameNode, Colors, ShapeUtils, Shapes, squish, unsquish, ViewUtils } = require('squish-1009'); const fs = require('fs'); diff --git a/src/dashboard/game-option.js b/src/dashboard/game-option.js index 7e97d748..eadff822 100644 --- a/src/dashboard/game-option.js +++ b/src/dashboard/game-option.js @@ -2,7 +2,7 @@ const { fork } = require('child_process'); const http = require('http'); const https = require('https'); const path = require('path'); -const { Asset, Game, ViewableGame, GameNode, Colors, ShapeUtils, Shapes, squish, unsquish, ViewUtils } = require('squish-1006'); +const { Asset, Game, ViewableGame, GameNode, Colors, ShapeUtils, Shapes, squish, unsquish, ViewUtils } = require('squish-1009'); const fs = require('fs'); const gameModal = require('./game-modal'); diff --git a/src/games/infinite-questions/index.js b/src/games/infinite-questions/index.js index c5f6a397..8e0ba682 100644 --- a/src/games/infinite-questions/index.js +++ b/src/games/infinite-questions/index.js @@ -242,13 +242,13 @@ class InfiniteQuestions extends Game { const keywordString = keywords.join(','); this.contentGenerator.requestContent({ - model: 'mistral-7b-instruct-v0.2', + model: 'mistral-7b-v0.2', prompt: `Generate 5 conversation starter questions in JSON format. The response should contain only JSON with a single key "questions" containing a list of strings. The questions should be about the following topics: ${keywordString}.` - }).then((_contentJson) => { + }).then((_content) => { // it doesnt _just_ give json of course - const leftParenIndex = _contentJson.response.lastIndexOf('{'); - const rightParenIndex = _contentJson.response.lastIndexOf('}'); - const contentJson = JSON.parse(_contentJson.response.substring(leftParenIndex, rightParenIndex + 1)); + const leftParenIndex = _content.lastIndexOf('{'); + const rightParenIndex = _content.lastIndexOf('}'); + const contentJson = JSON.parse(_content.substring(leftParenIndex, rightParenIndex + 1)); this.content = contentJson; this.error = null; this.loading = false; diff --git a/src/services/index.js b/src/services/index.js index 2dc7fc36..66a07fc2 100644 --- a/src/services/index.js +++ b/src/services/index.js @@ -1,19 +1,24 @@ const http = require('http'); const https = require('https'); +const { getConfigValue } = require('homegames-common'); + const services = {}; +const API_URL = getConfigValue('API_URL', 'https://api.homegames.io:443'); + +const parsedUrl = new URL(API_URL); +const isSecure = parsedUrl.protocol == 'https:'; + const supportedServices = { 'contentGenerator': { requestContent: (request) => new Promise((resolve, reject) => { const makePost = (path, _payload) => new Promise((resolve, reject) => { const payload = JSON.stringify(_payload); - let module, hostname, port; - - module = https; - port = 443; - hostname = 'api.homegames.io'; + const module = isSecure ? https : http; + const port = parsedUrl.port || isSecure ? 443 : 80; + const hostname = parsedUrl.hostname; const headers = {}; @@ -46,24 +51,23 @@ const supportedServices = { req.end(); }); - makePost('https://api.homegames.io/services', { type: 'content-generation', ...request }).then((response) => { + makePost(`${API_URL}/services`, { type: 'content-generation', ...request }).then((response) => { if (!response.startsWith('{')) { reject(response); } else { const requestId = JSON.parse(response).requestId; const interval = setInterval(() => { - https.get(`https://api.homegames.io/service_requests/${requestId}`, {}, (res) => { + https.get(`${API_URL}/service_requests/${requestId}`, {}, (res) => { let bufs = []; res.on('data', (chunk) => { bufs.push(chunk); }); res.on('end', () => { - const fin = JSON.parse(Buffer.concat(bufs)); - const parsed = fin;//JSON.parse(fin); - if (parsed.response) { + const r = JSON.parse(Buffer.concat(bufs)); + if (r.response) { clearInterval(interval); - resolve(parsed.response); + resolve(r.response); } }); }); diff --git a/src/util/homenames-helper.js b/src/util/homenames-helper.js index f459870e..c00e91a6 100644 --- a/src/util/homenames-helper.js +++ b/src/util/homenames-helper.js @@ -11,9 +11,15 @@ if (baseDir.endsWith('/src')) { const { getConfigValue, getUserHash, log } = require('homegames-common'); +const API_URL = getConfigValue('API_URL', 'https://api.homegames.io:443'); +const parsedUrl = new URL(API_URL); + +const isSecure = parsedUrl.protocol == 'https:'; + const HTTPS_ENABLED = getConfigValue('HTTPS_ENABLED', false); const DOMAIN_NAME = getConfigValue('DOMAIN_NAME', null); +const CERT_DOMAIN = getConfigValue('CERT_DOMAIN', null); const getLocalIP = () => { const ifaces = os.networkInterfaces(); @@ -32,7 +38,7 @@ const getLocalIP = () => { }; const getPublicIP = () => new Promise((resolve, reject) => { - https.get(`https://api.homegames.io/ip`, (res) => { + (isSecure ? https : http).get(`${API_URL}/ip`, (res) => { let buf = ''; res.on('data', (chunk) => { buf += chunk.toString(); @@ -48,7 +54,7 @@ const makeGet = (path = '', headers = {}, username) => new Promise((resolve, rej const protocol = HTTPS_ENABLED ? 'https' : 'http'; // todo: fix getPublicIP().then(publicIp => { - const host = HTTPS_ENABLED ? (DOMAIN_NAME || (getUserHash(publicIp) + '.homegames.link')) : 'localhost'; + const host = HTTPS_ENABLED ? (DOMAIN_NAME || (`${getUserHash(publicIp)}.${CERT_DOMAIN}`)) : 'localhost'; const base = `${protocol}://${host}:${getConfigValue('HOMENAMES_PORT')}`; (HTTPS_ENABLED ? https : http).get(`${base}${path}`, (res) => { @@ -73,7 +79,7 @@ const makePost = (path, _payload, username) => new Promise((resolve, reject) => port = getConfigValue('HOMENAMES_PORT'); getPublicIP().then(publicIp => { - hostname = HTTPS_ENABLED ? (DOMAIN_NAME || (getUserHash(publicIp) + '.homegames.link')) : 'localhost'; + hostname = HTTPS_ENABLED ? (DOMAIN_NAME || (`${getUserHash(publicIp)}.${CERT_DOMAIN}`)) : 'localhost'; const headers = {}; diff --git a/src/util/link-helper.js b/src/util/link-helper.js index 49403b41..0b7ab4b0 100644 --- a/src/util/link-helper.js +++ b/src/util/link-helper.js @@ -1,6 +1,7 @@ const { log } = require('homegames-common'); const WebSocket = require('ws'); +const http = require('http'); const https = require('https'); const os = require('os'); const fs = require('fs'); @@ -14,8 +15,12 @@ if (baseDir.endsWith('src')) { const { getConfigValue } = require('homegames-common'); +const API_URL = getConfigValue('API_URL', 'https://api.homegames.io:443'); +const parsedUrl = new URL(API_URL); +const isSecure = parsedUrl.protocol == 'https:'; + const getPublicIP = () => new Promise((resolve, reject) => { - https.get(`https://api.homegames.io/ip`, (res) => { + (isSecure ? https : http).get(`${API_URL}/ip`, (res) => { let buf = ''; res.on('data', (chunk) => { buf += chunk.toString(); @@ -55,8 +60,10 @@ const getClientInfo = () => new Promise((resolve, reject) => { }); }); +const LINK_URL = getConfigValue('LINK_URL', `wss://homegames.link`); + const linkConnect = (msgHandler) => new Promise((resolve, reject) => { - const client = new WebSocket('wss://homegames.link'); + const client = new WebSocket(LINK_URL); let interval; @@ -88,6 +95,7 @@ const linkConnect = (msgHandler) => new Promise((resolve, reject) => { client.on('message', msgHandler ? msgHandler : () => {}); client.on('error', (err) => { + console.log(err); log.error('Link client error'); log.error(err); reject(err); diff --git a/src/util/socket.js b/src/util/socket.js index 873c5349..ce393b85 100644 --- a/src/util/socket.js +++ b/src/util/socket.js @@ -15,6 +15,13 @@ if (baseDir.endsWith('src')) { const { getConfigValue, log, getUserHash } = require('homegames-common'); +const API_URL = getConfigValue('API_URL', 'https://api.homegames.io:443'); + +const LINK_PROXY_URL = getConfigValue('LINK_PROXY_URL', 'wss://public.homegames.link:81'); + +const parsedUrl = new URL(API_URL); +const isSecure = parsedUrl.protocol == 'https:'; + const HOMENAMES_PORT = getConfigValue('HOMENAMES_PORT', 7100); const BEZEL_SIZE_X = getConfigValue('BEZEL_SIZE_X', 15); const _BEZEL_SIZE_Y = getConfigValue('BEZEL_SIZE_Y', 15); @@ -23,9 +30,10 @@ const HOTLOAD_ENABLED = getConfigValue('HOTLOAD_ENABLED', false); const BEZEL_SIZE_Y = getConfigValue('BEZEL_SIZE_Y', 15); const DOMAIN_NAME = getConfigValue('DOMAIN_NAME', null); +const CERT_DOMAIN = getConfigValue('CERT_DOMAIN', null); const getPublicIP = () => new Promise((resolve, reject) => { - https.get(`https://api.homegames.io/ip`, (res) => { + (isSecure ? https : http).get(`${API_URL}/ip`, (res) => { let buf = ''; res.on('data', (chunk) => { buf += chunk.toString(); @@ -67,9 +75,6 @@ const listenable = function(obj, onChange) { const change = Reflect.deleteProperty(target, property); onChange && onChange(); return change; - }, - ayy() { - console.log('i am ayy'); } }; @@ -114,13 +119,14 @@ const generateProxyPlayerId = () => { }; const broadcast = (gameSession) => { - const proxyServer = new WebSocket('wss://public.homegames.link:81'); + const proxyServer = new WebSocket(LINK_PROXY_URL); proxyServer.on('open', () => { log.info('Opened connection to proxy server'); }); proxyServer.on('error', (err) => { + console.log(err); log.error(err); log.info('Unable to connect to proxy server. Public games will be unavailable.'); }); @@ -256,7 +262,7 @@ const socketServer = (gameSession, port, cb = null, certPath = null, username = const requestedGame = jsonMessage.clientInfo && jsonMessage.clientInfo.requestedGame; const req = (certPath ? https : http).request({ - hostname: certPath ? (DOMAIN_NAME || (getUserHash(publicIp)+ '.homegames.link')) : 'localhost', + hostname: certPath ? (DOMAIN_NAME || (`${getUserHash(publicIp)}.${CERT_DOMAIN}`)) : 'localhost', port: HOMENAMES_PORT, path: `/info/${ws.id}`, method: 'GET'