Documentation
¶
Overview ¶
Example ¶
package main
import (
"bufio"
"log"
"os"
"github.com/m-mizutani/badman"
"github.com/m-mizutani/badman/source"
)
func main() {
man := badman.New()
if err := man.Download(source.DefaultSet); err != nil {
log.Fatal("Fail to download:", err)
}
fd, err := os.Open("ipaddrs_in_traffic_logs.txt")
if err != nil {
log.Fatal("Fail to open a file:", err)
}
defer fd.Close()
scanner := bufio.NewScanner(fd)
for scanner.Scan() {
entities, err := man.Lookup(scanner.Text())
if err != nil {
log.Fatal("Fail to lookup:", err)
}
if len(entities) > 0 {
log.Printf("Matched %s in %s list (reason: %s)\n",
entities[0].Name, entities[0].Src, entities[0].Reason)
}
}
}
Output:
Index ¶
- type BadEntity
- type BadMan
- func (x *BadMan) Download(srcSet []Source) error
- func (x *BadMan) Dump(w io.Writer) error
- func (x *BadMan) Insert(entity BadEntity) error
- func (x *BadMan) Load(r io.Reader) error
- func (x *BadMan) Lookup(name string) ([]BadEntity, error)
- func (x *BadMan) ReplaceRepository(repo Repository)
- func (x BadMan) ReplaceSerializer(ser Serializer)
- type EntityQueue
- type GzipJSONSerializer
- type GzipMsgpackSerializer
- type JSONSerializer
- type MsgpackSerializer
- type Repository
- type Serializer
- type Source
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BadEntity ¶
BadEntity is IP address or domain name that is appeared in BlackList. Name indicates both IP address and domain name.
type BadMan ¶
type BadMan struct {
// contains filtered or unexported fields
}
BadMan is Main interface of badman pacakge.
func (*BadMan) Download ¶
Download accesses blacklist data via Sources and store entities that is included in blacklist into repository.
func (*BadMan) Dump ¶
Dump output serialized data into w to save current repository.
Example ¶
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"time"
"github.com/m-mizutani/badman"
)
func main() {
//SetUp
tmp, err := ioutil.TempFile("", "*.dat")
if err != nil {
log.Fatal(err)
}
tmp.Close()
// Example
man := badman.New()
if err := man.Insert(badman.BadEntity{
Name: "orange.example.com",
SavedAt: time.Now(),
Src: "clock",
}); err != nil {
log.Fatal("Fail to insert an entity:", err)
}
wfd, err := os.Create(tmp.Name())
if err != nil {
log.Fatal("Fail to create a file:", err)
}
// Save current repository to a file
if err := man.Dump(wfd); err != nil {
log.Fatal("Fail to dump repository")
}
wfd.Close()
// Restore repository from a file
rfd, err := os.Open(tmp.Name())
if err != nil {
log.Fatal("Fail to open a serialized data file:", err)
}
if err := man.Load(rfd); err != nil {
log.Fatal("Fail to load repository")
}
entities, _ := man.Lookup("orange.example.com")
fmt.Println(entities[0].Name)
// TearDown
rfd.Close()
os.Remove(tmp.Name())
}
Output: orange.example.com
func (*BadMan) Insert ¶
Insert adds an entity one by one. It's expected to use adding IoC by feed or something like that.
Example ¶
package main
import (
"fmt"
"log"
"time"
"github.com/m-mizutani/badman"
)
func main() {
man := badman.New()
if err := man.Insert(badman.BadEntity{
Name: "10.0.0.1",
SavedAt: time.Now(),
Src: "It's me",
Reason: "testing",
}); err != nil {
log.Fatal("Fail to insert an entity:", err)
}
entities, err := man.Lookup("10.0.0.1")
if err != nil {
log.Fatal("Fail to lookup an entity:", err)
}
fmt.Println(entities[0].Name)
}
Output: 10.0.0.1
func (*BadMan) Load ¶
Load input data that is serialized by Dump(). Please note to use same Serializer for Dump and Load.
func (*BadMan) Lookup ¶
Lookup searches BadEntity (both of IP address and domain name). If not found, the function returns ([]BadEntity{}, nil). A reason to return list of BadEntity is that multiple blacklists may have same entity.
func (*BadMan) ReplaceRepository ¶
func (x *BadMan) ReplaceRepository(repo Repository)
ReplaceRepository changes Repository to store entities. Entities in old repository are removed.
func (BadMan) ReplaceSerializer ¶
func (x BadMan) ReplaceSerializer(ser Serializer)
ReplaceSerializer just changes Serializer with ser.
type EntityQueue ¶
EntityQueue is message queue via channel.
type GzipJSONSerializer ¶
type GzipJSONSerializer struct{}
GzipJSONSerializer is simple line json serializer
func NewGzipJSONSerializer ¶
func NewGzipJSONSerializer() *GzipJSONSerializer
NewGzipJSONSerializer is constructor of GzipJSONSerializer
func (*GzipJSONSerializer) Deserialize ¶
func (x *GzipJSONSerializer) Deserialize(r io.Reader) chan *EntityQueue
Deserialize of GzipJSONSerializer reads reader and unmarshal gzipped nd-json.
func (*GzipJSONSerializer) Serialize ¶
func (x *GzipJSONSerializer) Serialize(ch chan *EntityQueue, w io.Writer) error
Serialize of GzipJSONSerializer marshals BadEntity to gzipped JSON and append line feed at tail.
type GzipMsgpackSerializer ¶
type GzipMsgpackSerializer struct{}
GzipMsgpackSerializer is MessagePack serializer
func NewGzipMsgpackSerializer ¶
func NewGzipMsgpackSerializer() *GzipMsgpackSerializer
NewGzipMsgpackSerializer is constructor of GzipMsgpackSerializer
func (*GzipMsgpackSerializer) Deserialize ¶
func (x *GzipMsgpackSerializer) Deserialize(r io.Reader) chan *EntityQueue
Deserialize of GzipMsgpackSerializer reads reader and unmarshal gzipped nd-json.
func (*GzipMsgpackSerializer) Serialize ¶
func (x *GzipMsgpackSerializer) Serialize(ch chan *EntityQueue, w io.Writer) error
Serialize of GzipMsgpackSerializer encodes BadEntity to MessagePack format.
type JSONSerializer ¶
type JSONSerializer struct{}
JSONSerializer is simple line json serializer
func NewJSONSerializer ¶
func NewJSONSerializer() *JSONSerializer
NewJSONSerializer is constructor of JSONSerializer
func (*JSONSerializer) Deserialize ¶
func (x *JSONSerializer) Deserialize(r io.Reader) chan *EntityQueue
Deserialize of JSONSerializer reads reader and unmarshal nd-json.
func (*JSONSerializer) Serialize ¶
func (x *JSONSerializer) Serialize(ch chan *EntityQueue, w io.Writer) error
Serialize of JSONSerializer marshals BadEntity to JSON and append line feed at tail.
type MsgpackSerializer ¶
type MsgpackSerializer struct{}
MsgpackSerializer is MessagePack serializer
func NewMsgpackSerializer ¶
func NewMsgpackSerializer() *MsgpackSerializer
NewMsgpackSerializer is constructor of MsgpackSerializer
func (*MsgpackSerializer) Deserialize ¶
func (x *MsgpackSerializer) Deserialize(r io.Reader) chan *EntityQueue
Deserialize of MsgpackSerializer reads reader and unmarshal gzipped nd-json.
func (*MsgpackSerializer) Serialize ¶
func (x *MsgpackSerializer) Serialize(ch chan *EntityQueue, w io.Writer) error
Serialize of MsgpackSerializer encodes BadEntity to MessagePack format.
type Repository ¶
type Repository interface {
Put(entities []*BadEntity) error
Get(name string) ([]BadEntity, error)
Del(name string) error
Dump() chan *EntityQueue
}
Repository is interface of data store.
func NewDynamoRepository ¶
func NewDynamoRepository(region, tableName string) Repository
NewDynamoRepository is constructor of dynamoRepository
func NewInMemoryRepository ¶
func NewInMemoryRepository() Repository
NewInMemoryRepository is constructor of inMemoryRepository
type Serializer ¶
type Serializer interface {
Serialize(ch chan *EntityQueue, w io.Writer) error
Deserialize(r io.Reader) chan *EntityQueue
}
Serializer converts array of BadEntity to byte array and the reverse.
type Source ¶
type Source interface {
Download() chan *EntityQueue
}
Source is interface of BlackList.
