From 6d4522880b7d8e3ab1bf3c86f93d54007e5da220 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Mon, 16 Sep 2024 14:19:29 -0400 Subject: [PATCH 1/2] feat: add ssl to content server adapter --- src/DigNetwork/ContentServer.ts | 35 +++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/DigNetwork/ContentServer.ts b/src/DigNetwork/ContentServer.ts index bde7829..0970a66 100644 --- a/src/DigNetwork/ContentServer.ts +++ b/src/DigNetwork/ContentServer.ts @@ -1,22 +1,31 @@ +import fs from "fs"; import http from "http"; import { URL } from "url"; import { Readable } from "stream"; -import { DataStore } from "../blockchain"; +import { getOrCreateSSLCerts } from "../utils/ssl"; export class ContentServer { private ipAddress: string; private storeId: string; - private static readonly port = 80; + private static certPath: string; + private static keyPath: string; + private static readonly port = 4161; constructor(ipAddress: string, storeId: string) { this.ipAddress = ipAddress; this.storeId = storeId; + + if (!ContentServer.certPath || !ContentServer.keyPath) { + const { certPath, keyPath } = getOrCreateSSLCerts(); + ContentServer.certPath = certPath; + ContentServer.keyPath = keyPath; + } } // Method to get the content of a specified key from the peer, with optional challenge query public async getKey(key: string, rootHash: string, challengeHex?: string): Promise { // Construct the base URL - let url = `http://${this.ipAddress}/chia.${this.storeId}.${rootHash}/${key}`; + let url = `https://${this.ipAddress}/chia.${this.storeId}.${rootHash}/${key}`; // If a challenge is provided, append it as a query parameter if (challengeHex) { @@ -43,25 +52,25 @@ export class ContentServer { // Method to get the .well-known information public async getWellKnown(): Promise { - const url = `http://${this.ipAddress}/.well-known`; + const url = `https://${this.ipAddress}/.well-known`; return this.fetchJson(url); } // Method to get the list of known stores public async getKnownStores(): Promise { - const url = `http://${this.ipAddress}/.well-known/stores`; + const url = `https://${this.ipAddress}/.well-known/stores`; return this.fetchJson(url); } // Method to get the index of all stores public async getStoresIndex(): Promise { - const url = `http://${this.ipAddress}/`; + const url = `https://${this.ipAddress}/`; return this.fetchJson(url); } // Method to get the index of keys in a store public async getKeysIndex(): Promise { - const url = `http://${this.ipAddress}/${this.storeId}`; + const url = `https://${this.ipAddress}/${this.storeId}`; return this.fetchJson(url); } @@ -69,7 +78,7 @@ export class ContentServer { public async headKey( key: string ): Promise<{ success: boolean; headers?: http.IncomingHttpHeaders }> { - const url = `http://${this.ipAddress}/${this.storeId}/${key}`; + const url = `https://${this.ipAddress}/${this.storeId}/${key}`; return this.head(url); // Return the object from head method } @@ -78,14 +87,14 @@ export class ContentServer { success: boolean; headers?: http.IncomingHttpHeaders; }> { - const url = `http://${this.ipAddress}/${this.storeId}`; + const url = `https://${this.ipAddress}/${this.storeId}`; console.log({ url }); return this.head(url); // Return the object from head method } public streamKey(key: string): Promise { return new Promise((resolve, reject) => { - const url = `http://${this.ipAddress}/${this.storeId}/${key}`; + const url = `https://${this.ipAddress}/${this.storeId}/${key}`; const urlObj = new URL(url); const requestOptions = { @@ -141,6 +150,9 @@ export class ContentServer { (urlObj.protocol === "http:" ? 80 : ContentServer.port), path: urlObj.pathname + urlObj.search, method: "HEAD", + key: fs.readFileSync(ContentServer.keyPath), + cert: fs.readFileSync(ContentServer.certPath), + rejectUnauthorized: false, }; const request = http.request(requestOptions, (response) => { @@ -244,6 +256,9 @@ export class ContentServer { port: urlObj.port || ContentServer.port, path: urlObj.pathname + urlObj.search, // Include query params method: "GET", + key: fs.readFileSync(ContentServer.keyPath), + cert: fs.readFileSync(ContentServer.certPath), + rejectUnauthorized: false, }; const request = http.request(requestOptions, (response) => { From ed310bbc1c07197037ff2509be4931983833b7c1 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Mon, 16 Sep 2024 14:20:05 -0400 Subject: [PATCH 2/2] chore(release): 0.0.1-alpha.18 --- CHANGELOG.md | 7 +++++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ecd5443..c8e2ede 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [0.0.1-alpha.18](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.17...v0.0.1-alpha.18) (2024-09-16) + + +### Features + +* add ssl to content server adapter ([6d45228](https://github.com/DIG-Network/dig-chia-sdk/commit/6d4522880b7d8e3ab1bf3c86f93d54007e5da220)) + ### [0.0.1-alpha.17](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.16...v0.0.1-alpha.17) (2024-09-16) diff --git a/package-lock.json b/package-lock.json index a65fe25..2de2bec 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.17", + "version": "0.0.1-alpha.18", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.17", + "version": "0.0.1-alpha.18", "license": "ISC", "dependencies": { "bip39": "^3.1.0", diff --git a/package.json b/package.json index 270e070..08bb0f2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.17", + "version": "0.0.1-alpha.18", "description": "", "type": "commonjs", "main": "./dist/index.js",