add relay proxy
This commit is contained in:
@ -43,8 +43,9 @@ func (h *forwardHandler) Init(md md.Metadata) (err error) {
|
||||
}
|
||||
|
||||
// Forward implements handler.Forwarder.
|
||||
func (h *forwardHandler) Forward(group *chain.NodeGroup) {
|
||||
func (h *forwardHandler) Forward(group *chain.NodeGroup, chain *chain.Chain) {
|
||||
h.group = group
|
||||
h.chain = chain
|
||||
}
|
||||
|
||||
func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn) {
|
||||
|
@ -14,5 +14,5 @@ type Handler interface {
|
||||
}
|
||||
|
||||
type Forwarder interface {
|
||||
Forward(*chain.NodeGroup)
|
||||
Forward(*chain.NodeGroup, *chain.Chain)
|
||||
}
|
||||
|
49
pkg/handler/relay/forward.go
Normal file
49
pkg/handler/relay/forward.go
Normal file
@ -0,0 +1,49 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/chain"
|
||||
"github.com/go-gost/gost/pkg/handler"
|
||||
)
|
||||
|
||||
func (h *relayHandler) handleForward(ctx context.Context, conn net.Conn, network string) {
|
||||
target := h.group.Next()
|
||||
if target == nil {
|
||||
h.logger.Error("no target available")
|
||||
return
|
||||
}
|
||||
|
||||
h.logger = h.logger.WithFields(map[string]interface{}{
|
||||
"dst": target.Addr(),
|
||||
})
|
||||
|
||||
h.logger.Infof("%s >> %s", conn.RemoteAddr(), target.Addr())
|
||||
|
||||
r := (&chain.Router{}).
|
||||
WithChain(h.chain).
|
||||
WithRetry(h.md.retryCount).
|
||||
WithLogger(h.logger)
|
||||
|
||||
cc, err := r.Dial(ctx, network, target.Addr())
|
||||
if err != nil {
|
||||
h.logger.Error(err)
|
||||
// TODO: the router itself may be failed due to the failed node in the router,
|
||||
// the dead marker may be a wrong operation.
|
||||
target.Marker().Mark()
|
||||
return
|
||||
}
|
||||
defer cc.Close()
|
||||
target.Marker().Reset()
|
||||
|
||||
t := time.Now()
|
||||
h.logger.Infof("%s <-> %s", conn.RemoteAddr(), target.Addr())
|
||||
handler.Transport(conn, cc)
|
||||
h.logger.
|
||||
WithFields(map[string]interface{}{
|
||||
"duration": time.Since(t),
|
||||
}).
|
||||
Infof("%s >-< %s", conn.RemoteAddr(), target.Addr())
|
||||
}
|
143
pkg/handler/relay/handler.go
Normal file
143
pkg/handler/relay/handler.go
Normal file
@ -0,0 +1,143 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/bypass"
|
||||
"github.com/go-gost/gost/pkg/chain"
|
||||
"github.com/go-gost/gost/pkg/handler"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
md "github.com/go-gost/gost/pkg/metadata"
|
||||
"github.com/go-gost/gost/pkg/registry"
|
||||
"github.com/go-gost/relay"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.RegisterHandler("relay", NewHandler)
|
||||
}
|
||||
|
||||
type relayHandler struct {
|
||||
group *chain.NodeGroup
|
||||
chain *chain.Chain
|
||||
bypass bypass.Bypass
|
||||
logger logger.Logger
|
||||
md metadata
|
||||
}
|
||||
|
||||
func NewHandler(opts ...handler.Option) handler.Handler {
|
||||
options := &handler.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
}
|
||||
|
||||
return &relayHandler{
|
||||
chain: options.Chain,
|
||||
bypass: options.Bypass,
|
||||
logger: options.Logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *relayHandler) Init(md md.Metadata) (err error) {
|
||||
return h.parseMetadata(md)
|
||||
}
|
||||
|
||||
// Forward implements handler.Forwarder.
|
||||
func (h *relayHandler) Forward(group *chain.NodeGroup, chain *chain.Chain) {
|
||||
h.group = group
|
||||
h.chain = chain
|
||||
}
|
||||
|
||||
func (h *relayHandler) Handle(ctx context.Context, conn net.Conn) {
|
||||
defer conn.Close()
|
||||
|
||||
start := time.Now()
|
||||
h.logger = h.logger.WithFields(map[string]interface{}{
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
"local": conn.LocalAddr().String(),
|
||||
})
|
||||
|
||||
h.logger.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
|
||||
defer func() {
|
||||
h.logger.WithFields(map[string]interface{}{
|
||||
"duration": time.Since(start),
|
||||
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
|
||||
}()
|
||||
|
||||
if h.md.readTimeout > 0 {
|
||||
conn.SetReadDeadline(time.Now().Add(h.md.readTimeout))
|
||||
}
|
||||
|
||||
req := relay.Request{}
|
||||
if _, err := req.ReadFrom(conn); err != nil {
|
||||
h.logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
conn.SetReadDeadline(time.Time{})
|
||||
|
||||
if req.Version != relay.Version1 {
|
||||
h.logger.Error("bad version")
|
||||
return
|
||||
}
|
||||
|
||||
var user, pass string
|
||||
var target string
|
||||
for _, f := range req.Features {
|
||||
if f.Type() == relay.FeatureUserAuth {
|
||||
feature := f.(*relay.UserAuthFeature)
|
||||
user, pass = feature.Username, feature.Password
|
||||
}
|
||||
if f.Type() == relay.FeatureTargetAddr {
|
||||
feature := f.(*relay.TargetAddrFeature)
|
||||
target = net.JoinHostPort(feature.Host, strconv.Itoa(int(feature.Port)))
|
||||
}
|
||||
}
|
||||
|
||||
if user != "" {
|
||||
h.logger = h.logger.WithFields(map[string]interface{}{"user": user})
|
||||
}
|
||||
if target != "" {
|
||||
h.logger = h.logger.WithFields(map[string]interface{}{"dst": target})
|
||||
}
|
||||
|
||||
resp := relay.Response{
|
||||
Version: relay.Version1,
|
||||
Status: relay.StatusOK,
|
||||
}
|
||||
if h.md.authenticator != nil && !h.md.authenticator.Authenticate(user, pass) {
|
||||
resp.Status = relay.StatusUnauthorized
|
||||
resp.WriteTo(conn)
|
||||
h.logger.Error("unauthorized")
|
||||
return
|
||||
}
|
||||
|
||||
network := "tcp"
|
||||
if (req.Flags & relay.FUDP) == relay.FUDP {
|
||||
network = "udp"
|
||||
}
|
||||
|
||||
if h.group != nil {
|
||||
if target != "" {
|
||||
resp.Status = relay.StatusForbidden
|
||||
resp.WriteTo(conn)
|
||||
h.logger.Error("forbidden")
|
||||
return
|
||||
}
|
||||
// forward mode
|
||||
h.handleForward(ctx, conn, network)
|
||||
return
|
||||
}
|
||||
|
||||
if target == "" {
|
||||
resp.Status = relay.StatusBadRequest
|
||||
resp.WriteTo(conn)
|
||||
h.logger.Error("bad request")
|
||||
return
|
||||
}
|
||||
|
||||
// proxy mode
|
||||
h.handleProxy(ctx, conn, network, target)
|
||||
}
|
41
pkg/handler/relay/metadata.go
Normal file
41
pkg/handler/relay/metadata.go
Normal file
@ -0,0 +1,41 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/auth"
|
||||
md "github.com/go-gost/gost/pkg/metadata"
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
authenticator auth.Authenticator
|
||||
readTimeout time.Duration
|
||||
retryCount int
|
||||
}
|
||||
|
||||
func (h *relayHandler) parseMetadata(md md.Metadata) (err error) {
|
||||
const (
|
||||
authsKey = "auths"
|
||||
readTimeout = "readTimeout"
|
||||
retryCount = "retry"
|
||||
)
|
||||
|
||||
if v, _ := md.Get(authsKey).([]interface{}); len(v) > 0 {
|
||||
authenticator := auth.NewLocalAuthenticator(nil)
|
||||
for _, auth := range v {
|
||||
if s, _ := auth.(string); s != "" {
|
||||
ss := strings.SplitN(s, ":", 2)
|
||||
if len(ss) == 1 {
|
||||
authenticator.Add(ss[0], "")
|
||||
} else {
|
||||
authenticator.Add(ss[0], ss[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
h.md.authenticator = authenticator
|
||||
}
|
||||
h.md.readTimeout = md.GetDuration(readTimeout)
|
||||
h.md.retryCount = md.GetInt(retryCount)
|
||||
return
|
||||
}
|
57
pkg/handler/relay/proxy.go
Normal file
57
pkg/handler/relay/proxy.go
Normal file
@ -0,0 +1,57 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/chain"
|
||||
util_relay "github.com/go-gost/gost/pkg/common/util/relay"
|
||||
"github.com/go-gost/gost/pkg/handler"
|
||||
"github.com/go-gost/relay"
|
||||
)
|
||||
|
||||
func (h *relayHandler) handleProxy(ctx context.Context, conn net.Conn, network, address string) {
|
||||
h.logger.Infof("%s >> %s", conn.RemoteAddr(), address)
|
||||
|
||||
resp := relay.Response{
|
||||
Version: relay.Version1,
|
||||
Status: relay.StatusOK,
|
||||
}
|
||||
|
||||
if h.bypass != nil && h.bypass.Contains(address) {
|
||||
h.logger.Info("bypass: ", address)
|
||||
resp.Status = relay.StatusForbidden
|
||||
resp.WriteTo(conn)
|
||||
return
|
||||
}
|
||||
|
||||
r := (&chain.Router{}).
|
||||
WithChain(h.chain).
|
||||
WithRetry(h.md.retryCount).
|
||||
WithLogger(h.logger)
|
||||
cc, err := r.Dial(ctx, network, address)
|
||||
if err != nil {
|
||||
resp.Status = relay.StatusNetworkUnreachable
|
||||
resp.WriteTo(conn)
|
||||
return
|
||||
}
|
||||
defer cc.Close()
|
||||
|
||||
if _, err := resp.WriteTo(conn); err != nil {
|
||||
h.logger.Error(err)
|
||||
}
|
||||
|
||||
if network == "udp" {
|
||||
conn = util_relay.UDPTunConn(conn)
|
||||
}
|
||||
|
||||
t := time.Now()
|
||||
h.logger.Infof("%s <-> %s", conn.RemoteAddr(), address)
|
||||
handler.Transport(conn, cc)
|
||||
h.logger.
|
||||
WithFields(map[string]interface{}{
|
||||
"duration": time.Since(t),
|
||||
}).
|
||||
Infof("%s >-< %s", conn.RemoteAddr(), address)
|
||||
}
|
@ -64,13 +64,7 @@ func (h *ssHandler) Handle(ctx context.Context, conn net.Conn) {
|
||||
|
||||
// standard UDP relay.
|
||||
if pc, ok := conn.(net.PacketConn); ok {
|
||||
if h.md.enableUDP {
|
||||
h.handleUDP(ctx, conn.RemoteAddr(), pc)
|
||||
return
|
||||
} else {
|
||||
h.logger.Error("UDP relay is diabled")
|
||||
}
|
||||
|
||||
h.handleUDP(ctx, pc, conn.RemoteAddr())
|
||||
return
|
||||
}
|
||||
|
||||
@ -84,21 +78,17 @@ func (h *ssHandler) Handle(ctx context.Context, conn net.Conn) {
|
||||
|
||||
br := bufio.NewReader(conn)
|
||||
data, err := br.Peek(3)
|
||||
conn.SetReadDeadline(time.Time{})
|
||||
if err != nil {
|
||||
h.logger.Error(err)
|
||||
h.discard(conn)
|
||||
return
|
||||
}
|
||||
conn.SetReadDeadline(time.Time{})
|
||||
|
||||
conn = handler.NewBufferReaderConn(conn, br)
|
||||
if data[2] == 0xff {
|
||||
if h.md.enableUDP {
|
||||
// UDP-over-TCP relay
|
||||
h.handleUDPTun(ctx, conn)
|
||||
} else {
|
||||
h.logger.Error("UDP relay is diabled")
|
||||
}
|
||||
// UDP-over-TCP relay
|
||||
h.handleUDPTun(ctx, conn)
|
||||
return
|
||||
}
|
||||
|
||||
@ -110,8 +100,6 @@ func (h *ssHandler) Handle(ctx context.Context, conn net.Conn) {
|
||||
return
|
||||
}
|
||||
|
||||
conn.SetReadDeadline(time.Time{})
|
||||
|
||||
h.logger = h.logger.WithFields(map[string]interface{}{
|
||||
"dst": addr.String(),
|
||||
})
|
||||
|
@ -11,7 +11,12 @@ import (
|
||||
"github.com/go-gost/gost/pkg/common/util/ss"
|
||||
)
|
||||
|
||||
func (h *ssHandler) handleUDP(ctx context.Context, raddr net.Addr, conn net.PacketConn) {
|
||||
func (h *ssHandler) handleUDP(ctx context.Context, conn net.PacketConn, raddr net.Addr) {
|
||||
if !h.md.enableUDP {
|
||||
h.logger.Error("UDP relay is diabled")
|
||||
return
|
||||
}
|
||||
|
||||
if h.md.cipher != nil {
|
||||
conn = h.md.cipher.PacketConn(conn)
|
||||
}
|
||||
@ -50,6 +55,11 @@ func (h *ssHandler) handleUDP(ctx context.Context, raddr net.Addr, conn net.Pack
|
||||
}
|
||||
|
||||
func (h *ssHandler) handleUDPTun(ctx context.Context, conn net.Conn) {
|
||||
if !h.md.enableUDP {
|
||||
h.logger.Error("UDP relay is diabled")
|
||||
return
|
||||
}
|
||||
|
||||
// obtain a udp connection
|
||||
r := (&chain.Router{}).
|
||||
WithChain(h.chain).
|
||||
|
Reference in New Issue
Block a user