Skip to content

Commit

Permalink
Merge pull request #2 from Soniczac7/dev
Browse files Browse the repository at this point in the history
Merge dev and master
  • Loading branch information
Soniczac7 authored Jul 3, 2022
2 parents 91ed526 + 0d2e988 commit c47b16d
Show file tree
Hide file tree
Showing 21 changed files with 707 additions and 5 deletions.
96 changes: 96 additions & 0 deletions BrowserIntercept.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
using static DownloadManager.Logging;

namespace DownloadManager
{
internal class BrowserIntercept
{
public BrowserIntercept _instance;
public Socket httpServer;
private int serverPort = 65535;
public Thread thread;

public void StartServer()
{
_instance = this;
Log("Starting internal server...", Color.Black);
try
{
thread = new Thread(new ThreadStart(ConnectionThreadMethod));
thread.Start();
Log("Internal server started. Listening for connections on port " + serverPort + ".", Color.Green);
}
catch (Exception ex)
{
Log("Error while starting internal server:" + Environment.NewLine + ex.Message + ex.StackTrace, Color.Red);
}
}

private void ConnectionThreadMethod()
{
try
{
httpServer = new Socket(SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, serverPort);
httpServer.Bind(endPoint);
httpServer.Listen(1);
ListenForConnections();
}
catch (Exception ex)
{
Log("Error while starting internal server:" + Environment.NewLine + ex.Message + ex.StackTrace, Color.Red);
}
}

private void ListenForConnections()
{
while (true)
{
String data = "";
byte[] bytes = new byte[2048];

Socket client = httpServer.Accept();

// Read inbound connection data
while (true)
{
int numBytes = client.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, numBytes);

if (data.IndexOf("\r\n") > -1)
{
break;
}
}

string[] splittedData = data.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
String url = splittedData[0].ToString().Replace("%22 HTTP/1.1", "");
url = url.Replace("GET /?url=%22", "");

if (!url.Contains("favicon.ico"))
{
Logging._instance.Invoke((MethodInvoker)delegate
{
Log("Request received for URL: " + url, Color.Black);
DownloadProgress downloadProgress = new DownloadProgress(url, Settings1.Default.defaultDownload, "", "", 0);
downloadProgress.Show();
//Log("--- Start Request ---", Color.Black);
//Log(data, Color.Black);
//Log("--- End Request ---", Color.Black);
});
}

String resHeader = "HTTP/1.1 200 OK\nServer: DownloadManager_Internal\nContent-Type: text/html; charset: UTF-8\n\n";
String resBody = "<!DOCTYPE html><html><head><title>Download Manager</title><style>body{background-color: rgb(18, 22, 58);color: white;font-family: Arial, Helvetica, sans-serif;}h1{font-size: 30px;}</style></head><body><h1>Download Manager</h1><hr><p>Please do not manually send requests to the internal server.</p><p>If a request is sent incorrectly the internal server may crash.If this occurs please restart the Download Manager application.</p></body></html>";
String resStr = resHeader + resBody;
byte[] resData = Encoding.ASCII.GetBytes(resStr);
client.SendTo(resData, client.RemoteEndPoint);
client.Close();
httpServer.Close();
ConnectionThreadMethod();
}
}
}
}
90 changes: 89 additions & 1 deletion DownloadForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 56 additions & 2 deletions DownloadForm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace DownloadManager
{
Expand All @@ -25,6 +26,7 @@ int nHeightEllipse // height of ellipse
public static DownloadForm _instance;
public Logging logging = new Logging();
Settings settings = new Settings();
BrowserIntercept browserIntercept = new BrowserIntercept();
public static int downloadsAmount = 0;
public static string downloadsFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop).Replace("Desktop", "Downloads") + "\\";

Expand All @@ -33,14 +35,15 @@ public DownloadForm()
_instance = this;
Logging.Log("Downloads folder: " + downloadsFolder, Color.Black);
InitializeComponent();
browserIntercept.StartServer();
textBox2.Text = Settings1.Default.defaultDownload;
Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 10, 10));
}

private void button1_Click(object sender, EventArgs e)
{
// Close
Application.Exit();
this.Hide();
}

private void button2_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -95,5 +98,56 @@ private void toolStripButton2_Click(object sender, EventArgs e)
{
settings.Show();
}

private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
// Open Form
this.Show();
}

private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
// Report a bug
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "https://github.com/Soniczac7/Download-Manager/issues/new?assignees=&labels=bug&template=bug_report.md&title=",
Arguments = "",
UseShellExecute = true,
RedirectStandardOutput = false,
RedirectStandardError = false,
RedirectStandardInput = false
};
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
}

private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
// Exit
browserIntercept.httpServer.Close();
try
{
browserIntercept.thread.Abort();
}
catch { }
Process.GetCurrentProcess().Kill();
}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "http://example.com",
Arguments = "",
UseShellExecute = true,
RedirectStandardError = false,
RedirectStandardInput = false,
RedirectStandardOutput = false
};
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
}
}
}
Loading

0 comments on commit c47b16d

Please sign in to comment.