mirror of
https://github.com/lwch/natpass.git
synced 2024-04-21 12:41:54 +00:00
52 lines
1013 B
Go
52 lines
1013 B
Go
package global
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"os"
|
|
|
|
"github.com/lwch/runtime"
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
type Tunnel struct {
|
|
Name string `yaml:"name"`
|
|
Target string `yaml:"target"`
|
|
Type string `yaml:"type"`
|
|
LocalAddr string `yaml:"local_addr"`
|
|
LocalPort uint16 `yaml:"local_port"`
|
|
RemoteAddr string `yaml:"remote_addr"`
|
|
RemotePort uint16 `yaml:"remote_port"`
|
|
}
|
|
|
|
type Configure struct {
|
|
ID string
|
|
Server string
|
|
Enc [md5.Size]byte
|
|
Tunnels []Tunnel
|
|
}
|
|
|
|
func LoadConf(dir string) *Configure {
|
|
var cfg struct {
|
|
ID string `yaml:"id"`
|
|
Server string `yaml:"server"`
|
|
Secret string `yaml:"secret"`
|
|
Tunnel []Tunnel `yaml:"tunnel"`
|
|
}
|
|
f, err := os.Open(dir)
|
|
runtime.Assert(err)
|
|
defer f.Close()
|
|
runtime.Assert(yaml.NewDecoder(f).Decode(&cfg))
|
|
for i, t := range cfg.Tunnel {
|
|
if t.Type != "tcp" {
|
|
t.Type = "udp"
|
|
}
|
|
cfg.Tunnel[i] = t
|
|
}
|
|
return &Configure{
|
|
ID: cfg.ID,
|
|
Server: cfg.Server,
|
|
Enc: md5.Sum([]byte(cfg.Secret)),
|
|
Tunnels: cfg.Tunnel,
|
|
}
|
|
}
|