-
-
Save hivefans/d6af1a9ea08de8204a8567c9aeda5eb9 to your computer and use it in GitHub Desktop.
|-|{"files":{"main.go":{"env":"plain"},"config.yaml":{"env":"plain"},"config.go":{"env":"plain"}},"tag":"Uncategorized"}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package honeycast | |
| import ( | |
| "io/ioutil" | |
| "regexp" | |
| "github.com/imdario/mergo" | |
| "gopkg.in/yaml.v2" | |
| ) | |
| type ( | |
| Config struct { | |
| Token string `yaml:"token"` | |
| Template string `yaml:"template"` | |
| Proxies ProxiesConfig `yaml:"proxies"` | |
| } | |
| ) | |
| type ProxiesConfig struct { | |
| SSH SSHProxyConfig `yaml:"ssh"` | |
| HTTP HTTPProxyConfig `yaml:"http"` | |
| } | |
| type HTTPProxyConfig struct { | |
| Port string `yaml:"port"` | |
| } | |
| type SSHProxyConfig struct { | |
| Key string `yaml:"key"` | |
| Port string `yaml:"port"` | |
| Banner string `yaml:"banner"` | |
| } | |
| func (pc *SSHProxyConfig) PrivateKey() (ssh.Signer, error) { | |
| b, err := ioutil.ReadFile(pc.Key) | |
| if err != nil { | |
| return nil, err | |
| } | |
| return ssh.ParsePrivateKey(b) | |
| } | |
| var DefaultConfig = Config{ | |
| Token: "token", | |
| Template: "c1", | |
| Proxies: ProxiesConfig{ | |
| HTTP: HTTPProxyConfig{ | |
| Port: ":8020", | |
| }, | |
| SSH: SSHProxyConfig{ | |
| Key: "./perms/id_rsa.key", | |
| Port: ":8022", | |
| }, | |
| }, | |
| } | |
| func NewConfig() (*Config, error) { | |
| c := DefaultConfig | |
| return &c, nil | |
| } | |
| func (c *Config) Load(file string) error { | |
| data, err := ioutil.ReadFile(file) | |
| if err != nil { | |
| return err | |
| } | |
| conf := Config{} | |
| err = yaml.Unmarshal(data, &conf) | |
| if err != nil { | |
| return err | |
| } | |
| return mergo.MergeWithOverwrite(c, conf) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| template: "test" | |
| token: test | |
| proxies: | |
| http: | |
| port: :8080 | |
| ssh: | |
| port: :8022 | |
| key: "./perm/perms" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| config, err := NewConfig() | |
| if err != nil { | |
| fmt.Fprintf(os.Stdout, err.Error()) | |
| return | |
| } | |
| if err := config.Load("config.yaml"); err != nil { | |
| fmt.Fprintf(os.Stdout, err.Error()) | |
| return | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment