Skip to content

Commit

Permalink
Merge pull request #4 from fatedier/master
Browse files Browse the repository at this point in the history
update
  • Loading branch information
max0y authored May 31, 2019
2 parents a75bafd + 95444ea commit 04978bb
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 44 deletions.
65 changes: 36 additions & 29 deletions client/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,43 @@ func HandleTcpWorkConnection(localInfo *config.LocalSvrConf, proxyPlugin plugin.
remote = frpIo.WithCompression(remote)
}

// check if we need to send proxy protocol info
var extraInfo []byte
if baseInfo.ProxyProtocolVersion != "" {
if m.SrcAddr != "" && m.SrcPort != 0 {
if m.DstAddr == "" {
m.DstAddr = "127.0.0.1"
}
h := &pp.Header{
Command: pp.PROXY,
SourceAddress: net.ParseIP(m.SrcAddr),
SourcePort: m.SrcPort,
DestinationAddress: net.ParseIP(m.DstAddr),
DestinationPort: m.DstPort,
}

if h.SourceAddress.To16() == nil {
h.TransportProtocol = pp.TCPv4
} else {
h.TransportProtocol = pp.TCPv6
}

if baseInfo.ProxyProtocolVersion == "v1" {
h.Version = 1
} else if baseInfo.ProxyProtocolVersion == "v2" {
h.Version = 2
}

buf := bytes.NewBuffer(nil)
h.WriteTo(buf)
extraInfo = buf.Bytes()
}
}

if proxyPlugin != nil {
// if plugin is set, let plugin handle connections first
workConn.Debug("handle by plugin: %s", proxyPlugin.Name())
proxyPlugin.Handle(remote, workConn)
proxyPlugin.Handle(remote, workConn, extraInfo)
workConn.Debug("handle by plugin finished")
return
} else {
Expand All @@ -520,34 +553,8 @@ func HandleTcpWorkConnection(localInfo *config.LocalSvrConf, proxyPlugin plugin.
workConn.Debug("join connections, localConn(l[%s] r[%s]) workConn(l[%s] r[%s])", localConn.LocalAddr().String(),
localConn.RemoteAddr().String(), workConn.LocalAddr().String(), workConn.RemoteAddr().String())

// check if we need to send proxy protocol info
if baseInfo.ProxyProtocolVersion != "" {
if m.SrcAddr != "" && m.SrcPort != 0 {
if m.DstAddr == "" {
m.DstAddr = "127.0.0.1"
}
h := &pp.Header{
Command: pp.PROXY,
SourceAddress: net.ParseIP(m.SrcAddr),
SourcePort: m.SrcPort,
DestinationAddress: net.ParseIP(m.DstAddr),
DestinationPort: m.DstPort,
}

if h.SourceAddress.To16() == nil {
h.TransportProtocol = pp.TCPv4
} else {
h.TransportProtocol = pp.TCPv6
}

if baseInfo.ProxyProtocolVersion == "v1" {
h.Version = 1
} else if baseInfo.ProxyProtocolVersion == "v2" {
h.Version = 2
}

h.WriteTo(localConn)
}
if len(extraInfo) > 0 {
localConn.Write(extraInfo)
}

frpIo.Join(localConn, remote)
Expand Down
3 changes: 3 additions & 0 deletions conf/frps_full.ini
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,6 @@ subdomain_host = frps.com

# if tcp stream multiplexing is used, default is true
tcp_mux = true

# custom 404 page for HTTP requests
# custom_404_page = /path/to/404.html
6 changes: 6 additions & 0 deletions models/config/server_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type ServerCommonConf struct {
Token string `json:"token"`
SubDomainHost string `json:"subdomain_host"`
TcpMux bool `json:"tcp_mux"`
Custom404Page string `json:"custom_404_page"`

AllowPorts map[int]struct{}
MaxPoolCount int64 `json:"max_pool_count"`
Expand Down Expand Up @@ -104,6 +105,7 @@ func GetDefaultServerConf() *ServerCommonConf {
MaxPortsPerClient: 0,
HeartBeatTimeout: 90,
UserConnTimeout: 10,
Custom404Page: "",
}
}

Expand Down Expand Up @@ -293,6 +295,10 @@ func UnmarshalServerConfFromIni(defaultCfg *ServerCommonConf, content string) (c
cfg.TcpMux = true
}

if tmpStr, ok = conf.Get("common", "custom_404_page"); ok {
cfg.Custom404Page = tmpStr
}

if tmpStr, ok = conf.Get("common", "heartbeat_timeout"); ok {
v, errRet := strconv.ParseInt(tmpStr, 10, 64)
if errRet != nil {
Expand Down
2 changes: 1 addition & 1 deletion models/plugin/http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (hp *HttpProxy) Name() string {
return PluginHttpProxy
}

func (hp *HttpProxy) Handle(conn io.ReadWriteCloser, realConn frpNet.Conn) {
func (hp *HttpProxy) Handle(conn io.ReadWriteCloser, realConn frpNet.Conn, extraBufToLocal []byte) {
wrapConn := frpNet.WrapReadWriteCloserToConn(conn, realConn)

sc, rd := gnet.NewSharedConn(wrapConn)
Expand Down
7 changes: 1 addition & 6 deletions models/plugin/https2http.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,11 @@ func (p *HTTPS2HTTPPlugin) genTLSConfig() (*tls.Config, error) {
return config, nil
}

func (p *HTTPS2HTTPPlugin) Handle(conn io.ReadWriteCloser, realConn frpNet.Conn) {
func (p *HTTPS2HTTPPlugin) Handle(conn io.ReadWriteCloser, realConn frpNet.Conn, extraBufToLocal []byte) {
wrapConn := frpNet.WrapReadWriteCloserToConn(conn, realConn)
p.l.PutConn(wrapConn)
}

func (p *HTTPS2HTTPPlugin) handleRequest(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
return
}

func (p *HTTPS2HTTPPlugin) Name() string {
return PluginHTTPS2HTTP
}
Expand Down
2 changes: 1 addition & 1 deletion models/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Create(name string, params map[string]string) (p Plugin, err error) {

type Plugin interface {
Name() string
Handle(conn io.ReadWriteCloser, realConn frpNet.Conn)
Handle(conn io.ReadWriteCloser, realConn frpNet.Conn, extraBufToLocal []byte)
Close() error
}

Expand Down
2 changes: 1 addition & 1 deletion models/plugin/socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewSocks5Plugin(params map[string]string) (p Plugin, err error) {
return
}

func (sp *Socks5Plugin) Handle(conn io.ReadWriteCloser, realConn frpNet.Conn) {
func (sp *Socks5Plugin) Handle(conn io.ReadWriteCloser, realConn frpNet.Conn, extraBufToLocal []byte) {
defer conn.Close()
wrapConn := frpNet.WrapReadWriteCloserToConn(conn, realConn)
sp.Server.ServeConn(wrapConn)
Expand Down
2 changes: 1 addition & 1 deletion models/plugin/static_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewStaticFilePlugin(params map[string]string) (Plugin, error) {
return sp, nil
}

func (sp *StaticFilePlugin) Handle(conn io.ReadWriteCloser, realConn frpNet.Conn) {
func (sp *StaticFilePlugin) Handle(conn io.ReadWriteCloser, realConn frpNet.Conn, extraBufToLocal []byte) {
wrapConn := frpNet.WrapReadWriteCloserToConn(conn, realConn)
sp.l.PutConn(wrapConn)
}
Expand Down
5 changes: 4 additions & 1 deletion models/plugin/unix_domain_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ func NewUnixDomainSocketPlugin(params map[string]string) (p Plugin, err error) {
return
}

func (uds *UnixDomainSocketPlugin) Handle(conn io.ReadWriteCloser, realConn frpNet.Conn) {
func (uds *UnixDomainSocketPlugin) Handle(conn io.ReadWriteCloser, realConn frpNet.Conn, extraBufToLocal []byte) {
localConn, err := net.DialUnix("unix", nil, uds.UnixAddr)
if err != nil {
return
}
if len(extraBufToLocal) > 0 {
localConn.Write(extraBufToLocal)
}

frpIo.Join(localConn, conn)
}
Expand Down
1 change: 1 addition & 0 deletions server/dashboard_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ func (svr *Service) getProxyStatsByTypeAndName(proxyType string, proxyName strin
proxyInfo.CurConns = ps.CurConns
proxyInfo.LastStartTime = ps.LastStartTime
proxyInfo.LastCloseTime = ps.LastCloseTime
code = 200
}

return
Expand Down
3 changes: 3 additions & 0 deletions server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ func NewService() (svr *Service, err error) {
return
}

// Init 404 not found page
vhost.NotFoundPagePath = cfg.Custom404Page

var (
httpMuxOn bool
httpsMuxOn bool
Expand Down
2 changes: 1 addition & 1 deletion utils/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"strings"
)

var version string = "0.26.0"
var version string = "0.27.0"

func Full() string {
return version
Expand Down
27 changes: 25 additions & 2 deletions utils/vhost/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
package vhost

import (
"bytes"
"io/ioutil"
"net/http"
"strings"

frpLog "github.com/fatedier/frp/utils/log"
"github.com/fatedier/frp/utils/version"
)

var (
NotFoundPagePath = ""
)

const (
NotFound = `<!DOCTYPE html>
<html>
Expand All @@ -46,18 +51,36 @@ Please try again later.</p>
`
)

func getNotFoundPageContent() []byte {
var (
buf []byte
err error
)
if NotFoundPagePath != "" {
buf, err = ioutil.ReadFile(NotFoundPagePath)
if err != nil {
frpLog.Warn("read custom 404 page error: %v", err)
buf = []byte(NotFound)
}
} else {
buf = []byte(NotFound)
}
return buf
}

func notFoundResponse() *http.Response {
header := make(http.Header)
header.Set("server", "frp/"+version.Full())
header.Set("Content-Type", "text/html")

res := &http.Response{
Status: "Not Found",
StatusCode: 404,
Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
Header: header,
Body: ioutil.NopCloser(strings.NewReader(NotFound)),
Body: ioutil.NopCloser(bytes.NewReader(getNotFoundPageContent())),
}
return res
}
Expand Down
3 changes: 2 additions & 1 deletion utils/vhost/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ func (p *ReverseProxy) serveHTTP(rw http.ResponseWriter, req *http.Request) {
if err != nil {
p.logf("http: proxy error: %v", err)
rw.WriteHeader(http.StatusNotFound)
rw.Write([]byte(NotFound))

rw.Write(getNotFoundPageContent())
return
}

Expand Down

0 comments on commit 04978bb

Please sign in to comment.