user 7 tahun lalu
induk
melakukan
7857f4ad87
3 mengubah file dengan 63 tambahan dan 3 penghapusan
  1. 15 3
      Makefile
  2. 25 0
      add_public_route
  3. 23 0
      remove_public_route

+ 15 - 3
Makefile

@@ -5,7 +5,7 @@ ID ?= ipfstest
 LOCAL_DIR ?= /tmp/ipfsdata
 LOCAL_STAGING_DIR ?= /tmp/staging
 
-default: build
+default: init build run
 
 init:
 	bash init_container
@@ -17,8 +17,7 @@ shell:
 	docker exec -it $(ID) /bin/bash
 
 run:
-	mkdir -p $(LOCAL_DIR)
-	mkdir -p $(LOCAL_STAGING_DIR)
+	mkdir -p $(LOCAL_DIR) $(LOCAL_STAGING_DIR)
 	docker run --name $(ID) -v $(LOCAL_STAGING_DIR):/staging -v $(LOCAL_DIR):/home/ipfs/.ipfs $(NAME)
 
 start:
@@ -35,3 +34,16 @@ rm: stop
 
 ps:
 	docker ps -a
+
+public:
+	bash add_public_route $(ID) $(PUBLIC_IP) $(PUBLIC_IFACE)
+	docker exec $(ID) ipfs config Addresses.Swarm --json '[ "/ip4/0.0.0.0/tcp/40001" ]'
+	docker restart $(ID)
+
+private:
+	bash remove_public_route $(ID) $(PUBLIC_IP) $(PUBLIC_IFACE)
+	docker exec $(ID) ipfs config Addresses.Swarm --json '[ "/ip4/127.0.0.1/tcp/40001" ]'
+	docker restart $(ID)
+
+genswarmkey:
+	bash generate_swarm_key

+ 25 - 0
add_public_route

@@ -0,0 +1,25 @@
+#!/bin/bash
+
+set -e
+set -x
+
+base="$( cd "$(dirname "$0")" ; pwd -P )"
+
+id="$1"
+public_ip="$2"
+public_iface="$3"
+
+if [ -z "$public_iface" ]; then
+    echo >&2 "usage: $0 ctid public_ip public_iface"
+    exit 1
+fi
+
+$base/remove_public_route "$id" "$public_ip" "$public_iface" || true
+
+docker_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $id)
+
+if [ -n "$docker_ip" ]; then
+    iptables -t nat -A PREROUTING -d $public_ip/32 -i $public_iface -j DNAT --to-destination $docker_ip
+fi
+
+exit 0

+ 23 - 0
remove_public_route

@@ -0,0 +1,23 @@
+#!/bin/bash
+
+set -e
+set -x
+
+base="$( cd "$(dirname "$0")" ; pwd -P )"
+
+id="$1"
+public_ip="$2"
+public_iface="$3"
+
+if [ -z "$public_iface" ]; then
+    echo >&2 "usage: $0 ctid public_ip public_iface"
+    exit 1
+fi
+
+docker_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $id)
+
+if [ -n "$docker_ip" ]; then
+    iptables -t nat -D PREROUTING -d $public_ip/32 -i $public_iface -j DNAT --to-destination $docker_ip || true
+fi
+
+exit 0