2023-07-02 19:00:59 +03:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Path string `json:"-"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
Debug bool `json:"debug"`
|
|
|
|
ExtraDomains map[string]string `json:"extraDomains"`
|
|
|
|
TlsCertPath string `json:"tlsCertPath"`
|
|
|
|
TlsKeyPath string `json:"tlsKeyPath"`
|
|
|
|
Host string `json:"host"`
|
|
|
|
TlsHost string `json:"tlsHost"`
|
2023-07-14 03:19:16 +03:00
|
|
|
TaskLifetime int64 `json:"taskLifetime"`
|
|
|
|
TaskGetDelay int64 `json:"taskGetDelay"`
|
2023-07-02 19:00:59 +03:00
|
|
|
AntiGateV2 struct {
|
|
|
|
TurnstileUserAgent string `json:"turnstileUserAgent"`
|
|
|
|
Ip string `json:"ip"`
|
|
|
|
} `json:"AntiGateV2"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func Default() *Config {
|
|
|
|
return &Config{
|
|
|
|
Path: "./data/config.json",
|
|
|
|
Token: "",
|
|
|
|
Debug: false,
|
|
|
|
ExtraDomains: map[string]string{},
|
|
|
|
TlsCertPath: "./data/ca.crt",
|
|
|
|
TlsKeyPath: "./data/ca.key",
|
|
|
|
Host: "127.0.0.1:80",
|
|
|
|
TlsHost: "127.0.0.1:443",
|
2023-07-14 03:19:16 +03:00
|
|
|
TaskLifetime: 60000,
|
|
|
|
TaskGetDelay: 0,
|
2023-07-02 19:00:59 +03:00
|
|
|
AntiGateV2: struct {
|
|
|
|
TurnstileUserAgent string `json:"turnstileUserAgent"`
|
|
|
|
Ip string `json:"ip"`
|
|
|
|
}{
|
|
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
|
|
|
|
"127.0.0.1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) Load() error {
|
|
|
|
raw, err := os.ReadFile(c.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(raw, c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// format config, if it isn't properly formatted
|
|
|
|
// also it merges user's config with our defaults
|
|
|
|
marshaled, err := json.MarshalIndent(c, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Compare(raw, marshaled) != 0 {
|
|
|
|
if err := os.WriteFile(c.Path, marshaled, 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) Save() error {
|
|
|
|
|
|
|
|
dir := c.Path
|
|
|
|
// converts "./user/config.json" to "./user"
|
|
|
|
if i := strings.LastIndex(dir, "/"); i != -1 {
|
|
|
|
dir = dir[0:i]
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
marshaled, err := json.MarshalIndent(c, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.WriteFile(c.Path, marshaled, 0600)
|
|
|
|
}
|