Documentation ¶
Overview ¶
仿真测试
连接白名单,P2P网络通信需要其他节点做中转,加入白名单功能,可以直接和指定节点建立连接
Index ¶
- Constants
- Variables
- func BuildArithmeticSequence(num int) []*big.Int
- func CheckIdInfo(idInfo IdInfo) bool
- func CheckPukAddr(pubKey []byte, addr AddressNet) bool
- func CheckSafeAddr(pubkey []byte) bool
- func CheckZn(text []byte) bool
- func CreateUUID() string
- func EqualIds(oldIds, newIds [][]byte) (isChange bool, collective [][]byte)
- func EqualNodes(nsm *NodeSimulationManager)
- func GetBinary(key []byte) string
- func HashPubKey(pubKey []byte) []byte
- func IntToBytes(n int64) []byte
- func LogicNodes(id []byte, idSuper, idOrdinary [][]byte, level uint) [][]byte
- func SearchNodeID(logicIDs [][]byte, findID []byte) []byte
- type AddressBytes
- type AddressNet
- func AddressFromB58String(str string) AddressNet
- func BuildAddr(pubKey []byte) AddressNet
- func GetMagneticID100(id *AddressNet) (logicIds []*AddressNet)
- func GetMagneticID16() (logicIds []*AddressNet)
- func GetQuarterLogicAddrNetByAddrNet(id *AddressNet) (logicIds []*AddressNet)
- func RemoveDuplicateAddress(addrs []*AddressNet) []*AddressNet
- type IdASC
- type IdDESC
- type IdInfo
- type Ids
- type Kademlia
- type KeyPairCurve
- type KeyPairED
- type LogicManager
- type LogicNumBuider
- type Node
- func (this *Node) AddSession(ss engine.Session)
- func (this *Node) CheckHaveOtherSessions(ss engine.Session) bool
- func (this *Node) FlashOnlineTime()
- func (this *Node) GetIsSuper() bool
- func (this *Node) GetSessions() []engine.Session
- func (this *Node) Proto() ([]byte, error)
- func (this *Node) RemoveSession(ss engine.Session) bool
- func (this *Node) SetIsSuper(isSuper bool)
- type NodeClass
- type NodeManager
- func (this *NodeManager) AddNode(node Node) bool
- func (this *NodeManager) AddNode_old(node Node) bool
- func (this *NodeManager) AddNodesClient(node Node)
- func (this *NodeManager) AddProxyNode(node Node)
- func (this *NodeManager) AddWhiteList(addr AddressNet) bool
- func (this *NodeManager) CheckClientNodeIsLogicNode()
- func (this *NodeManager) CheckNeedNode(nodeId *AddressNet) (isNeed bool, removeIDs [][]byte)
- func (this *NodeManager) CloseNotGodNodesUseSessIndex(sessIndex uint64)
- func (this *NodeManager) DelNode(id *AddressNet)
- func (this *NodeManager) DelWhiteList(addr AddressNet) bool
- func (this *NodeManager) EqualLogicNodes(ids []AddressNet) bool
- func (this *NodeManager) FindNearInSuper(nodeId, outId *AddressNet, includeSelf bool, blockAddr map[string]int) *AddressNet
- func (this *NodeManager) FindNearInSuperOnebyone(nodeId, outId *AddressNet, blockAddr map[string]int, ...) *AddressNet
- func (this *NodeManager) FindNearNodeId(nodeId, outId *AddressNet, includeSelf bool) *AddressNet
- func (this *NodeManager) FindNode(id *AddressNet) *Node
- func (this *NodeManager) FindNodeInLogic(id *AddressNet) *Node
- func (this *NodeManager) FindOneByOneList(addr *AddressNet) bool
- func (this *NodeManager) FindWhiteList(addr *AddressNet) bool
- func (this *NodeManager) GetAllNodes() []*Node
- func (this *NodeManager) GetAllNodesAddr() []AddressNet
- func (this *NodeManager) GetIdsForFar(id *AddressNet) []AddressNet
- func (this *NodeManager) GetLogicNodeInfo() []*Node
- func (this *NodeManager) GetLogicNodes() []AddressNet
- func (this *NodeManager) GetMachineID() string
- func (this *NodeManager) GetNodesClient() []AddressNet
- func (this *NodeManager) GetOneByOneNodes() []AddressNet
- func (this *NodeManager) GetProxyAll() []AddressNet
- func (this *NodeManager) GetProxyNode(id string) (node *Node, ok bool)
- func (this *NodeManager) GetSuperNodeIps() (ips []string)
- func (this *NodeManager) GetSuperPeerId() (sAddr *AddressNet)
- func (this *NodeManager) GetWhiltListNodes() []AddressNet
- func (this *NodeManager) IsLogicNode(addr AddressNet) bool
- func (this *NodeManager) SetMachineID(machineID string)
- func (this *NodeManager) SetNetType(t config.NetModel)
- func (this *NodeManager) SetSuperPeerId(sAddr *AddressNet)
- type NodeSimulation
- type NodeSimulationManager
- func (this *NodeSimulationManager) AddNetForSuperNode(newAddrOne, regAddr []byte, nowSuperAddrs [][]byte)
- func (this *NodeSimulationManager) AddNodeProxyIDs(proxyID ...[]byte)
- func (this *NodeSimulationManager) AddNodeSuperIDs(superID ...[]byte)
- func (this *NodeSimulationManager) BuildNode(superLen, clientLen int)
- func (this *NodeSimulationManager) BuildNodeLogicIDs()
- func (this *NodeSimulationManager) Discover()
- func (this *NodeSimulationManager) GetCustomNodes() map[string]*NodeSimulation
- func (this *NodeSimulationManager) GetStandardNodes() map[string]*NodeSimulation
- func (this *NodeSimulationManager) LoopDiscoverLogicNode(superAddr [][]byte) bool
- func (this *NodeSimulationManager) LoopSendMsgP2P(fromId, toId []byte) (bool, []byte)
- func (this *NodeSimulationManager) LoopSendMsgSearch(selfID, fromId, toId []byte) (bool, []byte)
- func (this *NodeSimulationManager) MardSuperNodeByClient()
- func (this *NodeSimulationManager) MsgPingNodesP2P()
- func (this *NodeSimulationManager) MsgPingNodesSearch()
- func (this *NodeSimulationManager) PrintlnLogicNodesNew(idB58 bool)
- func (this *NodeSimulationManager) PrintlnStandardLogicNodesNew(idB58 bool)
- type PrivateKeyCurve
- type PublicKeyCurve
- type TempId
Constants ¶
const ( Str_zaro = "0000000000000000000000000000000000000000000000000000000000000000" //字符串0 Str_maxNumber = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" //256位的最大数十六进制表示id )
Variables ¶
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的四分之一 )
var (
NodeIdLevel uint = 256 //节点id长度
)
var (
Zero = 8
)
Functions ¶
func BuildArithmeticSequence ¶
构建等差数列 @num int 几分之一 值为16,则为十六分之一
func CheckIdInfo ¶
检查idInfo是否合法 1.地址生成合法 2.签名正确 @return true:合法;false:不合法;
func CheckPukAddr ¶
func CheckPukAddr(pubKey []byte, addr AddressNet) bool
检查公钥生成的地址是否一样 @return bool 是否一样 true=相同;false=不相同;
func CheckSafeAddr ¶
func CreateUUID ¶
func CreateUUID() string
func HashPubKey ¶
func IntToBytes ¶
func LogicNodes ¶
计算一个节点,应该有哪些逻辑节点 @id []byte 计算的节点 @idSuper [][]byte 已知的逻辑节点 @idOrdinary [][]byte 补充的逻辑节点
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 GetQuarterLogicAddrNetByAddrNet ¶
func GetQuarterLogicAddrNetByAddrNet(id *AddressNet) (logicIds []*AddressNet)
得到保存数据的逻辑节点 @idStr id十六进制字符串 @return 4分之一节点
func (AddressNet) B58String ¶
func (this AddressNet) B58String() string
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 ¶
type Ids ¶
type Ids struct {
// contains filtered or unexported fields
}
type Kademlia ¶
type Kademlia struct {
// contains filtered or unexported fields
}
func NewKademlia ¶
type KeyPairCurve ¶
type KeyPairCurve struct { PrivateKey PrivateKeyCurve PublicKey PublicKeyCurve }
type KeyPairED ¶
type KeyPairED struct { PrivateKey ed25519.PrivateKey PublicKey ed25519.PublicKey }
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 ParseNodesProto ¶
func (*Node) AddSession ¶
func (*Node) CheckHaveOtherSessions ¶
检查除了参数中传入的session之外,是否还有其他session
func (*Node) FlashOnlineTime ¶
func (this *Node) FlashOnlineTime()
func (*Node) GetSessions ¶
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) 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) 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) 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) 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) 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) SetSuperPeerId ¶
func (this *NodeManager) SetSuperPeerId(sAddr *AddressNet)
设置超级节点id
type NodeSimulation ¶
一个节点,保存逻辑节点
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