mirror of
https://github.com/DeBrosOfficial/network.git
synced 2025-10-06 12:09:07 +00:00
- Require API key or JWT by default for client connections - Auto-derive namespace from JWT claim or API key format `ak_<rand>:<namespace>` - Deny calls if per-call namespace override mismatches resolved namespace - Guard Storage, PubSub, Database, and NetworkInfo operations with access checks - Add context helpers for consistent namespace override handling - Update docs and add end-to-end and unit tests for authentication logic
128 lines
4.4 KiB
Go
128 lines
4.4 KiB
Go
package config
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
)
|
|
|
|
// Config represents the main configuration for a network node
|
|
type Config struct {
|
|
Node NodeConfig `yaml:"node"`
|
|
Database DatabaseConfig `yaml:"database"`
|
|
Discovery DiscoveryConfig `yaml:"discovery"`
|
|
Security SecurityConfig `yaml:"security"`
|
|
Logging LoggingConfig `yaml:"logging"`
|
|
}
|
|
|
|
// NodeConfig contains node-specific configuration
|
|
type NodeConfig struct {
|
|
ID string `yaml:"id"` // Auto-generated if empty
|
|
Type string `yaml:"type"` // "bootstrap" or "node"
|
|
ListenAddresses []string `yaml:"listen_addresses"` // LibP2P listen addresses
|
|
DataDir string `yaml:"data_dir"` // Data directory
|
|
MaxConnections int `yaml:"max_connections"` // Maximum peer connections
|
|
DisableAnonRC bool `yaml:"disable_anon_rc"` // Disable Anyone proxy/SOCKS5
|
|
}
|
|
|
|
// DatabaseConfig contains database-related configuration
|
|
type DatabaseConfig struct {
|
|
DataDir string `yaml:"data_dir"`
|
|
ReplicationFactor int `yaml:"replication_factor"`
|
|
ShardCount int `yaml:"shard_count"`
|
|
MaxDatabaseSize int64 `yaml:"max_database_size"` // In bytes
|
|
BackupInterval time.Duration `yaml:"backup_interval"`
|
|
|
|
// RQLite-specific configuration
|
|
RQLitePort int `yaml:"rqlite_port"` // RQLite HTTP API port
|
|
RQLiteRaftPort int `yaml:"rqlite_raft_port"` // RQLite Raft consensus port
|
|
RQLiteJoinAddress string `yaml:"rqlite_join_address"` // Address to join RQLite cluster
|
|
}
|
|
|
|
// DiscoveryConfig contains peer discovery configuration
|
|
type DiscoveryConfig struct {
|
|
BootstrapPeers []string `yaml:"bootstrap_peers"` // Bootstrap peer addresses
|
|
DiscoveryInterval time.Duration `yaml:"discovery_interval"` // Discovery announcement interval
|
|
BootstrapPort int `yaml:"bootstrap_port"` // Default port for bootstrap nodes
|
|
HttpAdvAddress string `yaml:"http_adv_address"` // HTTP advertisement address
|
|
RaftAdvAddress string `yaml:"raft_adv_address"` // Raft advertisement
|
|
}
|
|
|
|
// SecurityConfig contains security-related configuration
|
|
type SecurityConfig struct {
|
|
EnableTLS bool `yaml:"enable_tls"`
|
|
PrivateKeyFile string `yaml:"private_key_file"`
|
|
CertificateFile string `yaml:"certificate_file"`
|
|
}
|
|
|
|
// LoggingConfig contains logging configuration
|
|
type LoggingConfig struct {
|
|
Level string `yaml:"level"` // debug, info, warn, error
|
|
Format string `yaml:"format"` // json, console
|
|
OutputFile string `yaml:"output_file"` // Empty for stdout
|
|
}
|
|
|
|
// ClientConfig represents configuration for network clients
|
|
type ClientConfig struct {
|
|
AppName string `yaml:"app_name"`
|
|
DatabaseName string `yaml:"database_name"`
|
|
BootstrapPeers []string `yaml:"bootstrap_peers"`
|
|
ConnectTimeout time.Duration `yaml:"connect_timeout"`
|
|
RetryAttempts int `yaml:"retry_attempts"`
|
|
}
|
|
|
|
// ParseMultiaddrs converts string addresses to multiaddr objects
|
|
func (c *Config) ParseMultiaddrs() ([]multiaddr.Multiaddr, error) {
|
|
var addrs []multiaddr.Multiaddr
|
|
for _, addr := range c.Node.ListenAddresses {
|
|
ma, err := multiaddr.NewMultiaddr(addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
addrs = append(addrs, ma)
|
|
}
|
|
return addrs, nil
|
|
}
|
|
|
|
// DefaultConfig returns a default configuration
|
|
func DefaultConfig() *Config {
|
|
return &Config{
|
|
Node: NodeConfig{
|
|
Type: "node",
|
|
ListenAddresses: []string{
|
|
"/ip4/0.0.0.0/tcp/4001", // TCP only - compatible with Anyone proxy/SOCKS5
|
|
},
|
|
DataDir: "./data",
|
|
MaxConnections: 50,
|
|
},
|
|
Database: DatabaseConfig{
|
|
DataDir: "./data/db",
|
|
ReplicationFactor: 3,
|
|
ShardCount: 16,
|
|
MaxDatabaseSize: 1024 * 1024 * 1024, // 1GB
|
|
BackupInterval: time.Hour * 24, // Daily backups
|
|
|
|
// RQLite-specific configuration
|
|
RQLitePort: 5001,
|
|
RQLiteRaftPort: 7001,
|
|
RQLiteJoinAddress: "", // Empty for bootstrap node
|
|
},
|
|
Discovery: DiscoveryConfig{
|
|
BootstrapPeers: []string{
|
|
"/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWGqqR8bxgmYsYrGYMKnUWwZUCpioLmA3H37ggRDnAiFa7",
|
|
},
|
|
BootstrapPort: 4001, // Default LibP2P port
|
|
DiscoveryInterval: time.Second * 15, // Back to 15 seconds for testing
|
|
HttpAdvAddress: "",
|
|
RaftAdvAddress: "",
|
|
},
|
|
Security: SecurityConfig{
|
|
EnableTLS: false,
|
|
},
|
|
Logging: LoggingConfig{
|
|
Level: "info",
|
|
Format: "console",
|
|
},
|
|
}
|
|
}
|