~emersion/tlstunnel

ref: aae358811d2df82fb744f3441e7543e0d9458136 tlstunnel/config.go -rw-r--r-- 2.1 KiB
aae35881Simon Ser Set PROXY protocol PP2_TYPE_SSL 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package tlstunnel

import (
	"bufio"
	"fmt"
	"io"
	"os"

	"github.com/google/shlex"
)

type Directive struct {
	Name     string
	Params   []string
	Children []*Directive
}

func (d *Directive) ParseParams(params ...*string) error {
	if len(d.Params) < len(params) {
		return fmt.Errorf("directive %q: want %v params, got %v", d.Name, len(params), len(d.Params))
	}
	for i, ptr := range params {
		if ptr == nil {
			continue
		}
		*ptr = d.Params[i]
	}
	return nil
}

func (d *Directive) ChildrenByName(name string) []*Directive {
	l := make([]*Directive, 0, len(d.Children))
	for _, child := range d.Children {
		if child.Name == name {
			l = append(l, child)
		}
	}
	return l
}

func (d *Directive) ChildByName(name string) *Directive {
	for _, child := range d.Children {
		if child.Name == name {
			return child
		}
	}
	return nil
}

func LoadConfig(path string) (*Directive, error) {
	f, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	return ParseConfig(f)
}

func ParseConfig(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 nil, 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] == '{' {
			words = words[:len(words)-1]

			var name string
			params := words
			if len(words) > 0 {
				name, params = words[0], words[1:]
			}

			d = &Directive{Name: name, Params: params}
			cur = d
			directives = append(directives, d)
		} else {
			d = &Directive{Name: words[0], Params: words[1:]}
			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 &Directive{Children: directives}, nil
}