From fecca0ae82000bc5d24b1345a55fc8429c3e28ab Mon Sep 17 00:00:00 2001
From: Markus Fleschutz <markus@fleschutz.de>
Date: Thu, 24 Jun 2021 11:39:48 +0200
Subject: [PATCH] Add start-ipfs-server.sh

---
 Data/scripts.csv              |  1 +
 README.md                     |  1 +
 Scripts/ipfs-server.sh        | 24 -------------------
 Scripts/start-ipfs-server.ps1 | 43 +++++++++++++++++++++++++++++++++++
 4 files changed, 45 insertions(+), 24 deletions(-)
 delete mode 100755 Scripts/ipfs-server.sh
 create mode 100755 Scripts/start-ipfs-server.ps1

diff --git a/Data/scripts.csv b/Data/scripts.csv
index 3a224857..b4101c45 100644
--- a/Data/scripts.csv
+++ b/Data/scripts.csv
@@ -181,6 +181,7 @@ speak-joke.ps1, speaks the next joke by text-to-speech (TTS)
 speak-test.ps1, performs a speak test by text-to-speech (TTS)
 speak-text.ps1, speaks the given text by text-to-speech (TTS)
 speak-time.ps1, speaks the current time by text-to-speech (TTS)
+start-ipfs-server.ps1, starts a local IPFS server
 switch-branch.ps1, switches the branch in the current/given Git repository (including submodules)
 switch-shelly1.ps1, switches a Shelly1 device in the local network
 sync-repo.ps1, synchronizes a Git repository by push & pull (including submodules)
diff --git a/README.md b/README.md
index 011583d3..8eb92428 100644
--- a/README.md
+++ b/README.md
@@ -218,6 +218,7 @@ Mega Collection of PowerShell Scripts
 * [set-timer.ps1](Scripts/set-timer.ps1) - sets a timer for a countdown
 * [simulate-matrix.ps1](Scripts/simulate-matrix.ps1) - simulates the Matrix (fun)
 * [simulate-presence.ps1](Scripts/simulate-presence.ps1) - simulates the human presence against burglars
+* [start-ipfs-server.ps1](Scripts/start-ipfs-server.ps1) - starts a local IPFS server
 * [switch-shelly1.ps1](Scripts/switch-shelly1.ps1) - switches a Shelly1 device in the local network
 * [translate-file.ps1](Scripts/translate-file.ps1) - translates the given text file into other languages
 * [translate-files.ps1](Scripts/translate-files.ps1) - translates the given text files into any supported language
diff --git a/Scripts/ipfs-server.sh b/Scripts/ipfs-server.sh
deleted file mode 100755
index ec1dd3f0..00000000
--- a/Scripts/ipfs-server.sh
+++ /dev/null
@@ -1,24 +0,0 @@
-#!/bin/sh
-
-# Syntax:       ./ipfs-server
-# Description:	starts an IPFS server
-# Author:	Markus Fleschutz
-# Source:	github.com/fleschutz/PowerShell
-# License:	CC0
-
-HOSTNAME="`hostname`"
-ipfs init --profile server
-
-ipfs config Addresses.API /ip4/0.0.0.0/tcp/5001
-ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/8765
-ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["http://raspi:5001", "http://127.0.0.1:5001", "https://webui.ipfs.io"]'
-ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST"]'
-
-echo "NOTE: Make sure port 4001 for IPv4/v6 is open by your router!"
-while true; do
-	ipfs daemon 
-	echo "IPFS server has stopped, restarting ..."
-	sleep 1
-done
-exit 0
-
diff --git a/Scripts/start-ipfs-server.ps1 b/Scripts/start-ipfs-server.ps1
new file mode 100755
index 00000000..ef31a728
--- /dev/null
+++ b/Scripts/start-ipfs-server.ps1
@@ -0,0 +1,43 @@
+<#
+.SYNTAX       start-ipfs-server.ps1
+.DESCRIPTION  starts a local IPFS server
+.LINK         https://github.com/fleschutz/PowerShell
+.NOTES        Author: Markus Fleschutz / License: CC0
+#>
+
+try {
+	$Result = (ipfs --version)
+	if ($lastExitCode -ne "0") { throw "Can't execute 'ipfs' - make sure IPFS is installed and available" }
+
+	"Step 1/4: initializing IPFS..."
+	& ipfs init --profile server
+
+	"Step 2/4: configuring IPFS..."
+	& ipfs config Addresses.API /ip4/0.0.0.0/tcp/5001
+	if ($lastExitCode -ne "0") { throw "'ipfs config Addresses.API' failed" }
+
+	& ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/8765
+	if ($lastExitCode -ne "0") { throw "'ipfs config Addresses.Gateway' failed" }
+
+#	& ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["http://raspi:5001", "http://127.0.0.1:5001", "https://webui.ipfs.io"]'
+#	if ($lastExitCode -ne "0") { throw "'ipfs config Access-Control-Allow-Origin' failed" }
+
+#	& ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST"]'
+#	if ($lastExitCode -ne "0") { throw "'ipfs config Access-Control-Allow-Methods' failed" }
+
+	"Step 3/4: increasing UDP receive buffer size..."
+	& sudo sysctl -w net.core.rmem_max=2500000
+	if ($lastExitCode -ne "0") { throw "'sysctl' failed" }
+
+	"Step 4/4: starting IPFS daemon..."
+	"NOTE: make sure your router does not block port 4001 (TCP/UDP) for IPv4/v6!"
+
+	& ipfs daemon 
+	if ($lastExitCode -ne "0") { throw "'ipfs daemon' failed" }
+
+	"IPFS server has stopped"
+	exit 1
+} catch {
+	write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
+	exit 1
+}