eikarna commited on
Commit
5f54a34
·
1 Parent(s): f9b869c

feat: fix start scripts & Dockerfile

Browse files
Files changed (4) hide show
  1. Dockerfile +7 -13
  2. README.md +1 -1
  3. requirements.txt +1 -1
  4. start.sh +42 -19
Dockerfile CHANGED
@@ -36,7 +36,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
36
  COPY . .
37
 
38
  # Install requirements (python 3)
39
- RUN pip install -r requirements.txt
 
40
 
41
  # Berikan izin eksekusi ke semua biner dan skrip yang relevan
42
  RUN chmod +x \
@@ -73,21 +74,13 @@ EXPOSE 20000-65535/udp
73
  # Needed by HuggingFace Spaces (to avoid starting stuck)
74
  EXPOSE 7860
75
 
76
- # Setup sudo
77
  RUN useradd -m nix
78
  RUN echo "nix ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
79
  USER nix
80
 
81
- # Setup VPN
82
- ENV INTERFACE=$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)' | head -1)
83
-
84
- # Setup iptables
85
- RUN iptables -t nat -A PREROUTING -i "$INTERFACE" -p udp --dport 1:5999 -j DNAT --to-destination :3671
86
- RUN iptables -t nat -A PREROUTING -i "$INTERFACE" -p udp --dport 6000:19999 -j DNAT --to-destination :5667
87
- RUN iptables -t nat -A PREROUTING -i "$INTERFACE" -p udp --dport 20000:65535 -j DNAT --to-destination :5666
88
- RUN iptables -t nat -L -n
89
-
90
  # Tentukan entrypoint yang akan menjalankan skrip startup
 
91
  ENTRYPOINT ["/app/start.sh"]
92
 
93
  # ==================================================================================================
@@ -103,8 +96,9 @@ ENTRYPOINT ["/app/start.sh"]
103
  # --sysctl net.ipv4.ip_forward=1 \
104
  # --sysctl net.core.rmem_max=16777216 \
105
  # --sysctl net.core.wmem_max=16777216 \
 
106
  # -p 8080:8080/tcp \
107
- # -p 1000-5000:1000-5000/udp \
108
  # <nama-image-anda>
109
  #
110
  # Di Hugging Face Spaces, Anda perlu mengkonfigurasi ini di `README.md` (metadata).
@@ -115,7 +109,7 @@ ENTRYPOINT ["/app/start.sh"]
115
  # colorFrom: blue
116
  # colorTo: green
117
  # sdk: docker
118
- # app_port: 8080
119
  # docker_args: "--cap-add=NET_ADMIN --cap-add=SYS_NICE"
120
  # ---
121
  #
 
36
  COPY . .
37
 
38
  # Install requirements (python 3)
39
+ # Pastikan Anda memiliki file requirements.txt dengan 'flask' di dalamnya
40
+ RUN pip install --no-cache-dir -r requirements.txt
41
 
42
  # Berikan izin eksekusi ke semua biner dan skrip yang relevan
43
  RUN chmod +x \
 
74
  # Needed by HuggingFace Spaces (to avoid starting stuck)
75
  EXPOSE 7860
76
 
77
+ # Setup sudo untuk user 'nix'
78
  RUN useradd -m nix
79
  RUN echo "nix ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
80
  USER nix
81
 
 
 
 
 
 
 
 
 
 
82
  # Tentukan entrypoint yang akan menjalankan skrip startup
83
+ # Semua logika dinamis (iptables, sysctl) dipindahkan ke start.sh
84
  ENTRYPOINT ["/app/start.sh"]
85
 
86
  # ==================================================================================================
 
96
  # --sysctl net.ipv4.ip_forward=1 \
97
  # --sysctl net.core.rmem_max=16777216 \
98
  # --sysctl net.core.wmem_max=16777216 \
99
+ # -p 7860:7860 \
100
  # -p 8080:8080/tcp \
101
+ # -p 1-65535:1-65535/udp \
102
  # <nama-image-anda>
103
  #
104
  # Di Hugging Face Spaces, Anda perlu mengkonfigurasi ini di `README.md` (metadata).
 
109
  # colorFrom: blue
110
  # colorTo: green
111
  # sdk: docker
112
+ # app_port: 7860
113
  # docker_args: "--cap-add=NET_ADMIN --cap-add=SYS_NICE"
114
  # ---
115
  #
README.md CHANGED
@@ -5,7 +5,7 @@ colorFrom: blue
5
  colorTo: green
6
  sdk: docker
7
  app_port: 7860
8
- docker_args: "--cap-add=NET_ADMIN --cap-add=SYS_NICE --sysctl net.ipv4.ip_forward=1 --sysctl net.core.rmem_max=16777216 --sysctl net.core.wmem_max=16777216 --sysctl net.core.netdev_max_backlog=5000 --sysctl net.core.somaxconn=65535 --sysctl vm.swappiness=1 --sysctl net.ipv4.tcp_fin_timeout=10 --sysctl net.ipv4.tcp_keepalive_time=60"
9
  ---
10
  Mereka memanggil ku seorang pahlawan, karena aku menyelamatkan
11
  mereka dari kehancuran.
 
5
  colorTo: green
6
  sdk: docker
7
  app_port: 7860
8
+ docker_args: "--user root --cap-add=NET_ADMIN --cap-add=SYS_NICE --sysctl net.ipv4.ip_forward=1 --sysctl net.core.rmem_max=16777216 --sysctl net.core.wmem_max=16777216 --sysctl net.core.netdev_max_backlog=5000 --sysctl net.core.somaxconn=65535 --sysctl vm.swappiness=1 --sysctl net.ipv4.tcp_fin_timeout=10 --sysctl net.ipv4.tcp_keepalive_time=60"
9
  ---
10
  Mereka memanggil ku seorang pahlawan, karena aku menyelamatkan
11
  mereka dari kehancuran.
requirements.txt CHANGED
@@ -1 +1 @@
1
- flask
 
1
+ flask
start.sh CHANGED
@@ -2,6 +2,7 @@
2
  set -m
3
 
4
  echo "=== Memulai Web Server Flask untuk IP Publik ==="
 
5
  python3 -c '
6
  from flask import Flask, Response
7
  import urllib.request
@@ -9,20 +10,21 @@ import json
9
  import os
10
 
11
  app = Flask(__name__)
12
- PORT = 7860
13
 
14
  def get_public_ip():
15
  """Fetches the public IP address from an external service."""
16
- urls = ["https://api.ipify.org?format=json", "https://ipinfo.io/json"]
17
  for url in urls:
18
  try:
19
- # Use a timeout to prevent the request from hanging indefinitely
20
  with urllib.request.urlopen(url, timeout=5) as response:
21
  if response.status == 200:
22
  data = json.loads(response.read().decode("utf-8"))
23
- if "ip" in data:
 
 
24
  print(f"Successfully fetched IP from {url}")
25
- return data["ip"]
26
  except Exception as e:
27
  print(f"Gagal mendapatkan IP dari {url}: {e}")
28
 
@@ -36,16 +38,43 @@ def get_ip_route():
36
  return Response(public_ip, mimetype="text/plain")
37
 
38
  if __name__ == "__main__":
39
- # Use host="0.0.0.0" to be accessible from outside the container.
40
- # Port 7860 is the default for Hugging Face Spaces.
41
- print(f"Server Flask IP dimulai di http://0.0.0.0:{PORT}")
42
  app.run(host="0.0.0.0", port=PORT)
43
  ' &
44
  WEBSERVER_PID=$!
45
  echo "Web server Flask berjalan di background dengan PID: $WEBSERVER_PID"
 
46
 
47
  echo "=== Memulai Konfigurasi Server VPN ==="
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  # 4. Mengatur Aturan Firewall (iptables)
50
  echo "Membersihkan aturan iptables sebelumnya..."
51
  sudo iptables -F
@@ -63,7 +92,6 @@ echo "Aturan iptables berhasil diterapkan."
63
  sudo iptables -t nat -L -n
64
 
65
  # 5. Menjalankan Layanan VPN di Background
66
-
67
  # badvpn-udpgw
68
  echo "Menjalankan badvpn-udpgw..."
69
  cd /app/udpgw
@@ -79,19 +107,14 @@ UDP_CUSTOM_PID=$!
79
  # zivpn
80
  echo "Menjalankan zivpn..."
81
  cd /app/zivpn
82
- ./udp-zivpn-linux-amd64 server -c config.json &
83
- ZIVPN_PID=$!
84
 
85
- echo "=== Semua layanan telah dimulai ==="
86
- echo "PID: webserver=$WEBSERVER_PID, badvpn=$BADVPN_PID, udp-custom=$UDP_CUSTOM_PID, zivpn=$ZIVPN_PID"
87
 
88
  # 6. Tunggu sinyal keluar dan bersihkan
89
  # trap "echo 'Menutup layanan...'; kill $WEBSERVER_PID $BADVPN_PID $UDP_CUSTOM_PID $ZIVPN_PID; exit 0" SIGINT SIGTERM
90
 
91
- # Tunggu semua proses background selesai
92
- # fg %1 akan membawa proses pertama ke foreground, menjaga kontainer tetap berjalan
93
- # dan memungkinkan trap untuk menangani sinyal dengan benar.
94
  # wait $WEBSERVER_PID
95
- # wait $BADVPN_PID
96
- # wait $UDP_CUSTOM_PID
97
- # wait $ZIVPN_PID
 
2
  set -m
3
 
4
  echo "=== Memulai Web Server Flask untuk IP Publik ==="
5
+ # Skrip Flask dijalankan di background. Port 7860 adalah default untuk HF Spaces.
6
  python3 -c '
7
  from flask import Flask, Response
8
  import urllib.request
 
10
  import os
11
 
12
  app = Flask(__name__)
13
+ PORT = int(os.environ.get("PORT", 7860))
14
 
15
  def get_public_ip():
16
  """Fetches the public IP address from an external service."""
17
+ urls = ["https://api.ipify.org?format=json", "https://ipinfo.io/json", "https://httpbin.org/ip"]
18
  for url in urls:
19
  try:
 
20
  with urllib.request.urlopen(url, timeout=5) as response:
21
  if response.status == 200:
22
  data = json.loads(response.read().decode("utf-8"))
23
+ # Handle different JSON responses
24
+ ip = data.get("ip") or data.get("origin")
25
+ if ip:
26
  print(f"Successfully fetched IP from {url}")
27
+ return ip
28
  except Exception as e:
29
  print(f"Gagal mendapatkan IP dari {url}: {e}")
30
 
 
38
  return Response(public_ip, mimetype="text/plain")
39
 
40
  if __name__ == "__main__":
 
 
 
41
  app.run(host="0.0.0.0", port=PORT)
42
  ' &
43
  WEBSERVER_PID=$!
44
  echo "Web server Flask berjalan di background dengan PID: $WEBSERVER_PID"
45
+ sleep 5 # Beri waktu sejenak untuk server Flask memulai
46
 
47
  echo "=== Memulai Konfigurasi Server VPN ==="
48
 
49
+ # 1. Mengaktifkan IP Forwarding & Optimasi Kernel (sysctl)
50
+ # Perintah ini memerlukan hak akses root, jadi kita gunakan sudo.
51
+ # Flag --cap-add=NET_ADMIN diperlukan saat menjalankan container.
52
+ echo "Mengaktifkan IP forwarding dan optimasi kernel..."
53
+ sudo sysctl -w net.ipv4.ip_forward=1
54
+ sudo sysctl -w net.core.rmem_max=16777216
55
+ sudo sysctl -w net.core.wmem_max=16777216
56
+ sudo sysctl -w net.core.netdev_max_backlog=5000
57
+ sudo sysctl -w net.core.somaxconn=65535
58
+ sudo sysctl -w vm.swappiness=1
59
+ # Nonaktifkan pesan error jika file tidak ada
60
+ sudo sysctl -w -e net.ipv4.tcp_fin_timeout=10
61
+ sudo sysctl -w -e net.ipv4.tcp_keepalive_time=60
62
+
63
+ # 2. Menemukan interface jaringan utama
64
+ # Ini harus dijalankan di dalam container saat runtime.
65
+ INTERFACE=$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)' | head -1)
66
+ if [ -z "$INTERFACE" ]; then
67
+ echo "ERROR: Tidak dapat menemukan interface jaringan default. Menggunakan 'eth0'."
68
+ INTERFACE="eth0"
69
+ fi
70
+ echo "Menggunakan interface: $INTERFACE"
71
+
72
+ # 3. Mengatur MTU & Offloading
73
+ # Memerlukan ethtool dan hak akses root (sudo).
74
+ echo "Mengatur MTU dan offloading pada interface $INTERFACE..."
75
+ sudo ip link set dev "$INTERFACE" mtu 9000
76
+ sudo ethtool -K "$INTERFACE" tso on gso on gro on || echo "Peringatan: ethtool tidak dapat mengatur offloading."
77
+
78
  # 4. Mengatur Aturan Firewall (iptables)
79
  echo "Membersihkan aturan iptables sebelumnya..."
80
  sudo iptables -F
 
92
  sudo iptables -t nat -L -n
93
 
94
  # 5. Menjalankan Layanan VPN di Background
 
95
  # badvpn-udpgw
96
  echo "Menjalankan badvpn-udpgw..."
97
  cd /app/udpgw
 
107
  # zivpn
108
  echo "Menjalankan zivpn..."
109
  cd /app/zivpn
110
+ ./udp-zivpn-linux-amd64 server -c config.json
111
+ # ZIVPN_PID=$!
112
 
113
+ # echo "=== Semua layanan telah dimulai ==="
114
+ # echo "PID: webserver=$WEBSERVER_PID, badvpn=$BADVPN_PID, udp-custom=$UDP_CUSTOM_PID, zivpn=$ZIVPN_PID"
115
 
116
  # 6. Tunggu sinyal keluar dan bersihkan
117
  # trap "echo 'Menutup layanan...'; kill $WEBSERVER_PID $BADVPN_PID $UDP_CUSTOM_PID $ZIVPN_PID; exit 0" SIGINT SIGTERM
118
 
119
+ # Tunggu semua proses background selesai. Ini menjaga container tetap berjalan.
 
 
120
  # wait $WEBSERVER_PID