-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
86 lines (72 loc) · 2.14 KB
/
Program.cs
File metadata and controls
86 lines (72 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using StkSocks;
using StkCli;
namespace stkhttor
{
class Program
{
static void SavePid()
{
Int32 nProcessID = System.Diagnostics.Process.GetCurrentProcess().Id;
File.WriteAllText("stkhttor.pid", nProcessID.ToString());
}
static void Main(string[] args)
{
Options opt = Options.GetOpts();
ArgHandler<Options> ah = new(opt);
ah.Title = "StkHTTor: http-Tor proxy";
ah.Copyright = "(c) Stein Krauz, 2022";
try {
ah.Parse(args);
}catch(ArgumentException ex) {
Console.WriteLine(ex.Message);
return;
}
TcpListener server = null;
SavePid();
HexDump.Utils.Trace = opt.Trace;
try
{
Int32 port = opt.HttpPort;
IPAddress localAddr = IPAddress.Parse(opt.HttpHost);
server = new TcpListener(localAddr, port);
server.Start();
Console.WriteLine("stkHTTor engaged.");
while(true)
{
HexDump.Utils.LogString("Waiting for a connection... ");
TcpClient client = server.AcceptTcpClient();
HexDump.Utils.LogString("Connected!");
Thread t = new Thread(new ParameterizedThreadStart(HandleConnect));
t.Start(client);
}
}
catch(SocketException e)
{
Console.WriteLine("ACHTUNG! SocketException: {0}", e);
}
finally
{
server.Stop();
}
}
static void HandleConnect(Object o)
{
TcpClient client = (TcpClient) o;
try {
Requestor r = new();
r.HandleRequest(client);
} catch(System.IO.IOException ioex) {
HexDump.Utils.LogString($"Connection aborted: {ioex.Message}");
return;
}
client.Close();
HexDump.Utils.LogString("Connection closed");
}
} //end class
}