Esempio n. 1
0
// Generic connection handler
//
// This redelegates to more specific proxy handlers that contain the
// main proxy loop logic.
func handleConnection(cConn net.Conn, serverAddr string) {
	var err error

	// Log disconnections
	defer func() {
		if err != nil && err != io.EOF {
			fmt.Printf("Session exits with error: %v\n", err)
		} else {
			fmt.Printf("Session exits cleanly\n")
		}
	}()

	defer cConn.Close()

	c := femebe.NewClientMessageStream(
		"Client", newBufWriteCon(cConn))

	unencryptServerConn, err := autoDial(serverAddr)
	if err != nil {
		fmt.Printf("Could not connect to server: %v\n", err)
	}

	tlsConf := tls.Config{}
	tlsConf.InsecureSkipVerify = true

	sConn, err := femebe.NegotiateTLS(
		unencryptServerConn, "prefer", &tlsConf)
	if err != nil {
		fmt.Printf("Could not negotiate TLS: %v\n", err)
	}

	s := femebe.NewServerMessageStream("Server", newBufWriteCon(sConn))
	if err != nil {
		fmt.Printf("Could not initialize connection to server: %v\n", err)
	}

	done := make(chan error)
	NewSimpleProxySession(done, &ProxyPair{c, cConn},
		&ProxyPair{s, sConn}).start()

	// Both sides must exit to finish
	_ = <-done
	_ = <-done
}
Esempio n. 2
0
// A helper that initializes a message, writes it into and then then
// reads it back out of femebe.
func firstMessageRoundTrip(t *testing.T,
	init func(m *femebe.Message)) (*femebe.Message, error) {
	// Pretend that a bad startup packet is being serialized
	// and sent to the server.
	sms, rwc := newTestServerStream(t)
	var m femebe.Message
	init(&m)
	sms.Send(&m)

	// Reuse the buffer that has been filled and pretend to be
	// serving a client connection isntead, which should result in
	// an error because the startup message is over-sized.
	cms := femebe.NewClientMessageStream("TestClientStream", rwc)
	if err := cms.Next(&m); err != nil {
		return nil, err
	}

	return &m, nil
}
Esempio n. 3
0
// Generic connection handler
//
// This redelegates to more specific proxy handlers that contain the
// main proxy loop logic.
func handleConnection(cConn net.Conn, rt *routingTable) {
	var err error

	// Log disconnections
	defer func() {
		if err != nil && err != io.EOF {
			log.Printf("Session exits with error: %v\n", err)
		} else {
			log.Printf("Session exits cleanly\n")
		}
	}()

	defer cConn.Close()

	c := femebe.NewClientMessageStream(
		"Client", newBufWriteCon(cConn))

	// Must interpret Startup and Cancel requests.
	//
	// SSL Negotiation requests not handled for now.
	var firstPacket femebe.Message
	c.Next(&firstPacket)

	// Handle Startup packets
	var sup *pgproto.Startup
	if sup, err = pgproto.ReadStartupMessage(&firstPacket); err != nil {
		log.Print(err)
		return
	}

	var ent *routingEntry
	if ent = rt.rewrite(sup); ent == nil {
		log.Print("Could not route startup packet")
		return
	}

	unencryptServerConn, err := autoDial(ent.addr)
	if err != nil {
		log.Printf("Could not connect to server: %v\n", err)
		return
	}

	tlsConf := tls.Config{}
	tlsConf.InsecureSkipVerify = true

	sConn, err := femebe.NegotiateTLS(
		unencryptServerConn, "prefer", &tlsConf)
	if err != nil {
		log.Printf("Could not negotiate TLS: %v\n", err)
		return
	}

	s := femebe.NewServerMessageStream("Server", newBufWriteCon(sConn))
	if err != nil {
		log.Printf("Could not initialize connection to server: %v\n", err)
		return
	}

	var rewrittenStatupMessage femebe.Message
	sup.FillMessage(&rewrittenStatupMessage)
	err = s.Send(&rewrittenStatupMessage)
	if err != nil {
		return
	}

	err = s.Flush()
	if err != nil {
		return
	}

	done := make(chan error)
	NewSimpleProxySession(done,
		&ProxyPair{c, cConn},
		&ProxyPair{s, sConn}).start()

	// Both sides must exit to finish
	_ = <-done
	_ = <-done
}
Esempio n. 4
0
func newTestClientStream(t *testing.T) (*femebe.MessageStream, *inMemRwc) {
	rwc := NewInMemRwc()
	return femebe.NewClientMessageStream("TestClientConn", rwc), rwc
}