mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-17 14:57:44 -05:00
fix http basic auth. fix #659
This commit is contained in:
parent
02685094d3
commit
100391fdb7
@ -3,6 +3,7 @@ package http
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -69,6 +70,23 @@ func isTimeout(err error) bool {
|
|||||||
return ok && nerr.Timeout()
|
return ok && nerr.Timeout()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseBasicAuth(auth string) (username, password string, ok bool) {
|
||||||
|
const prefix = "Basic "
|
||||||
|
if !strings.HasPrefix(auth, 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
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher dispatcher.Interface) error {
|
func (s *Server) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher dispatcher.Interface) error {
|
||||||
reader := bufio.NewReaderSize(conn, 2048)
|
reader := bufio.NewReaderSize(conn, 2048)
|
||||||
|
|
||||||
@ -85,7 +103,7 @@ Start:
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(s.config.Accounts) > 0 {
|
if len(s.config.Accounts) > 0 {
|
||||||
user, pass, ok := request.BasicAuth()
|
user, pass, ok := parseBasicAuth(request.Header.Get("Proxy-Authorization"))
|
||||||
if !ok || !s.config.HasAccount(user, pass) {
|
if !ok || !s.config.HasAccount(user, pass) {
|
||||||
_, err := conn.Write([]byte("HTTP/1.1 401 UNAUTHORIZED\r\n\r\n"))
|
_, err := conn.Write([]byte("HTTP/1.1 401 UNAUTHORIZED\r\n\r\n"))
|
||||||
return err
|
return err
|
||||||
|
@ -73,6 +73,12 @@ func TestHttpConformance(t *testing.T) {
|
|||||||
CloseAllServers(servers)
|
CloseAllServers(servers)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setProxyBasicAuth(req *http.Request, user, pass string) {
|
||||||
|
req.SetBasicAuth(user, pass)
|
||||||
|
req.Header.Set("Proxy-Authorization", req.Header.Get("Authorization"))
|
||||||
|
req.Header.Del("Authorization")
|
||||||
|
}
|
||||||
|
|
||||||
func TestHttpBasicAuth(t *testing.T) {
|
func TestHttpBasicAuth(t *testing.T) {
|
||||||
assert := With(t)
|
assert := With(t)
|
||||||
|
|
||||||
@ -131,7 +137,7 @@ func TestHttpBasicAuth(t *testing.T) {
|
|||||||
req, err := http.NewRequest("GET", "http://127.0.0.1:"+httpServerPort.String(), nil)
|
req, err := http.NewRequest("GET", "http://127.0.0.1:"+httpServerPort.String(), nil)
|
||||||
assert(err, IsNil)
|
assert(err, IsNil)
|
||||||
|
|
||||||
req.SetBasicAuth("a", "c")
|
setProxyBasicAuth(req, "a", "c")
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
assert(err, IsNil)
|
assert(err, IsNil)
|
||||||
assert(resp.StatusCode, Equals, 401)
|
assert(resp.StatusCode, Equals, 401)
|
||||||
@ -141,7 +147,7 @@ func TestHttpBasicAuth(t *testing.T) {
|
|||||||
req, err := http.NewRequest("GET", "http://127.0.0.1:"+httpServerPort.String(), nil)
|
req, err := http.NewRequest("GET", "http://127.0.0.1:"+httpServerPort.String(), nil)
|
||||||
assert(err, IsNil)
|
assert(err, IsNil)
|
||||||
|
|
||||||
req.SetBasicAuth("a", "b")
|
setProxyBasicAuth(req, "a", "b")
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
assert(err, IsNil)
|
assert(err, IsNil)
|
||||||
assert(resp.StatusCode, Equals, 200)
|
assert(resp.StatusCode, Equals, 200)
|
||||||
|
Loading…
Reference in New Issue
Block a user