You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.1 KiB
Go

7 years ago
package main
import (
"crypto/tls"
"fmt"
7 years ago
"io/ioutil"
"log"
"net/url"
"github.com/go-yaml/yaml"
)
type conf struct {
Listen []string `yaml:"listen"`
Forward []forward `yaml:"forward"`
Certificate []certificate `yaml:"certificate"`
DefaultBackend string `yaml:"default_backend"`
}
type forward struct {
SNI string `yaml:"sni"`
Backend string `yaml:"backend"`
}
type certificate struct {
Cert string `yaml:"cert"`
Key string `yaml:"key"`
}
func loadConfig(f string) (c *conf, err error) {
data, err := ioutil.ReadFile(f)
if err != nil {
return
}
c = &conf{}
if err = yaml.Unmarshal(data, c); err != nil {
return
}
if c.DefaultBackend == "" {
return nil, fmt.Errorf("config error, empty default backend")
}
7 years ago
return
}
func (f forward) match(sni string) bool {
if f.SNI == sni {
return true
}
return false
}
func (f forward) getBackend() *url.URL {
u, err := url.Parse(f.Backend)
if err != nil {
log.Println(err)
return nil
}
return u
}
func (c certificate) load() (tls.Certificate, error) {
cert, err := tls.LoadX509KeyPair(c.Cert, c.Key)
return cert, err
}