Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NTMinerNoDevFee: fix #131, restrict port to improve passthrough performance #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/DevConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

namespace NTMiner {
Expand Down Expand Up @@ -55,11 +56,13 @@ private static void RunDiversion(IntPtr handle, ref bool ranOnce) {
try {
while (s_running) {
uint readLength = 0;
uint writeLength = 0;
WINDIVERT_IPHDR* ipv4Header = null;
WINDIVERT_TCPHDR* tcpHdr = null;
WINDIVERT_ADDRESS addr = new WINDIVERT_ADDRESS();
NativeOverlapped nativeOverlapped = new NativeOverlapped();

if (!SafeNativeMethods.WinDivertRecv(handle, packet, (uint)packet.Length, ref addr, ref readLength)) {
if (!SafeNativeMethods.WinDivertRecv(handle, packet, (uint)packet.Length, ref readLength, ref addr)) {
continue;
}

Expand All @@ -70,7 +73,8 @@ private static void RunDiversion(IntPtr handle, ref bool ranOnce) {

fixed (byte* inBuf = packet) {
byte* payload = null;
SafeNativeMethods.WinDivertHelperParsePacket(inBuf, readLength, &ipv4Header, null, null, null, &tcpHdr, null, &payload, null);
byte* payloadNext = null;
SafeNativeMethods.WinDivertHelperParsePacket(inBuf, readLength, &ipv4Header, null, null, null, null, &tcpHdr, null, &payload, null, &payloadNext, null);

if (ipv4Header != null && tcpHdr != null && payload != null) {
string text = Marshal.PtrToStringAnsi((IntPtr)payload);
Expand All @@ -87,8 +91,8 @@ private static void RunDiversion(IntPtr handle, ref bool ranOnce) {
}
}

SafeNativeMethods.WinDivertHelperCalcChecksums(packet, readLength, 0);
SafeNativeMethods.WinDivertSendEx(handle, packet, readLength, 0, ref addr, IntPtr.Zero, IntPtr.Zero);
SafeNativeMethods.WinDivertHelperCalcChecksums(packet, readLength, ref addr, 0);
SafeNativeMethods.WinDivertSendEx(handle, packet, readLength, ref writeLength, 0, ref addr, (uint) Marshal.SizeOf(addr), ref nativeOverlapped);
}

}
Expand Down
17 changes: 12 additions & 5 deletions src/NTMinerNoDevFee/NoDevFee/NoDevFeeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ public static void StartAsync() {
Task.Factory.StartNew(() => {
WinDivertExtract.Extract();
int counter = 0;
string filter = $"outbound && ip && ip.DstAddr != 127.0.0.1 && tcp && tcp.PayloadLength > 100";
string filter = $"outbound && ip && ip.DstAddr != 127.0.0.1 && tcp && tcp.PayloadLength > 100"
+ " && tcp.Payload[0] == 0x7B" // ‘{'
+ " && tcp.Payload[-2] == 0x7D" // '}'
+ " && tcp.Payload[-1] == 0x0A" // '\n'
;
IntPtr divertHandle = SafeNativeMethods.WinDivertOpen(filter, WINDIVERT_LAYER.WINDIVERT_LAYER_NETWORK, 0, 0);
if (divertHandle != IntPtr.Zero) {
Task.Factory.StartNew(() => {
Expand Down Expand Up @@ -225,17 +229,20 @@ private static void RunDiversion(
return;
}
uint readLength = 0;
uint writeLength = 0;
WINDIVERT_IPHDR* ipv4Header = null;
WINDIVERT_TCPHDR* tcpHdr = null;
WINDIVERT_ADDRESS addr = new WINDIVERT_ADDRESS();
NativeOverlapped nativeOverlapped = new NativeOverlapped();

if (!SafeNativeMethods.WinDivertRecv(divertHandle, packet, (uint)packet.Length, ref addr, ref readLength)) {
if (!SafeNativeMethods.WinDivertRecv(divertHandle, packet, (uint)packet.Length, ref readLength, ref addr)) {
continue;
}

fixed (byte* inBuf = packet) {
byte* payload = null;
SafeNativeMethods.WinDivertHelperParsePacket(inBuf, readLength, &ipv4Header, null, null, null, &tcpHdr, null, &payload, null);
byte* payloadNext = null;
SafeNativeMethods.WinDivertHelperParsePacket(inBuf, readLength, &ipv4Header, null, null, null, null, &tcpHdr, null, &payload, null, &payloadNext, null);

if (ipv4Header != null && tcpHdr != null && payload != null) {
string ansiText = Marshal.PtrToStringAnsi((IntPtr)payload);
Expand All @@ -258,8 +265,8 @@ private static void RunDiversion(
}
}

SafeNativeMethods.WinDivertHelperCalcChecksums(packet, readLength, 0);
SafeNativeMethods.WinDivertSendEx(divertHandle, packet, readLength, 0, ref addr, IntPtr.Zero, IntPtr.Zero);
SafeNativeMethods.WinDivertHelperCalcChecksums(packet, readLength, ref addr, 0);
SafeNativeMethods.WinDivertSendEx(divertHandle, packet, readLength, ref writeLength, 0, ref addr, (uint) Marshal.SizeOf(addr), ref nativeOverlapped);
}
}
catch (Exception e) {
Expand Down
Loading