Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-russkikh committed Sep 14, 2024
2 parents c57adbc + 4a564b5 commit b710017
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
36 changes: 20 additions & 16 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ func (s *HTTPServer) authenticate(req *http.Request) (int, error) {
}

auth := req.Header.Get(proxyAuthHeaderKey)
if auth != "" {
enc := strings.TrimPrefix(auth, "Basic ")
str, err := base64.StdEncoding.DecodeString(enc)
if err != nil {
return http.StatusNotAcceptable, fmt.Errorf("decode username and password failed: %w", err)
}
pairs := bytes.SplitN(str, []byte(":"), 2)
if len(pairs) != 2 {
return http.StatusLengthRequired, fmt.Errorf("username and password format invalid")
}
if s.auth.Valid(string(pairs[0]), string(pairs[1])) {
return 0, nil
}
return http.StatusUnauthorized, fmt.Errorf("username and password not matching")
if auth == "" {
return http.StatusProxyAuthRequired, errors.New(http.StatusText(http.StatusProxyAuthRequired))
}

return http.StatusProxyAuthRequired, errors.New(http.StatusText(http.StatusProxyAuthRequired))
enc := strings.TrimPrefix(auth, "Basic ")
str, err := base64.StdEncoding.DecodeString(enc)
if err != nil {
return http.StatusNotAcceptable, fmt.Errorf("decode username and password failed: %w", err)
}
pairs := bytes.SplitN(str, []byte(":"), 2)
if len(pairs) != 2 {
return http.StatusLengthRequired, fmt.Errorf("username and password format invalid")
}
if s.auth.Valid(string(pairs[0]), string(pairs[1])) {
return 0, nil
}
return http.StatusUnauthorized, fmt.Errorf("username and password not matching")
}

func (s *HTTPServer) handleConn(req *http.Request, conn net.Conn) (peer net.Conn, err error) {
Expand Down Expand Up @@ -104,7 +104,11 @@ func (s *HTTPServer) serve(conn net.Conn) {

code, err := s.authenticate(req)
if err != nil {
_ = responseWith(req, code).Write(conn)
resp := responseWith(req, code)
if code == http.StatusProxyAuthRequired {
resp.Header.Set("Proxy-Authenticate", "Basic realm=\"Proxy\"")
}
_ = resp.Write(conn)
log.Println(err)
return
}
Expand Down
2 changes: 1 addition & 1 deletion systemd/wireproxy.service
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ RestrictAddressFamilies=AF_INET AF_INET6 AF_NETLINK
RestrictNamespaces=true
RestrictRealtime=true
SystemCallArchitectures=native
SystemCallFilter=@system-service
SystemCallFilter=@system-service @sandbox

[Install]
WantedBy=multi-user.target

0 comments on commit b710017

Please sign in to comment.