示例#1
0
func (c *clientConnection) handleStream(stream net.Conn, streamid uint32) {

	clog := c.log.New("stream", streamid)

	var clhello clHello
	var clecho clEcho
	var clrequest clRequest
	var clresponse clResponse

	//recv hello
	var err error = nil
	err = clhello.read(stream)
	if nil != err {
		clog.Error("error reading socks hello", log.Ctx{"error": err})
		stream.Close()
		return
	}
	clog.Debug(clhello.print())

	//send echo
	clecho.gen(0)
	clecho.write(stream)
	clog.Debug(clecho.print())

	//recv request
	err = clrequest.read(stream)
	if nil != err {
		clog.Error("error reading socks request", log.Ctx{"error": err})
		stream.Close()
		return
	}
	clog.Debug(clrequest.print())
	//connect

	clog.Debug("accepted socks request", log.Ctx{"request": clrequest.reqtype, "dst": clrequest.url})
	conn, err := net.DialTimeout(clrequest.reqtype, clrequest.url, time.Duration(500)*time.Millisecond)

	if err != nil {
		clog.Error("error dialing upstream", log.Ctx{"error": err})
		clresponse.gen(&clrequest, 4)
		clresponse.write(stream)
		clog.Debug(clresponse.print())
		stream.Close()
		return
	}
	//success
	clresponse.gen(&clrequest, 0)
	clresponse.write(stream)
	clog.Debug(clresponse.print())

	inbytes, outbytes := shared.Pipe(conn, stream)
	clog.Info("upstream connection closed", log.Ctx{"request": clrequest.reqtype, "dst": clrequest.url, "inbytes": inbytes, "outbytes": outbytes})
}
示例#2
0
文件: mux.go 项目: jsimonetti/tlstun
func handleSession(d *Daemon, conn net.Conn) {
	var err error
	var stream net.Conn

	connlog := d.log.New("raddr", conn.RemoteAddr())
	connlog.Debug("opening yamux stream")

	// Open a new stream
	if d.session == nil {
		d.log.Debug("session was not open, opening...")
		err = openSession(d)
		if err != nil {
			d.log.Error("could not open session", log.Ctx{"error": err})
			return
		}

	}
	stream, err = d.session.Open()
	if err != nil {
		if err == yamux.ErrSessionShutdown {
			d.log.Debug("session was shut down, reopening...")
			err = openSession(d)
			if err != nil {
				d.log.Error("could not open session", log.Ctx{"error": err})
				return
			}
		}
		stream, err = d.session.Open()
		if err != nil {
			d.log.Error("Stream open failed!", log.Ctx{"error": err})
			conn.Close()
			return
		}
	}

	inbytes, outbytes := shared.Pipe(conn, stream)
	connlog.Info("connection closed", log.Ctx{"inbytes": inbytes, "outbytes": outbytes})
}