Enchantrix/pkg/config/config.go
google-labs-jules[bot] 47e12b1875 feat: Implement xmrig-proxy functionality
This commit introduces the initial implementation of the xmrig-proxy functionality.

It includes:
- A new `proxy` command to the CLI to start the proxy.
- A new `pkg/proxy` package to encapsulate the core proxy logic.
- A mock proxy service that simulates workers connecting.
- The initial implementation of the XMRig Proxy API, with the `/` and `/workers.json` endpoints.
2025-10-31 05:03:46 +00:00

70 lines
1.3 KiB
Go

package config
import (
"encoding/json"
"io/ioutil"
"os"
"sync"
)
type Config struct {
mu sync.RWMutex
API struct {
ID string `json:"id"`
WorkerID string `json:"worker-id"`
} `json:"api"`
HTTP struct {
Enabled bool `json:"enabled"`
Host string `json:"host"`
Port int `json:"port"`
AccessToken string `json:"access-token"`
Restricted bool `json:"restricted"`
} `json:"http"`
Pools []struct {
URL string `json:"url"`
User string `json:"user"`
Pass string `json:"pass"`
} `json:"pools"`
}
func New() *Config {
cfg := &Config{}
if _, err := os.Stat("config.json"); err == nil {
cfg.Load("config.json")
} else {
cfg.API.ID = "enchantrix"
cfg.HTTP.Enabled = true
cfg.HTTP.Host = "127.0.0.1"
cfg.HTTP.Port = 8080
cfg.HTTP.Restricted = true
}
return cfg
}
func (c *Config) Load(path string) error {
c.mu.Lock()
defer c.mu.Unlock()
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
return json.Unmarshal(data, c)
}
func (c *Config) Get() *Config {
c.mu.RLock()
defer c.mu.RUnlock()
// To avoid returning a pointer to the internal struct, we create a copy
clone := *c
return &clone
}
func (c *Config) Update(newConfig *Config) {
c.mu.Lock()
defer c.mu.Unlock()
c.API = newConfig.API
c.HTTP = newConfig.HTTP
c.Pools = newConfig.Pools
}