Exemplo n.º 1
0
func WriteMsg(c conn.Conn, msg interface{}) (err error) {
	buffer, err := Pack(msg)
	if err != nil {
		return
	}

	//	c.Debug("Writing message: %s", string(buffer))
	log.Println(fmt.Sprintf("[DEBUG] Writing message: %s", string(buffer)))
	if err = binary.Write(c, binary.LittleEndian, int64(len(buffer))); err != nil {
		return
	}

	if _, err = c.Write(buffer); err != nil {
		return
	}

	return
}
Exemplo n.º 2
0
func (t *Tunnel) handlePublic(publicConn conn.Conn) {
	defer publicConn.Close()
	defer t.recoverPanic("Tunnel.handlePublic")

	//	publicConn.Info("New connection from %v", publicConn.RemoteAddr())
	log.Printf("[INFO] New connection from %v\n", publicConn.RemoteAddr())
	// connection hook
	if err := t.hooks.OnConnectionOpen(t, publicConn); err != nil {
		//		t.Error("OnConnectionOpen hook failed: %v", err)
		log.Printf("[ERROR] OnConnectionOpen hook failed: %v\n", err)
		return
	}

	startTime := time.Now()

	// open a proxy stream
	proxyConn, err := t.sess.openProxy(publicConn.RemoteAddr().String(), t.url)
	if err != nil {
		//		t.Error("Failed to open proxy connection: %v", err)
		log.Printf("[ERROR] Failed to open proxy connection: %v\n", err)
		return
	}
	defer proxyConn.Close()

	// join the public and proxy connections
	bytesIn, bytesOut := conn.Join(publicConn, proxyConn)

	if err = t.hooks.OnConnectionClose(t, publicConn, time.Now().Sub(startTime), bytesIn, bytesOut); err != nil {
		//		t.Error("OnConnectionClose hook failed: %v", err)
		log.Printf("[ERROR] OnConnectionClose hook failed: %v\n", err)
		return
	}
}
Exemplo n.º 3
0
func (s *Session) handleStream(stream conn.Conn) {
	defer s.recoverPanic("Session.handleStream")
	defer stream.Close()

	// make sure we only process streams while we're not shutting down
	if err := s.guard.Enter(); err != nil {
		log.Printf("[ERROR] Failing stream, session is shutting down %v\n", err)
		//		stream.Error("Failing stream, session is shutting down")
		return
	}
	defer s.guard.Exit()

	raw, err := proto.ReadMsg(stream)
	if err != nil {
		//		stream.Error("Failed to read message: %v")
		log.Printf("[ERROR] Failed to read message: %v\n", err)
		go s.Shutdown()
		return
	}

	switch msg := raw.(type) {
	case *proto.Bind:
		err = s.handleBind(stream, msg)
	case *proto.Unbind:
		err = s.handleUnbind(stream, msg)
	default:
		err = fmt.Errorf("Unknown message type: %v", reflect.TypeOf(raw))
	}

	if err != nil {
		//		stream.Error("Error on stream: %v", err)
		log.Printf("Error on stream: %v\n", err)
		go s.Shutdown()
		return
	}

	return
}