2016-06-24 15:43:58 +08:00
|
|
|
// Copyright 2016 fatedier, fatedier@gmail.com
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package vhost
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2016-07-24 15:00:18 +08:00
|
|
|
"bytes"
|
2016-08-29 10:53:02 +08:00
|
|
|
"encoding/base64"
|
2016-07-24 15:00:18 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-06-24 15:43:58 +08:00
|
|
|
"net/http"
|
2016-07-24 15:00:18 +08:00
|
|
|
"net/url"
|
2016-06-24 15:43:58 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
frpNet "github.com/fatedier/frp/utils/net"
|
|
|
|
"github.com/fatedier/frp/utils/pool"
|
2016-06-24 15:43:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type HttpMuxer struct {
|
|
|
|
*VhostMuxer
|
|
|
|
}
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
func GetHttpRequestInfo(c frpNet.Conn) (_ frpNet.Conn, _ map[string]string, err error) {
|
2016-08-29 10:53:02 +08:00
|
|
|
reqInfoMap := make(map[string]string, 0)
|
2017-06-09 01:33:57 +08:00
|
|
|
sc, rd := frpNet.NewShareConn(c)
|
2016-06-24 15:43:58 +08:00
|
|
|
|
|
|
|
request, err := http.ReadRequest(bufio.NewReader(rd))
|
|
|
|
if err != nil {
|
2016-08-29 10:53:02 +08:00
|
|
|
return sc, reqInfoMap, err
|
2016-06-24 15:43:58 +08:00
|
|
|
}
|
2016-08-29 10:53:02 +08:00
|
|
|
// hostName
|
2016-06-24 15:43:58 +08:00
|
|
|
tmpArr := strings.Split(request.Host, ":")
|
2016-08-29 10:53:02 +08:00
|
|
|
reqInfoMap["Host"] = tmpArr[0]
|
2016-12-18 22:40:58 +08:00
|
|
|
reqInfoMap["Path"] = request.URL.Path
|
|
|
|
reqInfoMap["Scheme"] = request.URL.Scheme
|
2016-08-29 10:53:02 +08:00
|
|
|
|
|
|
|
// Authorization
|
|
|
|
authStr := request.Header.Get("Authorization")
|
|
|
|
if authStr != "" {
|
|
|
|
reqInfoMap["Authorization"] = authStr
|
|
|
|
}
|
2016-06-24 15:43:58 +08:00
|
|
|
request.Body.Close()
|
2016-08-29 10:53:02 +08:00
|
|
|
return sc, reqInfoMap, nil
|
2016-06-24 15:43:58 +08:00
|
|
|
}
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
func NewHttpMuxer(listener frpNet.Listener, timeout time.Duration) (*HttpMuxer, error) {
|
2017-07-01 01:54:37 +08:00
|
|
|
mux, err := NewVhostMuxer(listener, GetHttpRequestInfo, HttpAuthFunc, ModifyHttpRequest, timeout)
|
2016-06-24 15:43:58 +08:00
|
|
|
return &HttpMuxer{mux}, err
|
|
|
|
}
|
2016-07-24 15:00:18 +08:00
|
|
|
|
2017-07-01 01:54:37 +08:00
|
|
|
func ModifyHttpRequest(c frpNet.Conn, rewriteHost string) (_ frpNet.Conn, err error) {
|
2017-06-09 01:33:57 +08:00
|
|
|
sc, rd := frpNet.NewShareConn(c)
|
2016-07-24 15:00:18 +08:00
|
|
|
var buff []byte
|
2017-07-01 01:54:37 +08:00
|
|
|
remoteIP := strings.Split(c.RemoteAddr().String(), ":")[0]
|
|
|
|
if buff, err = hostNameRewrite(rd, rewriteHost, remoteIP); err != nil {
|
2016-07-24 15:00:18 +08:00
|
|
|
return sc, err
|
|
|
|
}
|
|
|
|
err = sc.WriteBuff(buff)
|
|
|
|
return sc, err
|
|
|
|
}
|
|
|
|
|
2017-07-01 01:54:37 +08:00
|
|
|
func hostNameRewrite(request io.Reader, rewriteHost string, remoteIP string) (_ []byte, err error) {
|
2016-07-31 03:26:41 +08:00
|
|
|
buf := pool.GetBuf(1024)
|
|
|
|
defer pool.PutBuf(buf)
|
|
|
|
|
|
|
|
request.Read(buf)
|
2017-07-01 01:54:37 +08:00
|
|
|
retBuffer, err := parseRequest(buf, rewriteHost, remoteIP)
|
2016-07-24 15:00:18 +08:00
|
|
|
return retBuffer, err
|
|
|
|
}
|
|
|
|
|
2017-07-01 01:54:37 +08:00
|
|
|
func parseRequest(org []byte, rewriteHost string, remoteIP string) (ret []byte, err error) {
|
2016-07-24 15:00:18 +08:00
|
|
|
tp := bytes.NewBuffer(org)
|
|
|
|
// First line: GET /index.html HTTP/1.0
|
|
|
|
var b []byte
|
|
|
|
if b, err = tp.ReadBytes('\n'); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req := new(http.Request)
|
2016-07-26 00:18:19 +08:00
|
|
|
// we invoked ReadRequest in GetHttpHostname before, so we ignore error
|
2016-07-24 15:00:18 +08:00
|
|
|
req.Method, req.RequestURI, req.Proto, _ = parseRequestLine(string(b))
|
|
|
|
rawurl := req.RequestURI
|
2016-07-26 00:18:19 +08:00
|
|
|
// CONNECT www.google.com:443 HTTP/1.1
|
2016-07-24 15:00:18 +08:00
|
|
|
justAuthority := req.Method == "CONNECT" && !strings.HasPrefix(rawurl, "/")
|
|
|
|
if justAuthority {
|
|
|
|
rawurl = "http://" + rawurl
|
|
|
|
}
|
|
|
|
req.URL, _ = url.ParseRequestURI(rawurl)
|
|
|
|
if justAuthority {
|
|
|
|
// Strip the bogus "http://" back off.
|
|
|
|
req.URL.Scheme = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// RFC2616: first case
|
|
|
|
// GET /index.html HTTP/1.1
|
|
|
|
// Host: www.google.com
|
|
|
|
if req.URL.Host == "" {
|
2017-07-01 01:54:37 +08:00
|
|
|
var changedBuf []byte
|
|
|
|
if rewriteHost != "" {
|
|
|
|
changedBuf, err = changeHostName(tp, rewriteHost)
|
|
|
|
}
|
2016-07-24 15:00:18 +08:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.Write(b)
|
2017-07-01 01:54:37 +08:00
|
|
|
buf.WriteString(fmt.Sprintf("X-Forwarded-For: %s\n", remoteIP))
|
|
|
|
buf.WriteString(fmt.Sprintf("X-Real-IP: %s\n", remoteIP))
|
|
|
|
if len(changedBuf) == 0 {
|
|
|
|
tp.WriteTo(buf)
|
|
|
|
} else {
|
|
|
|
buf.Write(changedBuf)
|
|
|
|
}
|
2016-07-24 15:00:18 +08:00
|
|
|
return buf.Bytes(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RFC2616: second case
|
|
|
|
// GET http://www.google.com/index.html HTTP/1.1
|
|
|
|
// Host: doesntmatter
|
|
|
|
// In this case, any Host line is ignored.
|
2017-07-01 01:54:37 +08:00
|
|
|
if rewriteHost != "" {
|
|
|
|
hostPort := strings.Split(req.URL.Host, ":")
|
|
|
|
if len(hostPort) == 1 {
|
|
|
|
req.URL.Host = rewriteHost
|
|
|
|
} else if len(hostPort) == 2 {
|
|
|
|
req.URL.Host = fmt.Sprintf("%s:%s", rewriteHost, hostPort[1])
|
|
|
|
}
|
2016-07-26 00:18:19 +08:00
|
|
|
}
|
2016-07-24 15:00:18 +08:00
|
|
|
firstLine := req.Method + " " + req.URL.String() + " " + req.Proto
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.WriteString(firstLine)
|
2017-07-01 01:54:37 +08:00
|
|
|
buf.WriteString(fmt.Sprintf("X-Forwarded-For: %s\n", remoteIP))
|
|
|
|
buf.WriteString(fmt.Sprintf("X-Real-IP: %s\n", remoteIP))
|
2016-07-24 15:00:18 +08:00
|
|
|
tp.WriteTo(buf)
|
|
|
|
return buf.Bytes(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseRequestLine parses "GET /foo HTTP/1.1" into its three parts.
|
|
|
|
func parseRequestLine(line string) (method, requestURI, proto string, ok bool) {
|
|
|
|
s1 := strings.Index(line, " ")
|
|
|
|
s2 := strings.Index(line[s1+1:], " ")
|
|
|
|
if s1 < 0 || s2 < 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s2 += s1 + 1
|
|
|
|
return line[:s1], line[s1+1 : s2], line[s2+1:], true
|
|
|
|
}
|
|
|
|
|
2016-07-26 00:18:19 +08:00
|
|
|
func changeHostName(buff *bytes.Buffer, rewriteHost string) (_ []byte, err error) {
|
2016-07-24 15:00:18 +08:00
|
|
|
retBuf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
peek := buff.Bytes()
|
|
|
|
for len(peek) > 0 {
|
|
|
|
i := bytes.IndexByte(peek, '\n')
|
|
|
|
if i < 3 {
|
|
|
|
// Not present (-1) or found within the next few bytes,
|
|
|
|
// implying we're at the end ("\r\n\r\n" or "\n\n")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
kv := peek[:i]
|
|
|
|
j := bytes.IndexByte(kv, ':')
|
|
|
|
if j < 0 {
|
|
|
|
return nil, fmt.Errorf("malformed MIME header line: " + string(kv))
|
|
|
|
}
|
|
|
|
if strings.Contains(strings.ToLower(string(kv[:j])), "host") {
|
2016-07-26 00:18:19 +08:00
|
|
|
var hostHeader string
|
|
|
|
portPos := bytes.IndexByte(kv[j+1:], ':')
|
|
|
|
if portPos == -1 {
|
|
|
|
hostHeader = fmt.Sprintf("Host: %s\n", rewriteHost)
|
|
|
|
} else {
|
2017-07-01 01:54:37 +08:00
|
|
|
hostHeader = fmt.Sprintf("Host: %s:%s\n", rewriteHost, kv[j+portPos+2:])
|
2016-07-26 00:18:19 +08:00
|
|
|
}
|
2016-07-24 15:00:18 +08:00
|
|
|
retBuf.WriteString(hostHeader)
|
|
|
|
peek = peek[i+1:]
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
retBuf.Write(peek[:i])
|
|
|
|
retBuf.WriteByte('\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
peek = peek[i+1:]
|
|
|
|
}
|
|
|
|
retBuf.Write(peek)
|
|
|
|
return retBuf.Bytes(), err
|
|
|
|
}
|
2016-08-29 10:53:02 +08:00
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
func HttpAuthFunc(c frpNet.Conn, userName, passWord, authorization string) (bAccess bool, err error) {
|
2016-08-29 10:53:02 +08:00
|
|
|
s := strings.SplitN(authorization, " ", 2)
|
|
|
|
if len(s) != 2 {
|
|
|
|
res := noAuthResponse()
|
2017-03-09 02:03:47 +08:00
|
|
|
res.Write(c)
|
2016-08-29 10:53:02 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
b, err := base64.StdEncoding.DecodeString(s[1])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pair := strings.SplitN(string(b), ":", 2)
|
|
|
|
if len(pair) != 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if pair[0] != userName || pair[1] != passWord {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func noAuthResponse() *http.Response {
|
|
|
|
header := make(map[string][]string)
|
|
|
|
header["WWW-Authenticate"] = []string{`Basic realm="Restricted"`}
|
|
|
|
res := &http.Response{
|
|
|
|
Status: "401 Not authorized",
|
|
|
|
StatusCode: 401,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
Header: header,
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|