-
Notifications
You must be signed in to change notification settings - Fork 13
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
Push Publishing #82
Push Publishing #82
Changes from all commits
5c5b241
2482422
e309683
f6ef66a
7560c5b
aba5663
0e2cdf7
5ea514a
0c9d9cc
6af9780
62e76f9
afc7846
5c83611
6cf5956
564172f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
ggproto "github.com/gogo/protobuf/proto" | ||
p2p_crypto "github.com/libp2p/go-libp2p-crypto" | ||
p2p_host "github.com/libp2p/go-libp2p-host" | ||
p2p_peer "github.com/libp2p/go-libp2p-peer" | ||
p2p_pstore "github.com/libp2p/go-libp2p-peerstore" | ||
mc "github.com/mediachain/concat/mc" | ||
mcq "github.com/mediachain/concat/mc/query" | ||
|
@@ -18,6 +19,7 @@ import ( | |
"log" | ||
"os" | ||
"path" | ||
"strings" | ||
"sync" | ||
"time" | ||
) | ||
|
@@ -36,6 +38,7 @@ type Node struct { | |
home string | ||
db StatementDB | ||
ds Datastore | ||
auth PeerAuth | ||
mx sync.Mutex | ||
counter int | ||
} | ||
|
@@ -65,6 +68,11 @@ type Datastore interface { | |
Close() | ||
} | ||
|
||
type PeerAuth struct { | ||
peers map[p2p_peer.ID][]string | ||
mx sync.Mutex | ||
} | ||
|
||
type NodeInfo struct { | ||
Peer string `json:"peer"` | ||
Publisher string `json:"publisher"` | ||
|
@@ -85,6 +93,9 @@ var ( | |
MissingData = errors.New("Missing statement metadata") | ||
UnexpectedData = errors.New("Unexpected data object") | ||
BadData = errors.New("Bad data object; hash mismatch") | ||
BadPush = errors.New("Bad push value; unexpected object") | ||
BadResponse = errors.New("Bad response; unexpected object") | ||
BadRuleset = errors.New("Bad auth ruleset; unexpected object") | ||
) | ||
|
||
const ( | ||
|
@@ -95,6 +106,12 @@ const ( | |
|
||
var statusString = []string{"offline", "online", "public"} | ||
|
||
type PushError string | ||
|
||
func (s PushError) Error() string { | ||
return string(s) | ||
} | ||
|
||
type StreamError struct { | ||
Err string `json:"error"` | ||
} | ||
|
@@ -256,9 +273,10 @@ func (node *Node) openDS() error { | |
|
||
// persistent configuration | ||
type NodeConfig struct { | ||
Info string `json:"info,omitempty"` | ||
NAT string `json:"nat,omitempty"` | ||
Dir string `json:"dir,omitempty"` | ||
Info string `json:"info,omitempty"` | ||
NAT string `json:"nat,omitempty"` | ||
Dir string `json:"dir,omitempty"` | ||
Auth map[string]interface{} `json:"auth,omitempty"` | ||
} | ||
|
||
func (node *Node) saveConfig() error { | ||
|
@@ -268,6 +286,7 @@ func (node *Node) saveConfig() error { | |
if node.dir != nil { | ||
cfg.Dir = mc.FormatHandle(*node.dir) | ||
} | ||
cfg.Auth = node.auth.toJSON() | ||
|
||
bytes, err := json.Marshal(cfg) | ||
if err != nil { | ||
|
@@ -311,7 +330,8 @@ func (node *Node) loadConfig() error { | |
node.dir = &pinfo | ||
} | ||
|
||
return nil | ||
err = node.auth.fromJSON(cfg.Auth) | ||
return err | ||
} | ||
|
||
func (node *Node) doShutdown() { | ||
|
@@ -323,3 +343,106 @@ func (node *Node) doShutdown() { | |
node.ds.Close() | ||
os.Exit(0) | ||
} | ||
|
||
func (auth *PeerAuth) fromJSON(rmap map[string]interface{}) error { | ||
auth.mx.Lock() | ||
defer auth.mx.Unlock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this just executes when it goes out of scope? weird. I guess it's like a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's more like |
||
|
||
auth.peers = make(map[p2p_peer.ID][]string) | ||
for id58, xrules := range rmap { | ||
pid, err := p2p_peer.IDB58Decode(id58) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
xxrules, ok := xrules.([]interface{}) | ||
if !ok { | ||
return BadRuleset | ||
} | ||
|
||
rules := make([]string, len(xxrules)) | ||
for x, xxrule := range xxrules { | ||
rule, ok := xxrule.(string) | ||
if !ok { | ||
return BadRuleset | ||
} | ||
|
||
rules[x] = rule | ||
} | ||
|
||
auth.peers[pid] = rules | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (auth *PeerAuth) toJSON() map[string]interface{} { | ||
auth.mx.Lock() | ||
defer auth.mx.Unlock() | ||
|
||
rmap := make(map[string]interface{}) | ||
for pid, rules := range auth.peers { | ||
rmap[pid.Pretty()] = rules | ||
} | ||
|
||
return rmap | ||
} | ||
|
||
func (auth *PeerAuth) authorize(pid p2p_peer.ID, nss []string) bool { | ||
if len(nss) == 0 { | ||
return false | ||
} | ||
|
||
auth.mx.Lock() | ||
defer auth.mx.Unlock() | ||
|
||
rules := auth.peers[pid] | ||
if len(rules) == 0 { | ||
return false | ||
} | ||
|
||
for _, ns := range nss { | ||
if !auth.authorizeAllow(rules, ns) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (auth *PeerAuth) authorizeAllow(rules []string, ns string) bool { | ||
for _, rule := range rules { | ||
switch { | ||
case rule == "*": | ||
return true | ||
|
||
case strings.HasSuffix(rule, ".*"): | ||
if strings.HasPrefix(ns, rule[:len(rule)-2]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uhm, I don't think it's worth the trouble. |
||
return true | ||
} | ||
|
||
case rule == ns: | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (auth *PeerAuth) getRules(pid p2p_peer.ID) []string { | ||
auth.mx.Lock() | ||
defer auth.mx.Unlock() | ||
return auth.peers[pid] | ||
} | ||
|
||
func (auth *PeerAuth) setRules(pid p2p_peer.ID, rules []string) { | ||
auth.mx.Lock() | ||
auth.peers[pid] = rules | ||
auth.mx.Unlock() | ||
} | ||
|
||
func (auth *PeerAuth) clearRules(pid p2p_peer.ID) { | ||
auth.mx.Lock() | ||
delete(auth.peers, pid) | ||
auth.mx.Unlock() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be nice to fallback through a couple/one that we run
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, that's probably a good idea.
The code running ifconfig.co is available at github: https://github.com/martinp/ipd
So we can run it at
ifconfig.medichain.io
and have fallback toifconfig.co
.I'll open an enhancement issue and add support once we have the ifconfing server up.