-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatchman.go
97 lines (77 loc) · 2.59 KB
/
watchman.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
/*
* Watchman - Simple HTTP Reverse Proxy with authentication
* Copyright DesertBit
* Author: Roland Singer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"flag"
"net/http"
"net/http/httputil"
"path/filepath"
log "github.com/Sirupsen/logrus"
auth "github.com/abbot/go-http-auth"
)
const (
configName = "watchman.conf"
envDir = "WATCHMAN_DIR"
envConfig = "WATCHMAN_CONFIG"
)
var (
lookupDir = "." // Current working directory.
)
func main() {
// Get the lookup directory path from the environment variable if defined.
lookupDir = getEnv(envDir, lookupDir)
// Get the config path from the environment variable if defined.
// The default config path is just the config name.
// This will load the config from the current working directory.
configPath := getEnv(envConfig, configName)
// Get the config path from the command line arguments.
flag.StringVar(&configPath, "config", configPath, "set config file path.")
// Parse the flags.
flag.Parse()
// Load the config.
err := LoadConfig(filepath.Clean(lookupDir + "/" + configPath))
if err != nil {
log.Fatal(err)
}
// Create the secret provider.
secretProvider := auth.HtpasswdFileProvider(Config.PasswdFile)
// Create the authenticator.
authenticator := auth.NewBasicAuthenticator(Config.Description, secretProvider)
// Set the HTTP routes.
http.HandleFunc("/", authenticator.Wrap(handleReverseProxyFunc))
// Start the HTTP server.
log.Fatal(http.ListenAndServe(Config.listenAddress, nil))
}
// handleReverseProxyFunc proxies the traffic to the destination host.
func handleReverseProxyFunc(w http.ResponseWriter, authReq *auth.AuthenticatedRequest) {
// Get the http Request.
r := &authReq.Request
// Get the remote address.
address, _ := remoteAddress(r)
// Log
log.Infof("request from client '%s@%s': %s", authReq.Username, address, r.URL)
// Create the director.
director := func(req *http.Request) {
req = r
req.URL.Scheme = "http"
req.URL.Host = Config.destinationAddress
}
// Proxy the request.
proxy := &httputil.ReverseProxy{Director: director}
proxy.ServeHTTP(w, r)
}