Skip to content

Commit

Permalink
Delete some code
Browse files Browse the repository at this point in the history
  • Loading branch information
Hector Maldonado authored and Hector Maldonado committed Apr 10, 2019
1 parent f8b9c7c commit 2d75108
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 22 deletions.
56 changes: 44 additions & 12 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type proxyConfig struct {
username string
password string
targetUrl *url.URL
logLevel string
}


Expand All @@ -23,6 +24,7 @@ func init(){
viper.SetDefault("address","localhost")
viper.SetDefault("username","")
viper.SetDefault("password","")
viper.SetDefault("loglevel","info")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-","_") )
}
Expand All @@ -36,6 +38,7 @@ func parseProxyConfig() (*proxyConfig, error){
username = viper.GetString("username")
password = viper.GetString("password")
targetUrl = viper.GetString("targetUrl")
logLevel = viper.GetString("logLevel")
uri *url.URL
err error
)
Expand Down Expand Up @@ -65,6 +68,7 @@ func parseProxyConfig() (*proxyConfig, error){
username:username,
password:password,
targetUrl:uri,
logLevel:logLevel,

}

Expand Down
29 changes: 25 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,60 @@ import (
"github.com/xynova/ntlm-reverse-proxy/pkg/authenticator"
"fmt"
"os"
"strings"
)



func main() {

//panic("inconceivable")

config, err := parseProxyConfig()
if err != nil {
log.Fatal(err)
}

// Set log level
switch lvl := strings.ToLower(config.logLevel) ; lvl {
case "debug":
log.SetLevel(log.DebugLevel)
case "error":
log.SetLevel(log.ErrorLevel)
default:
log.SetLevel(log.InfoLevel)
}


// 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)
log.Printf("Starting unencrypted listener on %s:",listenAddr)
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{})
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)

// Only log the warning severity or above.
//log.SetLevel(log.WarnLevel)
}
8 changes: 4 additions & 4 deletions pkg/authenticator/ntlm2authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (a *ntlm2Authenticator) execChallengeRequest(url string, roundTripper *http
type1Header := ntlmHeaderValuePrefix + encBase64(a.getNTLM2NegotiateMsg())
msg1Req.Header.Add(authHeaderKey, type1Header)

log.Printf("%x: Negotiate NTML challenge ", roundTripper)
log.Debugf("%x: Negotiate NTML challenge ", roundTripper)
if resp, err = (*roundTripper).RoundTrip(msg1Req); err != nil {
return nil, err
}
Expand All @@ -138,7 +138,7 @@ func (a *ntlm2Authenticator) execChallengeRequest(url string, roundTripper *http
return nil, err
}

log.Printf("%x: Challenge received from server", roundTripper)
log.Debugf("%x: Challenge received from server", roundTripper)
return challengeBytes, nil
}

Expand All @@ -153,7 +153,7 @@ func (a *ntlm2Authenticator) execAuthRequest(url string, userAuthMsg *ntlm.Authe
type3Header := ntlmHeaderValuePrefix + encBase64(userAuthMsg.Bytes())
msg3Req.Header.Set(authHeaderKey, type3Header)

log.Printf("%x: Respond to NTML challenge", roundTripper)
log.Debugf("%x: Respond to NTML challenge", roundTripper)
if resp, err = (*roundTripper).RoundTrip(msg3Req); err != nil {
return false, err
}
Expand All @@ -162,7 +162,7 @@ func (a *ntlm2Authenticator) execAuthRequest(url string, userAuthMsg *ntlm.Authe
}

if resp.StatusCode == http.StatusOK {
log.Printf("%x: Challenge response was successful (%s)", roundTripper, resp.Status)
log.Debugf("%x: Challenge response was successful (%s)", roundTripper, resp.Status)
return true, nil
} else {
log.Warnf("%x: The Challenge response was unsuccessful (%s)", roundTripper, resp.Status)
Expand Down
4 changes: 2 additions & 2 deletions pkg/transport/ntlmTransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (t *ntlmTransport) RoundTrip(req *http.Request) ( *http.Response, error) {
// Try authorize request
if rt.StatusCode == http.StatusUnauthorized {

log.Printf("%x: Try to authenticate connection", &roundTripper)
log.Debugf("%x: Try to authenticate connection", &roundTripper)
// Ensure connection is reused
if err = authenticator.CloseResponseBody(rt); err != nil {
return nil, err
Expand All @@ -84,7 +84,7 @@ func (t *ntlmTransport) RoundTrip(req *http.Request) ( *http.Response, error) {

// Authorization did not succeed, return the first response
if connectionAuthorized {
log.Printf("%x: Connection authorized, re-issuing request", &roundTripper)
log.Debugf("%x: Connection authorized, re-issuing request", &roundTripper)
}


Expand Down

0 comments on commit 2d75108

Please sign in to comment.