-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.go
72 lines (62 loc) · 2.11 KB
/
init.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"time"
"github.com/urlund/nginx-mail-auth-http/types"
)
var (
cacheTTL string
cacheCleanup string
configFile string
configPath string
listen string
authKey string
authHeader string
version bool
config types.Config
proxyConfigCache map[string]types.ProxyConfig
timeout time.Duration
cleanup time.Duration
mu sync.Mutex
)
func init() {
proxyConfigCache = map[string]types.ProxyConfig{}
// why 8278? because is an unsigned port @ iana.org
// (and, 25 + 110 + 143 = 278 and starting with 8 because 8080 is starting with 8 and typically used for a personally hosted web server)
flag.StringVar(&listen, "listen", ":8278", "Address to handle requests on incoming connections")
flag.StringVar(&cacheTTL, "cache-ttl", "24h", "Time to keep proxy configs in cache since last usage (see: https://golang.org/pkg/time/#ParseDuration)")
flag.StringVar(&cacheCleanup, "cache-cleanup", "1m", "Interval between cache cleanups (see: https://golang.org/pkg/time/#ParseDuration)")
flag.StringVar(&configFile, "config-file", "config.json", "Name of config file")
flag.StringVar(&configPath, "config-path", "/etc/nginx-mail-auth-http", "Path where '-config-file' (and conf.d) can be found")
flag.StringVar(&authKey, "auth-key", "", "This header can be used as the shared secret to verify that the request comes from nginx")
flag.StringVar(&authHeader, "auth-header", "Auth-Key", "Checks the specified header in requests sent to the authentication server")
flag.BoolVar(&version, "version", version, "Show version")
flag.Parse()
if version {
fmt.Println("version: 1.0.1")
os.Exit(0)
}
var err error
timeout, err = time.ParseDuration(cacheTTL)
if err != nil {
panic(err)
}
cleanup, err = time.ParseDuration(cacheCleanup)
if err != nil {
panic(err)
}
jsonBlob, err := ioutil.ReadFile(filepath.Join(configPath, configFile))
if err != nil {
panic(err)
}
err = json.Unmarshal(jsonBlob, &config)
if err != nil {
panic(err)
}
}