Skip to content

Commit

Permalink
fix: Log multiplication when transfer log using APP itself
Browse files Browse the repository at this point in the history
Issue: #25
  • Loading branch information
kaaass committed Jul 11, 2023
1 parent c95acd4 commit f8d1c24
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.zerotier.sdk.VirtualNetworkFrameListener;
import com.zerotier.sdk.util.StringUtils;

import net.kaaass.zerotierfix.util.DebugLog;
import net.kaaass.zerotierfix.util.IPPacketUtils;
import net.kaaass.zerotierfix.util.InetAddressUtils;

Expand Down Expand Up @@ -129,7 +130,7 @@ public void run() {
boolean noDataBeenRead = true;
int readCount = TunTapAdapter.this.in.read(buffer.array());
if (readCount > 0) {
Log.d(TunTapAdapter.TAG, "Sending packet to ZeroTier. " + readCount + " bytes.");
DebugLog.d(TunTapAdapter.TAG, "Sending packet to ZeroTier. " + readCount + " bytes.");
var readData = new byte[readCount];
System.arraycopy(buffer.array(), 0, readData, 0, readCount);
byte iPVersion = IPPacketUtils.getIPVersion(readData);
Expand Down Expand Up @@ -334,7 +335,7 @@ private void handleIPv6Packet(byte[] packetData) {
if (result != ResultCode.RESULT_OK) {
Log.e(TAG, "Error calling processVirtualNetworkFrame: " + result.toString());
} else {
Log.d(TAG, "Packet sent to ZT");
DebugLog.d(TAG, "Packet sent to ZT");
this.ztService.setNextBackgroundTaskDeadline(nextDeadline[0]);
}
}
Expand Down Expand Up @@ -398,7 +399,7 @@ public boolean isRunning() {
@Override
public void onVirtualNetworkFrame(long networkId, long srcMac, long destMac, long etherType,
long vlanId, byte[] frameData) {
Log.d(TAG, "Got Virtual Network Frame. " +
DebugLog.d(TAG, "Got Virtual Network Frame. " +
" Network ID: " + StringUtils.networkIdToString(networkId) +
" Source MAC: " + StringUtils.macAddressToString(srcMac) +
" Dest MAC: " + StringUtils.macAddressToString(destMac) +
Expand Down Expand Up @@ -441,7 +442,7 @@ public void onVirtualNetworkFrame(long networkId, long srcMac, long destMac, lon
}
} else if (etherType == IPV4_PACKET) {
// 收到 IPv4 包。根据需要发送至 TUN
Log.d(TAG, "Got IPv4 packet. Length: " + frameData.length + " Bytes");
DebugLog.d(TAG, "Got IPv4 packet. Length: " + frameData.length + " Bytes");
try {
var sourceIP = IPPacketUtils.getSourceIP(frameData);
if (sourceIP != null) {
Expand All @@ -460,7 +461,7 @@ public void onVirtualNetworkFrame(long networkId, long srcMac, long destMac, lon
}
} else if (etherType == IPV6_PACKET) {
// 收到 IPv6 包。根据需要发送至 TUN,并更新 NDP 表
Log.d(TAG, "Got IPv6 packet. Length: " + frameData.length + " Bytes");
DebugLog.d(TAG, "Got IPv6 packet. Length: " + frameData.length + " Bytes");
try {
var sourceIP = IPPacketUtils.getSourceIP(frameData);
if (sourceIP != null) {
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/net/kaaass/zerotierfix/service/UdpCom.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.zerotier.sdk.PacketSender;
import com.zerotier.sdk.ResultCode;

import net.kaaass.zerotierfix.util.DebugLog;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -36,7 +38,7 @@ public int onSendPacketRequested(long j, InetSocketAddress inetSocketAddress, by
}
try {
DatagramPacket datagramPacket = new DatagramPacket(bArr, bArr.length, inetSocketAddress);
Log.d(TAG, "onSendPacketRequested: Sent " + datagramPacket.getLength() + " bytes to " + inetSocketAddress.toString());
DebugLog.d(TAG, "onSendPacketRequested: Sent " + datagramPacket.getLength() + " bytes to " + inetSocketAddress.toString());
this.svrSocket.send(datagramPacket);
return 0;
} catch (Exception unused) {
Expand All @@ -57,7 +59,7 @@ public void run() {
if (datagramPacket.getLength() > 0) {
byte[] bArr2 = new byte[datagramPacket.getLength()];
System.arraycopy(datagramPacket.getData(), 0, bArr2, 0, datagramPacket.getLength());
Log.d(TAG, "Got " + datagramPacket.getLength() + " Bytes From: " + datagramPacket.getAddress().toString() + ":" + datagramPacket.getPort());
DebugLog.d(TAG, "Got " + datagramPacket.getLength() + " Bytes From: " + datagramPacket.getAddress().toString() + ":" + datagramPacket.getPort());
ResultCode processWirePacket = this.node.processWirePacket(System.currentTimeMillis(), -1, new InetSocketAddress(datagramPacket.getAddress(), datagramPacket.getPort()), bArr2, jArr);
if (processWirePacket != ResultCode.RESULT_OK) {
Log.e(TAG, "processWirePacket returned: " + processWirePacket.toString());
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/net/kaaass/zerotierfix/util/DebugLog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.kaaass.zerotierfix.util;

import android.util.Log;

import net.kaaass.zerotierfix.BuildConfig;

/**
* 调试日志相关的工具函数,用于控制部分日志仅在调试模式下输出
*/
public class DebugLog {

public static void d(String tag, String message) {
if (BuildConfig.DEBUG) {
Log.d(tag, message);
}
}

public static void i(String tag, String message) {
if (BuildConfig.DEBUG) {
Log.i(tag, message);
}
}
}

0 comments on commit f8d1c24

Please sign in to comment.