PowerShell/Scripts/start-ipfs-server.ps1

54 lines
2.1 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-12-28 10:30:52 +01:00
Start an IPFS server
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2021-12-28 10:30:52 +01:00
This PowerShell script starts a local IPFS server as a daemon process.
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-09-27 08:35:45 +02:00
PS> ./start-ipfs-server
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-30 10:49:30 +01:00
.NOTES
Author: Markus Fleschutz / License: CC0
2021-06-24 11:39:48 +02:00
#>
try {
2021-08-31 10:32:20 +02:00
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2021-06-24 11:49:59 +02:00
2022-01-18 21:02:19 +01:00
"⏳ Step 1/5: Searching for IPFS executable..."
2021-08-31 10:56:52 +02:00
& ipfs --version
2021-06-24 11:39:48 +02:00
if ($lastExitCode -ne "0") { throw "Can't execute 'ipfs' - make sure IPFS is installed and available" }
2021-08-31 10:32:20 +02:00
""
2022-01-18 21:02:19 +01:00
"⏳ Step 2/5: Initializing IPFS with server profile..."
2021-06-24 11:39:48 +02:00
& ipfs init --profile server
2021-08-31 10:56:52 +02:00
2022-01-18 21:02:19 +01:00
"⏳ Step 3/5: Configuring IPFS..."
2021-06-24 11:39:48 +02:00
& ipfs config Addresses.API /ip4/0.0.0.0/tcp/5001
2022-03-03 09:49:58 +01:00
if ($lastExitCode -ne "0") { throw "'ipfs config Addresses.API' failed (exit code $lastExitCode)" }
2021-06-24 11:39:48 +02:00
& ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/8765
2022-03-03 09:49:58 +01:00
if ($lastExitCode -ne "0") { throw "'ipfs config Addresses.Gateway' failed (exit code $lastExitCode)" }
2021-06-24 11:39:48 +02:00
2021-06-25 15:02:33 +02:00
$Hostname = $(hostname)
2022-04-13 12:06:32 +02:00
& ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '[\"http://miami:5001\", \"http://localhost:3000\", \"http://127.0.0.1:5001\", \"https://webui.ipfs.io\"]'
2022-03-03 09:49:58 +01:00
if ($lastExitCode -ne "0") { throw "'ipfs config Access-Control-Allow-Origin' failed (exit code $lastExitCode)" }
2021-06-24 11:39:48 +02:00
2021-06-25 15:02:33 +02:00
& ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '[\"PUT\", \"POST\"]'
2022-03-03 09:49:58 +01:00
if ($lastExitCode -ne "0") { throw "'ipfs config Access-Control-Allow-Methods' failed (exit code $lastExitCode)" }
2021-08-31 10:32:20 +02:00
""
2022-01-18 21:02:19 +01:00
"⏳ Step 4/5: Increasing UDP receive buffer size..."
2021-06-24 11:39:48 +02:00
& sudo sysctl -w net.core.rmem_max=2500000
2022-03-03 09:49:58 +01:00
if ($lastExitCode -ne "0") { throw "'sysctl' failed (exit code $lastExitCode)" }
2021-08-31 10:32:20 +02:00
""
2022-01-18 21:02:19 +01:00
"⏳ Step 5/5: Starting IPFS daemon..."
2021-06-28 21:29:58 +02:00
# Start-Process nohup 'ipfs daemon'
Start-Process nohup -ArgumentList 'ipfs','daemon' -RedirectStandardOutput "$HOME/console.out" -RedirectStandardError "$HOME/console.err"
2021-06-24 11:39:48 +02:00
2021-08-31 10:32:20 +02:00
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ started IPFS server in $Elapsed sec"
2022-02-10 08:57:52 +01:00
"⚠️ NOTE: make sure your router does not block TCP/UDP port 4001 for IPv4 & IPv6:wq!"
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-06-24 11:39:48 +02:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-06-24 11:39:48 +02:00
exit 1
}