styx/graph/main.go
2020-08-28 15:55:18 +02:00

208 lines
3.1 KiB
Go

package graph
import (
"context"
"github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"google.golang.org/grpc"
)
func ConnectToDgraph(setupSchema bool) (*dgo.Dgraph, error) {
conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
if err != nil {
return nil, err
}
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(conn))
if setupSchema {
err = setupDgraphSchema(dgraphClient)
if err != nil {
return nil, err
}
}
return dgraphClient, nil
}
func setupDgraphSchema(c *dgo.Dgraph) error {
// err := c.Alter(context.Background(), &api.Operation{
// DropAll: true,
// })
// if err != nil {
// return err
// }
err := c.Alter(context.Background(), &api.Operation{
Schema: `
id: string @index(exact, term) .
nodeType: string @index(term) .
ndata: string .
nodeOne: uid .
nodeTwo: uid .
subNode: uid .
sourceName: string @index(term) .
timestamp: string .
created: string .
modified: string .
certNode: uid .
shodanNode: uid .
pasteNode: uid .
match: uid .
type Node {
id: string
nodeType: string
ndata: string
created: string
modified: string
certNode: CertNode
shodanNode: ShodanNode
pasteNode: PasteNode
match: Match
}
nodes: [uid] .
target: string @index(term) .
type Match {
id: string
nodes: Node
timestamp: string
target: string
}
type Edge {
id: string
nodeOne: uid
nodeTwo: uid
timestamp: string
sourceName: string
}
fingerprint: string @index(exact, term) .
notBefore: string .
notAfter: string .
cn: string @index(term) .
sourceName: string @index(term) .
serialNumber: string @index(term) .
basicConstraints: string .
chain: [uid].
raw: uid .
type CertNode {
id: string
fingerprint: string
notBefore: string
notAfter: string
cn: string
sourceName: string
serialNumber: string
basicConstraints: string
raw: CertRaw
chain: CertNode
}
type CertRaw {
id: string
nodeType: string
created: string
modified: string
}
hostData: uid .
type ShodanNode {
id: string
nodeType: string
created: string
modified: string
hostData: uid
}
product: string @index(term) .
hostnames: [string] @index(term) .
version: string .
title: string @index(term) .
ip: string @index(term) .
os: string .
organization: string @index(term) .
isp: string .
cpe: [string] .
asn: string .
port: int .
html: string .
banner: string .
transport: string .
domains: [string] .
timestamp: string .
type Hostdata {
product: string
hostnames: [string]
version: string
title: string
ip: string
os: string
organization: string
isp: string
cpe: [string]
asn: string
port: int
html: string
banner: string
transport: string
domains: [string]
timestamp: string
}
fullPaste: uid .
meta: uid .
full: string @index(term) .
scrape_url: string .
full_url: string .
date: string .
key: string .
size: string .
expire: string .
title: string @index(term) .
syntax: string .
user: string @index(term) .
type PasteMeta {
scrape_url: string
full_url: string
date: string
key: string
size: string
expire: string
title: string
syntax: string
user: string
}
type FullPaste {
meta: PasteMeta
full: string
nodeType: string
}
type PasteNode {
id: string
nodeType: string
created: string
modified: string
fullPaste: FullPaste
}
`})
if err != nil {
return err
}
return nil
}