Skip to content

Commit

Permalink
增加多用户模式
Browse files Browse the repository at this point in the history
  • Loading branch information
lanyi1998 committed Dec 30, 2021
1 parent 014ea6f commit 2bfc357
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 72 deletions.
72 changes: 50 additions & 22 deletions Dns/Core.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"time"
)

var DnsData []DnsInfo
var DnsData map[string][]DnsInfo

var rw sync.RWMutex

Expand All @@ -26,7 +26,7 @@ type DnsInfo struct {

var D DnsInfo

//监听dns端口
// ListingDnsServer 监听dns端口
func ListingDnsServer() {
if runtime.GOOS != "windows" && os.Geteuid() != 0 {
log.Fatal("Please run as root")
Expand Down Expand Up @@ -59,26 +59,32 @@ func serverDNS(addr *net.UDPAddr, conn *net.UDPConn, msg dnsmessage.Message) {
queryNameStr = question.Name.String()
queryType = question.Type
queryName, _ = dnsmessage.NewName(queryNameStr)
resource dnsmessage.Resource
)
//域名过滤,避免网络扫描
//域名过滤,少于5位的不存储,避免网络扫描的垃圾数据
queryDoamin := strings.Split(strings.Replace(queryNameStr, fmt.Sprintf(".%s.", Core.Config.Dns.Domain), "", 1), ".")
if strings.Contains(queryNameStr, Core.Config.Dns.Domain) {
D.Set(DnsInfo{
Subdomain: queryNameStr[:len(queryNameStr)-1],
Ipaddress: addr.IP.String(),
Time: time.Now().Unix(),
})
} else {
return
if verifyToken(queryDoamin[len(queryDoamin)-1]) {
D.Set(queryDoamin[len(queryDoamin)-1], DnsInfo{
Subdomain: queryNameStr[:len(queryNameStr)-1],
Ipaddress: addr.IP.String(),
Time: time.Now().Unix(),
})
} else {
D.Set("other", DnsInfo{
Subdomain: queryNameStr[:len(queryNameStr)-1],
Ipaddress: addr.IP.String(),
Time: time.Now().Unix(),
})
}

}
var resource dnsmessage.Resource
switch queryType {
case dnsmessage.TypeA:
resource = NewAResource(queryName, [4]byte{127, 0, 0, 1})
default:
//fmt.Printf("not support dns queryType: [%s] \n", queryTypeStr)
return
resource = NewAResource(queryName, [4]byte{127, 0, 0, 1})
}

// send response
msg.Response = true
msg.Answers = append(msg.Answers, resource)
Expand All @@ -102,27 +108,49 @@ func NewAResource(query dnsmessage.Name, a [4]byte) dnsmessage.Resource {
Header: dnsmessage.ResourceHeader{
Name: query,
Class: dnsmessage.ClassINET,
TTL: 600,
TTL: 0,
},
Body: &dnsmessage.AResource{
A: a,
},
}
}

func (d *DnsInfo) Set(data DnsInfo) {
func (d *DnsInfo) Set(token string, data DnsInfo) {
rw.Lock()
DnsData = append(DnsData, data)
if DnsData[token] == nil {
DnsData[token] = []DnsInfo{data}
} else {
DnsData[token] = append(DnsData[token], data)
}
rw.Unlock()
}

func (d *DnsInfo) Get() string {
func (d *DnsInfo) Get(token string) string {
rw.RLock()
v, _ := json.Marshal(DnsData)
res := ""
if DnsData[token] != nil {
v, _ := json.Marshal(DnsData[token])
res = string(v)
} else {
res = "error"
}
rw.RUnlock()
return string(v)
return res
}

func (d *DnsInfo) Clear() {
DnsData = (DnsData)[0:0]
func (d *DnsInfo) Clear(token string) {
DnsData[token] = []DnsInfo{}
DnsData["other"] = []DnsInfo{}
}

func verifyToken(token string) bool {
tokens := strings.Split(Core.Config.HTTP.Token, ",")
flag := false
for _, v := range tokens {
if v == token {
flag = true
}
}
return flag
}
4 changes: 1 addition & 3 deletions Http/Core.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"net/http"
)

var DnsData = make(map[string]string)

//go:embed template
var template embed.FS

Expand All @@ -18,7 +16,7 @@ func ListingHttpManagementServer() {
mux.Handle("/template/", http.FileServer(http.FS(template)))
mux.HandleFunc("/", index)
}
mux.HandleFunc("/api/verifyToken", verifyToken)
mux.HandleFunc("/api/verifyToken", verifyTokenApi)
mux.HandleFunc("/api/getDnsData", GetDnsData)
mux.HandleFunc("/api/Clean", Clean)
mux.HandleFunc("/api/verifyDns", verifyDns)
Expand Down
37 changes: 24 additions & 13 deletions Http/Route.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@ import (
"io/ioutil"
"log"
"net/http"
"strings"
)

type RespData struct {
HTTPStatusCode string
Msg string
}

var resp RespData
type queryInfo struct {
Query string // 首字母大写
}

func index(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/template", http.StatusMovedPermanently)
}

func GetDnsData(w http.ResponseWriter, r *http.Request) {
key := r.Header.Get("token")
if key == Core.Config.HTTP.Token {
if verifyToken(key) {
fmt.Fprintf(w, JsonRespData(RespData{
HTTPStatusCode: "200",
Msg: Dns.D.Get(),
Msg: Dns.D.Get(key),
}))
} else {
fmt.Fprintf(w, JsonRespData(RespData{
Expand All @@ -36,14 +39,14 @@ func GetDnsData(w http.ResponseWriter, r *http.Request) {
}
}

func verifyToken(w http.ResponseWriter, r *http.Request) {
func verifyTokenApi(w http.ResponseWriter, r *http.Request) {
var data map[string]string
token, _ := ioutil.ReadAll(r.Body)
json.Unmarshal(token, &data)
if data["token"] == Core.Config.HTTP.Token {
if verifyToken(data["token"]) {
fmt.Fprintf(w, JsonRespData(RespData{
HTTPStatusCode: "200",
Msg: Core.Config.Dns.Domain,
Msg: data["token"] + "." + Core.Config.Dns.Domain,
}))
} else {
fmt.Fprintf(w, JsonRespData(RespData{
Expand All @@ -63,8 +66,8 @@ func JsonRespData(resp RespData) string {

func Clean(w http.ResponseWriter, r *http.Request) {
key := r.Header.Get("token")
if key == Core.Config.HTTP.Token {
Dns.D.Clear()
if verifyToken(key) {
Dns.D.Clear(key)
fmt.Fprintf(w, JsonRespData(RespData{
HTTPStatusCode: "200",
Msg: "success",
Expand All @@ -77,20 +80,28 @@ func Clean(w http.ResponseWriter, r *http.Request) {
}
}

func verifyDns(w http.ResponseWriter, r *http.Request) {
type queryInfo struct {
Query string // 首字母大写
func verifyToken(token string) bool {
tokens := strings.Split(Core.Config.HTTP.Token, ",")
flag := false
for _, v := range tokens {
if v == token {
flag = true
}
}
return flag
}

func verifyDns(w http.ResponseWriter, r *http.Request) {
var Q queryInfo
key := r.Header.Get("token")
if key == Core.Config.HTTP.Token {
if verifyToken(key) {
body, _ := ioutil.ReadAll(r.Body)
json.Unmarshal(body, &Q)
resp := RespData{
HTTPStatusCode: "200",
Msg: "false",
}
for _, v := range Dns.DnsData {
for _, v := range Dns.DnsData[key] {
if v.Subdomain == Q.Query {
resp.Msg = "true"
break
Expand Down
16 changes: 11 additions & 5 deletions Http/template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<meta name="keywords" content="dnslog"/>
<meta name="description" content="dnslog"/>
<script src="vue.js"></script>
<!-- <script src="https://cn.vuejs.org/js/vue.js"></script>-->
<script src="axios.min.js"></script>
</head>
<body>
Expand All @@ -19,7 +18,7 @@
<button type="button" v-on:click="GenPayload">复制随机子域名</button>
<button type="button" v-on:click="Clean">清空</button>
<br><br>
<div id="myDomain">&nbsp;</div>
<div id="myDomain">{{ randDomain }}</div>
<br>
<center>
<table id="myRecords" width=700 border="0" cellpadding="5" cellspacing="1" bgcolor="#EFF3FF"
Expand Down Expand Up @@ -83,6 +82,7 @@
DnsData: "",
Token: "",
domain: "",
randDomain: "",
},
methods: {
GetDomain: function () {
Expand All @@ -94,10 +94,15 @@
res => {
if (res['data']['HTTPStatusCode'] == 200) {
if (res['data']['Msg'] != 'null') {
this.DnsData = JSON.parse(res['data']['Msg']).reverse()
for (var i = 0; i < this.DnsData.length; i++) {
this.DnsData[i].Time = formatTimeToStr(this.DnsData[i].Time)
let DnsData = JSON.parse(res['data']['Msg']).reverse();
let DnsDatas = new Array();
for (var i = 0; i < DnsData.length; i++) {
if (DnsData[i].Subdomain.indexOf(this.Token) !== -1) {
DnsData[i].Time = formatTimeToStr(DnsData[i].Time)
DnsDatas.push(DnsData[i])
}
}
this.DnsData = DnsDatas
} else {
this.DnsData = ""
}
Expand Down Expand Up @@ -133,6 +138,7 @@
document.execCommand("Copy"); // 执行浏览器复制命令
let creatDom = document.getElementById("creatDom");
creatDom.parentNode.removeChild(creatDom);
app.randDomain = randDomain
alert("复制成功");
}
},
Expand Down
Loading

0 comments on commit 2bfc357

Please sign in to comment.