mirror of
https://github.com/fatedier/frp.git
synced 2025-01-22 17:42:09 +00:00
format
This commit is contained in:
parent
6742fa2ea8
commit
70e2aee46d
@ -175,7 +175,7 @@ func (hp *HttpProxy) ConnectHandler(rw http.ResponseWriter, req *http.Request) {
|
|||||||
client.Close()
|
client.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
client.Write([]byte("HTTP/1.0 200 OK\r\n\r\n"))
|
client.Write([]byte("HTTP/1.1 200 OK\r\n\r\n"))
|
||||||
|
|
||||||
go frpIo.Join(remote, client)
|
go frpIo.Join(remote, client)
|
||||||
}
|
}
|
||||||
|
@ -15,9 +15,12 @@
|
|||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fatedier/frp/utils/log"
|
"github.com/fatedier/frp/utils/log"
|
||||||
@ -48,6 +51,13 @@ type WrapReadWriteCloserConn struct {
|
|||||||
log.Logger
|
log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WrapReadWriteCloserToConn(rwc io.ReadWriteCloser) Conn {
|
||||||
|
return &WrapReadWriteCloserConn{
|
||||||
|
ReadWriteCloser: rwc,
|
||||||
|
Logger: log.NewPrefixLogger(""),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (conn *WrapReadWriteCloserConn) LocalAddr() net.Addr {
|
func (conn *WrapReadWriteCloserConn) LocalAddr() net.Addr {
|
||||||
return (*net.TCPAddr)(nil)
|
return (*net.TCPAddr)(nil)
|
||||||
}
|
}
|
||||||
@ -57,47 +67,15 @@ func (conn *WrapReadWriteCloserConn) RemoteAddr() net.Addr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (conn *WrapReadWriteCloserConn) SetDeadline(t time.Time) error {
|
func (conn *WrapReadWriteCloserConn) SetDeadline(t time.Time) error {
|
||||||
return nil
|
return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *WrapReadWriteCloserConn) SetReadDeadline(t time.Time) error {
|
func (conn *WrapReadWriteCloserConn) SetReadDeadline(t time.Time) error {
|
||||||
return nil
|
return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *WrapReadWriteCloserConn) SetWriteDeadline(t time.Time) error {
|
func (conn *WrapReadWriteCloserConn) SetWriteDeadline(t time.Time) error {
|
||||||
return nil
|
return &net.OpError{Op: "set", Net: "wrap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
|
||||||
}
|
|
||||||
|
|
||||||
func WrapReadWriteCloserToConn(rwc io.ReadWriteCloser) Conn {
|
|
||||||
return &WrapReadWriteCloserConn{
|
|
||||||
ReadWriteCloser: rwc,
|
|
||||||
Logger: log.NewPrefixLogger(""),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Listener interface {
|
|
||||||
Accept() (Conn, error)
|
|
||||||
Close() error
|
|
||||||
log.Logger
|
|
||||||
}
|
|
||||||
|
|
||||||
type LogListener struct {
|
|
||||||
l net.Listener
|
|
||||||
net.Listener
|
|
||||||
log.Logger
|
|
||||||
}
|
|
||||||
|
|
||||||
func WrapLogListener(l net.Listener) Listener {
|
|
||||||
return &LogListener{
|
|
||||||
l: l,
|
|
||||||
Listener: l,
|
|
||||||
Logger: log.NewPrefixLogger(""),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (logL *LogListener) Accept() (Conn, error) {
|
|
||||||
c, err := logL.l.Accept()
|
|
||||||
return WrapConn(c), err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConnectServer(protocol string, addr string) (c Conn, err error) {
|
func ConnectServer(protocol string, addr string) (c Conn, err error) {
|
||||||
@ -136,3 +114,45 @@ func ConnectServerByHttpProxy(httpProxy string, protocol string, addr string) (c
|
|||||||
return nil, fmt.Errorf("unsupport protocol: %s", protocol)
|
return nil, fmt.Errorf("unsupport protocol: %s", protocol)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SharedConn struct {
|
||||||
|
Conn
|
||||||
|
sync.Mutex
|
||||||
|
buf *bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
// the bytes you read in io.Reader, will be reserved in SharedConn
|
||||||
|
func NewShareConn(conn Conn) (*SharedConn, io.Reader) {
|
||||||
|
sc := &SharedConn{
|
||||||
|
Conn: conn,
|
||||||
|
buf: bytes.NewBuffer(make([]byte, 0, 1024)),
|
||||||
|
}
|
||||||
|
return sc, io.TeeReader(conn, sc.buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sc *SharedConn) Read(p []byte) (n int, err error) {
|
||||||
|
sc.Lock()
|
||||||
|
if sc.buf == nil {
|
||||||
|
sc.Unlock()
|
||||||
|
return sc.Conn.Read(p)
|
||||||
|
}
|
||||||
|
sc.Unlock()
|
||||||
|
n, err = sc.buf.Read(p)
|
||||||
|
|
||||||
|
if err == io.EOF {
|
||||||
|
sc.Lock()
|
||||||
|
sc.buf = nil
|
||||||
|
sc.Unlock()
|
||||||
|
var n2 int
|
||||||
|
n2, err = sc.Conn.Read(p[n:])
|
||||||
|
|
||||||
|
n += n2
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sc *SharedConn) WriteBuff(buffer []byte) (err error) {
|
||||||
|
sc.buf.Reset()
|
||||||
|
_, err = sc.buf.Write(buffer)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
46
utils/net/listener.go
Normal file
46
utils/net/listener.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
// Copyright 2017 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 net
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/fatedier/frp/utils/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Listener interface {
|
||||||
|
Accept() (Conn, error)
|
||||||
|
Close() error
|
||||||
|
log.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
type LogListener struct {
|
||||||
|
l net.Listener
|
||||||
|
net.Listener
|
||||||
|
log.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func WrapLogListener(l net.Listener) Listener {
|
||||||
|
return &LogListener{
|
||||||
|
l: l,
|
||||||
|
Listener: l,
|
||||||
|
Logger: log.NewPrefixLogger(""),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logL *LogListener) Accept() (Conn, error) {
|
||||||
|
c, err := logL.l.Accept()
|
||||||
|
return WrapConn(c), err
|
||||||
|
}
|
@ -35,7 +35,7 @@ type HttpMuxer struct {
|
|||||||
|
|
||||||
func GetHttpRequestInfo(c frpNet.Conn) (_ frpNet.Conn, _ map[string]string, err error) {
|
func GetHttpRequestInfo(c frpNet.Conn) (_ frpNet.Conn, _ map[string]string, err error) {
|
||||||
reqInfoMap := make(map[string]string, 0)
|
reqInfoMap := make(map[string]string, 0)
|
||||||
sc, rd := newShareConn(c)
|
sc, rd := frpNet.NewShareConn(c)
|
||||||
|
|
||||||
request, err := http.ReadRequest(bufio.NewReader(rd))
|
request, err := http.ReadRequest(bufio.NewReader(rd))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -62,7 +62,7 @@ func NewHttpMuxer(listener frpNet.Listener, timeout time.Duration) (*HttpMuxer,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func HttpHostNameRewrite(c frpNet.Conn, rewriteHost string) (_ frpNet.Conn, err error) {
|
func HttpHostNameRewrite(c frpNet.Conn, rewriteHost string) (_ frpNet.Conn, err error) {
|
||||||
sc, rd := newShareConn(c)
|
sc, rd := frpNet.NewShareConn(c)
|
||||||
var buff []byte
|
var buff []byte
|
||||||
if buff, err = hostNameRewrite(rd, rewriteHost); err != nil {
|
if buff, err = hostNameRewrite(rd, rewriteHost); err != nil {
|
||||||
return sc, err
|
return sc, err
|
||||||
|
@ -179,7 +179,7 @@ func readHandshake(rd io.Reader) (host string, err error) {
|
|||||||
|
|
||||||
func GetHttpsHostname(c frpNet.Conn) (sc frpNet.Conn, _ map[string]string, err error) {
|
func GetHttpsHostname(c frpNet.Conn) (sc frpNet.Conn, _ map[string]string, err error) {
|
||||||
reqInfoMap := make(map[string]string, 0)
|
reqInfoMap := make(map[string]string, 0)
|
||||||
sc, rd := newShareConn(c)
|
sc, rd := frpNet.NewShareConn(c)
|
||||||
host, err := readHandshake(rd)
|
host, err := readHandshake(rd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return sc, reqInfoMap, err
|
return sc, reqInfoMap, err
|
||||||
|
@ -13,9 +13,7 @@
|
|||||||
package vhost
|
package vhost
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -209,45 +207,3 @@ func (l *Listener) Close() error {
|
|||||||
func (l *Listener) Name() string {
|
func (l *Listener) Name() string {
|
||||||
return l.name
|
return l.name
|
||||||
}
|
}
|
||||||
|
|
||||||
type sharedConn struct {
|
|
||||||
frpNet.Conn
|
|
||||||
sync.Mutex
|
|
||||||
buff *bytes.Buffer
|
|
||||||
}
|
|
||||||
|
|
||||||
// the bytes you read in io.Reader, will be reserved in sharedConn
|
|
||||||
func newShareConn(conn frpNet.Conn) (*sharedConn, io.Reader) {
|
|
||||||
sc := &sharedConn{
|
|
||||||
Conn: conn,
|
|
||||||
buff: bytes.NewBuffer(make([]byte, 0, 1024)),
|
|
||||||
}
|
|
||||||
return sc, io.TeeReader(conn, sc.buff)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sc *sharedConn) Read(p []byte) (n int, err error) {
|
|
||||||
sc.Lock()
|
|
||||||
if sc.buff == nil {
|
|
||||||
sc.Unlock()
|
|
||||||
return sc.Conn.Read(p)
|
|
||||||
}
|
|
||||||
sc.Unlock()
|
|
||||||
n, err = sc.buff.Read(p)
|
|
||||||
|
|
||||||
if err == io.EOF {
|
|
||||||
sc.Lock()
|
|
||||||
sc.buff = nil
|
|
||||||
sc.Unlock()
|
|
||||||
var n2 int
|
|
||||||
n2, err = sc.Conn.Read(p[n:])
|
|
||||||
|
|
||||||
n += n2
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sc *sharedConn) WriteBuff(buffer []byte) (err error) {
|
|
||||||
sc.buff.Reset()
|
|
||||||
_, err = sc.buff.Write(buffer)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user