Skip to content

Commit

Permalink
add tor
Browse files Browse the repository at this point in the history
  • Loading branch information
aredoff committed Nov 24, 2023
1 parent 536801b commit 2c6bd3e
Show file tree
Hide file tree
Showing 15 changed files with 399 additions and 111 deletions.
5 changes: 5 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func (a *App) Use(p Plugin) {
if ok {
webclientMixin.webclientInitialization()
}

dnsclientMixin, ok := p.(dnsclientMixinInterface)
if ok {
dnsclientMixin.dnsclientInitialization()
}
}

func (a *App) Activate() {
Expand Down
19 changes: 15 additions & 4 deletions cmd/iptec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,30 @@ import (
"fmt"

"github.com/aredoff/iptec"
"github.com/aredoff/iptec/plugins/bogon"
"github.com/aredoff/iptec/plugins/dnsbl"
"github.com/aredoff/iptec/plugins/firehol"
"github.com/aredoff/iptec/plugins/tor"
)

func main() {
a := iptec.New()
defer a.Close()
// a.Use(asn.New())
// a.Use(dns.New())
// a.Use(firehol.New())
// a.Use(bogon.New())
a.Use(dnsbl.New("8.8.8.8"))
a.Use(firehol.New())
a.Use(bogon.New())
a.Use(dnsbl.New())
a.Use(tor.New())
a.Activate()
report, err := a.Find("2002:c000:200::1")
report, err := a.Find("1.1.1.1")
// report, err := a.Find("2002:c000:200::1")
// report, err := a.Find("192.42.116.195")
// report, err := a.Find("2a0b:f4c2::29")
// report, err = a.Find("2001:67c:6ec:203:192:42:116:180")
// report, err = a.Find("2a0b:f4c2:2::38")
// report, err = a.Find("2a0b:f4c2::25")
// report, err = a.Find("2a0b:f4c1:2::251")
if err != nil {
fmt.Println("error")
}
Expand Down
15 changes: 15 additions & 0 deletions dns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package iptec

import "github.com/aredoff/iptec/dnslient"

type dnsclientMixinInterface interface {
dnsclientInitialization()
}

type DnsClient struct {
Dns *dnslient.Dns
}

func (m *DnsClient) dnsclientInitialization() {
m.Dns = dnslient.New()
}
53 changes: 53 additions & 0 deletions dnslient/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package dnslient

import (
"context"
"net"
"time"
)

func New() *Dns {
return &Dns{
timeout: 4 * time.Second,
}
}

type Dns struct {
client net.Resolver
timeout time.Duration
}

func (c *Dns) A(host string) (records []string, err error) {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
records, err = c.client.LookupHost(ctx, host)
return
}

func (c *Dns) TXT(host string) (records []string, err error) {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
records, err = c.client.LookupTXT(ctx, host)
return
}

func (c *Dns) CNAME(host string) (records string, err error) {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
records, err = c.client.LookupCNAME(ctx, host)
return
}

func (c *Dns) MX(host string) ([]string, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
mxs, err := c.client.LookupMX(ctx, host)
if err != nil {
return nil, err
}
records := make([]string, 0, len(mxs))
for _, v := range mxs {
records = append(records, v.Host)
}
return records, nil
}
85 changes: 0 additions & 85 deletions plugins/dnsbl/dnsbl.go

This file was deleted.

46 changes: 30 additions & 16 deletions plugins/dnsbl/plugin.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dnsbl

import (
"fmt"
"net"
"strings"
"sync"

"github.com/aredoff/iptec"
Expand All @@ -11,29 +13,28 @@ const (
name = "dnsbl"
)

func New(resolver string) *Dnsbl {
type providerResult struct {
dnsbl string
exist bool
reason string
}

func New() *Dnsbl {
return &Dnsbl{
name: name,
resolver: resolver,
name: name,
}
}

type Dnsbl struct {
name string
resolver string
client *dnsblCLient
name string
iptec.DnsClient
}

func (p *Dnsbl) Name() string {
return p.name
}

func (p *Dnsbl) Activate() error {
var err error
p.client, err = newDnsblClient(p.resolver)
if err != nil {
return err
}
return nil
}

Expand All @@ -43,7 +44,7 @@ func (p *Dnsbl) Find(address net.IP) (iptec.PluginReport, error) {
return nil, err
}
var wg sync.WaitGroup
ch := make(chan *addressResult, 16)
ch := make(chan *providerResult, 16)
var list []string
if address.To4() != nil {
list = sourcesIpv4
Expand All @@ -54,10 +55,23 @@ func (p *Dnsbl) Find(address net.IP) (iptec.PluginReport, error) {
wg.Add(1)
go func(provider string) {
defer wg.Done()
rep := p.client.Find(reverseAddress, provider)
if rep != nil {
rep.dnsbl = provider
ch <- rep
var res *providerResult
recordsA, err := p.Dns.A(fmt.Sprintf("%s.%s.", reverseAddress, provider))
if err == nil && len(recordsA) > 0 {
if checkArecord(recordsA[0]) {
res = &providerResult{
dnsbl: provider,
exist: true,
reason: "",
}
recoredsTXT, err := p.Dns.TXT(fmt.Sprintf("%s.%s.", reverseAddress, provider))
if err == nil && len(recoredsTXT) > 0 {
res.reason = strings.Join(recoredsTXT, ",")
}
}
}
if res != nil {
ch <- res
}
}(v)
}
Expand Down
1 change: 0 additions & 1 deletion plugins/dnsbl/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ var (
}

sourcesIpv6 = []string{
"dnsbl6.anticaptcha.net",
"all.v6.ascc.dnsbl.bit.nl",
"ipv6.all.dnsbl.bit.nl",
"bitonly.dnsbl.bit.nl",
Expand Down
11 changes: 11 additions & 0 deletions plugins/dnsbl/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,14 @@ func reverseIP(address net.IP) (string, error) {
}
return fmt.Sprintf("%d.%d.%d.%d", segments[3], segments[2], segments[1], segments[0]), nil
}

func checkArecord(recored string) bool {
answerAddress := net.ParseIP(recored)
if answerAddress != nil {
segments := answerAddress.To4()
if segments != nil && segments[3] > 1 {
return true
}
}
return false
}
5 changes: 4 additions & 1 deletion plugins/firehol/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ func (p *Firehol) Activate() error {
data, err := p.Cash.Get("firehol")
if err == nil {
p.Log.Info("Load data from cash.")
p.data.Deserialization(data)
err := p.data.Deserialization(data)
if err != nil {
return err
}
return nil
}
p.Log.Info("Collect data from firehol.org.")
Expand Down
4 changes: 0 additions & 4 deletions plugins/tor/dnsbl.go

This file was deleted.

Loading

0 comments on commit 2c6bd3e

Please sign in to comment.