2016-11-17 15:50:28 -08:00
package p2p
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
2017-10-31 15:24:11 -07:00
"github.com/ethereum/go-ethereum/log"
2016-11-17 15:50:28 -08:00
"github.com/ethereum/go-ethereum/p2p/discover"
)
const (
NODE_NAME_LENGTH = 32
PERMISSIONED_CONFIG = "permissioned-nodes.json"
)
// check if a given node is permissioned to connect to the change
func isNodePermissioned ( nodename string , currentNode string , datadir string , direction string ) bool {
var permissonedList [ ] string
nodes := parsePermissionedNodes ( datadir )
for _ , v := range nodes {
permissonedList = append ( permissonedList , v . ID . String ( ) )
}
2017-10-31 15:24:11 -07:00
log . Debug ( "isNodePermissioned" , "permisssionedList %v" , permissonedList )
2016-11-17 15:50:28 -08:00
for _ , v := range permissonedList {
if v == nodename {
2017-10-31 15:24:11 -07:00
log . Debug ( "isNodePermissioned" , "connection" , direction , "nodename" , nodename [ : NODE_NAME_LENGTH ] , "ALLOWED-BY" , currentNode [ : NODE_NAME_LENGTH ] )
2016-11-17 15:50:28 -08:00
return true
}
2017-10-31 15:24:11 -07:00
log . Debug ( "isNodePermissioned" , "connection" , direction , "nodename" , nodename [ : NODE_NAME_LENGTH ] , "DENIED-BY" , currentNode [ : NODE_NAME_LENGTH ] )
2016-11-17 15:50:28 -08:00
}
2017-10-31 15:24:11 -07:00
log . Debug ( "isNodePermissioned" , "connection" , direction , "nodename" , nodename [ : NODE_NAME_LENGTH ] , "DENIED-BY" , currentNode [ : NODE_NAME_LENGTH ] )
2016-11-17 15:50:28 -08:00
return false
}
//this is a shameless copy from the config.go. It is a duplication of the code
//for the timebeing to allow reload of the permissioned nodes while the server is running
func parsePermissionedNodes ( DataDir string ) [ ] * discover . Node {
2017-10-31 15:24:11 -07:00
log . Debug ( "parsePermissionedNodes DataDir %v, file %v" , DataDir , PERMISSIONED_CONFIG )
2016-11-17 15:50:28 -08:00
path := filepath . Join ( DataDir , PERMISSIONED_CONFIG )
if _ , err := os . Stat ( path ) ; err != nil {
2017-10-31 15:24:11 -07:00
log . Error ( "Read Error for permissioned-nodes.json file. This is because 'permissioned' flag is specified but no permissioned-nodes.json file is present." , "err" , err )
2016-11-17 15:50:28 -08:00
return nil
}
// Load the nodes from the config file
blob , err := ioutil . ReadFile ( path )
if err != nil {
2017-10-31 15:24:11 -07:00
log . Error ( "parsePermissionedNodes: Failed to access nodes" , "err" , err )
2016-11-17 15:50:28 -08:00
return nil
}
nodelist := [ ] string { }
if err := json . Unmarshal ( blob , & nodelist ) ; err != nil {
2017-10-31 15:24:11 -07:00
log . Error ( "parsePermissionedNodes: Failed to load nodes" , "err" , err )
2016-11-17 15:50:28 -08:00
return nil
}
// Interpret the list as a discovery node array
var nodes [ ] * discover . Node
for _ , url := range nodelist {
if url == "" {
2017-10-31 15:24:11 -07:00
log . Error ( "parsePermissionedNodes: Node URL blank" )
2016-11-17 15:50:28 -08:00
continue
}
node , err := discover . ParseNode ( url )
if err != nil {
2017-10-31 15:24:11 -07:00
log . Error ( "parsePermissionedNodes: Node URL" , "url" , url , "err" , err )
2016-11-17 15:50:28 -08:00
continue
}
nodes = append ( nodes , node )
}
return nodes
}