Exemple #1
0
func TestConnOverTCP(t *testing.T) {
	frame := trace.NewFrame()
	x := tcp.Transport

	ready := make(chan int, 2)
	sent, recv := make(map[byte]struct{}), make(map[byte]struct{})

	// Accepter endpoint
	go func() {
		l := NewListener(frame.Refine("listener"), x, tcp.Addr(":17222"))
		ready <- 1 // SYNC: Notify that listener is accepting
		testGreedyRead(t, l.Accept(), recv)
		ready <- 1
	}()

	// Dialer endpoint
	<-ready // SYNC: Wait for listener to start accepting
	d := NewDialer(frame.Refine("dialer"), x)
	conn := d.Dial(tcp.Addr("localhost:17222"))
	testGreedyWrite(t, conn, sent)
	<-ready // SYNC: Wait for accepter goroutine to complete

	// Make sure all marked writes have been received
	if !reflect.DeepEqual(sent, recv) {
		t.Errorf("expected %#v, got %#v", sent, recv)
		failNow()
	}
}
Exemple #2
0
func NewServer(inAddr, outAddr string) (*Server, error) {
	t := tele.NewStructOverTCP()
	l := t.Listen(tcp.Addr(outAddr))
	if outAddr == "" {
		outAddr = l.Addr().String()
		fmt.Println(outAddr)
	}
	srv := &Server{frame: trace.NewFrame("tele", "server"), tele: t, inAddr: inAddr, outAddr: outAddr}
	srv.frame.Bind(srv)
	go srv.loop(l)
	return srv, nil
}
Exemple #3
0
func (cli *Client) loop(l net.Listener) {
	for {
		inConn, err := l.Accept()
		if err != nil {
			cli.frame.Printf("accept on tcp address %s (%s)", cli.inAddr, err)
			os.Exit(1)
		}
		// Contact teleport server
		ds, _ := cli.tele.DialSession(tcp.Addr(cli.outAddr))
		tele := ds.Dial()
		// Write an empty chunk to mark the beginning of connection
		if err = tele.Write(&cargo{}); err != nil {
			cli.frame.Printf("first write (%s)", err)
			tele.Close()
			inConn.Close()
			continue
		}
		// Begin proxying
		Proxy(inConn, tele)
	}
}