Skip to content

Commit

Permalink
feat: add value field to the dns log (#67)
Browse files Browse the repository at this point in the history
record resolve value of dns log to facilitate the observation of dns rebinding.
  • Loading branch information
Li4n0 authored May 23, 2023
1 parent c53ec79 commit 223c14a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ jobs:
uses: actions/checkout@v2
- name: create dist directory
run: mkdir frontend/dist && touch frontend/dist/revsuit
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.19.x
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from 'axios'
import {store} from "@/main";

const service = axios.create({
baseURL: location.pathname.slice(0, -'/admin/'.length) + "/api", // api的base_url
baseURL: "../api", // api的base_url
timeout: 5000, // request timeout
validateStatus: function (status) {
if (status === 403) {
Expand All @@ -12,4 +12,4 @@ const service = axios.create({
}
})

export default service
export default service
9 changes: 9 additions & 0 deletions frontend/src/views/logs/Dns.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ const columns = [
filterIcon: 'filterIcon',
},
},
{
title: 'VALUE',
dataIndex: 'value',
key: 'value',
scopedSlots: {
filterDropdown: 'filterDropdown',
filterIcon: 'filterIcon',
},
},
{
title: 'REMOTE IP',
key: 'remote_ip',
Expand Down
2 changes: 1 addition & 1 deletion frontend/vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
devServer: {
proxy: {
'/api': {
target: 'http://localhost:10000',
target: 'http://127.0.0.1:10000/revsuit/',
}
},
},
Expand Down
28 changes: 16 additions & 12 deletions pkg/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (s *Server) newZone(name string) *newdns.Zone {
"ns2.hostmaster.com.",
"ns3.hostmaster.com.",
},
Handler: func(lookedName, remoteAddr string) ([]newdns.Set, error) {
Handler: func(lookedName, remoteAddr string) (set []newdns.Set, err error) {
ip := strings.Split(remoteAddr, ":")[0]

for _, _rule := range s.getRules() {
Expand All @@ -135,12 +135,24 @@ func (s *Server) newZone(name string) *newdns.Zone {
continue
}

r, err := newRecord(_rule, flag, domain, ip, ipinfo.Area(ip))
if _rule.Value != "" {
_type := _rule.Type
if _rule.Type == newdns.REBINDING {
_type = newdns.A
}
set = newSet(_rule, name, rule.CompileTpl(_rule.Value, vars), ip, _type)
}

var value string
if len(set) > 0 && len(set[0].Records) > 0 {
value = set[0].Records[0].Address
}
r, err := newRecord(_rule, flag, domain, value, ip, ipinfo.Area(ip))
if err != nil {
log.Warn("DNS record(rule_id:%s) created failed :%s", _rule.Name, err)
return nil, nil
}
log.Info("DNS record[id:%d rule:%s remote_ip:%s] has been created", r.ID, _rule.Name, ip)
log.Info("DNS record[id:%d rule:%s remote_ip:%s, value:%s] has been created", r.ID, _rule.Name, ip, value)

//only send to client or notify user when this connection recorded first time.
var count int64
Expand Down Expand Up @@ -168,15 +180,7 @@ func (s *Server) newZone(name string) *newdns.Zone {
}()
}
}
if _rule.Value != "" {
value := rule.CompileTpl(_rule.Value, vars)
_type := _rule.Type
if _rule.Type == newdns.REBINDING {
_type = newdns.A
}

return newSet(_rule, name, value, ip, _type), nil
}
return set, err
}

return nil, nil
Expand Down
7 changes: 6 additions & 1 deletion pkg/dns/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var _ record.Record = (*Record)(nil)

type Record struct {
Domain string `gorm:"index" form:"domain" json:"domain"`
Value string `form:"value" json:"value"`

record.BaseRecord
Rule Rule `gorm:"foreignKey:RuleName;references:Name;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" form:"-" json:"-" notice:"-"`
Expand All @@ -30,7 +31,7 @@ func (r Record) Notice() {
notice.Notice(r)
}

func newRecord(rule *Rule, flag, domain, remoteIp, ipArea string) (r *Record, err error) {
func newRecord(rule *Rule, flag, domain, value, remoteIp, ipArea string) (r *Record, err error) {
r = &Record{
BaseRecord: record.BaseRecord{
Flag: flag,
Expand All @@ -39,6 +40,7 @@ func newRecord(rule *Rule, flag, domain, remoteIp, ipArea string) (r *Record, er
RequestTime: time.Now(),
},
Domain: domain,
Value: value,
Rule: *rule,
}

Expand Down Expand Up @@ -89,6 +91,9 @@ func Records(c *gin.Context) {
if dnsRecord.RuleName != "" {
db.Where("rule_name = ?", dnsRecord.RuleName)
}
if dnsRecord.Value != "" {
db.Where("address= ?", dnsRecord.Value)
}

//Delete records
if c.Request.Method == http.MethodDelete {
Expand Down

0 comments on commit 223c14a

Please sign in to comment.