-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
229 additions
and
101 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,4 @@ | ||
0.2 | ||
改进加密(以后应该不需要改进了) | ||
添加tcpDNS Over udpDNS,让客户端的tcpDNS解析更快 | ||
修复了一些小bug,至于CPU占用100%,目前还不知道有没有解决 |
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,37 @@ | ||
// CuteBi_XorCrypt.go | ||
package main | ||
|
||
import ( | ||
"encoding/base64" | ||
"errors" | ||
) | ||
|
||
var CuteBi_XorCrypt_password []byte | ||
|
||
/* 一个简单的异或加密 */ | ||
func CuteBi_XorCrypt(data []byte, passwordSub int) int { | ||
for dataSub := 0; dataSub < len(data); { | ||
data[dataSub] ^= CuteBi_XorCrypt_password[passwordSub] | byte(passwordSub) //如果只是data[dataSub] ^= CuteBi_XorCrypt_password[passwordSub],则密码"12"跟密码"1212"没用任何区别 | ||
dataSub++ | ||
passwordSub++ | ||
if passwordSub == len(CuteBi_XorCrypt_password) { | ||
passwordSub = 0 | ||
} | ||
} | ||
|
||
return passwordSub | ||
} | ||
|
||
func CuteBi_decrypt_host(host []byte) ([]byte, error) { | ||
hostDec := make([]byte, len(host)) | ||
n, err := base64.StdEncoding.Decode(hostDec, host) | ||
if err != nil { | ||
return nil, err | ||
} | ||
CuteBi_XorCrypt(hostDec, 0) | ||
if hostDec[n-1] != 0 { | ||
return nil, errors.New("Decrypt failed.") | ||
} | ||
|
||
return hostDec[:n-1], nil | ||
} |
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
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,94 @@ | ||
// dns.go | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"log" | ||
"net" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func dns_tcpOverUdp(cConn *net.TCPConn, host string, buffer []byte) { | ||
log.Println("Start dns_tcpOverUdp") | ||
defer cConn.Close() | ||
|
||
var err error | ||
var WLen, RLen, payloadLen int | ||
var pkgLen uint16 | ||
for { | ||
cConn.SetReadDeadline(time.Now().Add(tcp_timeout)) | ||
RLen, err = cConn.Read(buffer[payloadLen:]) | ||
if RLen <= 0 || err != nil { | ||
return | ||
} | ||
//解密 | ||
if len(CuteBi_XorCrypt_password) != 0 { | ||
CuteBi_XorCrypt(buffer[payloadLen:payloadLen+RLen], 0) | ||
} | ||
payloadLen += RLen | ||
if payloadLen > 2 { | ||
pkgLen = (uint16(buffer[0]) << 8) | (uint16(buffer[1])) //包长度转换 | ||
//防止访问非法数据 | ||
if int(pkgLen)+2 >= len(buffer) { | ||
return | ||
} | ||
//如果读取到了一个完整的包,就跳出循环 | ||
if int(pkgLen)+2 <= payloadLen { | ||
break | ||
} | ||
} | ||
} | ||
/* 连接目标地址 */ | ||
sConn, dialErr := net.Dial("udp", host) | ||
if dialErr != nil { | ||
log.Println(dialErr) | ||
cConn.Write([]byte("Proxy address [" + host + "] DNS Dial() error")) | ||
return | ||
} | ||
defer sConn.Close() | ||
sConn.SetReadDeadline(time.Now().Add(udp_timeout)) | ||
if WLen, err = sConn.Write(buffer[2:payloadLen]); WLen <= 0 || err != nil { | ||
return | ||
} | ||
if RLen, err = sConn.Read(buffer[2:]); RLen <= 0 || err != nil { | ||
return | ||
} | ||
//包长度转换 | ||
buffer[0] = byte(RLen << 8) | ||
buffer[1] = byte(RLen >> 8) | ||
//加密 | ||
if len(CuteBi_XorCrypt_password) != 0 { | ||
CuteBi_XorCrypt(buffer[:2+RLen], 0) | ||
} | ||
cConn.Write(buffer[:2+RLen]) | ||
} | ||
|
||
func Respond_HttpDNS(cConn *net.TCPConn, header []byte) bool { | ||
var domain string | ||
httpDNS_DomainSub := bytes.Index(header[:], []byte("?dn=")) | ||
if httpDNS_DomainSub < 0 { | ||
return false | ||
} | ||
if _, err := fmt.Sscanf(string(header[httpDNS_DomainSub+4:]), "%s", &domain); err != nil { | ||
log.Println(err) | ||
return false | ||
} | ||
log.Println("httpDNS domain: [" + domain + "]") | ||
defer cConn.Close() | ||
ips, err := net.LookupHost(domain) | ||
if err != nil { | ||
cConn.Write([]byte("HTTP/1.0 404 Not Found\r\nConnection: Close\r\nServer: CuteBi Linux Network httpDNS, (%>w<%)\r\nContent-type: charset=utf-8\r\n\r\n<html><head><title>HTTP DNS Server</title></head><body>查询域名失败<br/><br/>By: 萌萌萌得不要不要哒<br/>E-mail: [email protected]</body></html>")) | ||
log.Println("httpDNS domain: [" + domain + "], Lookup failed") | ||
} else { | ||
for i := 0; i < len(ips); i++ { | ||
if strings.Contains(ips[i], ":") == false { //跳过ipv6 | ||
fmt.Fprintf(cConn, "HTTP/1.0 200 OK\r\nConnection: Close\r\nServer: CuteBi Linux Network httpDNS, (%%>w<%%)\r\nContent-Length: %d\r\n\r\n%s", len(string(ips[i])), string(ips[i])) | ||
break | ||
} | ||
} | ||
log.Println("httpDNS domain: ["+domain+"], IPS: ", ips) | ||
} | ||
return true | ||
} |
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.