2016-11-17 15:50:28 -08:00
package p2p
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
2018-08-31 04:35:35 -07:00
"strings"
2016-11-17 15:50:28 -08:00
2017-10-31 15:24:11 -07:00
"github.com/ethereum/go-ethereum/log"
2019-01-29 03:08:50 -08:00
"github.com/ethereum/go-ethereum/p2p/enode"
2019-03-08 01:09:24 -08:00
"github.com/ethereum/go-ethereum/params"
2016-11-17 15:50:28 -08:00
)
const (
2019-03-08 01:09:24 -08:00
NODE_NAME_LENGTH = 32
2016-11-17 15:50:28 -08:00
)
2019-03-26 08:20:06 -07:00
//TODO update this based on permission changes
2016-11-17 15:50:28 -08:00
// check if a given node is permissioned to connect to the change
func isNodePermissioned ( nodename string , currentNode string , datadir string , direction string ) bool {
2017-11-14 13:43:43 -08:00
var permissionedList [ ] string
2018-11-19 23:43:56 -08:00
nodes := ParsePermissionedNodes ( datadir )
2016-11-17 15:50:28 -08:00
for _ , v := range nodes {
2019-01-29 03:08:50 -08:00
permissionedList = append ( permissionedList , v . ID ( ) . String ( ) )
2016-11-17 15:50:28 -08:00
}
2017-11-14 13:43:43 -08:00
log . Debug ( "isNodePermissioned" , "permissionedList" , permissionedList )
for _ , v := range permissionedList {
2016-11-17 15:50:28 -08:00
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 ] )
2018-08-31 04:35:35 -07:00
// check if the node is blacklisted
2019-03-08 01:09:24 -08:00
if isNodeBlackListed ( nodename , datadir ) {
2018-08-31 04:35:35 -07:00
return false
}
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
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
2019-03-08 01:09:24 -08:00
func ParsePermissionedNodes ( DataDir string ) [ ] * enode . Node {
2016-11-17 15:50:28 -08:00
2019-02-13 18:24:32 -08:00
log . Debug ( "parsePermissionedNodes" , "DataDir" , DataDir , "file" , params . PERMISSIONED_CONFIG )
2016-11-17 15:50:28 -08:00
2019-02-13 18:24:32 -08:00
path := filepath . Join ( DataDir , params . PERMISSIONED_CONFIG )
2016-11-17 15:50:28 -08:00
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
2019-01-29 03:08:50 -08:00
var nodes [ ] * enode . Node
2016-11-17 15:50:28 -08:00
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
}
2019-01-29 03:08:50 -08:00
node , err := enode . ParseV4 ( url )
2016-11-17 15:50:28 -08:00
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
}
2018-08-31 04:35:35 -07:00
// This function checks if the node is black-listed
2019-03-08 01:09:24 -08:00
func isNodeBlackListed ( nodeName , dataDir string ) bool {
2019-02-13 18:24:32 -08:00
log . Debug ( "isNodeBlackListed" , "DataDir" , dataDir , "file" , params . BLACKLIST_CONFIG )
2018-08-31 04:35:35 -07:00
2019-02-13 18:24:32 -08:00
path := filepath . Join ( dataDir , params . BLACKLIST_CONFIG )
2018-08-31 04:35:35 -07:00
if _ , err := os . Stat ( path ) ; err != nil {
2018-09-17 22:52:46 -07:00
log . Debug ( "Read Error for disallowed-nodes.json file. disallowed-nodes.json file is not present." , "err" , err )
2018-08-31 04:35:35 -07:00
return false
}
// Load the nodes from the config file
blob , err := ioutil . ReadFile ( path )
if err != nil {
2018-09-17 22:52:46 -07:00
log . Debug ( "isNodeBlackListed: Failed to access nodes" , "err" , err )
2019-02-13 18:24:32 -08:00
return true
2018-08-31 04:35:35 -07:00
}
nodelist := [ ] string { }
if err := json . Unmarshal ( blob , & nodelist ) ; err != nil {
2018-09-17 22:52:46 -07:00
log . Debug ( "parsePermissionedNodes: Failed to load nodes" , "err" , err )
2018-08-31 04:35:35 -07:00
return false
}
for _ , v := range nodelist {
if strings . Contains ( v , nodeName ) {
return true
}
}
return false
}