2019-07-09 02:08:35 -04:00
|
|
|
package i2pbrowserproxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-07-09 10:07:41 -04:00
|
|
|
"encoding/base64"
|
2019-07-09 02:08:35 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2019-07-09 10:05:11 -04:00
|
|
|
"net/http"
|
2019-07-09 10:07:41 -04:00
|
|
|
"strings"
|
2019-07-09 02:08:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Credentials struct {
|
2019-07-09 10:05:11 -04:00
|
|
|
User string
|
|
|
|
Site string
|
2019-07-09 02:08:35 -04:00
|
|
|
}
|
|
|
|
|
2019-07-09 14:23:39 -04:00
|
|
|
// This part is copied directly from the Go source code https://golang.org/src/net/http/request.go?s=29249:29315#L872
|
|
|
|
// https://golang.org/LICENSE
|
|
|
|
/*
|
|
|
|
Copyright (c) 2009 The Go Authors. All rights reserved.
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are
|
|
|
|
met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the following disclaimer
|
|
|
|
in the documentation and/or other materials provided with the
|
|
|
|
distribution.
|
|
|
|
* Neither the name of Google Inc. nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived from
|
|
|
|
this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-07-09 10:05:11 -04:00
|
|
|
func ProxyBasicAuth(r *http.Request) (username, password string, ok bool) {
|
|
|
|
auth := r.Header.Get("Proxy-Authorization")
|
|
|
|
if auth == "" {
|
|
|
|
return
|
2019-07-09 02:08:35 -04:00
|
|
|
}
|
2019-07-09 10:07:41 -04:00
|
|
|
return parseBasicAuth(auth)
|
2019-07-09 02:08:35 -04:00
|
|
|
}
|
2019-07-09 10:05:11 -04:00
|
|
|
func parseBasicAuth(auth string) (username, password string, ok bool) {
|
|
|
|
const prefix = "Basic "
|
|
|
|
// Case insensitive prefix match. See Issue 22736.
|
|
|
|
if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cs := string(c)
|
|
|
|
s := strings.IndexByte(cs, ':')
|
|
|
|
if s < 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return cs[:s], cs[s+1:], true
|
2019-07-09 02:08:35 -04:00
|
|
|
|
2019-07-09 10:05:11 -04:00
|
|
|
}
|
2019-08-14 15:03:48 -04:00
|
|
|
|
2019-07-09 14:23:39 -04:00
|
|
|
// End copied part
|
|
|
|
|
2019-07-09 02:08:35 -04:00
|
|
|
func DecodeIdentity(body *http.Request) (*http.Request, *Credentials, error) {
|
|
|
|
var creds Credentials
|
|
|
|
bb, err := ioutil.ReadAll(body.Body)
|
|
|
|
if err != nil {
|
2019-08-30 22:44:01 -04:00
|
|
|
return nil, nil, err
|
2019-07-09 02:08:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(body.Method, body.URL.String(), bytes.NewReader(bb))
|
|
|
|
if err != nil {
|
2019-08-30 22:44:01 -04:00
|
|
|
return nil, nil, err
|
2019-07-09 02:08:35 -04:00
|
|
|
}
|
2019-07-09 10:05:11 -04:00
|
|
|
var ok bool
|
|
|
|
creds.User, creds.Site, ok = ProxyBasicAuth(body)
|
|
|
|
if ok {
|
|
|
|
log.Println("OK", creds.User, creds.Site)
|
2019-07-09 02:08:35 -04:00
|
|
|
}
|
2019-08-30 22:44:01 -04:00
|
|
|
creds.User += body.RemoteAddr
|
2019-07-09 02:08:35 -04:00
|
|
|
return req, &creds, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *SAMMultiProxy) Signin(w http.ResponseWriter, r *http.Request) (*samClient, *http.Request) {
|
|
|
|
if m.aggressive {
|
|
|
|
return m.findClient(r.Host), r
|
|
|
|
}
|
|
|
|
r, creds, err := DecodeIdentity(r)
|
|
|
|
if err != nil {
|
|
|
|
if err.Error() == "EOF" {
|
|
|
|
log.Println("No auth parameters passed, falling back to general")
|
|
|
|
return m.clients["general"], r
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return m.findClient(creds.Site), r
|
|
|
|
}
|