2021-08-21 01:20:40 -04:00
|
|
|
//go:build !confonly
|
2021-03-11 03:46:17 -05:00
|
|
|
// +build !confonly
|
|
|
|
|
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-03-13 18:09:51 -05:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
|
2021-03-11 03:46:17 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/session"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet/grpc/encoding"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet/tls"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Listener struct {
|
|
|
|
encoding.UnimplementedGunServiceServer
|
|
|
|
ctx context.Context
|
|
|
|
handler internet.ConnHandler
|
|
|
|
local net.Addr
|
|
|
|
config *Config
|
|
|
|
locker *internet.FileLocker // for unix domain socket
|
|
|
|
|
|
|
|
s *grpc.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Listener) Tun(server encoding.GunService_TunServer) error {
|
|
|
|
tunCtx, cancel := context.WithCancel(l.ctx)
|
2021-03-15 04:50:09 -04:00
|
|
|
l.handler(encoding.NewGunConn(server, cancel))
|
2021-03-11 03:46:17 -05:00
|
|
|
<-tunCtx.Done()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Listener) Close() error {
|
|
|
|
l.s.Stop()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Listener) Addr() net.Addr {
|
|
|
|
return l.local
|
|
|
|
}
|
|
|
|
|
|
|
|
func Listen(ctx context.Context, address net.Address, port net.Port, settings *internet.MemoryStreamConfig, handler internet.ConnHandler) (internet.Listener, error) {
|
2021-03-11 10:02:31 -05:00
|
|
|
grpcSettings := settings.ProtocolSettings.(*Config)
|
2021-03-11 03:46:17 -05:00
|
|
|
var listener *Listener
|
|
|
|
if port == net.Port(0) { // unix
|
|
|
|
listener = &Listener{
|
|
|
|
handler: handler,
|
|
|
|
local: &net.UnixAddr{
|
|
|
|
Name: address.Domain(),
|
|
|
|
Net: "unix",
|
|
|
|
},
|
2021-03-11 10:02:31 -05:00
|
|
|
config: grpcSettings,
|
2021-03-11 03:46:17 -05:00
|
|
|
}
|
|
|
|
} else { // tcp
|
|
|
|
listener = &Listener{
|
|
|
|
handler: handler,
|
|
|
|
local: &net.TCPAddr{
|
|
|
|
IP: address.IP(),
|
|
|
|
Port: int(port),
|
|
|
|
},
|
2021-03-11 10:02:31 -05:00
|
|
|
config: grpcSettings,
|
2021-03-11 03:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
listener.ctx = ctx
|
|
|
|
|
|
|
|
config := tls.ConfigFromStreamSettings(settings)
|
|
|
|
|
|
|
|
var s *grpc.Server
|
|
|
|
if config == nil {
|
|
|
|
s = grpc.NewServer()
|
|
|
|
} else {
|
2021-09-03 16:34:34 -04:00
|
|
|
// gRPC server may silently ignore TLS errors
|
2021-03-11 03:46:17 -05:00
|
|
|
s = grpc.NewServer(grpc.Creds(credentials.NewTLS(config.GetTLSConfig(tls.WithNextProto("h2")))))
|
|
|
|
}
|
|
|
|
listener.s = s
|
|
|
|
|
|
|
|
if settings.SocketSettings != nil && settings.SocketSettings.AcceptProxyProtocol {
|
|
|
|
newError("accepting PROXY protocol").AtWarning().WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
var streamListener net.Listener
|
|
|
|
var err error
|
|
|
|
if port == net.Port(0) { // unix
|
|
|
|
streamListener, err = internet.ListenSystem(ctx, &net.UnixAddr{
|
|
|
|
Name: address.Domain(),
|
|
|
|
Net: "unix",
|
|
|
|
}, settings.SocketSettings)
|
|
|
|
if err != nil {
|
|
|
|
newError("failed to listen on ", address).Base(err).AtError().WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
locker := ctx.Value(address.Domain())
|
|
|
|
if locker != nil {
|
|
|
|
listener.locker = locker.(*internet.FileLocker)
|
|
|
|
}
|
|
|
|
} else { // tcp
|
|
|
|
streamListener, err = internet.ListenSystem(ctx, &net.TCPAddr{
|
|
|
|
IP: address.IP(),
|
|
|
|
Port: int(port),
|
|
|
|
}, settings.SocketSettings)
|
|
|
|
if err != nil {
|
|
|
|
newError("failed to listen on ", address, ":", port).Base(err).AtError().WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 10:02:31 -05:00
|
|
|
encoding.RegisterGunServiceServerX(s, listener, grpcSettings.ServiceName)
|
2021-03-11 03:46:17 -05:00
|
|
|
|
|
|
|
if err = s.Serve(streamListener); err != nil {
|
|
|
|
newError("Listener for grpc ended").Base(err).WriteToLog()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return listener, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(internet.RegisterTransportListener(protocolName, Listen))
|
|
|
|
}
|