diff options
| author | bitflash-coin <bookofshamers@outlook.com> | 2026-08-04 06:34:32 -0700 |
|---|---|---|
| committer | bitflash-coin <bookofshamers@outlook.com> | 2026-08-04 06:34:32 -0700 |
| commit | 5ce650382352c1503f40abf08129ada69402af55 (patch) | |
| tree | 6a18198901f1e2c4933f1ac53c9365eec512bf63 /relay/install-bitflash-relay.sh | |
| parent | f34113291da7e5e3dc7d2468d4f9c212c05c584b (diff) | |
public commit
Diffstat (limited to 'relay/install-bitflash-relay.sh')
| -rw-r--r-- | relay/install-bitflash-relay.sh | 345 |
1 files changed, 345 insertions, 0 deletions
diff --git a/relay/install-bitflash-relay.sh b/relay/install-bitflash-relay.sh new file mode 100644 index 0000000..fd901ac --- /dev/null +++ b/relay/install-bitflash-relay.sh | |||
| @@ -0,0 +1,345 @@ | |||
| 1 | #!/usr/bin/env bash | ||
| 2 | # Bitflash rendezvous relay -- one-shot installer for Linux. | ||
| 3 | # Anyone can run this on a machine with a PUBLIC IP (a cheap VPS) to become a | ||
| 4 | # meeting node that lets Bitflash users behind CGNAT reach each other. | ||
| 5 | # The relay holds NO keys and reads NOTHING -- it only forwards ciphertext. | ||
| 6 | # | ||
| 7 | # Usage: | ||
| 8 | # sudo bash install-bitflash-relay.sh [PORT] | ||
| 9 | # PORT defaults to 8434. Remember to open that port in your firewall/panel. | ||
| 10 | |||
| 11 | set -e | ||
| 12 | PORT="${1:-8434}" | ||
| 13 | DIR="$HOME/bitflash-relay" | ||
| 14 | mkdir -p "$DIR" && cd "$DIR" | ||
| 15 | |||
| 16 | echo ">> writing sources to $DIR" | ||
| 17 | |||
| 18 | cat > btfrv.h <<'BTFRV_H_EOF' | ||
| 19 | // Copyright (c) 2026 Bitflash developers | ||
| 20 | // Distributed under the MIT/X11 software license. | ||
| 21 | #ifndef BITFLASH_BTFRV_H | ||
| 22 | #define BITFLASH_BTFRV_H | ||
| 23 | #include <stdint.h> | ||
| 24 | namespace btf | ||
| 25 | { | ||
| 26 | typedef intptr_t RvSocket; | ||
| 27 | static const RvSocket RV_INVALID = (RvSocket)-1; | ||
| 28 | bool RvInit(); | ||
| 29 | bool RvRelayRun(unsigned short port); | ||
| 30 | void RvRelayStop(); | ||
| 31 | RvSocket RvServiceRegister(const char* relay_host, unsigned short port, | ||
| 32 | const unsigned char my_pubkey[32]); | ||
| 33 | RvSocket RvClientConnect(const char* relay_host, unsigned short port, | ||
| 34 | const unsigned char target_pubkey[32]); | ||
| 35 | bool RvReadN(RvSocket s, void* buf, int n); | ||
| 36 | bool RvWriteN(RvSocket s, const void* buf, int n); | ||
| 37 | void RvClose(RvSocket s); | ||
| 38 | } // namespace btf | ||
| 39 | #endif | ||
| 40 | BTFRV_H_EOF | ||
| 41 | |||
| 42 | cat > btfrv.cpp <<'BTFRV_CPP_EOF' | ||
| 43 | // Copyright (c) 2026 Bitflash developers | ||
| 44 | // Distributed under the MIT/X11 software license. | ||
| 45 | #include "btfrv.h" | ||
| 46 | #ifdef _WIN32 | ||
| 47 | #include <winsock2.h> | ||
| 48 | #include <ws2tcpip.h> | ||
| 49 | #define CLOSESOCK closesocket | ||
| 50 | #else | ||
| 51 | #include <sys/socket.h> | ||
| 52 | #include <netinet/in.h> | ||
| 53 | #include <arpa/inet.h> | ||
| 54 | #include <netdb.h> | ||
| 55 | #include <unistd.h> | ||
| 56 | #include <csignal> | ||
| 57 | typedef int SOCKET; | ||
| 58 | #define INVALID_SOCKET (-1) | ||
| 59 | #define SOCKET_ERROR (-1) | ||
| 60 | #define CLOSESOCK ::close | ||
| 61 | #ifndef SD_BOTH | ||
| 62 | #define SD_BOTH SHUT_RDWR | ||
| 63 | #endif | ||
| 64 | #endif | ||
| 65 | #include <cstring> | ||
| 66 | #include <cstdio> | ||
| 67 | #include <string> | ||
| 68 | #include <map> | ||
| 69 | #include <thread> | ||
| 70 | #include <mutex> | ||
| 71 | #include <atomic> | ||
| 72 | namespace btf | ||
| 73 | { | ||
| 74 | static std::atomic<bool> g_stop(false); | ||
| 75 | static SOCKET g_listen = INVALID_SOCKET; | ||
| 76 | static std::map<std::string, SOCKET> g_waiting; | ||
| 77 | static std::mutex g_mtx; | ||
| 78 | bool RvInit() | ||
| 79 | { | ||
| 80 | static bool done = false; | ||
| 81 | if (done) return true; | ||
| 82 | #ifdef _WIN32 | ||
| 83 | WSADATA wsa; | ||
| 84 | if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) return false; | ||
| 85 | #else | ||
| 86 | signal(SIGPIPE, SIG_IGN); | ||
| 87 | #endif | ||
| 88 | done = true; | ||
| 89 | return true; | ||
| 90 | } | ||
| 91 | static bool ReadN(SOCKET s, void* buf, int n) | ||
| 92 | { | ||
| 93 | char* p = (char*)buf; | ||
| 94 | int off = 0; | ||
| 95 | while (off < n) | ||
| 96 | { | ||
| 97 | int r = recv(s, p + off, n - off, 0); | ||
| 98 | if (r <= 0) return false; | ||
| 99 | off += r; | ||
| 100 | } | ||
| 101 | return true; | ||
| 102 | } | ||
| 103 | static bool WriteN(SOCKET s, const void* buf, int n) | ||
| 104 | { | ||
| 105 | const char* p = (const char*)buf; | ||
| 106 | int off = 0; | ||
| 107 | while (off < n) | ||
| 108 | { | ||
| 109 | int r = send(s, p + off, n - off, 0); | ||
| 110 | if (r <= 0) return false; | ||
| 111 | off += r; | ||
| 112 | } | ||
| 113 | return true; | ||
| 114 | } | ||
| 115 | static SOCKET ConnectTo(const char* host, unsigned short port) | ||
| 116 | { | ||
| 117 | struct addrinfo hints, *res = NULL; | ||
| 118 | memset(&hints, 0, sizeof(hints)); | ||
| 119 | hints.ai_family = AF_INET; | ||
| 120 | hints.ai_socktype = SOCK_STREAM; | ||
| 121 | char portstr[16]; sprintf(portstr, "%u", (unsigned)port); | ||
| 122 | if (getaddrinfo(host, portstr, &hints, &res) != 0 || !res) return INVALID_SOCKET; | ||
| 123 | SOCKET s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); | ||
| 124 | if (s == INVALID_SOCKET) { freeaddrinfo(res); return INVALID_SOCKET; } | ||
| 125 | if (connect(s, res->ai_addr, (int)res->ai_addrlen) != 0) | ||
| 126 | { | ||
| 127 | CLOSESOCK(s); freeaddrinfo(res); return INVALID_SOCKET; | ||
| 128 | } | ||
| 129 | freeaddrinfo(res); | ||
| 130 | return s; | ||
| 131 | } | ||
| 132 | static void Forward(SOCKET from, SOCKET to) | ||
| 133 | { | ||
| 134 | char buf[32 * 1024]; | ||
| 135 | for (;;) | ||
| 136 | { | ||
| 137 | int r = recv(from, buf, sizeof(buf), 0); | ||
| 138 | if (r <= 0) break; | ||
| 139 | if (!WriteN(to, buf, r)) break; | ||
| 140 | } | ||
| 141 | shutdown(to, SD_BOTH); | ||
| 142 | CLOSESOCK(from); | ||
| 143 | } | ||
| 144 | static void HandleIncoming(SOCKET s) | ||
| 145 | { | ||
| 146 | unsigned char hdr[33]; | ||
| 147 | if (!ReadN(s, hdr, 33)) { CLOSESOCK(s); return; } | ||
| 148 | char role = (char)hdr[0]; | ||
| 149 | std::string pubkey((char*)hdr + 1, 32); | ||
| 150 | if (role == 'S') | ||
| 151 | { | ||
| 152 | std::lock_guard<std::mutex> lk(g_mtx); | ||
| 153 | std::map<std::string, SOCKET>::iterator it = g_waiting.find(pubkey); | ||
| 154 | if (it != g_waiting.end()) CLOSESOCK(it->second); | ||
| 155 | g_waiting[pubkey] = s; | ||
| 156 | } | ||
| 157 | else if (role == 'C') | ||
| 158 | { | ||
| 159 | SOCKET svc = INVALID_SOCKET; | ||
| 160 | { | ||
| 161 | std::lock_guard<std::mutex> lk(g_mtx); | ||
| 162 | std::map<std::string, SOCKET>::iterator it = g_waiting.find(pubkey); | ||
| 163 | if (it != g_waiting.end()) { svc = it->second; g_waiting.erase(it); } | ||
| 164 | } | ||
| 165 | if (svc == INVALID_SOCKET) | ||
| 166 | { | ||
| 167 | unsigned char no = 0x00; | ||
| 168 | WriteN(s, &no, 1); | ||
| 169 | CLOSESOCK(s); | ||
| 170 | return; | ||
| 171 | } | ||
| 172 | unsigned char ok = 0x01; | ||
| 173 | bool a = WriteN(svc, &ok, 1); | ||
| 174 | bool b = WriteN(s, &ok, 1); | ||
| 175 | if (!a || !b) { CLOSESOCK(svc); CLOSESOCK(s); return; } | ||
| 176 | std::thread(Forward, s, svc).detach(); | ||
| 177 | std::thread(Forward, svc, s).detach(); | ||
| 178 | } | ||
| 179 | else | ||
| 180 | { | ||
| 181 | CLOSESOCK(s); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | bool RvRelayRun(unsigned short port) | ||
| 185 | { | ||
| 186 | if (!RvInit()) return false; | ||
| 187 | g_stop = false; | ||
| 188 | g_listen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | ||
| 189 | if (g_listen == INVALID_SOCKET) return false; | ||
| 190 | int yes = 1; | ||
| 191 | setsockopt(g_listen, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes)); | ||
| 192 | struct sockaddr_in addr; | ||
| 193 | memset(&addr, 0, sizeof(addr)); | ||
| 194 | addr.sin_family = AF_INET; | ||
| 195 | addr.sin_addr.s_addr = INADDR_ANY; | ||
| 196 | addr.sin_port = htons(port); | ||
| 197 | if (bind(g_listen, (struct sockaddr*)&addr, sizeof(addr)) != 0) { CLOSESOCK(g_listen); return false; } | ||
| 198 | if (listen(g_listen, 16) != 0) { CLOSESOCK(g_listen); return false; } | ||
| 199 | while (!g_stop) | ||
| 200 | { | ||
| 201 | struct sockaddr_in cli; | ||
| 202 | socklen_t len = sizeof(cli); | ||
| 203 | SOCKET s = accept(g_listen, (struct sockaddr*)&cli, &len); | ||
| 204 | if (s == INVALID_SOCKET) | ||
| 205 | { | ||
| 206 | if (g_stop) break; | ||
| 207 | continue; | ||
| 208 | } | ||
| 209 | std::thread(HandleIncoming, s).detach(); | ||
| 210 | } | ||
| 211 | CLOSESOCK(g_listen); | ||
| 212 | g_listen = INVALID_SOCKET; | ||
| 213 | return true; | ||
| 214 | } | ||
| 215 | void RvRelayStop() | ||
| 216 | { | ||
| 217 | g_stop = true; | ||
| 218 | if (g_listen != INVALID_SOCKET) | ||
| 219 | CLOSESOCK(g_listen); | ||
| 220 | } | ||
| 221 | RvSocket RvServiceRegister(const char* relay_host, unsigned short port, | ||
| 222 | const unsigned char my_pubkey[32]) | ||
| 223 | { | ||
| 224 | if (!RvInit()) return RV_INVALID; | ||
| 225 | SOCKET s = ConnectTo(relay_host, port); | ||
| 226 | if (s == INVALID_SOCKET) return RV_INVALID; | ||
| 227 | unsigned char hdr[33]; | ||
| 228 | hdr[0] = (unsigned char)'S'; | ||
| 229 | memcpy(hdr + 1, my_pubkey, 32); | ||
| 230 | if (!WriteN(s, hdr, 33)) { CLOSESOCK(s); return RV_INVALID; } | ||
| 231 | unsigned char paired = 0; | ||
| 232 | if (!ReadN(s, &paired, 1) || paired != 0x01) { CLOSESOCK(s); return RV_INVALID; } | ||
| 233 | return (RvSocket)s; | ||
| 234 | } | ||
| 235 | RvSocket RvClientConnect(const char* relay_host, unsigned short port, | ||
| 236 | const unsigned char target_pubkey[32]) | ||
| 237 | { | ||
| 238 | if (!RvInit()) return RV_INVALID; | ||
| 239 | SOCKET s = ConnectTo(relay_host, port); | ||
| 240 | if (s == INVALID_SOCKET) return RV_INVALID; | ||
| 241 | unsigned char hdr[33]; | ||
| 242 | hdr[0] = (unsigned char)'C'; | ||
| 243 | memcpy(hdr + 1, target_pubkey, 32); | ||
| 244 | if (!WriteN(s, hdr, 33)) { CLOSESOCK(s); return RV_INVALID; } | ||
| 245 | unsigned char paired = 0; | ||
| 246 | if (!ReadN(s, &paired, 1) || paired != 0x01) { CLOSESOCK(s); return RV_INVALID; } | ||
| 247 | return (RvSocket)s; | ||
| 248 | } | ||
| 249 | bool RvReadN(RvSocket s, void* buf, int n) { return ReadN((SOCKET)s, buf, n); } | ||
| 250 | bool RvWriteN(RvSocket s, const void* buf, int n) { return WriteN((SOCKET)s, buf, n); } | ||
| 251 | void RvClose(RvSocket s) { if (s != RV_INVALID) CLOSESOCK((SOCKET)s); } | ||
| 252 | } // namespace btf | ||
| 253 | BTFRV_CPP_EOF | ||
| 254 | |||
| 255 | cat > bitflashd.cpp <<'BITFLASHD_EOF' | ||
| 256 | // Copyright (c) 2026 Bitflash developers | ||
| 257 | // Distributed under the MIT/X11 software license. | ||
| 258 | #include "btfrv.h" | ||
| 259 | #include <cstdio> | ||
| 260 | #include <cstdlib> | ||
| 261 | #include <cstring> | ||
| 262 | int main(int argc, char* argv[]) | ||
| 263 | { | ||
| 264 | unsigned short port = 8434; | ||
| 265 | if (argc >= 2) | ||
| 266 | port = (unsigned short)atoi(argv[1]); | ||
| 267 | printf("bitflashd -- Bitflash rendezvous relay\n"); | ||
| 268 | printf("listening on 0.0.0.0:%u (open this port in your firewall)\n", port); | ||
| 269 | fflush(stdout); | ||
| 270 | if (!btf::RvInit()) | ||
| 271 | { | ||
| 272 | fprintf(stderr, "bitflashd: socket init failed\n"); | ||
| 273 | return 1; | ||
| 274 | } | ||
| 275 | if (!btf::RvRelayRun(port)) | ||
| 276 | { | ||
| 277 | fprintf(stderr, "bitflashd: could not bind/listen on port %u\n", port); | ||
| 278 | return 1; | ||
| 279 | } | ||
| 280 | return 0; | ||
| 281 | } | ||
| 282 | BITFLASHD_EOF | ||
| 283 | |||
| 284 | # g++ (Debian/Ubuntu: build-essential; RHEL/Alma: gcc-c++) | ||
| 285 | if ! command -v g++ >/dev/null 2>&1; then | ||
| 286 | echo ">> installing g++" | ||
| 287 | if command -v apt-get >/dev/null 2>&1; then apt-get update && apt-get install -y g++ | ||
| 288 | elif command -v dnf >/dev/null 2>&1; then dnf install -y gcc-c++ | ||
| 289 | elif command -v yum >/dev/null 2>&1; then yum install -y gcc-c++ | ||
| 290 | else echo "!! install a C++ compiler (g++) manually and re-run"; exit 1 | ||
| 291 | fi | ||
| 292 | fi | ||
| 293 | |||
| 294 | echo ">> building bitflashd" | ||
| 295 | g++ -std=gnu++14 -O2 -pthread bitflashd.cpp btfrv.cpp -o bitflashd | ||
| 296 | echo ">> build OK: $DIR/bitflashd" | ||
| 297 | |||
| 298 | # Basic anti-flood firewall rules (idempotent). These soak up single-source | ||
| 299 | # connection floods; volumetric DDoS still needs provider-level protection. | ||
| 300 | # Limits are generous so many users behind one CGNAT public IP aren't blocked. | ||
| 301 | if command -v iptables >/dev/null 2>&1; then | ||
| 302 | echo ">> applying connection rate-limit on port $PORT" | ||
| 303 | # Cap concurrent connections per source IP (stops one IP hogging thousands) | ||
| 304 | iptables -C INPUT -p tcp --dport "$PORT" -m connlimit --connlimit-above 200 --connlimit-mask 32 -j REJECT --reject-with tcp-reset 2>/dev/null || \ | ||
| 305 | iptables -A INPUT -p tcp --dport "$PORT" -m connlimit --connlimit-above 200 --connlimit-mask 32 -j REJECT --reject-with tcp-reset | ||
| 306 | # Cap NEW-connection rate per source IP (stops connection floods) | ||
| 307 | iptables -C INPUT -p tcp --dport "$PORT" -m conntrack --ctstate NEW -m hashlimit \ | ||
| 308 | --hashlimit-name btfrelay --hashlimit-mode srcip --hashlimit-above 60/min --hashlimit-burst 100 -j DROP 2>/dev/null || \ | ||
| 309 | iptables -A INPUT -p tcp --dport "$PORT" -m conntrack --ctstate NEW -m hashlimit \ | ||
| 310 | --hashlimit-name btfrelay --hashlimit-mode srcip --hashlimit-above 60/min --hashlimit-burst 100 -j DROP | ||
| 311 | # Persist across reboots if the tooling is present | ||
| 312 | command -v netfilter-persistent >/dev/null 2>&1 && netfilter-persistent save >/dev/null 2>&1 || true | ||
| 313 | else | ||
| 314 | echo ">> iptables not found; skipping rate-limit (host on a provider with anti-DDoS)" | ||
| 315 | fi | ||
| 316 | |||
| 317 | # systemd service so it survives reboots (skipped if systemd is absent) | ||
| 318 | if command -v systemctl >/dev/null 2>&1 && [ -d /etc/systemd/system ]; then | ||
| 319 | cat > /etc/systemd/system/bitflashd.service <<EOF | ||
| 320 | [Unit] | ||
| 321 | Description=Bitflash rendezvous relay | ||
| 322 | After=network.target | ||
| 323 | |||
| 324 | [Service] | ||
| 325 | ExecStart=$DIR/bitflashd $PORT | ||
| 326 | Restart=always | ||
| 327 | User=$(id -un) | ||
| 328 | |||
| 329 | [Install] | ||
| 330 | WantedBy=multi-user.target | ||
| 331 | EOF | ||
| 332 | systemctl daemon-reload | ||
| 333 | systemctl enable --now bitflashd | ||
| 334 | sleep 1 | ||
| 335 | systemctl status bitflashd --no-pager || true | ||
| 336 | echo ">> relay running as a service on port $PORT" | ||
| 337 | else | ||
| 338 | echo ">> systemd not found; run it manually: $DIR/bitflashd $PORT" | ||
| 339 | fi | ||
| 340 | |||
| 341 | echo | ||
| 342 | echo "===================================================================" | ||
| 343 | echo " DONE. Open TCP port $PORT in your firewall / cloud panel." | ||
| 344 | echo " Your relay address for Bitflash users: <YOUR_PUBLIC_IP>:$PORT" | ||
| 345 | echo "===================================================================" | ||