Skip to content

Commit

Permalink
add padding_auth plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
IrineSistiana committed Sep 23, 2023
1 parent 7d8a8b2 commit a921583
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugin/enabled_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import (

// executable and matcher
_ "github.com/IrineSistiana/mosdns/v5/plugin/mark"
_ "github.com/IrineSistiana/mosdns/v5/plugin/padding_auth"

// server
_ "github.com/IrineSistiana/mosdns/v5/plugin/server/http_server"
Expand Down
119 changes: 119 additions & 0 deletions plugin/padding_auth/padding_auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright (C) 2020-2022, IrineSistiana
*
* This file is part of mosdns.
*
* mosdns is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mosdns is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package padding_auth

import (
"bytes"
"context"
"crypto/md5"

"github.com/IrineSistiana/mosdns/v5/pkg/dnsutils"
"github.com/IrineSistiana/mosdns/v5/pkg/query_context"
"github.com/IrineSistiana/mosdns/v5/plugin/executable/sequence"
"github.com/miekg/dns"
)

const PluginType = "padding_auth"

func init() {
sequence.MustRegExecQuickSetup(PluginType, func(_ sequence.BQ, args string) (any, error) {
return newPaddingAuth(args), nil
})
sequence.MustRegMatchQuickSetup(PluginType, func(_ sequence.BQ, args string) (sequence.Matcher, error) {
return newPaddingAuth(args), nil
})
}

var _ sequence.RecursiveExecutable = (*paddingAuth)(nil)
var _ sequence.Matcher = (*paddingAuth)(nil)

type paddingAuth struct {
key []byte
}

func (m *paddingAuth) Match(_ context.Context, qCtx *query_context.Context) (bool, error) {
q := qCtx.Q()

opt := q.IsEdns0()
if opt == nil {
return false, nil
}
for i, optRR := range opt.Option {
if padding, ok := optRR.(*dns.EDNS0_PADDING); ok {
if bytes.Equal(padding.Padding, m.key) {
opt.Option = append(opt.Option[:i], opt.Option[i+1:]...)
return true, nil
}
}
}

return false, nil
}

func (m *paddingAuth) Exec(ctx context.Context, qCtx *query_context.Context, next sequence.ChainWalker) error {
upgraded := m.addPadding(qCtx.Q())
err := next.ExecNext(ctx, qCtx)
if err != nil {
return err
}

if r := qCtx.R(); r != nil {
if upgraded {
dnsutils.RemoveEDNS0(r)
}
}
return nil
}

func (m *paddingAuth) addPadding(q *dns.Msg) (upgraded bool) {
padding := &dns.EDNS0_PADDING{
Padding: append([]byte(nil), m.key...),
}

opt := q.IsEdns0()
if opt == nil {
upgraded = true
o := new(dns.OPT)
o.SetUDPSize(dns.MinMsgSize)
o.Hdr.Name = "."
o.Hdr.Rrtype = dns.TypeOPT
q.Extra = append(q.Extra, o)
return
}

var overwritten bool
for o := range opt.Option {
if opt.Option[o].Option() == dns.EDNS0PADDING {
opt.Option[o] = padding
overwritten = true
break
}
}
if !overwritten {
opt.Option = append(opt.Option, padding)
}
return
}

// newPaddingAuth format: str_key
func newPaddingAuth(s string) *paddingAuth {
key := md5.Sum([]byte(s))
return &paddingAuth{key: key[:]}
}

0 comments on commit a921583

Please sign in to comment.