add http node settings

This commit is contained in:
ginuerzh
2023-01-29 20:31:13 +08:00
parent 24037aba7b
commit 66234239e4
8 changed files with 120 additions and 26 deletions

View File

@ -1,15 +1,19 @@
package local
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/http/httputil"
"time"
"github.com/go-gost/core/chain"
"github.com/go-gost/core/handler"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
netpkg "github.com/go-gost/x/internal/net"
"github.com/go-gost/x/internal/util/forward"
@ -132,6 +136,31 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
marker.Reset()
}
if protocol == forward.ProtoHTTP &&
target.Options().HTTP != nil {
req, err := http.ReadRequest(bufio.NewReader(rw))
if err != nil {
return err
}
httpSettings := target.Options().HTTP
if httpSettings.Host != "" {
req.Host = httpSettings.Host
}
for k, v := range httpSettings.Header {
req.Header.Set(k, v)
}
if log.IsLevelEnabled(logger.TraceLevel) {
dump, _ := httputil.DumpRequest(req, false)
log.Trace(string(dump))
}
if err := req.Write(cc); err != nil {
return err
}
}
t := time.Now()
log.Debugf("%s <-> %s", conn.RemoteAddr(), target.Addr)
netpkg.Transport(rw, cc)

View File

@ -1,15 +1,19 @@
package remote
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/http/httputil"
"time"
"github.com/go-gost/core/chain"
"github.com/go-gost/core/handler"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
netpkg "github.com/go-gost/x/internal/net"
"github.com/go-gost/x/internal/util/forward"
@ -124,6 +128,30 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
marker.Reset()
}
if protocol == forward.ProtoHTTP &&
target.Options().HTTP != nil {
req, err := http.ReadRequest(bufio.NewReader(rw))
if err != nil {
return err
}
httpSettings := target.Options().HTTP
if httpSettings.Host != "" {
req.Host = httpSettings.Host
}
for k, v := range httpSettings.Header {
req.Header.Set(k, v)
}
if log.IsLevelEnabled(logger.TraceLevel) {
dump, _ := httputil.DumpRequest(req, false)
log.Trace(string(dump))
}
if err := req.Write(cc); err != nil {
return err
}
}
t := time.Now()
log.Debugf("%s <-> %s", conn.RemoteAddr(), target.Addr)
netpkg.Transport(rw, cc)

View File

@ -199,11 +199,13 @@ func (h *relayHandler) handleTunnel(ctx context.Context, conn net.Conn, tunnelID
Status: relay.StatusOK,
}
if h.ep == nil {
resp.Status = relay.StatusServiceUnavailable
resp.WriteTo(conn)
return
}
/*
if h.ep == nil {
resp.Status = relay.StatusServiceUnavailable
resp.WriteTo(conn)
return
}
*/
uuid, err := uuid.NewRandom()
if err != nil {
@ -214,8 +216,12 @@ func (h *relayHandler) handleTunnel(ctx context.Context, conn net.Conn, tunnelID
connectorID := relay.NewTunnelID(uuid[:])
addr := ":0"
if h.ep != nil {
addr = h.ep.Addr().String()
}
af := &relay.AddrFeature{}
err = af.ParseFrom(h.ep.Addr().String())
err = af.ParseFrom(addr)
if err != nil {
log.Warn(err)
}

View File

@ -123,9 +123,8 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
var tunnelID relay.TunnelID
defer func() {
if tunnelID.IsZero() || err != nil {
if err != nil {
conn.Close()
}
log.WithFields(map[string]any{
@ -161,6 +160,7 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
var user, pass string
var address string
var tunnelID relay.TunnelID
for _, f := range req.Features {
switch f.Type() {
case relay.FeatureUserAuth:
@ -195,20 +195,15 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
}
if h.hop != nil {
/*
if address != "" {
resp.Status = relay.StatusForbidden
log.Error("forward mode, CONNECT method is forbidden")
_, err := resp.WriteTo(conn)
return err
}
*/
defer conn.Close()
// forward mode
return h.handleForward(ctx, conn, network, log)
}
switch req.Cmd & relay.CmdMask {
case relay.CmdConnect:
defer conn.Close()
if !tunnelID.IsZero() {
return h.handleConnectTunnel(ctx, conn, network, address, tunnelID, log)
}
@ -217,6 +212,8 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
if !tunnelID.IsZero() {
return h.handleTunnel(ctx, conn, tunnelID, log)
}
defer conn.Close()
return h.handleBind(ctx, conn, network, address, log)
default:
resp.Status = relay.StatusBadRequest