mirror of https://github.com/hashicorp/consul
Making suggested changes
parent
a05563b788
commit
46bea72212
|
@ -924,9 +924,9 @@ func (a *Agent) listenAndServeDNS() error {
|
||||||
s, _ := NewDNSServer(a)
|
s, _ := NewDNSServer(a)
|
||||||
|
|
||||||
grpcDNS.NewServer(grpcDNS.Config{
|
grpcDNS.NewServer(grpcDNS.Config{
|
||||||
Logger: a.logger.Named("grpc-api.dns"),
|
Logger: a.logger.Named("grpc-api.dns"),
|
||||||
DNSServeMux: s.mux,
|
DNSServeMux: s.mux,
|
||||||
LocalAddress: grpcDNS.Local{IP: net.IPv4(127, 0, 0, 1), Port: a.config.GRPCPort},
|
LocalAddr: grpcDNS.LocalAddr{IP: net.IPv4(127, 0, 0, 1), Port: a.config.GRPCPort},
|
||||||
}).Register(a.externalGRPCServer)
|
}).Register(a.externalGRPCServer)
|
||||||
|
|
||||||
a.dnsServers = append(a.dnsServers, s)
|
a.dnsServers = append(a.dnsServers, s)
|
||||||
|
|
|
@ -15,15 +15,15 @@ import (
|
||||||
"github.com/hashicorp/consul/proto-public/pbdns"
|
"github.com/hashicorp/consul/proto-public/pbdns"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Local struct {
|
type LocalAddr struct {
|
||||||
IP net.IP
|
IP net.IP
|
||||||
Port int
|
Port int
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Logger hclog.Logger
|
Logger hclog.Logger
|
||||||
DNSServeMux *dns.ServeMux
|
DNSServeMux *dns.ServeMux
|
||||||
LocalAddress Local
|
LocalAddr LocalAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
|
@ -40,7 +40,7 @@ func (s *Server) Register(grpcServer *grpc.Server) {
|
||||||
|
|
||||||
// BufferResponseWriter writes a DNS response to a byte buffer.
|
// BufferResponseWriter writes a DNS response to a byte buffer.
|
||||||
type BufferResponseWriter struct {
|
type BufferResponseWriter struct {
|
||||||
ResponseBuffer []byte
|
responseBuffer []byte
|
||||||
LocalAddress net.Addr
|
LocalAddress net.Addr
|
||||||
RemoteAddress net.Addr
|
RemoteAddress net.Addr
|
||||||
Logger hclog.Logger
|
Logger hclog.Logger
|
||||||
|
@ -64,15 +64,14 @@ func (b *BufferResponseWriter) WriteMsg(m *dns.Msg) error {
|
||||||
b.Logger.Error("error packing message", "err", err)
|
b.Logger.Error("error packing message", "err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
b.ResponseBuffer = msgBytes
|
b.responseBuffer = msgBytes
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write writes a raw buffer back to the client.
|
// Write writes a raw buffer back to the client.
|
||||||
func (b *BufferResponseWriter) Write(m []byte) (int, error) {
|
func (b *BufferResponseWriter) Write(m []byte) (int, error) {
|
||||||
b.Logger.Info("Write was called")
|
b.Logger.Debug("Write was called")
|
||||||
copy(b.ResponseBuffer, m)
|
return copy(b.responseBuffer, m), nil
|
||||||
return len(b.ResponseBuffer), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close closes the connection.
|
// Close closes the connection.
|
||||||
|
@ -108,11 +107,11 @@ func (s *Server) Query(ctx context.Context, req *pbdns.QueryRequest) (*pbdns.Que
|
||||||
switch req.GetProtocol() {
|
switch req.GetProtocol() {
|
||||||
case pbdns.Protocol_PROTOCOL_TCP:
|
case pbdns.Protocol_PROTOCOL_TCP:
|
||||||
remote = pr.Addr
|
remote = pr.Addr
|
||||||
local = &net.TCPAddr{IP: s.LocalAddress.IP, Port: s.LocalAddress.Port}
|
local = &net.TCPAddr{IP: s.LocalAddr.IP, Port: s.LocalAddr.Port}
|
||||||
case pbdns.Protocol_PROTOCOL_UDP:
|
case pbdns.Protocol_PROTOCOL_UDP:
|
||||||
remoteAddr := pr.Addr.(*net.TCPAddr)
|
remoteAddr := pr.Addr.(*net.TCPAddr)
|
||||||
remote = &net.UDPAddr{IP: remoteAddr.IP, Port: remoteAddr.Port}
|
remote = &net.UDPAddr{IP: remoteAddr.IP, Port: remoteAddr.Port}
|
||||||
local = &net.UDPAddr{IP: s.LocalAddress.IP, Port: s.LocalAddress.Port}
|
local = &net.UDPAddr{IP: s.LocalAddr.IP, Port: s.LocalAddr.Port}
|
||||||
default:
|
default:
|
||||||
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("error protocol type not set: %v", req.GetProtocol()))
|
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("error protocol type not set: %v", req.GetProtocol()))
|
||||||
}
|
}
|
||||||
|
@ -131,7 +130,7 @@ func (s *Server) Query(ctx context.Context, req *pbdns.QueryRequest) (*pbdns.Que
|
||||||
}
|
}
|
||||||
s.DNSServeMux.ServeDNS(respWriter, msg)
|
s.DNSServeMux.ServeDNS(respWriter, msg)
|
||||||
|
|
||||||
queryResponse := &pbdns.QueryResponse{Msg: respWriter.ResponseBuffer}
|
queryResponse := &pbdns.QueryResponse{Msg: respWriter.responseBuffer}
|
||||||
|
|
||||||
return queryResponse, nil
|
return queryResponse, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,7 +58,7 @@ func (s *DNSTestSuite) TestProxy_Success() {
|
||||||
server := NewServer(Config{
|
server := NewServer(Config{
|
||||||
Logger: hclog.Default(),
|
Logger: hclog.Default(),
|
||||||
DNSServeMux: mux,
|
DNSServeMux: mux,
|
||||||
LocalAddress: Local{
|
LocalAddr: LocalAddr{
|
||||||
net.IPv4(127, 0, 0, 1),
|
net.IPv4(127, 0, 0, 1),
|
||||||
0,
|
0,
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue