Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ipset support timeout, hosts add update api #785

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/hosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func NewHosts(m domain.Matcher[*IPs]) *Hosts {
}
}

func (h *Hosts) GetMatcher() domain.Matcher[*IPs] {
return h.matcher
}

func (h *Hosts) Lookup(fqdn string) (ipv4, ipv6 []netip.Addr) {
ips, ok := h.matcher.Match(fqdn)
if !ok {
Expand Down
42 changes: 40 additions & 2 deletions plugin/executable/hosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ package hosts
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/IrineSistiana/mosdns/v5/coremain"
"github.com/IrineSistiana/mosdns/v5/pkg/hosts"
"github.com/IrineSistiana/mosdns/v5/pkg/matcher/domain"
"github.com/IrineSistiana/mosdns/v5/pkg/query_context"
"github.com/IrineSistiana/mosdns/v5/plugin/executable/sequence"
"github.com/go-chi/chi/v5"
"github.com/miekg/dns"
"go.uber.org/zap"
"net/http"
"os"
)

Expand All @@ -49,8 +53,10 @@ type Hosts struct {
h *hosts.Hosts
}

func Init(_ *coremain.BP, args any) (any, error) {
return NewHosts(args.(*Args))
func Init(bp *coremain.BP, args any) (any, error) {
h, err := NewHosts(args.(*Args))
bp.RegAPI(h.Api(bp.L()))
return h, err
}

func NewHosts(args *Args) (*Hosts, error) {
Expand Down Expand Up @@ -87,3 +93,35 @@ func (h *Hosts) Exec(_ context.Context, qCtx *query_context.Context) error {
}
return nil
}

func (h *Hosts) Api(logger *zap.Logger) *chi.Mux {
router := chi.NewRouter()
router.Post("/update", func(writer http.ResponseWriter, request *http.Request) {
b := request.Body
payload := map[string]interface{}{}
payload["code"] = -1
if err := domain.LoadFromTextReader[(*hosts.IPs)](h.h.GetMatcher().(*domain.MixMatcher[*hosts.IPs]), b, hosts.ParseIPs); err != nil {
payload["msg"] = err.Error()
if err := respondWithJSON(writer, http.StatusOK, payload); err != nil {
logger.Error("fail to response hosts update", zap.Error(err))
}
return
}
payload["msg"] = "ok"
payload["code"] = 0
if err := respondWithJSON(writer, http.StatusOK, payload); err != nil {
logger.Error("fail to response hosts update", zap.Error(err))
}
})
return router
}

func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) error {
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
if _, err := w.Write(response); err != nil {
return err
}
return nil
}
19 changes: 15 additions & 4 deletions plugin/executable/ipset/ipset.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ func init() {
type Args struct {
SetName4 string `yaml:"set_name4"`
SetName6 string `yaml:"set_name6"`
Mask4 int `yaml:"mask4"` // default 24
Mask6 int `yaml:"mask6"` // default 32
Mask4 int `yaml:"mask4"` // default 24
Mask6 int `yaml:"mask6"` // default 32
Timeout4 int `yaml:"timeout4"` // default -1, not use
Timeout6 int `yaml:"timeout6"` // default -1, not use
}

var _ sequence.Executable = (*ipSetPlugin)(nil)
Expand All @@ -52,21 +54,30 @@ func QuickSetup(_ sequence.BQ, s string) (any, error) {
args := new(Args)
for _, argsStr := range fs {
ss := strings.Split(argsStr, ",")
if len(ss) != 3 {
return nil, fmt.Errorf("invalid args, expect 5 fields, got %d", len(ss))
if len(ss) != 3 && len(ss) != 4 {
return nil, fmt.Errorf("invalid args, expect 3 or 4 fields, got %d", len(ss))
}

m, err := strconv.Atoi(ss[2])
if err != nil {
return nil, fmt.Errorf("invalid mask, %w", err)
}
ttl := -1
if len(ss) == 4 {
if ttl, err = strconv.Atoi(ss[3]); err != nil {
return nil, fmt.Errorf("invalid timeout, %w", err)
}
}

switch ss[1] {
case "inet":
args.Mask4 = m
args.SetName4 = ss[0]
args.Timeout4 = ttl
case "inet6":
args.Mask6 = m
args.SetName6 = ss[0]
args.Timeout6 = ttl
default:
return nil, fmt.Errorf("invalid set family, %s", ss[0])
}
Expand Down
11 changes: 9 additions & 2 deletions plugin/executable/ipset/ipset_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ func newIpSetPlugin(args *Args) (*ipSetPlugin, error) {
}, nil
}

func addIpSet(nl *ipset.NetLink, setName string, prefix netip.Prefix, timeout int) error {
if timeout == -1 {
return ipset.AddPrefix(nl, setName, prefix)
}
return ipset.AddPrefix(nl, setName, prefix, ipset.OptTimeout(uint32(timeout)))
}

func (p *ipSetPlugin) Exec(_ context.Context, qCtx *query_context.Context) error {
r := qCtx.R()
if r != nil {
Expand All @@ -79,7 +86,7 @@ func (p *ipSetPlugin) addIPSet(r *dns.Msg) error {
if !ok {
return fmt.Errorf("invalid A record with ip: %s", rr.A)
}
if err := ipset.AddPrefix(p.nl, p.args.SetName4, netip.PrefixFrom(addr, p.args.Mask4)); err != nil {
if err := addIpSet(p.nl, p.args.SetName4, netip.PrefixFrom(addr, p.args.Mask4), p.args.Timeout4); err != nil {
return err
}

Expand All @@ -91,7 +98,7 @@ func (p *ipSetPlugin) addIPSet(r *dns.Msg) error {
if !ok {
return fmt.Errorf("invalid AAAA record with ip: %s", rr.AAAA)
}
if err := ipset.AddPrefix(p.nl, p.args.SetName6, netip.PrefixFrom(addr, p.args.Mask6)); err != nil {
if err := addIpSet(p.nl, p.args.SetName6, netip.PrefixFrom(addr, p.args.Mask6), p.args.Timeout6); err != nil {
return err
}
default:
Expand Down