0.2.8
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/binary"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
@@ -64,53 +65,90 @@ func NewClient(cfg *common.ReticulumConfig) (*Client, error) {
|
||||
}
|
||||
|
||||
func (c *Client) Start() error {
|
||||
// Initialize interfaces
|
||||
for _, ifaceConfig := range c.config.Interfaces {
|
||||
log.Printf("Starting Reticulum client...")
|
||||
log.Printf("Configuration: %+v", c.config)
|
||||
|
||||
// Initialize transport
|
||||
t, err := transport.NewTransport(c.config)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to initialize transport: %v", err)
|
||||
}
|
||||
c.transport = t
|
||||
log.Printf("Transport initialized")
|
||||
|
||||
log.Printf("Initializing network interfaces...")
|
||||
for name, ifaceConfig := range c.config.Interfaces {
|
||||
if !ifaceConfig.Enabled {
|
||||
log.Printf("Skipping disabled interface %s", name)
|
||||
continue
|
||||
}
|
||||
|
||||
log.Printf("Configuring interface %s (%s)", name, ifaceConfig.Type)
|
||||
var iface common.NetworkInterface
|
||||
|
||||
switch ifaceConfig.Type {
|
||||
case "TCPClientInterface":
|
||||
log.Printf("Connecting to %s:%d via TCP...", ifaceConfig.TargetHost, ifaceConfig.TargetPort)
|
||||
client, err := interfaces.NewTCPClient(
|
||||
ifaceConfig.Name,
|
||||
name,
|
||||
ifaceConfig.TargetHost,
|
||||
ifaceConfig.TargetPort,
|
||||
ifaceConfig.KISSFraming,
|
||||
ifaceConfig.I2PTunneled,
|
||||
ifaceConfig.Enabled,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create TCP interface %s: %v", ifaceConfig.Name, err)
|
||||
return fmt.Errorf("failed to create TCP interface %s: %v", name, err)
|
||||
}
|
||||
|
||||
if err := client.Start(); err != nil {
|
||||
return fmt.Errorf("failed to start TCP interface %s: %v", name, err)
|
||||
}
|
||||
|
||||
iface = client
|
||||
log.Printf("Successfully connected to %s:%d", ifaceConfig.TargetHost, ifaceConfig.TargetPort)
|
||||
|
||||
case "UDPInterface":
|
||||
addr := fmt.Sprintf("%s:%d", ifaceConfig.Address, ifaceConfig.Port)
|
||||
target := fmt.Sprintf("%s:%d", ifaceConfig.TargetHost, ifaceConfig.TargetPort)
|
||||
log.Printf("Starting UDP interface on %s...", addr)
|
||||
udp, err := interfaces.NewUDPInterface(
|
||||
ifaceConfig.Name,
|
||||
name,
|
||||
addr,
|
||||
"", // No target address for client initially
|
||||
target,
|
||||
ifaceConfig.Enabled,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create UDP interface %s: %v", ifaceConfig.Name, err)
|
||||
return fmt.Errorf("failed to create UDP interface %s: %v", name, err)
|
||||
}
|
||||
iface = udp
|
||||
|
||||
default:
|
||||
return fmt.Errorf("unsupported interface type: %s", ifaceConfig.Type)
|
||||
if err := udp.Start(); err != nil {
|
||||
return fmt.Errorf("failed to start UDP interface %s: %v", name, err)
|
||||
}
|
||||
|
||||
iface = udp
|
||||
log.Printf("UDP interface listening on %s", addr)
|
||||
}
|
||||
|
||||
c.interfaces = append(c.interfaces, iface)
|
||||
log.Printf("Created interface %s", iface.GetName())
|
||||
if iface != nil {
|
||||
// Set packet callback
|
||||
iface.SetPacketCallback(c.transport.HandlePacket)
|
||||
c.interfaces = append(c.interfaces, iface)
|
||||
log.Printf("Created and started interface %s (type=%v, enabled=%v)",
|
||||
name, iface.GetType(), iface.IsEnabled())
|
||||
}
|
||||
}
|
||||
|
||||
// Start periodic announces
|
||||
go func() {
|
||||
for {
|
||||
c.sendAnnounce()
|
||||
time.Sleep(30 * time.Second)
|
||||
}
|
||||
}()
|
||||
// Register announce handler with explicit type
|
||||
var handler transport.AnnounceHandler = &ClientAnnounceHandler{client: c}
|
||||
c.transport.RegisterAnnounceHandler(handler)
|
||||
|
||||
// Send initial announce
|
||||
log.Printf("Sending initial announce...")
|
||||
if err := c.sendAnnounce(); err != nil {
|
||||
log.Printf("Warning: Failed to send initial announce: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("Client started with %d interfaces", len(c.interfaces))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -119,17 +157,33 @@ func (c *Client) handlePacket(data []byte, p *packet.Packet) {
|
||||
return
|
||||
}
|
||||
|
||||
packetType := data[0]
|
||||
header := data[0]
|
||||
packetType := header & 0x03 // Extract packet type from header
|
||||
|
||||
switch packetType {
|
||||
case 0x04: // Announce packet
|
||||
c.handleAnnounce(data[1:])
|
||||
case announce.PACKET_TYPE_ANNOUNCE:
|
||||
log.Printf("Received announce packet:")
|
||||
log.Printf(" Raw data: %x", data)
|
||||
|
||||
// Create announce instance
|
||||
a, err := announce.New(c.identity, []byte("RNS.Go.Client"), false)
|
||||
if err != nil {
|
||||
log.Printf("Failed to create announce handler: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Handle the announce
|
||||
if err := a.HandleAnnounce(data[1:]); err != nil {
|
||||
log.Printf("Failed to handle announce: %v", err)
|
||||
}
|
||||
|
||||
default:
|
||||
c.transport.HandlePacket(data, p)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) handleAnnounce(data []byte) {
|
||||
if len(data) < 42 { // 32 bytes hash + 8 bytes timestamp + 1 byte hops + 1 byte flags
|
||||
if len(data) < 42 {
|
||||
log.Printf("Received malformed announce packet (too short)")
|
||||
return
|
||||
}
|
||||
@@ -144,33 +198,41 @@ func (c *Client) handleAnnounce(data []byte) {
|
||||
log.Printf(" Hops: %d", hops)
|
||||
log.Printf(" Flags: %x", flags)
|
||||
|
||||
// Extract public key if present (after flags)
|
||||
if len(data) > 42 {
|
||||
pubKeyLen := 32 // Ed25519 public key length
|
||||
pubKey := data[42 : 42+pubKeyLen]
|
||||
log.Printf(" Public Key: %x", pubKey)
|
||||
|
||||
// Extract app data if present
|
||||
dataLen := binary.BigEndian.Uint16(data[42:44])
|
||||
if len(data) >= 44+int(dataLen) {
|
||||
appData := data[44 : 44+dataLen]
|
||||
log.Printf(" App Data: %s", string(appData))
|
||||
var appData []byte
|
||||
if len(data) > 42+pubKeyLen+2 {
|
||||
dataLen := binary.BigEndian.Uint16(data[42+pubKeyLen : 42+pubKeyLen+2])
|
||||
if len(data) >= 42+pubKeyLen+2+int(dataLen) {
|
||||
appData = data[42+pubKeyLen+2 : 42+pubKeyLen+2+int(dataLen)]
|
||||
log.Printf(" App Data: %s", string(appData))
|
||||
}
|
||||
}
|
||||
|
||||
// Store the identity for future use with all required parameters
|
||||
if !identity.ValidateAnnounce(data, destHash, pubKey, data[len(data)-64:], appData) {
|
||||
log.Printf("Failed to validate announce")
|
||||
return
|
||||
}
|
||||
log.Printf("Successfully validated and stored announce")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) sendAnnounce() {
|
||||
func (c *Client) sendAnnounce() error {
|
||||
// Create announce packet
|
||||
identityHash := c.identity.Hash()
|
||||
announceData := make([]byte, 0)
|
||||
|
||||
// Create header
|
||||
header := announce.CreateHeader(
|
||||
announce.IFAC_NONE,
|
||||
announce.HEADER_TYPE_1,
|
||||
0x00,
|
||||
announce.PROP_TYPE_BROADCAST,
|
||||
announce.DEST_TYPE_SINGLE,
|
||||
announce.PACKET_TYPE_ANNOUNCE,
|
||||
0x00,
|
||||
)
|
||||
// Add header
|
||||
header := []byte{0x01, 0x00} // Announce packet type
|
||||
announceData = append(announceData, header...)
|
||||
|
||||
// Add destination hash (16 bytes truncated)
|
||||
identityHash := c.identity.Hash()
|
||||
// Add destination hash
|
||||
announceData = append(announceData, identityHash...)
|
||||
|
||||
// Add context byte
|
||||
@@ -197,17 +259,32 @@ func (c *Client) sendAnnounce() {
|
||||
log.Printf(" Packet Length: %d bytes", len(announceData))
|
||||
log.Printf(" Full Packet: %x", announceData)
|
||||
|
||||
sentCount := 0
|
||||
// Send on all interfaces
|
||||
for _, iface := range c.interfaces {
|
||||
log.Printf("Sending on interface %s (%s):", iface.GetName(), iface.GetType())
|
||||
log.Printf("Attempting to send on interface %s:", iface.GetName())
|
||||
log.Printf(" Type: %v", iface.GetType())
|
||||
log.Printf(" MTU: %d bytes", iface.GetMTU())
|
||||
log.Printf(" Status: enabled=%v", iface.IsEnabled())
|
||||
|
||||
if !iface.IsEnabled() {
|
||||
log.Printf(" Skipping disabled interface")
|
||||
continue
|
||||
}
|
||||
|
||||
if err := iface.Send(announceData, ""); err != nil {
|
||||
log.Printf(" Failed to send: %v", err)
|
||||
} else {
|
||||
log.Printf(" Successfully sent announce")
|
||||
sentCount++
|
||||
}
|
||||
}
|
||||
|
||||
if sentCount == 0 {
|
||||
return fmt.Errorf("no interfaces available to send announce")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Stop() {
|
||||
@@ -265,22 +342,67 @@ func (c *Client) handleLinkClosed(l *link.Link) {
|
||||
log.Printf("Link closed")
|
||||
}
|
||||
|
||||
type ClientAnnounceHandler struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
func (h *ClientAnnounceHandler) AspectFilter() []string {
|
||||
return []string{"RNS.Go.Client"}
|
||||
}
|
||||
|
||||
func (h *ClientAnnounceHandler) ReceivedAnnounce(destinationHash []byte, announcedIdentity interface{}, appData []byte) error {
|
||||
log.Printf("=== Received Announce Details ===")
|
||||
log.Printf("Destination Hash: %x", destinationHash)
|
||||
log.Printf("App Data: %s", string(appData))
|
||||
|
||||
// Type assert the identity
|
||||
if id, ok := announcedIdentity.(*identity.Identity); ok {
|
||||
log.Printf("Identity Public Key: %x", id.GetPublicKey())
|
||||
|
||||
// Create packet hash for storage
|
||||
packetHash := identity.TruncatedHash(append(destinationHash, id.GetPublicKey()...))
|
||||
log.Printf("Generated Packet Hash: %x", packetHash)
|
||||
|
||||
// Store the peer identity with all required parameters
|
||||
identity.Remember(packetHash, destinationHash, id.GetPublicKey(), appData)
|
||||
log.Printf("Identity stored successfully")
|
||||
log.Printf("===========================")
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Printf("Error: Invalid identity type")
|
||||
log.Printf("===========================")
|
||||
return fmt.Errorf("invalid identity type")
|
||||
}
|
||||
|
||||
func (h *ClientAnnounceHandler) ReceivePathResponses() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
log.Printf("Starting Reticulum Go client...")
|
||||
log.Printf("Config path: %s", *configPath)
|
||||
log.Printf("Target hash: %s", *targetHash)
|
||||
|
||||
var cfg *common.ReticulumConfig
|
||||
var err error
|
||||
|
||||
if *configPath == "" {
|
||||
log.Printf("No config path specified, using default configuration")
|
||||
cfg, err = config.InitConfig()
|
||||
} else {
|
||||
log.Printf("Loading configuration from: %s", *configPath)
|
||||
cfg, err = config.LoadConfig(*configPath)
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load config: %v", err)
|
||||
}
|
||||
log.Printf("Configuration loaded successfully")
|
||||
|
||||
if *generateIdentity {
|
||||
log.Printf("Generating new identity...")
|
||||
id, err := identity.New()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to generate identity: %v", err)
|
||||
@@ -299,29 +421,79 @@ func main() {
|
||||
log.Fatalf("Failed to start client: %v", err)
|
||||
}
|
||||
|
||||
// Wait for interrupt
|
||||
log.Printf("Client running, press Ctrl+C to exit")
|
||||
|
||||
// If target is specified, start interactive mode
|
||||
if *targetHash != "" {
|
||||
targetBytes, err := identity.HashFromString(*targetHash)
|
||||
if err != nil {
|
||||
log.Fatalf("Invalid target hash: %v", err)
|
||||
}
|
||||
link, err := client.transport.GetLink(targetBytes)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to get link: %v", err)
|
||||
}
|
||||
log.Printf("Starting interactive mode...")
|
||||
interactiveLoop(link)
|
||||
return
|
||||
}
|
||||
|
||||
// Wait for interrupt if no target specified
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-sigChan
|
||||
log.Printf("Received interrupt signal, shutting down...")
|
||||
}
|
||||
|
||||
func interactiveLoop(link *transport.Link) {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
connected := make(chan struct{})
|
||||
disconnected := make(chan struct{})
|
||||
|
||||
// Set up connection status handlers
|
||||
link.OnConnected(func() {
|
||||
connected <- struct{}{}
|
||||
})
|
||||
|
||||
link.OnDisconnected(func() {
|
||||
disconnected <- struct{}{}
|
||||
})
|
||||
|
||||
// Wait for initial connection
|
||||
select {
|
||||
case <-connected:
|
||||
log.Println("Connected to target")
|
||||
case <-time.After(10 * time.Second):
|
||||
log.Fatal("Connection timeout")
|
||||
return
|
||||
}
|
||||
|
||||
// Start input loop
|
||||
for {
|
||||
fmt.Print("> ")
|
||||
input, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
fmt.Printf("Error reading input: %v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
input = strings.TrimSpace(input)
|
||||
if input == "quit" || input == "exit" {
|
||||
select {
|
||||
case <-disconnected:
|
||||
log.Println("Connection lost")
|
||||
return
|
||||
}
|
||||
default:
|
||||
fmt.Print("> ")
|
||||
input, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return
|
||||
}
|
||||
log.Printf("Error reading input: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if err := link.Send([]byte(input)); err != nil {
|
||||
fmt.Printf("Failed to send: %v\n", err)
|
||||
input = strings.TrimSpace(input)
|
||||
if input == "quit" || input == "exit" {
|
||||
return
|
||||
}
|
||||
|
||||
if err := link.Send([]byte(input)); err != nil {
|
||||
log.Printf("Failed to send: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user