-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Soniczac7/dev
Merge dev and master
- Loading branch information
Showing
21 changed files
with
707 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.