nodeStore

package
v0.0.0-...-58a2b35 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

仿真测试

连接白名单,P2P网络通信需要其他节点做中转,加入白名单功能,可以直接和指定节点建立连接

Index

Constants

View Source
const (
	Str_zaro      = "0000000000000000000000000000000000000000000000000000000000000000" //字符串0
	Str_maxNumber = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" //256位的最大数十六进制表示id

)

Variables

View Source
var (
	Number_interval   []*big.Int = make([]*big.Int, 0) //相隔距离16分之一
	Number_interval06 []*big.Int = make([]*big.Int, 0) //相隔距离6分之一

	//	Number_max       *big.Int //最大id数
	//	Number_half      *big.Int //最大id的二分之一
	Number_quarter []*big.Int = make([]*big.Int, 0) //最大id的四分之一

)
View Source
var (
	NodeIdLevel uint = 256 //节点id长度
)
View Source
var (
	Zero = 8
)

Functions

func BuildArithmeticSequence

func BuildArithmeticSequence(num int) []*big.Int

构建等差数列 @num int 几分之一 值为16,则为十六分之一

func CheckIdInfo

func CheckIdInfo(idInfo IdInfo) bool

检查idInfo是否合法 1.地址生成合法 2.签名正确 @return true:合法;false:不合法;

func CheckPukAddr

func CheckPukAddr(pubKey []byte, addr AddressNet) bool

检查公钥生成的地址是否一样 @return bool 是否一样 true=相同;false=不相同;

func CheckSafeAddr

func CheckSafeAddr(pubkey []byte) bool

func CheckZn

func CheckZn(text []byte) bool

func CreateUUID

func CreateUUID() string

func EqualIds

func EqualIds(oldIds, newIds [][]byte) (isChange bool, collective [][]byte)

对比两个id是否一样 @return bool 是否改变 true=已经改变;false=未改变; @return [][]byte 共有的逻辑节点

func EqualNodes

func EqualNodes(nsm *NodeSimulationManager)

对比标准构建的逻辑节点和自动发现的逻辑节点差异

func GetBinary

func GetBinary(key []byte) string

func HashPubKey

func HashPubKey(pubKey []byte) []byte

func IntToBytes

func IntToBytes(n int64) []byte

func LogicNodes

func LogicNodes(id []byte, idSuper, idOrdinary [][]byte, level uint) [][]byte

计算一个节点,应该有哪些逻辑节点 @id []byte 计算的节点 @idSuper [][]byte 已知的逻辑节点 @idOrdinary [][]byte 补充的逻辑节点

func SearchNodeID

func SearchNodeID(logicIDs [][]byte, findID []byte) []byte

在逻辑节点中搜索节点id

Types

type AddressBytes

type AddressBytes []AddressNet

对地址通过[]bytes大小排序

func (AddressBytes) Len

func (this AddressBytes) Len() int

func (AddressBytes) Less

func (this AddressBytes) Less(i, j int) bool

func (AddressBytes) Swap

func (this AddressBytes) Swap(i, j int)

type AddressNet

type AddressNet []byte

节点地址

func AddressFromB58String

func AddressFromB58String(str string) AddressNet

func BuildAddr

func BuildAddr(pubKey []byte) AddressNet

通过公钥生成网络节点地址,将公钥两次hash得到网络节点地址 @version []byte 版本号(如比特币主网版本号“0x00")

func GetMagneticID100

func GetMagneticID100(id *AddressNet) (logicIds []*AddressNet)

获得指定节点的100个磁力节点

func GetMagneticID16

func GetMagneticID16() (logicIds []*AddressNet)

获得指定节点的16个磁力节点

func GetQuarterLogicAddrNetByAddrNet

func GetQuarterLogicAddrNetByAddrNet(id *AddressNet) (logicIds []*AddressNet)

得到保存数据的逻辑节点 @idStr id十六进制字符串 @return 4分之一节点

func RemoveDuplicateAddress

func RemoveDuplicateAddress(addrs []*AddressNet) []*AddressNet

去除重复地址

func (AddressNet) B58String

func (this AddressNet) B58String() string

type IdASC

type IdASC struct {
	// contains filtered or unexported fields
}

从小到大排序

func NewIdASC

func NewIdASC(findNode *big.Int, nodes []*big.Int) *IdASC

func (IdASC) Len

func (this IdASC) Len() int

func (IdASC) Less

func (this IdASC) Less(i, j int) bool

func (IdASC) Sort

func (this IdASC) Sort() []*big.Int

func (IdASC) Swap

func (this IdASC) Swap(i, j int)

type IdDESC

type IdDESC []*big.Int

从大到小排序

func (IdDESC) Len

func (this IdDESC) Len() int

func (IdDESC) Less

func (this IdDESC) Less(i, j int) bool

func (IdDESC) Swap

func (this IdDESC) Swap(i, j int)

type IdInfo

type IdInfo struct {
	Id   AddressNet        `json:"id"`   //id,节点网络地址
	EPuk ed25519.PublicKey `json:"epuk"` //ed25519公钥,身份密钥的公钥
	CPuk dh.Key            `json:"cpuk"` //curve25519公钥,DH公钥
	V    uint32            `json:"v"`    //DH公钥版本,低版本将被弃用,用于自动升级更换DH公钥协议
	Sign []byte            `json:"sign"` //ed25519私钥签名,Sign(V + CPuk)

}

Id信息

func BuildIdinfo

func BuildIdinfo(key keystore.KeystoreInterface, pwd string) (*IdInfo, error)

加载本地私钥生成idinfo

func ParseIdInfo

func ParseIdInfo(bs []byte) (*IdInfo, error)

func (*IdInfo) CheckSignDHPuk

func (this *IdInfo) CheckSignDHPuk() bool

验证签名

func (*IdInfo) Proto

func (this *IdInfo) Proto() ([]byte, error)

func (*IdInfo) SignDHPuk

func (this *IdInfo) SignDHPuk(prk ed25519.PrivateKey)

给idInfo签名

type Ids

type Ids struct {
	// contains filtered or unexported fields
}

func NewIds

func NewIds(id []byte, level uint) *Ids

func (*Ids) AddId

func (this *Ids) AddId(id []byte) (ok bool, removeIDs [][]byte)

添加一个id @return bool 是否是自己需要的节点

func (*Ids) GetIds

func (this *Ids) GetIds() [][]byte

获取所有id

func (*Ids) GetIndex

func (this *Ids) GetIndex(index int) []byte

通过下标获取id

func (*Ids) RemoveId

func (this *Ids) RemoveId(id []byte)

删除一个id

type Kademlia

type Kademlia struct {
	// contains filtered or unexported fields
}

func NewKademlia

func NewKademlia(length int) *Kademlia

func (*Kademlia) Add

func (this *Kademlia) Add(nodes ...*big.Int)

添加节点

func (*Kademlia) Get

func (this *Kademlia) Get(nodeId *big.Int) []*big.Int

获得这个节点由近到远距离排序的节点列表

func (*Kademlia) Len

func (this *Kademlia) Len() int

func (*Kademlia) Less

func (this *Kademlia) Less(i, j int) bool

func (*Kademlia) Swap

func (this *Kademlia) Swap(i, j int)

type KeyPairCurve

type KeyPairCurve struct {
	PrivateKey PrivateKeyCurve
	PublicKey  PublicKeyCurve
}

func GenerateKey_curve

func GenerateKey_curve() *KeyPairCurve

生成一对密钥

type KeyPairED

type KeyPairED struct {
	PrivateKey ed25519.PrivateKey
	PublicKey  ed25519.PublicKey
}

func GenerateKey_ed

func GenerateKey_ed() *KeyPairED

生成一对密钥

type LogicManager

type LogicManager struct {
	// contains filtered or unexported fields
}

func NewLogicManager

func NewLogicManager(id []byte) *LogicManager

type LogicNumBuider

type LogicNumBuider struct {
	// contains filtered or unexported fields
}

func NewLogicNumBuider

func NewLogicNumBuider(id []byte, level uint) *LogicNumBuider

func (*LogicNumBuider) GetNodeNetworkNum

func (this *LogicNumBuider) GetNodeNetworkNum() []*[]byte

得到每个节点网络的网络号,不包括本节点 @id *utils.Multihash 要计算的id @level int 深度

type Node

type Node struct {
	IdInfo       IdInfo `json:"idinfo"`  //节点id信息,id字符串以16进制显示
	IsSuper      uint32 `json:"issuper"` //是不是超级节点,超级节点有外网ip地址,可以为其他节点提供代理服务
	Addr         string `json:"addr"`    //外网ip地址
	TcpPort      uint16 `json:"tcpport"` //TCP端口
	IsApp        bool   `json:"isapp"`   //是不是手机端节点
	MachineID    int64  `json:"mid"`     //每个节点启动的时候生成一个随机数,用作判断多个节点使用同一个key连入网络的情况【已经废弃,不再使用,为了向前兼容,所以保留】
	MachineIDStr string `json:"mid2"`    //设备id,由使用者控制,用作判断多个节点使用同一个key连入网络的情况
	Version      uint64 `json:"v"`       //版本号

	Type       NodeClass        `json:"-"`          //
	Lock       *sync.RWMutex    `json:"-"`          //
	Sessions   []engine.Session `json:"-"`          //节点的多个session
	SetGod     bool             `json:"setgod"`     //设置上帝节点
	SetGodTime int64            `json:"setgodtime"` //设置上帝节点时间
	QuicPort   uint16           `json:"quicport"`   //Quic端口
	// contains filtered or unexported fields
}

保存节点的id ip地址 不同协议的端口

func ParseNodeProto

func ParseNodeProto(bs []byte) (*Node, error)

func ParseNodesProto

func ParseNodesProto(bs *[]byte) ([]Node, error)

func (*Node) AddSession

func (this *Node) AddSession(ss engine.Session)

func (*Node) CheckHaveOtherSessions

func (this *Node) CheckHaveOtherSessions(ss engine.Session) bool

检查除了参数中传入的session之外,是否还有其他session

func (*Node) FlashOnlineTime

func (this *Node) FlashOnlineTime()

func (*Node) GetIsSuper

func (this *Node) GetIsSuper() bool

获取这个节点是否是超级节点

func (*Node) GetSessions

func (this *Node) GetSessions() []engine.Session

func (*Node) Proto

func (this *Node) Proto() ([]byte, error)

func (*Node) RemoveSession

func (this *Node) RemoveSession(ss engine.Session) bool

func (*Node) SetIsSuper

func (this *Node) SetIsSuper(isSuper bool)

获取这个节点是否是超级节点

type NodeClass

type NodeClass int
const (
	Node_min = 0 //每个节点的最少连接数量

	Node_type_all        NodeClass = 0 //包含所有类型
	Node_type_logic      NodeClass = 1 //自己需要的逻辑节点
	Node_type_client     NodeClass = 2 //保存其他逻辑节点连接到自己的节点,都是超级节点
	Node_type_proxy      NodeClass = 3 //被代理的节点
	Node_type_other      NodeClass = 4 //每个节点有最少连接数量
	Node_type_white_list NodeClass = 5 //连接白名单
	Node_type_oneByone   NodeClass = 6 //onebyone规则连接类型
)

type NodeManager

type NodeManager struct {
	NodeSelf *Node //

	OutFindNode      chan *AddressNet //= make(chan *AddressNet, 1000) //需要查询的逻辑节点
	OutCloseConnName chan *AddressNet //= make(chan *AddressNet, 1000) //废弃的nodeid,需要询问是否关闭
	SuperPeerId      *AddressNet      //超级节点名称
	NodeIdLevel      uint             //= 256  //节点id长度
	HaveNewNode      chan *Node       //= make(chan *Node, 100) //当添加新的超级节点时,给他个信号
	WhiteList        *sync.Map        //new(sync.Map) //连接白名单
	NetType          config.NetModel  //网络类型:正式网络release/测试网络test

	IsHeadTailModl bool
	AreaNameSelf   []byte // 本节点大区名
	// contains filtered or unexported fields
}

func NewNodeManager

func NewNodeManager(key keystore.KeystoreInterface, pwd string) (*NodeManager, error)

func (*NodeManager) AddNode

func (this *NodeManager) AddNode(node Node) bool

添加一个超级节点 检查这个节点是否是自己的逻辑节点,如果是,则保存 不保存自己

func (*NodeManager) AddNode_old

func (this *NodeManager) AddNode_old(node Node) bool

添加一个超级节点 检查这个节点是否是自己的逻辑节点,如果是,则保存 不保存自己

func (*NodeManager) AddNodesClient

func (this *NodeManager) AddNodesClient(node Node)

添加一个被其他节点当作逻辑节点的连接

func (*NodeManager) AddProxyNode

func (this *NodeManager) AddProxyNode(node Node)

添加一个代理节点

func (*NodeManager) AddWhiteList

func (this *NodeManager) AddWhiteList(addr AddressNet) bool

添加一个地址到白名单 @return bool 这个连接是否已经存在

func (*NodeManager) CheckClientNodeIsLogicNode

func (this *NodeManager) CheckClientNodeIsLogicNode()

检查连接到本机的节点是否可以作为逻辑节点

func (*NodeManager) CheckNeedNode

func (this *NodeManager) CheckNeedNode(nodeId *AddressNet) (isNeed bool, removeIDs [][]byte)

检查节点是否是本节点的逻辑节点 只检查,不保存

func (*NodeManager) CloseNotGodNodesUseSessIndex

func (this *NodeManager) CloseNotGodNodesUseSessIndex(sessIndex uint64)

* 关闭所有不是上帝节点的节点连接信息,根据上帝节点session的index进行判断 * * @auth: qlw * @param sessIndex uint64 上帝节点的session连接index

func (*NodeManager) DelNode

func (this *NodeManager) DelNode(id *AddressNet)

删除一个节点,包括超级节点和代理节点

func (*NodeManager) DelWhiteList

func (this *NodeManager) DelWhiteList(addr AddressNet) bool

删除一个地址到白名单 @return bool 这个连接是否已经存在

func (*NodeManager) EqualLogicNodes

func (this *NodeManager) EqualLogicNodes(ids []AddressNet) bool

对比逻辑节点是否有变化

func (*NodeManager) FindNearInSuper

func (this *NodeManager) FindNearInSuper(nodeId, outId *AddressNet, includeSelf bool, blockAddr map[string]int) *AddressNet

在超级节点中找到最近的节点,不包括代理节点,带黑名单 @nodeId *AddressNet 要查找的节点 @outId *AddressNet 排除一个节点 @includeSelf bool 是否包括自己 @blockAddr []*AddressNet 已经试过,但失败了被拉黑的节点 @return *AddressNet 查找到的节点id,可能为空

func (*NodeManager) FindNearInSuperOnebyone

func (this *NodeManager) FindNearInSuperOnebyone(nodeId, outId *AddressNet, blockAddr map[string]int, sessionEngine *engine.Engine) *AddressNet

在超级节点中找到最近的节点,不包括代理节点,带黑名单,在onebyone规则下使用 @nodeId *AddressNet 要查找的节点 @outId *AddressNet 排除一个节点 @blockAddr []*AddressNet 已经试过,但失败了被拉黑的节点 @return *AddressNet 查找到的节点id,可能为空

func (*NodeManager) FindNearNodeId

func (this *NodeManager) FindNearNodeId(nodeId, outId *AddressNet, includeSelf bool) *AddressNet

在节点中找到最近的节点,包括代理节点

func (*NodeManager) FindNode

func (this *NodeManager) FindNode(id *AddressNet) *Node

通过id查找一个节点

func (*NodeManager) FindNodeInLogic

func (this *NodeManager) FindNodeInLogic(id *AddressNet) *Node

func (*NodeManager) FindOneByOneList

func (this *NodeManager) FindOneByOneList(addr *AddressNet) bool

func (*NodeManager) FindWhiteList

func (this *NodeManager) FindWhiteList(addr *AddressNet) bool

func (*NodeManager) GetAllNodes

func (this *NodeManager) GetAllNodes() []*Node

得到所有连接的节点信息,不包括本节点

func (*NodeManager) GetAllNodesAddr

func (this *NodeManager) GetAllNodesAddr() []AddressNet

得到所有连接的节点信息,不包括本节点

func (*NodeManager) GetIdsForFar

func (this *NodeManager) GetIdsForFar(id *AddressNet) []AddressNet

获得一个节点更远的节点中,比自己更远的节点

func (*NodeManager) GetLogicNodeInfo

func (this *NodeManager) GetLogicNodeInfo() []*Node

得到所有逻辑节点,不包括本节点,也不包括代理节点

func (*NodeManager) GetLogicNodes

func (this *NodeManager) GetLogicNodes() []AddressNet

得到所有逻辑节点,不包括本节点,也不包括代理节点

func (*NodeManager) GetMachineID

func (this *NodeManager) GetMachineID() string

* GetMachineID 获取机器id * * @return machineID string 机器id

func (*NodeManager) GetNodesClient

func (this *NodeManager) GetNodesClient() []AddressNet

获取本机被其他当作逻辑节点的连接

func (*NodeManager) GetOneByOneNodes

func (this *NodeManager) GetOneByOneNodes() []AddressNet

获取所有onebyone规则连接下的节点

func (*NodeManager) GetProxyAll

func (this *NodeManager) GetProxyAll() []AddressNet

获得所有代理节点

func (*NodeManager) GetProxyNode

func (this *NodeManager) GetProxyNode(id string) (node *Node, ok bool)

得到一个代理节点

func (*NodeManager) GetSuperNodeIps

func (this *NodeManager) GetSuperNodeIps() (ips []string)

获得本机所有逻辑节点的ip地址

func (*NodeManager) GetSuperPeerId

func (this *NodeManager) GetSuperPeerId() (sAddr *AddressNet)

获取超级节点id

func (*NodeManager) GetWhiltListNodes

func (this *NodeManager) GetWhiltListNodes() []AddressNet

在连接中获得白名单节点

func (*NodeManager) IsLogicNode

func (this *NodeManager) IsLogicNode(addr AddressNet) bool

func (*NodeManager) SetMachineID

func (this *NodeManager) SetMachineID(machineID string)

* SetMachineID 设置机器id * 机器id由使用者控制,p2p只负责传输和保存 * * @param machineID string 机器id

func (*NodeManager) SetNetType

func (this *NodeManager) SetNetType(t config.NetModel)

设置网络环境

func (*NodeManager) SetSuperPeerId

func (this *NodeManager) SetSuperPeerId(sAddr *AddressNet)

设置超级节点id

type NodeSimulation

type NodeSimulation struct {
	Id     []byte
	Logic  [][]byte
	Client [][]byte
}

一个节点,保存逻辑节点

type NodeSimulationManager

type NodeSimulationManager struct {
	IDdepth           uint                       //id有多少位
	NodeSuperIDs      [][]byte                   //所有已知的超级节点id
	NodeStandardSuper map[string]*NodeSimulation //标准的逻辑节点
	NodeCustomSuper   map[string]*NodeSimulation //通过节点发现构建的
	Kad               *Kademlia                  //
}

多个节点管理器

func (*NodeSimulationManager) AddNetForSuperNode

func (this *NodeSimulationManager) AddNetForSuperNode(newAddrOne, regAddr []byte, nowSuperAddrs [][]byte)

一个超级节点加入网络 会引起节点震荡,所有在网的节点都要重新找逻辑节点 @newAddrOne []byte 新加入的节点 @regAddr []byte 加入网络时,首个连入的节点 @nowSuperAddrs [][]byte 已经组网的节点

func (*NodeSimulationManager) AddNodeProxyIDs

func (this *NodeSimulationManager) AddNodeProxyIDs(proxyID ...[]byte)

添加指定的代理节点ID @proxyID []byte 指定的节点id

func (*NodeSimulationManager) AddNodeSuperIDs

func (this *NodeSimulationManager) AddNodeSuperIDs(superID ...[]byte)

添加指定的超级节点ID @superID []byte 指定的节点id

func (*NodeSimulationManager) BuildNode

func (this *NodeSimulationManager) BuildNode(superLen, clientLen int)

随机数构建足够数量的节点 @superLen int @clientLen int

func (*NodeSimulationManager) BuildNodeLogicIDs

func (this *NodeSimulationManager) BuildNodeLogicIDs()

构建标准的逻辑节点

func (*NodeSimulationManager) Discover

func (this *NodeSimulationManager) Discover()

节点发现,找到自己的逻辑节点 超级节点列表中第一个节点为引导节点 一个节点一个节点的加入网络

func (*NodeSimulationManager) GetCustomNodes

func (this *NodeSimulationManager) GetCustomNodes() map[string]*NodeSimulation

获取自定义节点

func (*NodeSimulationManager) GetStandardNodes

func (this *NodeSimulationManager) GetStandardNodes() map[string]*NodeSimulation

获取标准节点

func (*NodeSimulationManager) LoopDiscoverLogicNode

func (this *NodeSimulationManager) LoopDiscoverLogicNode(superAddr [][]byte) bool

循环发现自己的逻辑节点 @return bool 是否有逻辑节点改变

func (*NodeSimulationManager) LoopSendMsgP2P

func (this *NodeSimulationManager) LoopSendMsgP2P(fromId, toId []byte) (bool, []byte)

func (*NodeSimulationManager) LoopSendMsgSearch

func (this *NodeSimulationManager) LoopSendMsgSearch(selfID, fromId, toId []byte) (bool, []byte)

func (*NodeSimulationManager) MardSuperNodeByClient

func (this *NodeSimulationManager) MardSuperNodeByClient()

把客户端节点添加到超级节点中去

func (*NodeSimulationManager) MsgPingNodesP2P

func (this *NodeSimulationManager) MsgPingNodesP2P()

节点之间互相ping

func (*NodeSimulationManager) MsgPingNodesSearch

func (this *NodeSimulationManager) MsgPingNodesSearch()

每个节点搜索逻辑节点,对比结果是否一致

func (*NodeSimulationManager) PrintlnLogicNodesNew

func (this *NodeSimulationManager) PrintlnLogicNodesNew(idB58 bool)

func (*NodeSimulationManager) PrintlnStandardLogicNodesNew

func (this *NodeSimulationManager) PrintlnStandardLogicNodesNew(idB58 bool)

打印标准逻辑节点

type PrivateKeyCurve

type PrivateKeyCurve [32]byte //dh私钥

type PublicKeyCurve

type PublicKeyCurve [32]byte //dh公钥

type TempId

type TempId struct {
	SuperPeerId *AddressNet `json:"superpeerid"` //更新在线时间
	PeerId      *AddressNet `json:"peerid"`      //更新在线时间
	UpdateTime  int64       `json:"updatetime"`  //更新在线时间
}

临时id

func NewTempId

func NewTempId(superId, peerId *AddressNet) *TempId

创建一个临时id

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL