Skip to content

Commit ce3f3bb

Browse files
author
Delta-Kronecker
committed
launcher: clean session output, detail/log hotkeys (D/V), simplified main menu
1 parent af08fd4 commit ce3f3bb

1 file changed

Lines changed: 105 additions & 67 deletions

File tree

scripts/start-tor.cs

Lines changed: 105 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -775,49 +775,48 @@ private static void ShowMainMenu(out int mode, out int strategy, out bool fragme
775775
while (true)
776776
{
777777
Console.WriteLine();
778-
Console.WriteLine(" ====================");
779-
Console.WriteLine(" TorJet v" + TorJetVersion.App);
780-
Console.WriteLine(" ====================");
781-
Console.WriteLine(" 0) Start Tor");
782-
Console.WriteLine(" 1) Connection mode : " + ModeNames[mode]);
783-
Console.WriteLine(" 2) Strategy level : " + StrategyNames[strategy]);
784-
Console.WriteLine(" 3) Fragment (xray) : " + (fragment ? "on" : "off"));
785-
Console.WriteLine(" 4) Conflux sets : " + (confluxSets == 0 ? "consensus" : confluxSets.ToString()));
786-
Console.WriteLine(" 5) Conflux legs : " + (confluxLegs == 0 ? "consensus" : confluxLegs.ToString()));
787-
Console.WriteLine(" 6) Exit");
788-
Console.WriteLine(" C) Stop Tor and return to menu");
789-
Console.Write(" Choose 0-6, C (Enter = 0): ");
778+
Console.WriteLine(" =====================================");
779+
Console.WriteLine(" TorJet v" + TorJetVersion.App);
780+
Console.WriteLine(" =====================================");
781+
Console.WriteLine();
782+
Console.WriteLine(" 1) Start Tor");
783+
Console.WriteLine(" 2) Connection mode : " + ModeNames[mode]);
784+
Console.WriteLine(" 3) Strategy level : " + StrategyNames[strategy]);
785+
Console.WriteLine(" 4) Fragment (xray) : " + (fragment ? "on" : "off"));
786+
Console.WriteLine(" 5) Conflux sets : " + (confluxSets == 0 ? "consensus" : confluxSets.ToString()));
787+
Console.WriteLine(" 6) Conflux legs : " + (confluxLegs == 0 ? "consensus" : confluxLegs.ToString()));
788+
Console.WriteLine(" 7) View log");
789+
Console.WriteLine(" 8) Quit");
790+
Console.WriteLine();
791+
Console.Write(" Enter 1-8 (Enter = Start): ");
790792
string input;
791793
try { input = Console.ReadLine(); }
792794
catch { mode = -1; return; }
793795
if (string.IsNullOrWhiteSpace(input)) return;
794796
int n;
795797
if (int.TryParse(input.Trim(), out n))
796798
{
797-
if (n == 0) return;
798-
if (n == 1)
799+
if (n == 1) return;
800+
if (n == 2)
799801
{
800802
int m = ShowMenu();
801803
if (m >= 0) { mode = m; WriteModeFile(mode); }
802804
}
803-
else if (n == 2)
805+
else if (n == 3)
804806
{
805807
int s = ShowStrategyMenu();
806808
if (s >= 0) { strategy = s; WriteStrategyFile(strategy); }
807809
}
808-
else if (n == 3)
810+
else if (n == 4)
809811
{
810812
fragment = !fragment;
811813
WriteFragmentFile(fragment);
812814
}
813-
else if (n == 4) { PromptConfluxSets(); }
814-
else if (n == 5) { PromptConfluxLegs(); }
815-
else if (n == 6) { mode = -1; return; }
816-
else Console.WriteLine(" Invalid choice, try again.");
817-
}
818-
else if (input.Trim().Equals("c", StringComparison.OrdinalIgnoreCase))
819-
{
820-
StopTor();
815+
else if (n == 5) { PromptConfluxSets(); }
816+
else if (n == 6) { PromptConfluxLegs(); }
817+
else if (n == 7) { ViewLog(); }
818+
else if (n == 8) { mode = -1; return; }
819+
else Console.WriteLine(" Invalid choice, try again.");
821820
}
822821
else
823822
{
@@ -827,7 +826,7 @@ private static void ShowMainMenu(out int mode, out int strategy, out bool fragme
827826
{
828827
int s = ParseStrategy(input.Trim());
829828
if (s >= 0) { strategy = s; WriteStrategyFile(strategy); }
830-
else Console.WriteLine(" Invalid choice, try again.");
829+
else Console.WriteLine(" Invalid choice, try again.");
831830
}
832831
}
833832
}
@@ -1420,17 +1419,17 @@ private static int AddConfluxLeg(string setId)
14201419
return status == 250 ? 0 : 1;
14211420
}
14221421

1423-
private static int ConfluxStatus()
1422+
private static int ConfluxStatus(bool detailed)
14241423
{
14251424
var legList = ConfluxQuery();
14261425
if (legList == null)
14271426
{
14281427
Console.WriteLine("[x] cannot reach tor control port on 127.0.0.1:9051 (is tor running?).");
14291428
return 1;
14301429
}
1431-
bool debug = Environment.GetEnvironmentVariable("BENCH_DEBUG") == "1";
1430+
bool debug = detailed || Environment.GetEnvironmentVariable("BENCH_DEBUG") == "1";
14321431
Console.WriteLine();
1433-
Console.WriteLine("Conflux status:");
1432+
Console.WriteLine(Stamp() + "conflux status:");
14341433
if (legList.Count == 0)
14351434
{
14361435
Console.WriteLine(" NO conflux circuits - conflux is NOT engaging (check consensus support).");
@@ -1443,8 +1442,11 @@ private static int ConfluxStatus()
14431442
sets[leg[0]].Add(leg);
14441443
}
14451444
int legs = legList.Count;
1446-
Console.WriteLine(" conflux sets : " + sets.Count);
1447-
Console.WriteLine(" conflux legs : " + legs);
1445+
int linkedTotal = 0;
1446+
foreach (string[] leg in legList)
1447+
if (leg[4].Equals("LINKED", StringComparison.OrdinalIgnoreCase)) linkedTotal++;
1448+
Console.WriteLine(" sets: " + sets.Count + " legs: " + legs +
1449+
" (" + linkedTotal + " linked)");
14481450
int idx = 1;
14491451
foreach (var kv in sets)
14501452
{
@@ -1454,7 +1456,7 @@ private static int ConfluxStatus()
14541456
foreach (string[] leg in kv.Value)
14551457
{
14561458
if (leg[2] != "?") exit = leg[2];
1457-
if (leg[4] == "LINKED")
1459+
if (leg[4].Equals("LINKED", StringComparison.OrdinalIgnoreCase))
14581460
{
14591461
linked++;
14601462
int rttUs;
@@ -1463,22 +1465,22 @@ private static int ConfluxStatus()
14631465
}
14641466
}
14651467
string rtt = bestUs == int.MaxValue ? "?" : (bestUs / 1000) + " ms";
1466-
Console.WriteLine(" set " + idx + ": " + kv.Value.Count + " leg(s) (" + linked +
1467-
" linked), exit " + exit + ", best RTT " + rtt);
1468+
Console.WriteLine(" set " + idx + ": " + kv.Value.Count + " legs (" + linked +
1469+
" linked) best " + rtt + " exit " + exit);
14681470
if (debug)
14691471
{
14701472
foreach (string[] leg in kv.Value)
14711473
{
14721474
int rttUs;
14731475
string r = int.TryParse(leg[5], out rttUs) && rttUs > 0
14741476
? (rttUs / 1000) + " ms" : "?";
1475-
Console.WriteLine(" [leg] circ " + leg[1] + " " + leg[4] + " RTT " + r);
1477+
Console.WriteLine(" [leg] circ " + leg[1] + " " + leg[4] +
1478+
" RTT " + r);
14761479
}
14771480
Console.WriteLine(" [bw] exit bandwidth: " + ExitBandwidth(exit) + " bytes/s");
14781481
}
14791482
idx++;
14801483
}
1481-
Console.WriteLine(" conflux is ACTIVE (" + sets.Count + " set(s), " + legs + " leg(s)).");
14821484
return 0;
14831485
}
14841486

@@ -1555,7 +1557,7 @@ private static void CircuitWatchLoop()
15551557
lock (consoleLock)
15561558
{
15571559
ClearProgressLine();
1558-
Console.WriteLine("circuit monitor: added a leg to set " +
1560+
Console.WriteLine(Stamp() + "monitor: added a leg to set " +
15591561
kv.Key + " (" + kv.Value + "/" +
15601562
confluxLegs + " legs)");
15611563
}
@@ -1680,29 +1682,14 @@ private static void CircuitWatchLoop()
16801682
lock (consoleLock)
16811683
{
16821684
ClearProgressLine();
1683-
Console.WriteLine("circuit monitor: conflux legs:");
1684-
foreach (string[] leg in legList)
1685-
{
1686-
string reason;
1687-
toRemoveReasons.TryGetValue(leg[1], out reason);
1688-
string mark = string.IsNullOrEmpty(reason)
1689-
? "" : " <-- removing (" + reason + ")";
1690-
if (leg[4].Equals("LINKED", StringComparison.OrdinalIgnoreCase))
1691-
{
1692-
int rttUs;
1693-
string r = int.TryParse(leg[5], out rttUs) && rttUs > 0
1694-
? (rttUs / 1000) + " ms" : "?";
1695-
Console.WriteLine(" leg " + leg[1] + " RTT " + r + mark);
1696-
}
1697-
else
1698-
{
1699-
Console.WriteLine(" leg " + leg[1] + " UNLINKED" + mark);
1700-
}
1701-
}
17021685
int closed = 0;
1686+
int weakN = 0, stuckN = 0;
17031687
string closedIds = "";
17041688
foreach (string[] leg in toRemove)
17051689
{
1690+
string reason;
1691+
toRemoveReasons.TryGetValue(leg[0], out reason);
1692+
if (reason == "weak") weakN++; else stuckN++;
17061693
if (ControlSend("CLOSECIRCUIT " + leg[0]))
17071694
{
17081695
closed++;
@@ -1714,9 +1701,12 @@ private static void CircuitWatchLoop()
17141701
if (closed > 0)
17151702
{
17161703
lastClose = DateTime.UtcNow;
1717-
Console.WriteLine("circuit monitor: closed " + closed + " weak leg" +
1718-
(closed > 1 ? "s" : "") + " (" + closedIds +
1719-
") - replacement is being built.");
1704+
string what = "";
1705+
if (weakN > 0) what = weakN + " weak";
1706+
if (stuckN > 0)
1707+
what = (what.Length > 0 ? what + ", " : "") + stuckN + " stuck-unlinked";
1708+
Console.WriteLine(Stamp() + "monitor: closed " + what +
1709+
" leg(s) (" + closedIds + ") - replacement building");
17201710
}
17211711
}
17221712
}
@@ -1818,6 +1808,48 @@ private static void ClearProgressLine()
18181808
}
18191809
}
18201810

1811+
// Local clock stamp used on launcher log lines, e.g. "[12:34:56] ".
1812+
private static string Stamp()
1813+
{
1814+
return "[" + DateTime.Now.ToString("HH:mm:ss") + "] ";
1815+
}
1816+
1817+
// Shows the tail of tor.log. tor writes every line with its own exact
1818+
// timestamp (e.g. "Aug 12 12:34:56.789 [notice] ..."), which is kept
1819+
// as-is; lines without one get a local clock stamp added.
1820+
private static void ViewLog()
1821+
{
1822+
const int maxLines = 40;
1823+
try
1824+
{
1825+
if (!File.Exists(TorLog))
1826+
{
1827+
Console.WriteLine("[!] no log yet at " + TorLog);
1828+
return;
1829+
}
1830+
string[] lines = File.ReadAllLines(TorLog);
1831+
int start = Math.Max(0, lines.Length - maxLines);
1832+
lock (consoleLock)
1833+
{
1834+
ClearProgressLine();
1835+
Console.WriteLine(Stamp() + "tor log - last " +
1836+
(lines.Length - start) + " of " +
1837+
lines.Length + " line(s):");
1838+
for (int i = start; i < lines.Length; i++)
1839+
{
1840+
string l = lines[i];
1841+
if (l.Length == 0) continue;
1842+
bool hasTs = Regex.IsMatch(l, @"^[A-Z][a-z]{2}\s+\d{1,2}\s+\d{2}:\d{2}");
1843+
Console.WriteLine(" " + (hasTs ? l : Stamp() + l));
1844+
}
1845+
}
1846+
}
1847+
catch (Exception ex)
1848+
{
1849+
Console.WriteLine("[!] cannot read log: " + ex.Message);
1850+
}
1851+
}
1852+
18211853
// Starts tor for the given mode, writes torrc, stops any previous run,
18221854
// and waits until bootstrap reaches 100%. Returns the process or null.
18231855
// `aborted` is set when the user pressed C to stop during bootstrap.
@@ -2519,12 +2551,10 @@ private static int RunTorSession(int mode, int strategy, bool fragment,
25192551
else
25202552
ToggleTun();
25212553
}
2522-
Console.WriteLine(" P : toggle the Windows system proxy on/off");
2523-
Console.WriteLine(" T : toggle TUN mode (all traffic through Tor)");
2524-
Console.WriteLine(" S : run a speed test through the Tor proxy");
2525-
Console.WriteLine(" A : add a leg to a conflux set");
2526-
Console.WriteLine(" L : show conflux set status");
2527-
Console.WriteLine(" C : stop Tor and return to the menu");
2554+
Console.WriteLine(" P toggle system proxy D conflux details");
2555+
Console.WriteLine(" T toggle TUN mode L conflux summary");
2556+
Console.WriteLine(" S speed test V view tor log");
2557+
Console.WriteLine(" A add a leg to a set C stop Tor, back to menu");
25282558
Console.WriteLine();
25292559
while (true)
25302560
{
@@ -2560,7 +2590,15 @@ private static int RunTorSession(int mode, int strategy, bool fragment,
25602590
}
25612591
else if (ki.Key == ConsoleKey.L)
25622592
{
2563-
lock (consoleLock) ConfluxStatus();
2593+
lock (consoleLock) ConfluxStatus(false);
2594+
}
2595+
else if (ki.Key == ConsoleKey.D)
2596+
{
2597+
lock (consoleLock) ConfluxStatus(true);
2598+
}
2599+
else if (ki.Key == ConsoleKey.V)
2600+
{
2601+
lock (consoleLock) ViewLog();
25642602
}
25652603
else if (ki.Key == ConsoleKey.T)
25662604
{
@@ -2673,7 +2711,7 @@ private static int Main(string[] args)
26732711

26742712
if (args.Length > 0 && args[0] == "--conflux-status")
26752713
{
2676-
return ConfluxStatus();
2714+
return ConfluxStatus(true);
26772715
}
26782716
if (args.Length > 0 && args[0] == "--conflux-add")
26792717
{
@@ -2691,7 +2729,7 @@ private static int Main(string[] args)
26912729
if (p == null) { if (aborted) { Cleanup(); return 1; } Console.WriteLine("[x] " + err); Cleanup(); return 1; }
26922730
Console.WriteLine("bootstrap OK. Waiting 30s for conflux sets to build...");
26932731
Thread.Sleep(30000);
2694-
int code = ConfluxStatus();
2732+
int code = ConfluxStatus(false);
26952733
Cleanup();
26962734
return code;
26972735
}

0 commit comments

Comments
 (0)