2023-11-16 20:09:40 +03:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2023-11-29 19:21:53 +03:00
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/gob"
|
|
|
|
"encoding/hex"
|
2023-11-16 20:09:40 +03:00
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
|
2023-11-29 18:52:59 +03:00
|
|
|
"github.com/Neur0toxine/sshpoke/pkg/smarttypes"
|
2023-11-16 20:09:40 +03:00
|
|
|
"github.com/docker/docker/client"
|
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
|
|
|
)
|
|
|
|
|
2023-11-29 19:21:53 +03:00
|
|
|
var (
|
|
|
|
Default Config
|
|
|
|
configHash string
|
|
|
|
)
|
2023-11-16 20:09:40 +03:00
|
|
|
|
|
|
|
type Config struct {
|
2023-11-22 22:21:11 +03:00
|
|
|
Debug bool `mapstructure:"debug" json:"debug"`
|
|
|
|
API API `mapstructure:"api" json:"api"`
|
|
|
|
Docker DockerConfig `mapstructure:"docker" json:"docker"`
|
|
|
|
DefaultServer string `mapstructure:"default_server" json:"default_server"`
|
2023-11-29 18:52:59 +03:00
|
|
|
Services []Service `mapstructure:"services" json:"services,omitempty"`
|
2023-11-22 22:21:11 +03:00
|
|
|
Servers []Server `mapstructure:"servers" json:"servers"`
|
2023-11-16 20:09:40 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 19:21:53 +03:00
|
|
|
func HasBeenUpdated() bool {
|
|
|
|
return generateConfigHash() != configHash
|
|
|
|
}
|
|
|
|
|
|
|
|
func Rehash() {
|
|
|
|
configHash = generateConfigHash()
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateConfigHash() string {
|
|
|
|
h := sha1.New()
|
|
|
|
_ = gob.NewEncoder(h).Encode(Default)
|
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
2023-11-29 18:52:59 +03:00
|
|
|
type Service struct {
|
|
|
|
Name smarttypes.MatchableString `mapstructure:"name" json:"name"`
|
|
|
|
Params ServiceLabels `mapstructure:"params" json:"params"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServiceLabels struct {
|
|
|
|
Enable smarttypes.BoolStr `mapstructure:"enable" json:"enable"`
|
|
|
|
Network string `mapstructure:"network" json:"network,omitempty"`
|
|
|
|
Server string `mapstructure:"server" json:"server,omitempty"`
|
|
|
|
Port string `mapstructure:"port" json:"port,omitempty"`
|
|
|
|
RemoteHost string `mapstructure:"remote_host" json:"remote_host,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-11-19 13:06:38 +03:00
|
|
|
type API struct {
|
2023-11-22 23:31:35 +03:00
|
|
|
Rest WebAPI `mapstructure:"rest" json:"rest"`
|
2023-11-22 22:21:11 +03:00
|
|
|
Plugin PluginAPI `mapstructure:"plugin" json:"plugin"`
|
2023-11-21 18:55:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type WebAPI struct {
|
2023-11-22 22:21:11 +03:00
|
|
|
Port int `mapstructure:"port" json:"port" validate:"gte=0,lte=65535"`
|
|
|
|
Token string `mapstructure:"token" json:"token"`
|
2023-11-21 18:55:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type PluginAPI struct {
|
2023-11-22 22:21:11 +03:00
|
|
|
Port int `mapstructure:"port" json:"port" validate:"gte=0,lte=65535"`
|
2023-11-19 13:06:38 +03:00
|
|
|
}
|
|
|
|
|
2023-11-16 20:09:40 +03:00
|
|
|
type DockerConfig struct {
|
2023-11-22 22:21:11 +03:00
|
|
|
FromEnv *bool `mapstructure:"from_env,omitempty" json:"from_env"`
|
|
|
|
CertPath string `mapstructure:"cert_path" json:"cert_path,omitempty"`
|
|
|
|
TLSVerify *bool `mapstructure:"tls_verify,omitempty" json:"tls_verify,omitempty"`
|
|
|
|
Host string `mapstructure:"host" json:"host,omitempty"`
|
|
|
|
Version string `mapstructure:"version" json:"version,omitempty"`
|
2023-11-16 20:09:40 +03:00
|
|
|
}
|
|
|
|
|
2023-11-17 20:39:00 +03:00
|
|
|
type DriverParams map[string]interface{}
|
|
|
|
|
|
|
|
type DriverType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
DriverSSH DriverType = "ssh"
|
|
|
|
DriverPlugin DriverType = "plugin"
|
2023-11-22 22:21:11 +03:00
|
|
|
DriverNil DriverType = "nil"
|
2023-11-17 20:39:00 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2023-11-22 22:21:11 +03:00
|
|
|
Name string `mapstructure:"name" json:"name" validate:"required"`
|
|
|
|
Driver DriverType `mapstructure:"driver" json:"driver"`
|
|
|
|
Params DriverParams `mapstructure:"params" json:"params,omitempty"`
|
2023-11-17 20:39:00 +03:00
|
|
|
}
|
|
|
|
|
2023-11-16 20:09:40 +03:00
|
|
|
func (d DockerConfig) Opts(c *client.Client) error {
|
2023-11-17 20:39:00 +03:00
|
|
|
if d.FromEnv == nil || *d.FromEnv {
|
2023-11-16 20:09:40 +03:00
|
|
|
return client.FromEnv(c)
|
|
|
|
}
|
|
|
|
ops := []client.Opt{
|
|
|
|
d.withTLSClientConfig(),
|
|
|
|
d.withHost(),
|
|
|
|
d.withVersion(),
|
|
|
|
}
|
|
|
|
for _, op := range ops {
|
|
|
|
if err := op(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d DockerConfig) withTLSClientConfig() client.Opt {
|
|
|
|
return func(c *client.Client) error {
|
|
|
|
dockerCertPath := d.CertPath
|
|
|
|
if dockerCertPath == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
skipTLSVerify := false
|
|
|
|
if d.TLSVerify != nil && !(*d.TLSVerify) {
|
|
|
|
skipTLSVerify = true
|
|
|
|
}
|
|
|
|
options := tlsconfig.Options{
|
|
|
|
CAFile: filepath.Join(dockerCertPath, "ca.pem"),
|
|
|
|
CertFile: filepath.Join(dockerCertPath, "cert.pem"),
|
|
|
|
KeyFile: filepath.Join(dockerCertPath, "key.pem"),
|
|
|
|
InsecureSkipVerify: skipTLSVerify,
|
|
|
|
}
|
|
|
|
tlsConfig, err := tlsconfig.Client(options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.WithHTTPClient(&http.Client{
|
|
|
|
Transport: &http.Transport{TLSClientConfig: tlsConfig},
|
|
|
|
CheckRedirect: client.CheckRedirect,
|
|
|
|
})(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d DockerConfig) withHost() client.Opt {
|
|
|
|
return func(c *client.Client) error {
|
|
|
|
if host := d.Host; host != "" {
|
|
|
|
return client.WithHost(host)(c)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d DockerConfig) withVersion() client.Opt {
|
|
|
|
return func(c *client.Client) error {
|
|
|
|
return client.WithVersion(d.Version)(c)
|
|
|
|
}
|
|
|
|
}
|