Initial commit
This commit is contained in:
commit
21eed45822
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2020 Simon Ser
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
|
@ -0,0 +1,70 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"io"
|
||||
"bufio"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/shlex"
|
||||
)
|
||||
|
||||
type Directive struct {
|
||||
Params []string
|
||||
Children []*Directive
|
||||
}
|
||||
|
||||
func Load(path string) ([]*Directive, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
return Parse(f)
|
||||
}
|
||||
|
||||
func Parse(r io.Reader) ([]*Directive, error) {
|
||||
scanner := bufio.NewScanner(r)
|
||||
|
||||
var directives []*Directive
|
||||
var cur *Directive
|
||||
for scanner.Scan() {
|
||||
l := scanner.Text()
|
||||
words, err := shlex.Split(l)
|
||||
if err != nil {
|
||||
return directives, fmt.Errorf("failed to parse config file: %v", err)
|
||||
} else if len(words) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if len(words) == 1 && l[len(l) - 1] == '}' {
|
||||
if cur == nil {
|
||||
return nil, fmt.Errorf("unexpected '}'")
|
||||
}
|
||||
cur = nil
|
||||
continue
|
||||
}
|
||||
|
||||
var d *Directive
|
||||
if words[len(words) - 1] == "{" && l[len(l) - 1] == '{' {
|
||||
d = &Directive{
|
||||
Params: words[:len(words) - 1],
|
||||
}
|
||||
cur = d
|
||||
directives = append(directives, d)
|
||||
} else {
|
||||
d = &Directive{Params: words}
|
||||
if cur != nil {
|
||||
cur.Children = append(cur.Children, d)
|
||||
} else {
|
||||
directives = append(directives, d)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, fmt.Errorf("failed to read config file: %v", err)
|
||||
}
|
||||
|
||||
return directives, nil
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
module git.sr.ht/~emersion/tlstunnel
|
||||
|
||||
go 1.15
|
||||
|
||||
require github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
|
|
@ -0,0 +1,2 @@
|
|||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
|
||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
|
Loading…
Reference in New Issue