From 21eed45822e0cc53dc75a299a210bb446a05add4 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 8 Sep 2020 12:11:32 +0200 Subject: [PATCH] Initial commit --- LICENSE | 21 +++++++++++++++++ README.md | 7 ++++++ config.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 5 ++++ go.sum | 2 ++ main.go | 13 +++++++++++ 6 files changed, 118 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 config.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0243e5a --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a723e18 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# tlstunnel + +A TLS reverse proxy. + +## License + +MIT diff --git a/config.go b/config.go new file mode 100644 index 0000000..48105a8 --- /dev/null +++ b/config.go @@ -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 +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8c33a07 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.sr.ht/~emersion/tlstunnel + +go 1.15 + +require github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0be9157 --- /dev/null +++ b/go.sum @@ -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= diff --git a/main.go b/main.go new file mode 100644 index 0000000..7f4bd33 --- /dev/null +++ b/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "log" +) + +func main() { + directives, err := Load("config") + if err != nil { + log.Fatalf("failed to load config file: %v", err) + } + _ = directives +}