diff --git a/config/config.go b/config/config.go index b71ad07..71461f8 100644 --- a/config/config.go +++ b/config/config.go @@ -6,11 +6,13 @@ import ( "strconv" ) +// Nonnegative integer ID of vertex +type Vertex uint16 + const ( - Boardcast Vertex = math.MaxUint32 - iota // Normal boardcast, boardcast with route table - ControlMessage Vertex = math.MaxUint32 - iota // p2p mode: boardcast to every know keer and prevent dup/ super mode: send to supernode - PingMessage Vertex = math.MaxUint32 - iota // boardsact to every know peer but don't transit - SuperNodeMessage Vertex = math.MaxUint32 - iota + Boardcast Vertex = math.MaxUint16 - iota // Normal boardcast, boardcast with route table + ControlMessage Vertex = math.MaxUint16 - iota // p2p mode: boardcast to every know keer and prevent dup/ super mode: send to supernode + SuperNodeMessage Vertex = math.MaxUint16 - iota Special_NodeID Vertex = SuperNodeMessage ) @@ -18,6 +20,7 @@ type EdgeConfig struct { Interface InterfaceConf NodeID Vertex NodeName string + DefaultTTL uint8 PrivKey string ListenPort int LogLevel LoggerInfo @@ -36,7 +39,7 @@ type SuperConfig struct { RePushConfigInterval float64 StatePassword string GraphRecalculateSetting GraphRecalculateSetting - Peers []PeerInfo + Peers []SuperPeerInfo } type InterfaceConf struct { @@ -59,6 +62,13 @@ type PeerInfo struct { Static bool } +type SuperPeerInfo struct { + NodeID Vertex + Name string + PubKey string + PSKey string +} + type LoggerInfo struct { LogLevel string LogTransit bool @@ -67,17 +77,12 @@ type LoggerInfo struct { LogNTP bool } -// Nonnegative integer ID of vertex -type Vertex uint32 - func (v *Vertex) ToString() string { switch *v { case Boardcast: return "B" case ControlMessage: return "C" - case PingMessage: - return "P" case SuperNodeMessage: return "S" default: @@ -136,6 +141,7 @@ type HTTP_Peerinfo struct { PSKey string Connurl map[string]bool } + type HTTP_Peers map[string]HTTP_Peerinfo const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" diff --git a/device/device.go b/device/device.go index 97660c9..74e0728 100644 --- a/device/device.go +++ b/device/device.go @@ -87,14 +87,15 @@ type Device struct { indexTable IndexTable cookieChecker CookieChecker - MsgCount uint32 IsSuperNode bool ID config.Vertex + DefaultTTL uint8 graph *path.IG l2fib sync.Map LogLevel config.LoggerInfo DRoute config.DynamicRouteInfo DupData fixed_time_cache.Cache + Version string pool struct { messageBuffers *WaitPool @@ -309,7 +310,7 @@ func (device *Device) SetPrivateKey(sk NoisePrivateKey) error { return nil } -func NewDevice(tapDevice tap.Device, id config.Vertex, bind conn.Bind, logger *Logger, graph *path.IG, IsSuperNode bool, configpath string, econfig *config.EdgeConfig, sconfig *config.SuperConfig, superevents *path.SUPER_Events) *Device { +func NewDevice(tapDevice tap.Device, id config.Vertex, bind conn.Bind, logger *Logger, graph *path.IG, IsSuperNode bool, configpath string, econfig *config.EdgeConfig, sconfig *config.SuperConfig, superevents *path.SUPER_Events, version string) *Device { device := new(Device) device.state.state = uint32(deviceStateDown) device.closed = make(chan struct{}) @@ -328,6 +329,7 @@ func NewDevice(tapDevice tap.Device, id config.Vertex, bind conn.Bind, logger *L device.IsSuperNode = IsSuperNode device.ID = id device.graph = graph + device.Version = version device.rate.limiter.Init() device.indexTable.Init() @@ -350,6 +352,7 @@ func NewDevice(tapDevice tap.Device, id config.Vertex, bind conn.Bind, logger *L device.Event_Supernode_OK = make(chan struct{}, 4) device.LogLevel = econfig.LogLevel device.ResetConnInterval = device.EdgeConfig.ResetConnInterval + device.DefaultTTL = econfig.DefaultTTL go device.RoutineSetEndpoint() go device.RoutineRegister() go device.RoutineSendPing() @@ -383,25 +386,32 @@ func NewDevice(tapDevice tap.Device, id config.Vertex, bind conn.Bind, logger *L } func (device *Device) LookupPeerIDAtConfig(pk NoisePublicKey) (ID config.Vertex, err error) { - var peerlist []config.PeerInfo if device.IsSuperNode { + var peerlist []config.SuperPeerInfo if device.SuperConfig == nil { return 0, errors.New("Superconfig is nil") } peerlist = device.SuperConfig.Peers + pkstr := PubKey2Str(pk) + for _, peerinfo := range peerlist { + if peerinfo.PubKey == pkstr { + return peerinfo.NodeID, nil + } + } } else { + var peerlist []config.PeerInfo if device.EdgeConfig == nil { return 0, errors.New("EdgeConfig is nil") } peerlist = device.EdgeConfig.Peers - } - - pkstr := PubKey2Str(pk) - for _, peerinfo := range peerlist { - if peerinfo.PubKey == pkstr { - return peerinfo.NodeID, nil + pkstr := PubKey2Str(pk) + for _, peerinfo := range peerlist { + if peerinfo.PubKey == pkstr { + return peerinfo.NodeID, nil + } } } + return 0, errors.New("Peer not found in the config file.") } diff --git a/device/receive.go b/device/receive.go index 3a83084..6ae85db 100644 --- a/device/receive.go +++ b/device/receive.go @@ -471,9 +471,6 @@ func (peer *Peer) RoutineSequentialReceiver() { case config.Boardcast: should_receive = true should_transfer = true - case config.PingMessage: - peer.LastPingReceived = time.Now() - should_process = true case config.SuperNodeMessage: should_process = true case config.ControlMessage: @@ -533,7 +530,7 @@ func (peer *Peer) RoutineSequentialReceiver() { if packet_type != path.NornalPacket { if device.LogLevel.LogControl { if peer.GetEndpointDstStr() != "" { - fmt.Println("Control: Received MID:" + strconv.Itoa(int(EgHeader.GetMessageID())) + " From:" + peer.GetEndpointDstStr() + " " + device.sprint_received(packet_type, elem.packet[path.EgHeaderLen:])) + fmt.Println("Control: Received From:" + peer.GetEndpointDstStr() + " " + device.sprint_received(packet_type, elem.packet[path.EgHeaderLen:])) } } err = device.process_received(packet_type, peer, elem.packet[path.EgHeaderLen:]) diff --git a/device/receivesendproc.go b/device/receivesendproc.go index ce3ab3e..443b625 100644 --- a/device/receivesendproc.go +++ b/device/receivesendproc.go @@ -33,10 +33,8 @@ func (device *Device) SendPacket(peer *Peer, packet []byte, offset int) { if device.LogLevel.LogControl { EgHeader, _ := path.NewEgHeader(packet[:path.EgHeaderLen]) if EgHeader.GetUsage() != path.NornalPacket { - device.MsgCount += 1 - EgHeader.SetMessageID(device.MsgCount) if peer.GetEndpointDstStr() != "" { - fmt.Println("Control: Send MID:" + strconv.Itoa(int(device.MsgCount)) + " To:" + peer.GetEndpointDstStr() + " " + device.sprint_received(EgHeader.GetUsage(), packet[path.EgHeaderLen:])) + fmt.Println("Control: Send To:" + peer.GetEndpointDstStr() + " " + device.sprint_received(EgHeader.GetUsage(), packet[path.EgHeaderLen:])) } } } @@ -145,7 +143,7 @@ func (device *Device) process_received(msg_type path.Usage, peer *Peer, body []b } case path.PingPacket: if content, err := path.ParsePingMsg(body); err == nil { - return device.process_ping(content) + return device.process_ping(peer, content) } case path.PongPacket: if content, err := path.ParsePongMsg(body); err == nil { @@ -207,13 +205,29 @@ func (device *Device) sprint_received(msg_type path.Usage, body []byte) (ret str } func (device *Device) server_process_RegisterMsg(peer *Peer, content path.RegisterMsg) error { + UpdateErrorMsg := path.UpdateErrorMsg{ + Node_id: peer.ID, + Action: path.NoAction, + ErrorCode: 0, + ErrorMsg: "", + } if peer.ID != content.Node_id { - UpdateErrorMsg := path.UpdateErrorMsg{ + UpdateErrorMsg = path.UpdateErrorMsg{ Node_id: peer.ID, Action: path.Shutdown, ErrorCode: 401, - ErrorMsg: "Your node ID is not match with our nodeID", + ErrorMsg: "Your node ID is not match with our registered nodeID", } + } + if content.Version != device.Version { + UpdateErrorMsg = path.UpdateErrorMsg{ + Node_id: peer.ID, + Action: path.Shutdown, + ErrorCode: 400, + ErrorMsg: "Your version is not match with our version: " + device.Version, + } + } + if UpdateErrorMsg.Action != path.NoAction { body, err := path.GetByte(&UpdateErrorMsg) if err != nil { return err @@ -221,11 +235,11 @@ func (device *Device) server_process_RegisterMsg(peer *Peer, content path.Regist buf := make([]byte, path.EgHeaderLen+len(body)) header, err := path.NewEgHeader(buf[:path.EgHeaderLen]) header.SetSrc(device.ID) - header.SetTTL(200) + header.SetTTL(device.DefaultTTL) header.SetUsage(path.UpdateError) header.SetPacketLength(uint16(len(body))) copy(buf[path.EgHeaderLen:], body) - header.SetDst(config.ControlMessage) + header.SetDst(peer.ID) device.SendPacket(peer, buf, MessageTransportOffsetContent) return nil } @@ -238,7 +252,8 @@ func (device *Device) server_process_Pong(content path.PongMsg) error { return nil } -func (device *Device) process_ping(content path.PingMsg) error { +func (device *Device) process_ping(peer *Peer, content path.PingMsg) error { + peer.LastPingReceived = time.Now() PongMSG := path.PongMsg{ Src_nodeID: content.Src_nodeID, Dst_nodeID: device.ID, @@ -254,7 +269,7 @@ func (device *Device) process_ping(content path.PingMsg) error { buf := make([]byte, path.EgHeaderLen+len(body)) header, err := path.NewEgHeader(buf[:path.EgHeaderLen]) header.SetSrc(device.ID) - header.SetTTL(200) + header.SetTTL(device.DefaultTTL) header.SetUsage(path.PongPacket) header.SetPacketLength(uint16(len(body))) copy(buf[path.EgHeaderLen:], body) @@ -285,7 +300,7 @@ func (device *Device) process_pong(peer *Peer, content path.PongMsg) error { buf := make([]byte, path.EgHeaderLen+len(body)) header, err := path.NewEgHeader(buf[:path.EgHeaderLen]) header.SetSrc(device.ID) - header.SetTTL(200) + header.SetTTL(device.DefaultTTL) header.SetUsage(path.QueryPeer) header.SetPacketLength(uint16(len(body))) copy(buf[path.EgHeaderLen:], body) @@ -525,7 +540,7 @@ func (device *Device) RoutineRegister() { Node_id: device.ID, PeerStateHash: device.peers.Peer_state, NhStateHash: device.graph.NhTableHash, - Name: device.EdgeConfig.NodeName, + Version: device.Version, }) buf := make([]byte, path.EgHeaderLen+len(body)) header, _ := path.NewEgHeader(buf[0:path.EgHeaderLen]) @@ -610,7 +625,7 @@ func (device *Device) GeneratePingPacket(src_nodeID config.Vertex) ([]byte, erro if err != nil { return nil, err } - header.SetDst(config.PingMessage) + header.SetDst(config.ControlMessage) header.SetTTL(0) header.SetSrc(device.ID) header.SetUsage(path.PingPacket) @@ -646,7 +661,7 @@ func (device *Device) process_RequestPeerMsg(content path.QueryPeerMsg) error { buf := make([]byte, path.EgHeaderLen+len(body)) header, _ := path.NewEgHeader(buf[0:path.EgHeaderLen]) header.SetDst(config.ControlMessage) - header.SetTTL(200) + header.SetTTL(device.DefaultTTL) header.SetSrc(device.ID) header.SetUsage(path.BoardcastPeer) header.SetPacketLength(uint16(len(body))) diff --git a/device/send.go b/device/send.go index b9cb5f7..5a983fa 100644 --- a/device/send.go +++ b/device/send.go @@ -262,7 +262,7 @@ func (device *Device) RoutineReadFromTUN() { EgBody.SetSrc(device.ID) EgBody.SetDst(dst_nodeID) EgBody.SetPacketLength(uint16(len(elem.packet) - path.EgHeaderLen)) - EgBody.SetTTL(200) + EgBody.SetTTL(device.DefaultTTL) EgBody.SetUsage(path.NornalPacket) if dst_nodeID != config.Boardcast { diff --git a/example_config/p2p_mode/n1.yaml b/example_config/p2p_mode/n1.yaml index c70c108..dd98058 100644 --- a/example_config/p2p_mode/n1.yaml +++ b/example_config/p2p_mode/n1.yaml @@ -4,12 +4,13 @@ interface: vppifaceid: 1 vppbridgeid: 4242 macaddrprefix: AA:BB:CC:DD:EE - mtu: 1400 + mtu: 1404 recvaddr: 127.0.0.1:4001 sendaddr: 127.0.0.1:5001 l2headermode: kbdbg nodeid: 1 nodename: Node01 +defaultttl: 200 privkey: aABzjKhWdkFfQ29ZuijtMp1h1TNJe66SDCwvfmvQznw= listenport: 3001 loglevel: diff --git a/example_config/p2p_mode/n2.yaml b/example_config/p2p_mode/n2.yaml index f397b75..051fef9 100644 --- a/example_config/p2p_mode/n2.yaml +++ b/example_config/p2p_mode/n2.yaml @@ -4,12 +4,13 @@ interface: vppifaceid: 2 vppbridgeid: 4242 macaddrprefix: AA:BB:CC:DD:EE - mtu: 1400 + mtu: 1404 recvaddr: 127.0.0.1:4002 sendaddr: 127.0.0.1:5002 l2headermode: kbdbg nodeid: 2 nodename: Node02 +defaultttl: 200 privkey: UNZMzPX5fG/8yGC8edVj/ksF9N6ARRqdq7fqE/PD7ls= listenport: 3002 loglevel: diff --git a/example_config/p2p_mode/n3.yaml b/example_config/p2p_mode/n3.yaml index 4c5f3b6..1afb64f 100644 --- a/example_config/p2p_mode/n3.yaml +++ b/example_config/p2p_mode/n3.yaml @@ -4,12 +4,13 @@ interface: vppifaceid: 3 vppbridgeid: 4242 macaddrprefix: AA:BB:CC:DD:EE - mtu: 1400 + mtu: 1404 recvaddr: 127.0.0.1:4003 sendaddr: 127.0.0.1:5003 l2headermode: kbdbg nodeid: 3 nodename: Node03 +defaultttl: 200 privkey: gJy35nbsd8FuuxyWHjsefN+U+oM7RkuIB1EanNLSVHg= listenport: 3003 loglevel: diff --git a/example_config/p2p_mode/n4.yaml b/example_config/p2p_mode/n4.yaml index 623512f..872c9af 100644 --- a/example_config/p2p_mode/n4.yaml +++ b/example_config/p2p_mode/n4.yaml @@ -4,12 +4,13 @@ interface: vppifaceid: 4 vppbridgeid: 4242 macaddrprefix: AA:BB:CC:DD:EE - mtu: 1400 + mtu: 1404 recvaddr: 127.0.0.1:4004 sendaddr: 127.0.0.1:5004 l2headermode: kbdbg nodeid: 4 nodename: Node04 +defaultttl: 200 privkey: wAdLgCk0SHiO11/aUf9944focD1BUCH5b6Pe+cRHHXQ= listenport: 3004 loglevel: diff --git a/example_config/p2p_mode/n5.yaml b/example_config/p2p_mode/n5.yaml index 148d1a4..927bdd0 100644 --- a/example_config/p2p_mode/n5.yaml +++ b/example_config/p2p_mode/n5.yaml @@ -4,12 +4,13 @@ interface: vppifaceid: 5 vppbridgeid: 4242 macaddrprefix: AA:BB:CC:DD:EE - mtu: 1400 + mtu: 1404 recvaddr: 127.0.0.1:4005 sendaddr: 127.0.0.1:5005 l2headermode: kbdbg nodeid: 5 nodename: Node05 +defaultttl: 200 privkey: gLmzeCbmN/hjiE+ehNXL9IxuG9hhWIYv2s16/DOW6FE= listenport: 3005 loglevel: diff --git a/example_config/p2p_mode/n6.yaml b/example_config/p2p_mode/n6.yaml index 75a59ae..c91b223 100644 --- a/example_config/p2p_mode/n6.yaml +++ b/example_config/p2p_mode/n6.yaml @@ -4,12 +4,13 @@ interface: vppifaceid: 6 vppbridgeid: 4242 macaddrprefix: AA:BB:CC:DD:EE - mtu: 1400 + mtu: 1404 recvaddr: 127.0.0.1:4006 sendaddr: 127.0.0.1:5006 l2headermode: kbdbg nodeid: 6 nodename: Node06 +defaultttl: 200 privkey: IIX5F6oWZUS2dlhxWFJ7TxdJtDCr5jzeuhxUB6YM7Us= listenport: 3006 loglevel: diff --git a/example_config/static_mode/n1.yaml b/example_config/static_mode/n1.yaml index a4aaec0..a07def2 100644 --- a/example_config/static_mode/n1.yaml +++ b/example_config/static_mode/n1.yaml @@ -4,12 +4,13 @@ interface: vppifaceid: 1 vppbridgeid: 4242 macaddrprefix: AA:BB:CC:DD:EE - mtu: 1400 + mtu: 1404 recvaddr: 127.0.0.1:4001 sendaddr: 127.0.0.1:5001 l2headermode: kbdbg nodeid: 1 nodename: Node01 +defaultttl: 200 privkey: aABzjKhWdkFfQ29ZuijtMp1h1TNJe66SDCwvfmvQznw= listenport: 3001 loglevel: diff --git a/example_config/static_mode/n2.yaml b/example_config/static_mode/n2.yaml index 2e6f069..f67d301 100644 --- a/example_config/static_mode/n2.yaml +++ b/example_config/static_mode/n2.yaml @@ -4,12 +4,13 @@ interface: vppifaceid: 2 vppbridgeid: 4242 macaddrprefix: AA:BB:CC:DD:EE - mtu: 1400 + mtu: 1404 recvaddr: 127.0.0.1:4002 sendaddr: 127.0.0.1:5002 l2headermode: kbdbg nodeid: 2 nodename: Node02 +defaultttl: 200 privkey: UNZMzPX5fG/8yGC8edVj/ksF9N6ARRqdq7fqE/PD7ls= listenport: 3002 loglevel: diff --git a/example_config/static_mode/n3.yaml b/example_config/static_mode/n3.yaml index 1380222..51a37c5 100644 --- a/example_config/static_mode/n3.yaml +++ b/example_config/static_mode/n3.yaml @@ -4,12 +4,13 @@ interface: vppifaceid: 3 vppbridgeid: 4242 macaddrprefix: AA:BB:CC:DD:EE - mtu: 1400 + mtu: 1404 recvaddr: 127.0.0.1:4003 sendaddr: 127.0.0.1:5003 l2headermode: kbdbg nodeid: 3 nodename: Node03 +defaultttl: 200 privkey: gJy35nbsd8FuuxyWHjsefN+U+oM7RkuIB1EanNLSVHg= listenport: 3003 loglevel: diff --git a/example_config/static_mode/n4.yaml b/example_config/static_mode/n4.yaml index f68d1f4..206ea28 100644 --- a/example_config/static_mode/n4.yaml +++ b/example_config/static_mode/n4.yaml @@ -4,12 +4,13 @@ interface: vppifaceid: 4 vppbridgeid: 4242 macaddrprefix: AA:BB:CC:DD:EE - mtu: 1400 + mtu: 1404 recvaddr: 127.0.0.1:4004 sendaddr: 127.0.0.1:5004 l2headermode: kbdbg nodeid: 4 nodename: Node04 +defaultttl: 200 privkey: wAdLgCk0SHiO11/aUf9944focD1BUCH5b6Pe+cRHHXQ= listenport: 3004 loglevel: diff --git a/example_config/static_mode/n5.yaml b/example_config/static_mode/n5.yaml index d136864..73078e7 100644 --- a/example_config/static_mode/n5.yaml +++ b/example_config/static_mode/n5.yaml @@ -4,12 +4,13 @@ interface: vppifaceid: 5 vppbridgeid: 4242 macaddrprefix: AA:BB:CC:DD:EE - mtu: 1400 + mtu: 1404 recvaddr: 127.0.0.1:4005 sendaddr: 127.0.0.1:5005 l2headermode: kbdbg nodeid: 5 nodename: Node05 +defaultttl: 200 privkey: gLmzeCbmN/hjiE+ehNXL9IxuG9hhWIYv2s16/DOW6FE= listenport: 3005 loglevel: diff --git a/example_config/static_mode/n6.yaml b/example_config/static_mode/n6.yaml index 427a94b..7aa288e 100644 --- a/example_config/static_mode/n6.yaml +++ b/example_config/static_mode/n6.yaml @@ -4,12 +4,13 @@ interface: vppifaceid: 6 vppbridgeid: 4242 macaddrprefix: AA:BB:CC:DD:EE - mtu: 1400 + mtu: 1404 recvaddr: 127.0.0.1:4006 sendaddr: 127.0.0.1:5006 l2headermode: kbdbg nodeid: 6 nodename: Node06 +defaultttl: 200 privkey: IIX5F6oWZUS2dlhxWFJ7TxdJtDCr5jzeuhxUB6YM7Us= listenport: 3006 loglevel: diff --git a/example_config/super_mode/n1.yaml b/example_config/super_mode/n1.yaml index f25c42f..8c9f8bd 100644 --- a/example_config/super_mode/n1.yaml +++ b/example_config/super_mode/n1.yaml @@ -4,12 +4,13 @@ interface: vppifaceid: 1 vppbridgeid: 4242 macaddrprefix: AA:BB:CC:DD:EE - mtu: 1400 + mtu: 1404 recvaddr: 127.0.0.1:4001 sendaddr: 127.0.0.1:5001 l2headermode: kbdbg nodeid: 1 nodename: Node01 +defaultttl: 200 privkey: 6GyDagZKhbm5WNqMiRHhkf43RlbMJ34IieTlIuvfJ1M= listenport: 3001 loglevel: diff --git a/example_config/super_mode/n2.yaml b/example_config/super_mode/n2.yaml index d84be9c..0e6b3be 100644 --- a/example_config/super_mode/n2.yaml +++ b/example_config/super_mode/n2.yaml @@ -4,12 +4,13 @@ interface: vppifaceid: 2 vppbridgeid: 4242 macaddrprefix: AA:BB:CC:DD:EE - mtu: 1400 + mtu: 1404 recvaddr: 127.0.0.1:4002 sendaddr: 127.0.0.1:5002 l2headermode: kbdbg nodeid: 2 nodename: Node02 +defaultttl: 200 privkey: OH8BsVUU2Rqzeu9B2J5GPG8PUmxWfX8uVvNFZKhVF3o= listenport: 3002 loglevel: diff --git a/example_config/super_mode/s1.yaml b/example_config/super_mode/s1.yaml index 0d0e39c..5278a35 100644 --- a/example_config/super_mode/s1.yaml +++ b/example_config/super_mode/s1.yaml @@ -16,10 +16,10 @@ graphrecalculatesetting: recalculatecooldown: 5 peers: - nodeid: 1 + name: "Node_01" pubkey: ZqzLVSbXzjppERslwbf2QziWruW3V/UIx9oqwU8Fn3I= - endpoint: 127.0.0.1:3001 - static: true + pskey: "" - nodeid: 2 + name: "Node_02" pubkey: dHeWQtlTPQGy87WdbUARS4CtwVaR2y7IQ1qcX4GKSXk= - endpoint: 127.0.0.1:3002 - static: true \ No newline at end of file + pskey: "" \ No newline at end of file diff --git a/main_edge.go b/main_edge.go index 1c4fd39..d8f7fb5 100644 --- a/main_edge.go +++ b/main_edge.go @@ -138,23 +138,23 @@ func Edge(configPath string, useUAPI bool, printExample bool) (err error) { printExampleEdgeConf() return nil } - var tconfig config.EdgeConfig + var econfig config.EdgeConfig //printExampleConf() //return - err = readYaml(configPath, &tconfig) + err = readYaml(configPath, &econfig) if err != nil { fmt.Printf("Error read config: %s :", configPath) fmt.Print(err) return err } - NodeName := tconfig.NodeName + NodeName := econfig.NodeName if len(NodeName) > 32 { return errors.New("Node name can't longer than 32 :" + NodeName) } var logLevel int - switch tconfig.LogLevel.LogLevel { + switch econfig.LogLevel.LogLevel { case "verbose", "debug": logLevel = device.LogLevelVerbose case "error": @@ -177,45 +177,49 @@ func Edge(configPath string, useUAPI bool, printExample bool) (err error) { var thetap tap.Device // open TUN device (or use supplied fd) - switch tconfig.Interface.Itype { + switch econfig.Interface.Itype { case "dummy": thetap, err = tap.CreateDummyTAP() case "stdio": - thetap, err = tap.CreateStdIOTAP(tconfig.Interface,tconfig.NodeID) + thetap, err = tap.CreateStdIOTAP(econfig.Interface, econfig.NodeID) case "udpsock": - lis, _ := net.ResolveUDPAddr("udp", tconfig.Interface.RecvAddr) - sen, _ := net.ResolveUDPAddr("udp", tconfig.Interface.SendAddr) - thetap, err = tap.CreateUDPSockTAP(tconfig.Interface,tconfig.NodeID, lis, sen) + lis, _ := net.ResolveUDPAddr("udp", econfig.Interface.RecvAddr) + sen, _ := net.ResolveUDPAddr("udp", econfig.Interface.SendAddr) + thetap, err = tap.CreateUDPSockTAP(econfig.Interface, econfig.NodeID, lis, sen) case "vpp": - thetap, err = tap.CreateVppTAP(tconfig.Interface,tconfig.NodeID,tconfig.LogLevel.LogLevel) + thetap, err = tap.CreateVppTAP(econfig.Interface, econfig.NodeID, econfig.LogLevel.LogLevel) case "tap": - thetap, err = tap.CreateTAP(tconfig.Interface,tconfig.NodeID) + thetap, err = tap.CreateTAP(econfig.Interface, econfig.NodeID) default: - return errors.New("Unknow interface type:" + tconfig.Interface.Itype) + return errors.New("Unknow interface type:" + econfig.Interface.Itype) } if err != nil { logger.Errorf("Failed to create TAP device: %v", err) os.Exit(ExitSetupFailed) } + if econfig.DefaultTTL <= 0 { + return errors.New("DefaultTTL must > 0") + } + //////////////////////////////////////////////////// // Config - if tconfig.DynamicRoute.P2P.UseP2P == false && tconfig.DynamicRoute.SuperNode.UseSuperNode == false { - tconfig.LogLevel.LogNTP = false // NTP in static mode is useless + if econfig.DynamicRoute.P2P.UseP2P == false && econfig.DynamicRoute.SuperNode.UseSuperNode == false { + econfig.LogLevel.LogNTP = false // NTP in static mode is useless } - graph := path.NewGraph(3, false, tconfig.DynamicRoute.P2P.GraphRecalculateSetting, tconfig.DynamicRoute.NTPconfig, tconfig.LogLevel.LogNTP) - graph.SetNHTable(tconfig.NextHopTable, [32]byte{}) + graph := path.NewGraph(3, false, econfig.DynamicRoute.P2P.GraphRecalculateSetting, econfig.DynamicRoute.NTPconfig, econfig.LogLevel.LogNTP) + graph.SetNHTable(econfig.NextHopTable, [32]byte{}) - the_device := device.NewDevice(thetap, tconfig.NodeID, conn.NewDefaultBind(), logger, graph, false, configPath, &tconfig, nil, nil) + the_device := device.NewDevice(thetap, econfig.NodeID, conn.NewDefaultBind(), logger, graph, false, configPath, &econfig, nil, nil,Version) defer the_device.Close() var sk [32]byte - sk_slice, _ := base64.StdEncoding.DecodeString(tconfig.PrivKey) + sk_slice, _ := base64.StdEncoding.DecodeString(econfig.PrivKey) copy(sk[:], sk_slice) the_device.SetPrivateKey(sk) the_device.IpcSet("fwmark=0\n") - the_device.IpcSet("listen_port=" + strconv.Itoa(tconfig.ListenPort) + "\n") + the_device.IpcSet("listen_port=" + strconv.Itoa(econfig.ListenPort) + "\n") the_device.IpcSet("replace_peers=true\n") - for _, peerconf := range tconfig.Peers { + for _, peerconf := range econfig.Peers { sk_slice, _ = base64.StdEncoding.DecodeString(peerconf.PubKey) copy(sk[:], sk_slice) if peerconf.NodeID >= config.SuperNodeMessage { @@ -235,11 +239,11 @@ func Edge(configPath string, useUAPI bool, printExample bool) (err error) { } } - if tconfig.DynamicRoute.SuperNode.UseSuperNode { - if tconfig.DynamicRoute.SuperNode.ConnURLV4 != "" { - sk_slice, _ = base64.StdEncoding.DecodeString(tconfig.DynamicRoute.SuperNode.PubKeyV4) + if econfig.DynamicRoute.SuperNode.UseSuperNode { + if econfig.DynamicRoute.SuperNode.ConnURLV4 != "" { + sk_slice, _ = base64.StdEncoding.DecodeString(econfig.DynamicRoute.SuperNode.PubKeyV4) copy(sk[:], sk_slice) - endpoint, err := the_device.Bind().ParseEndpoint(tconfig.DynamicRoute.SuperNode.ConnURLV4) + endpoint, err := the_device.Bind().ParseEndpoint(econfig.DynamicRoute.SuperNode.ConnURLV4) if err != nil { return err } @@ -248,13 +252,13 @@ func Edge(configPath string, useUAPI bool, printExample bool) (err error) { return err } peer.StaticConn = false - peer.ConnURL = tconfig.DynamicRoute.SuperNode.ConnURLV4 + peer.ConnURL = econfig.DynamicRoute.SuperNode.ConnURLV4 peer.SetEndpointFromPacket(endpoint) } - if tconfig.DynamicRoute.SuperNode.ConnURLV6 != "" { - sk_slice, _ = base64.StdEncoding.DecodeString(tconfig.DynamicRoute.SuperNode.PubKeyV6) + if econfig.DynamicRoute.SuperNode.ConnURLV6 != "" { + sk_slice, _ = base64.StdEncoding.DecodeString(econfig.DynamicRoute.SuperNode.PubKeyV6) copy(sk[:], sk_slice) - endpoint, err := the_device.Bind().ParseEndpoint(tconfig.DynamicRoute.SuperNode.ConnURLV6) + endpoint, err := the_device.Bind().ParseEndpoint(econfig.DynamicRoute.SuperNode.ConnURLV6) if err != nil { return err } @@ -263,7 +267,7 @@ func Edge(configPath string, useUAPI bool, printExample bool) (err error) { return err } peer.StaticConn = false - peer.ConnURL = tconfig.DynamicRoute.SuperNode.ConnURLV6 + peer.ConnURL = econfig.DynamicRoute.SuperNode.ConnURLV6 peer.SetEndpointFromPacket(endpoint) } the_device.Event_Supernode_OK <- struct{}{} diff --git a/main_super.go b/main_super.go index bd43bbf..126046e 100644 --- a/main_super.go +++ b/main_super.go @@ -43,12 +43,12 @@ func printExampleSuperConf() { LogControl: true, }, RePushConfigInterval: 30, - Peers: []config.PeerInfo{ + Peers: []config.SuperPeerInfo{ { - NodeID: 2, - PubKey: "NuYJ/3Ght+C4HovFq5Te/BrIazo6zwDJ8Bdu4rQCz0o=", - EndPoint: "127.0.0.1:3002", - Static: true, + NodeID: 2, + Name: "Node02", + PubKey: "NuYJ/3Ght+C4HovFq5Te/BrIazo6zwDJ8Bdu4rQCz0o=", + PSKey: "NuYJ/3Ght+C4HovFq5Te/BrIazo6zwDJ8Bdu4rQCz0o=", }, }, GraphRecalculateSetting: config.GraphRecalculateSetting{ @@ -116,8 +116,8 @@ func Super(configPath string, useUAPI bool, printExample bool) (err error) { thetap4, _ := tap.CreateDummyTAP() thetap6, _ := tap.CreateDummyTAP() http_graph = path.NewGraph(3, true, sconfig.GraphRecalculateSetting, config.NTPinfo{}, sconfig.LogLevel.LogNTP) - http_device4 = device.NewDevice(thetap4, config.SuperNodeMessage, conn.NewCustomBind(true, false), logger4, http_graph, true, configPath, nil, &sconfig, &super_chains) - http_device6 = device.NewDevice(thetap6, config.SuperNodeMessage, conn.NewCustomBind(false, true), logger6, http_graph, true, configPath, nil, &sconfig, &super_chains) + http_device4 = device.NewDevice(thetap4, config.SuperNodeMessage, conn.NewCustomBind(true, false), logger4, http_graph, true, configPath, nil, &sconfig, &super_chains, Version) + http_device6 = device.NewDevice(thetap6, config.SuperNodeMessage, conn.NewCustomBind(false, true), logger6, http_graph, true, configPath, nil, &sconfig, &super_chains, Version) defer http_device4.Close() defer http_device6.Close() var sk [32]byte @@ -143,6 +143,7 @@ func Super(configPath string, useUAPI bool, printExample bool) (err error) { http_device6.IpcSet("replace_peers=true\n") for _, peerconf := range sconfig.Peers { + http_peerinfos.Store(peerconf.NodeID, peerconf.Name) var pk device.NoisePublicKey pk_slice, err := base64.StdEncoding.DecodeString(peerconf.PubKey) @@ -166,14 +167,12 @@ func Super(configPath string, useUAPI bool, printExample bool) (err error) { return err } peer4.StaticConn = true - peer4.ConnURL = peerconf.EndPoint peer6, err := http_device6.NewPeer(pk, peerconf.NodeID) if err != nil { fmt.Printf("Error create peer id %v\n", peerconf.NodeID) return err } peer6.StaticConn = true - peer6.ConnURL = peerconf.EndPoint if peerconf.PSKey != "" { var psk device.NoisePresharedKey psk_slice, err := base64.StdEncoding.DecodeString(peerconf.PSKey) @@ -225,7 +224,6 @@ func Event_server_event_hendler(graph *path.IG, events path.SUPER_Events) { case reg_msg := <-events.Event_server_register: copy(http_PeerState[http_PeerID2Map[reg_msg.Node_id]].NhTableState[:], reg_msg.NhStateHash[:]) copy(http_PeerState[http_PeerID2Map[reg_msg.Node_id]].PeerInfoState[:], reg_msg.PeerStateHash[:]) - http_peerinfos.Store(reg_msg.Node_id, reg_msg.Name) PubKey := http_PeerID2Map[reg_msg.Node_id] if peer := http_device4.LookupPeerByStr(PubKey); peer != nil { if connstr := peer.GetEndpointDstStr(); connstr != "" { diff --git a/path/header.go b/path/header.go index a10816a..6f30b50 100644 --- a/path/header.go +++ b/path/header.go @@ -7,7 +7,7 @@ import ( "github.com/KusakabeSi/EtherGuardVPN/config" ) -const EgHeaderLen = 16 +const EgHeaderLen = 8 type EgHeader struct { buf []byte @@ -21,13 +21,12 @@ const ( UpdatePeer //Comes from server UpdateNhTable + UpdateError PingPacket //Comes from other peer PongPacket //Send to everyone, include server QueryPeer BoardcastPeer - - UpdateError ) func NewEgHeader(pac []byte) (e EgHeader, err error) { @@ -40,44 +39,37 @@ func NewEgHeader(pac []byte) (e EgHeader, err error) { } func (e EgHeader) GetDst() config.Vertex { - return config.Vertex(binary.BigEndian.Uint32(e.buf[0:4])) + return config.Vertex(binary.BigEndian.Uint16(e.buf[0:2])) } func (e EgHeader) SetDst(node_ID config.Vertex) { - binary.BigEndian.PutUint32(e.buf[0:4], uint32(node_ID)) + binary.BigEndian.PutUint16(e.buf[0:2], uint16(node_ID)) } func (e EgHeader) GetSrc() config.Vertex { - return config.Vertex(binary.BigEndian.Uint32(e.buf[4:8])) + return config.Vertex(binary.BigEndian.Uint16(e.buf[2:4])) } func (e EgHeader) SetSrc(node_ID config.Vertex) { - binary.BigEndian.PutUint32(e.buf[4:8], uint32(node_ID)) + binary.BigEndian.PutUint16(e.buf[2:4], uint16(node_ID)) } func (e EgHeader) GetTTL() uint8 { - return e.buf[8] + return e.buf[4] } func (e EgHeader) SetTTL(ttl uint8) { - e.buf[8] = ttl + e.buf[4] = ttl } func (e EgHeader) GetUsage() Usage { - return Usage(e.buf[9]) + return Usage(e.buf[5]) } func (e EgHeader) SetUsage(usage Usage) { - e.buf[9] = uint8(usage) + e.buf[5] = uint8(usage) } func (e EgHeader) GetPacketLength() uint16 { - return binary.BigEndian.Uint16(e.buf[10:12]) + return binary.BigEndian.Uint16(e.buf[6:8]) } func (e EgHeader) SetPacketLength(length uint16) { - binary.BigEndian.PutUint16(e.buf[10:12], length) -} - -func (e EgHeader) GetMessageID() uint32 { - return binary.BigEndian.Uint32(e.buf[12:16]) -} -func (e EgHeader) SetMessageID(MessageID uint32) { - binary.BigEndian.PutUint32(e.buf[12:16], MessageID) + binary.BigEndian.PutUint16(e.buf[6:8], length) } diff --git a/path/metamessage.go b/path/metamessage.go index d691420..75975a0 100644 --- a/path/metamessage.go +++ b/path/metamessage.go @@ -24,11 +24,11 @@ type RegisterMsg struct { Node_id config.Vertex PeerStateHash [32]byte NhStateHash [32]byte - Name string + Version string } func (c *RegisterMsg) ToString() string { - return "RegisterMsg Node_id:" + c.Node_id.ToString() + " Name:" + c.Name + " PeerHash:" + base64.StdEncoding.EncodeToString(c.PeerStateHash[:]) + " NhHash:" + base64.StdEncoding.EncodeToString(c.NhStateHash[:]) + return "RegisterMsg Node_id:" + c.Node_id.ToString() + " Version:" + c.Version + " PeerHash:" + base64.StdEncoding.EncodeToString(c.PeerStateHash[:]) + " NhHash:" + base64.StdEncoding.EncodeToString(c.NhStateHash[:]) } func ParseRegisterMsg(bin []byte) (StructPlace RegisterMsg, err error) { @@ -42,7 +42,8 @@ func ParseRegisterMsg(bin []byte) (StructPlace RegisterMsg, err error) { type ErrorAction int const ( - Shutdown ErrorAction = iota + NoAction ErrorAction = iota + Shutdown Panic )