This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
flixproxy.go
167 lines (143 loc) · 4.41 KB
/
flixproxy.go
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// flixproxy.go
//
// Copyright © 2015 Janne Snabb <snabb AT epipe.com>
//
// This file is part of Flixproxy.
//
// Flixproxy is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Flixproxy is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Flixproxy. If not, see <http://www.gnu.org/licenses/>.
//
// Flixproxy - DNS, HTTP and TLS proxy
//
// Please see https://github.com/snabb/flixproxy for more information.
package main
import (
"fmt"
"io/ioutil"
"os"
"os/signal"
"runtime"
"syscall"
"github.com/ogier/pflag"
"github.com/snabb/flixproxy/access"
"github.com/snabb/flixproxy/dnsproxy"
"github.com/snabb/flixproxy/httpproxy"
"github.com/snabb/flixproxy/tlsproxy"
"gopkg.in/inconshreveable/log15.v2"
"gopkg.in/yaml.v2"
)
// Default configuration file location:
const CONFIG_FILE = "flixproxy.conf"
type config struct {
Acl access.Config
Logging []LoggingTarget
DNS []dnsproxy.Config
HTTP []httpproxy.Config
TLS []tlsproxy.Config
}
func parseConfig(configFile string) (config config, err error) {
configText, err := ioutil.ReadFile(configFile)
if err != nil {
return config, err
}
err = yaml.Unmarshal(configText, &config)
return config, err
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
configFile := pflag.StringP("conf", "c", CONFIG_FILE, "configuration file")
testConfig := pflag.BoolP("test", "t", false, "test configuration")
version := pflag.BoolP("version", "v", false, "version")
pflag.Parse()
if pflag.NArg() > 0 {
pflag.Usage()
os.Exit(2)
}
if *version {
printVersion()
os.Exit(0)
}
logger := log15.New()
config, err := parseConfig(*configFile)
if err != nil {
logger.Crit("error parsing configuration", "err", err)
os.Exit(2)
}
if *testConfig {
fmt.Println("Configuration file parsed successfully.")
fmt.Printf("%+v\n", config)
os.Exit(0)
}
setupLogging(logger, config.Logging)
logger.Info("starting listeners")
var proxies []interface {
Stop()
}
for _, proxyConfig := range config.DNS {
proxies = append(proxies,
dnsproxy.New(proxyConfig, config.Acl.GetAcl(proxyConfig.Acl), logger.New("s", "DNS")))
}
for _, proxyConfig := range config.HTTP {
proxies = append(proxies,
httpproxy.New(proxyConfig, config.Acl.GetAcl(proxyConfig.Acl), logger.New("s", "HTTP")))
}
for _, proxyConfig := range config.TLS {
proxies = append(proxies,
tlsproxy.New(proxyConfig, config.Acl.GetAcl(proxyConfig.Acl), logger.New("s", "TLS")))
}
sigCexit := make(chan os.Signal, 1)
signal.Notify(sigCexit, syscall.SIGTERM, syscall.SIGINT) // terminate gracefully
sigChup := make(chan os.Signal, 1)
signal.Notify(sigChup, syscall.SIGHUP) // reopen logs
logger.Info("entering main loop")
MAINLOOP:
for {
select {
// there will probably be something more here in the future XXX
case <-sigCexit:
logger.Debug("exit signal received")
break MAINLOOP
case <-sigChup:
setupLogging(logger, config.Logging)
logger.Debug("reopened logs")
}
}
logger.Info("exiting, stopping listeners")
for _, proxy := range proxies {
proxy.Stop()
}
logger.Info("bye")
}
func getGoEnvironment() (environment string) {
return fmt.Sprintf("%s %s (%s/%s)", runtime.Compiler, runtime.Version(),
runtime.GOOS, runtime.GOARCH)
}
func printVersion() {
fmt.Println("Flixproxy", VERSION, "- DNS, HTTP and TLS proxy")
fmt.Println("Built with", getGoEnvironment())
fmt.Print(`
Copyright © 2015 Janne Snabb <snabb AT epipe.com>
Flixproxy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Flixproxy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Flixproxy. If not, see <http://www.gnu.org/licenses/>.
`)
}
// eof