Skip to content

Commit

Permalink
torflux hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
NOXCIS committed Nov 30, 2024
1 parent 3f1cbc7 commit ae2bf0a
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 26 deletions.
17 changes: 10 additions & 7 deletions WG-Dash/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# builder: WG-Dash Builder for Brcypt & Psutil Binary
FROM alpine:latest AS builder
# pybuild: WG-Dash Builder for Brcypt | Psutil | TorFlux
FROM alpine:latest AS pybuild
LABEL maintainer="NOXCIS"
RUN apk add --no-cache sudo build-base musl-dev rust cargo linux-headers go upx
WORKDIR /opt/wireguarddashboard/src
RUN apk add --no-cache sudo gcc musl-dev rust cargo linux-headers
COPY ./src/builder.sh ./src/builder_requirements.txt /opt/wireguarddashboard/src/
RUN chmod u+x ./builder.sh && ./builder.sh
COPY ./src/builder.sh ./src/builder_requirements.txt ./src/torflux.go ./src/go.mod . /opt/wireguarddashboard/src/
RUN chmod u+x ./builder.sh && ./builder.sh \
&& go build -buildmode=pie -ldflags="-s -w" -o torflux




Expand All @@ -16,8 +18,9 @@ COPY ./src /opt/wireguarddashboard/src/
COPY --from=noxcis/tor-bins:latest /lyrebird /usr/local/bin/lyrebird
COPY --from=noxcis/tor-bins:latest /webtunnel /usr/local/bin/webtunnel
COPY --from=noxcis/tor-bins:latest /snowflake /usr/local/bin/snowflake
COPY --from=builder /opt/wireguarddashboard/src/venv /opt/wireguarddashboard/src/venv
COPY --from=builder /opt/wireguarddashboard/src/log /opt/wireguarddashboard/src/log/
COPY --from=pybuild /opt/wireguarddashboard/src/venv /opt/wireguarddashboard/src/venv
COPY --from=pybuild /opt/wireguarddashboard/src/log /opt/wireguarddashboard/src/log/
COPY --from=pybuild /opt/wireguarddashboard/src/torflux /opt/wireguarddashboard/src/torflux

RUN apk add --no-cache wireguard-tools sudo iptables tzdata && \
apk upgrade && \
Expand Down
67 changes: 48 additions & 19 deletions WG-Dash/src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ trap 'stop_service' SIGTERM
TORRC_PATH="/etc/tor/torrc"
DNS_TORRC_PATH="/etc/tor/dnstorrc"
INET_ADDR="$(hostname -i | awk '{print $1}')"
log_dir="./log"
dashes='------------------------------------------------------------'
equals='============================================================'

Expand Down Expand Up @@ -141,31 +142,59 @@ make_dns_torrc() {
echo -e "SocksPort ${INET_ADDR}:9053 \n" >> "$DNS_TORRC_PATH"
printf "%s\n" "$dashes"
}

run_tor_flux() {
printf "%s\n" "$equals"
printf "[TOR] Starting Tor ...\n"
{ date; tor -f /etc/tor/torrc; printf "\n\n"; } >> ./log/tor_startup_log.txt &
{ date; tor -f /etc/tor/dnstorrc; printf "\n\n"; } >> ./log/tor_startup_log.txt &
# Start both Tor processes
{ date; tor -f /etc/tor/torrc; printf "\n\n"; } >> "$log_dir/tor_startup_log_$(date +'%Y-%m-%d_%H-%M-%S').txt" &
{ date; tor -f /etc/tor/dnstorrc; printf "\n\n"; } >> "$log_dir/dns_tor_startup_log_$(date +'%Y-%m-%d_%H-%M-%S').txt" &

start_time=$(date +%s)
retries=0
max_retries=142 # 5 minutes with 3s intervals

# Wait for Tor to be fully booted
latest_log=$(ls "$log_dir/tor_startup_log_"*.txt | sort -V | tail -n 1)
while ! grep -q 'Bootstrapped 100%' "$latest_log" && [ $retries -lt $max_retries ]; do
sleep 3
retries=$((retries + 1))
latest_log=$(ls "$log_dir/tor_startup_log_"*.txt | sort -V | tail -n 1)

elapsed_time=$(( $(date +%s) - start_time ))
if [ $elapsed_time -ge 300 ]; then
echo "[TOR] Bootstrap timeout. Restarting Tor..."
pkill tor >/dev/null 2>&1
sleep 0.5

# Restart Tor processes and capture their PIDs
{ date; tor -f /etc/tor/torrc; printf "\n\n"; } >> "$log_dir/tor_startup_log_$(date +'%Y-%m-%d_%H-%M-%S').txt" &
{ date; tor -f /etc/tor/dnstorrc; printf "\n\n"; } >> "$log_dir/dns_tor_startup_log_$(date +'%Y-%m-%d_%H-%M-%S').txt" &


start_time=$(date +%s)
retries=0
fi
done

TOR_PID=$!
if [ $retries -ge $max_retries ]; then
echo "[TOR] Exiting: Bootstrap unsuccessful."
return
fi

# Main loop for periodic circuit renewal
while true; do
sleep_time=$(( RANDOM % (1642 - 300 + 1) + 300 ))
sleep_kill=$(awk -v seed="$RANDOM" 'BEGIN { srand(seed); printf "%.2f\n", 0.04 + (rand() * (0.50 - 0.04)) }')
#sleep_time=$(( RANDOM % (15 - 10 + 1) + 10 ))
printf "[TOR] New Circuit in $sleep_time seconds...\n"
printf "%s\n" "$equals"
sleep $sleep_time
printf "%s\n" "$equals"
printf "[TOR] Restarting Tor...\n"
pkill tor
sleep $sleep_kill
{ date; tor -f /etc/tor/torrc; printf "\n\n"; } >> ./log/tor_startup_log.txt &
{ date; tor -f /etc/tor/dnstorrc; printf "\n\n"; } >> ./log/tor_startup_log.txt &
TOR_PID=$!
sleep_time=$(( RANDOM % 600 + 142 ))
printf "%s\n" "$dashes"
echo "[TOR] New circuit in $sleep_time seconds..."
printf "%s\n" "$dashes"
sleep "$sleep_time"
printf "%s\n" "$dashes"
echo "[TOR] Sending Signal for New Circuits..."
./torflux &
printf "%s\n" "$dashes"
done

}


ensure_blocking() {
sleep 1s
echo "Ensuring container continuation."
Expand Down
3 changes: 3 additions & 0 deletions WG-Dash/src/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module torflux

go 1.22.5
134 changes: 134 additions & 0 deletions WG-Dash/src/torflux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright(C) 2024 NOXCIS [https://github.com/NOXCIS]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"bufio"
"bytes"
"fmt"
"net"
"os"
"strings"
"time"
)

const (
logDir = "./log"
logFile = logDir + "/tor_circuit_refresh_log.txt"
bufferSize = 8192
torControlPort1 = 9051
torControlPort2 = 9054
socketTimeout = 5 * time.Second
)

// logMessage logs messages to a file and optionally prints to the console.
func logMessage(message string, addNewlines, toConsole bool) {
timestamp := time.Now().Format("2006-01-02 15:04:05")
logMsg := fmt.Sprintf("[%s] %s", timestamp, message)

if toConsole {
fmt.Println(logMsg)
}

// Ensure log directory exists
if err := os.MkdirAll(logDir, 0755); err != nil {
fmt.Fprintf(os.Stderr, "[ERROR] Could not create log directory: %v\n", err)
return
}

// Open or create log file
f, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "[ERROR] Could not open log file: %v\n", err)
return
}
defer f.Close()

writer := bufio.NewWriter(f)
writer.WriteString(logMsg + "\n")
if addNewlines {
writer.WriteString(strings.Repeat("\n", 5))
}
writer.Flush()
}

// sendSignal sends a NEWNYM signal to the Tor control port and retrieves circuit status.
func sendSignal(port int, password string, statusBuffer *bytes.Buffer) bool {
address := fmt.Sprintf("127.0.0.1:%d", port)
conn, err := net.DialTimeout("tcp", address, socketTimeout)
if err != nil {
logMessage("[TOR-FLUX] [ERROR] Connection to Tor control port failed", false, false)
return false
}
defer conn.Close()

conn.SetDeadline(time.Now().Add(socketTimeout))

reader := bufio.NewReader(conn)

// Authenticate
if password != "" {
authCmd := fmt.Sprintf("AUTHENTICATE \"%s\"\r\n", password)
fmt.Fprint(conn, authCmd)

resp, _ := reader.ReadString('\n')
if !strings.Contains(resp, "250") {
logMessage("[TOR-FLUX] [ERROR] Tor authentication failed", false, false)
return false
}
}

// Send NEWNYM signal
fmt.Fprint(conn, "SIGNAL NEWNYM\r\n")
resp, _ := reader.ReadString('\n')
if !strings.Contains(resp, "250") {
logMessage("[TOR-FLUX] [ERROR] Failed to send NEWNYM signal", false, false)
return false
}

// Get circuit status
fmt.Fprint(conn, "GETINFO circuit-status\r\n")
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
statusBuffer.WriteString(line)
}

logMessage("[TOR-FLUX] Current Circuit Status:", false, false)
logMessage(statusBuffer.String(), false, false)
logMessage("[TOR-FLUX] New Tor Circuits Requested Successfully.", false, false)

return true
}

func main() {
password := os.Getenv("VANGUARD")
if password == "" {
logMessage("[TOR-FLUX] [ERROR] Tor control port password (VANGUARD) is not set or empty.", false, true)
os.Exit(1)
}

logMessage("[TOR-FLUX] Starting Tor circuit refresh...", false, true)

// Send NEWNYM signal and get circuit statuses
statusBuffer1 := &bytes.Buffer{}
statusBuffer2 := &bytes.Buffer{}
sendSignal(torControlPort1, password, statusBuffer1)
sendSignal(torControlPort2, password, statusBuffer2)

logMessage("[TOR-FLUX] Tor circuit refresh completed.", true, true)
}
37 changes: 37 additions & 0 deletions configs/logs/tor_circuit_refresh_log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[2024-11-30 12:52:17] [TOR-FLUX] Starting Tor circuit refresh...
[2024-11-30 12:52:22] [TOR-FLUX] Current Circuit Status:
[2024-11-30 12:52:22] 250+circuit-status=
287 EXTENDED $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$E8965A79FB2F335194141E8968755524840C44B6~Piratenpartei08 BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2024-11-30T17:52:16.723281
279 BUILT $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$9308F49A225022FA39011033E1C31EFF5B7B5000~freeiranandtheworld,$AD08584AC6A2A421DAEDF227DA6F0DE53DFE40B6~FreeExit BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2024-11-30T17:50:13.021081
281 BUILT $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$67BE9A0658EF0106FCCB98FF1C68D6AAFFFC3CA9~ChocolateBit,$A5AEBB58BD8E17F74DEC3F6F8B8ACF410A8B1A9A~berenstainbears BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2024-11-30T17:50:43.141214
284 BUILT $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$06E72526BBE040C51C5ADFBAA07ADD9AEB5E1FA1~wien,$5A8292926C5E5A246D35B843A72942FCEC235BAC~FreeExit BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2024-11-30T17:51:16.335735
278 BUILT $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$6E207FAFBC87E3E1199B0D3DDC61EE095DC9B5EA~OgheiK2ZoogoaGooc4E,$311A4533F7A2415F42346A6C8FA77E6FD279594C~DigiGesTor3e2 BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2024-11-30T17:49:56.897035
275 BUILT $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$DB5DE88B4914F60CDEEF581ECCA8DEA59EF26A90~Piratenpartei06,$14AE2154A26F1D42C3C3BEDC10D05FDD9F8545BB~freeasf BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2024-11-30T17:49:32.692689
274 BUILT $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$D8514224881C60572E06351F2BD650BA14ACA75C~experiment626,$9C61FC0A01401EDF71C4048665E53968E81351FC~DigiGesTor5e2 BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2024-11-30T17:49:21.620934
283 BUILT $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$DDDA625FF6C1F1F4554281C2D6DC3FBAD092CB5F~TurtleRelay,$9AA3FF35E7A549D2337E962333D366E102FE4D50~DigiGesTor3e1 BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2024-11-30T17:51:05.249282
277 BUILT $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$706A7674A217BA905FE677E82236B7B968A23DB7~rofltor04,$8C25BA134D579B8AAF420E01215EB2CF06AAE907~DigiGesTor5e4 BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2024-11-30T17:49:45.821602
286 BUILT $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$D7C218579374ED6525E12362A0B2A3DAEB0B5CEC~Unnamed,$BCF55F865EE6EF17E25EFEAF851BC429F190B85D~DigiGesTor5e1 BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2024-11-30T17:51:47.531552
.
250 OK

[2024-11-30 12:52:22] [TOR-FLUX] New Tor Circuits Requested Successfully.
[2024-11-30 12:52:27] [TOR-FLUX] Current Circuit Status:
[2024-11-30 12:52:27] 250+circuit-status=
2118 BUILT $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$69A412D32D4012972877914E7222DDB7C3C76D42~prsv,$2B4D5AA9997D4DDFF02BC52E173CF0C8FFFD1B9C~JustExiting BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2024-11-30T17:33:07.373796
2105 BUILT $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$D89C4A0F59305D4ECF8B6B614AAA7A8B63ACFD63~Meunvon,$3687FEC7E73F61AC66F7AE251E7DEE6BBD8C0252~Quintex33 BUILD_FLAGS=NEED_CAPACITY,NEED_UPTIME PURPOSE=CONFLUX_LINKED TIME_CREATED=2024-11-30T17:32:39.296581
2176 BUILT $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$593A4431DA2E315883406AECE1D1188E02F23D8F~klarheit,$B7047FBDE9C53C39011CA84E5CB2A8E3543066D0~Quintex11 BUILD_FLAGS=NEED_CAPACITY,NEED_UPTIME PURPOSE=CONFLUX_LINKED TIME_CREATED=2024-11-30T17:34:45.760770
2053 BUILT $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$923DE89BAD9206721D2B1D60AC7E987097EDF070~DyingHead,$1E5E5C0F0E10F5CA69D0F634B2420BD4CF60C80C~NeelTorExit3D BUILD_FLAGS=NEED_CAPACITY,NEED_UPTIME PURPOSE=CONFLUX_LINKED TIME_CREATED=2024-11-30T17:29:16.630924
1784 BUILT $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$C00C926124C9DCC6647964931B56BD874B931E8D~striga,$267AA485F9292289FF2500280BC63EC85BD9FFF3~PflashPunk BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2024-11-30T17:22:28.704792
2192 BUILT $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$8F83B3999B8E16F6C4C7E41665E23FADA300D4C4~InterFlowers404,$887CB7C9DD87ADD7B0A2771280AC17ED913136A5~Emerald173 BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2024-11-30T17:47:48.262733
2193 BUILT $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$4D4CFA0CC8EED2A51C6D73EABA999EAAE833D0B7~YoYuD1N04NoExit,$81EDFBC8F6F5C7CF0ADD5F8E08BC8FABA04089C6~CalyxInstitute17 BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2024-11-30T17:47:49.644234
2194 BUILT $08B85FE2212DC548403FB684265D2B63B9C2C237~Femb01tNWh,$30ABFEA883B663C22D35B7467F13476304B40581~glenda1,$B0E93B10BD817250A818ABF7F5C2444AF364DD67~SkyLights BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2024-11-30T17:47:50.642987
.
250 OK

[2024-11-30 12:52:27] [TOR-FLUX] New Tor Circuits Requested Successfully.
[2024-11-30 12:52:27] [TOR-FLUX] Tor circuit refresh completed.





0 comments on commit ae2bf0a

Please sign in to comment.