Skip to content

Commit

Permalink
修复了一个bug, 该bug导致处理某些非DNS查询的UDP包时功能会出现异常
Browse files Browse the repository at this point in the history
  • Loading branch information
nICEnnnnnnnLee committed Mar 15, 2019
1 parent 0327df8 commit 257fec1
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

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

6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@
![](https://raw.githubusercontent.com/nICEnnnnnnnLee/PureHost/master/view/function-preview.jpg)

## 更新日志
* v3.0
修复了一个bug, 该bug导致处理某些非DNS查询的UDP包时功能会出现异常
* v2.0
* 增加基础域名匹配
* 增加基础域名匹配(全域名匹配优先任意匹配)
e.g. host文件
```
127.0.0.3 www.test.com
127.0.0.1 *.test.com
127.0.0.2 test.test.com
```
那么,
```
test.test.com --> 127.0.0.2
www.test.com --> 127.0.0.3
test.com --> 127.0.0.1
123.test.com --> 127.0.0.1
xxx.test.com --> 127.0.0.1
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "top.nicelee.purehost"
minSdkVersion 24
targetSdkVersion 28
versionCode 2
versionName "2.0"
versionCode 3
versionName "3.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Binary file added app/release/PureHost v2.0.apk
Binary file not shown.
Binary file added app/release/PureHost v3.0.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion app/release/output.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":1,"versionName":"1.0","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":3,"versionName":"3.0","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
65 changes: 35 additions & 30 deletions app/src/main/java/top/nicelee/purehost/vpn/LocalVpnService.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,41 +212,46 @@ void onIPPacketReceived(IPHeader ipHeader, int size) throws IOException {
//if (ipHeader.getSourceIP() == intLocalIP && udpHeader.getSourcePort() != udpServer.port) {
if (ipHeader.getDestinationIP() != CommonMethods.ipStringToInt(udpServer.localIP)){
{
m_DNSBuffer.clear();
m_DNSBuffer.limit(ipHeader.getDataLength() - 8);
DnsPacket dnsPacket = DnsPacket.FromBytes(m_DNSBuffer);
//Short dnsId = dnsPacket.Header.getID();

boolean isNeedPollution = false;
Question question = dnsPacket.Questions[0];
//System.out.printf("DNS 查询的地址是%s \r\n", question.Domain);
String ipAddr = ConfigReader.domainIpMap.get(question.Domain);
if (ipAddr != null) {
isNeedPollution = true;
}else{
Matcher matcher = ConfigReader.patternRootDomain.matcher(question.Domain);
if(matcher.find()){
//System.out.printf("DNS 查询的地址根目录是%s \r\n", matcher.group(1));
ipAddr = ConfigReader.rootDomainIpMap.get(matcher.group(1));
if (ipAddr != null){
isNeedPollution = true;

try{
m_DNSBuffer.clear();
m_DNSBuffer.limit(ipHeader.getDataLength() - 8);
DnsPacket dnsPacket = DnsPacket.FromBytes(m_DNSBuffer);
//Short dnsId = dnsPacket.Header.getID();

boolean isNeedPollution = false;
Question question = dnsPacket.Questions[0];
//System.out.printf("DNS 查询的地址是%s \r\n", question.Domain);
String ipAddr = ConfigReader.domainIpMap.get(question.Domain);
if (ipAddr != null) {
isNeedPollution = true;
}else{
Matcher matcher = ConfigReader.patternRootDomain.matcher(question.Domain);
if(matcher.find()){
//System.out.printf("DNS 查询的地址根目录是%s \r\n", matcher.group(1));
ipAddr = ConfigReader.rootDomainIpMap.get(matcher.group(1));
if (ipAddr != null){
isNeedPollution = true;
}
}
}
}
if(isNeedPollution){
createDNSResponseToAQuery(udpHeader.m_Data, dnsPacket, ipAddr);
if(isNeedPollution){
createDNSResponseToAQuery(udpHeader.m_Data, dnsPacket, ipAddr);

ipHeader.setTotalLength(20 + 8 + dnsPacket.Size);
udpHeader.setTotalLength(8 + dnsPacket.Size);
ipHeader.setTotalLength(20 + 8 + dnsPacket.Size);
udpHeader.setTotalLength(8 + dnsPacket.Size);

ipHeader.setSourceIP(dstIP);
udpHeader.setSourcePort(dstPort);
ipHeader.setDestinationIP(originIP);
udpHeader.setDestinationPort(originPort);
ipHeader.setSourceIP(dstIP);
udpHeader.setSourcePort(dstPort);
ipHeader.setDestinationIP(originIP);
udpHeader.setDestinationPort(originPort);

CommonMethods.ComputeUDPChecksum(ipHeader, udpHeader);
vpnOutput.write(ipHeader.m_Data, ipHeader.m_Offset, ipHeader.getTotalLength());
break;
CommonMethods.ComputeUDPChecksum(ipHeader, udpHeader);
vpnOutput.write(ipHeader.m_Data, ipHeader.m_Offset, ipHeader.getTotalLength());
break;
}
}catch (Exception e){
System.out.println("当前udp包不是DNS报文");
}
}
if(NATSessionManager.getSession(originPort) == null){
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/top/nicelee/purehost/vpn/dns/DnsPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@ public class DnsPacket {
public int Size;

public static DnsPacket FromBytes(ByteBuffer buffer) {
if (buffer.limit() < 12)
if (buffer.limit() < 12){
//System.out.println("DNS size < 12");
return null;
if (buffer.limit() > 512)
}

if (buffer.limit() > 512){
//System.out.println("DNS size > 512");
return null;
}

DnsPacket packet = new DnsPacket();
packet.Size = buffer.limit();
packet.Header = DnsHeader.FromBytes(buffer);

if (packet.Header.QuestionCount > 2 || packet.Header.ResourceCount > 50 || packet.Header.AResourceCount > 50 || packet.Header.EResourceCount > 50) {
//System.out.println("DNS sources too complicated");
return null;
}

Expand Down

0 comments on commit 257fec1

Please sign in to comment.