-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
68 lines (52 loc) · 1.66 KB
/
main.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
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/xynova/ntlm-reverse-proxy/pkg/authenticator"
"github.com/xynova/ntlm-reverse-proxy/pkg/transport"
"net/http"
"net/http/httputil"
"os"
)
func main() {
//panic("inconceivable")
config, err := parseProxyConfig()
if err != nil {
log.Fatal(err)
}
// Set log level
if config.verboseLogs {
log.SetLevel(log.DebugLevel)
}
// Create NTLM transport
authenticator := authenticator.NewNtlmAuthenticator( config.username, config.password )
transport := transport.NewNtlmTransport(authenticator, transport.DefaultHttpTransportFactory)
// Create reverse proxy with NTLM transport
proxy := httputil.NewSingleHostReverseProxy(config.targetUrl)
proxy.Transport = transport
proxy.ErrorHandler = func(rw http.ResponseWriter, req *http.Request, err error) {
log.Errorf("http: proxy error: %v", err)
rw.WriteHeader(http.StatusBadGateway)
}
// Start server
listenAddr := fmt.Sprintf("%s:%d",config.address,config.port)
if len(config.tlsKeyFile) > 0 {
log.Infof("Starting TLS listener on %s:",listenAddr)
log.Fatal(http.ListenAndServeTLS(listenAddr,config.tlsCertFile, config.tlsKeyFile, http.Handler(proxy)))
}else {
log.Infof("Starting unencrypted listener on %s:",listenAddr)
log.Fatal(http.ListenAndServe( listenAddr, http.Handler(proxy)))
}
}
func init() {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.JSONFormatter{
FieldMap: log.FieldMap{
log.FieldKeyTime: "timestamp",
},
})
log.SetLevel(log.InfoLevel)
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)
}