Exemple #1
0
func complex_handshake(rw *bufio.ReadWriter, input []byte) bool {
	result, scheme, challenge, digest := validateClient(input)
	if !result {
		return result
	}
	log.Debugf("Validate Client %v scheme %v challenge %0X digest %0X", result, scheme, challenge, digest)
	s1 := create_s1()
	log.Debug("s1 length", len(s1))
	off := getDigestOffset(s1, scheme)
	log.Debug("s1 digest offset", off)
	buf := new(bytes.Buffer)
	buf.Write(s1[:off])
	buf.Write(s1[off+32:])
	tempHash, _ := HMACsha256(buf.Bytes(), GENUINE_FMS_KEY[:36])
	copy(s1[off:], tempHash)
	log.Debug("s1 length", len(s1))
	//compute the challenge digest
	tempHash, _ = HMACsha256(digest, GENUINE_FMS_KEY[:68])
	log.Debug("s2 length tempHash", len(tempHash))
	randBytes := create_s2()
	log.Debug("s2 length", len(randBytes))
	lastHash, _ := HMACsha256(randBytes, tempHash)
	log.Debug("s2 length lastHash", len(lastHash))
	log.Debug("s2 length", len(randBytes))
	buf = new(bytes.Buffer)
	buf.WriteByte(0x03)
	buf.Write(s1)
	buf.Write(randBytes)
	buf.Write(lastHash)
	log.Debug("send s0s1s2", buf.Len())
	rw.Write(buf.Bytes())
	rw.Flush()
	ReadBuf(rw, HANDSHAKE_SIZE)
	return true
}
Exemple #2
0
// sendResponse send resp to client, sendResponse must be goroutine safe.
func (server *Server) writeHTTPResponse(rwr *bufio.ReadWriter, cb string, proto *Proto) (err error) {
	var pb []byte
	if proto.Body == nil {
		proto.Body = emptyJSONBody
	}
	if pb, err = json.Marshal(proto); err != nil {
		log.Error("json.Marshal() error(%v)", err)
		return
	}
	if len(cb) != 0 {
		if _, err = rwr.WriteString(cb); err != nil {
			log.Error("http rwr.Write() error(%v)", err)
			return
		}
		if err = rwr.WriteByte('='); err != nil {
			log.Error("http rwr.Write() error(%v)", err)
			return
		}
	}
	if _, err = rwr.Write(pb); err != nil {
		log.Error("http rwr.Write() error(%v)", err)
		return
	}
	if err = rwr.Flush(); err != nil {
		log.Error("http rwr.Flush() error(%v)", err)
	}
	proto.Reset()
	return
}
Exemple #3
0
func simpleCmdLocal(rw *bufio.ReadWriter, cmd []byte) error {
	if _, err := rw.Write(cmd); err != nil {
		return err
	}

	if err := rw.Flush(); err != nil {
		return err
	}

	resHeader, err := binprot.ReadResponseHeader(rw)
	if err != nil {
		return err
	}

	err = binprot.DecodeError(resHeader)
	if err != nil {
		if _, ioerr := rw.Discard(int(resHeader.TotalBodyLength)); ioerr != nil {
			return ioerr
		}
		return err
	}

	// Read in the message bytes from the body
	if _, err := rw.Discard(int(resHeader.TotalBodyLength)); err != nil {
		return err
	}

	return nil
}
Exemple #4
0
func getMetadataCommon(rw *bufio.ReadWriter, getCmd []byte) (common.Metadata, error) {
	if _, err := rw.Write(getCmd); err != nil {
		return common.Metadata{}, err
	}

	if err := rw.Flush(); err != nil {
		return common.Metadata{}, err
	}

	resHeader, err := binprot.ReadResponseHeader(rw)
	if err != nil {
		return common.Metadata{}, err
	}

	err = binprot.DecodeError(resHeader)
	if err != nil {
		// read in the message "Not found" after a miss
		if _, ioerr := rw.Discard(int(resHeader.TotalBodyLength)); ioerr != nil {
			return common.Metadata{}, ioerr
		}
		return common.Metadata{}, err
	}

	serverFlags := make([]byte, 4)
	binary.Read(rw, binary.BigEndian, &serverFlags)

	var metaData common.Metadata
	binary.Read(rw, binary.BigEndian, &metaData)

	return metaData, nil
}
Exemple #5
0
func writeChunks(r io.Reader, w *bufio.ReadWriter) error {
	buf := make([]byte, 32*1024)
	for {
		n, err := r.Read(buf)

		if n > 0 {
			length := fmt.Sprintf("%x\r\n", n)
			w.WriteString(length)

			_, err := w.Write(buf[0:n])
			if err != nil {
				return err
			}

			w.WriteString("\r\n")
			w.Flush()
		}

		if err != nil && err != io.EOF {
			return err
		}
		if err == io.EOF {
			break
		}
	}
	return nil
}
Exemple #6
0
// socksAuthRFC1929 authenticates the client via RFC 1929 username/password
// auth.  As a design decision any valid username/password is accepted as this
// field is primarily used as an out-of-band argument passing mechanism for
// pluggable transports.
func socksAuthRFC1929(rw *bufio.ReadWriter, req *SocksRequest) (err error) {
	sendErrResp := func() {
		// Swallow the write/flush error here, we are going to close the
		// connection and the original failure is more useful.
		resp := []byte{socksAuthRFC1929Ver, socksAuthRFC1929Fail}
		rw.Write(resp[:])
		socksFlushBuffers(rw)
	}

	// Validate the fixed parts of the command message.
	if err = socksReadByteVerify(rw, "auth version", socksAuthRFC1929Ver); err != nil {
		sendErrResp()
		return
	}

	// Read the username.
	var ulen byte
	if ulen, err = socksReadByte(rw); err != nil {
		return
	}
	if ulen < 1 {
		sendErrResp()
		err = fmt.Errorf("RFC1929 username with 0 length")
		return
	}
	var uname []byte
	if uname, err = socksReadBytes(rw, int(ulen)); err != nil {
		return
	}
	req.Username = string(uname)

	// Read the password.
	var plen byte
	if plen, err = socksReadByte(rw); err != nil {
		return
	}
	if plen < 1 {
		sendErrResp()
		err = fmt.Errorf("RFC1929 password with 0 length")
		return
	}
	var passwd []byte
	if passwd, err = socksReadBytes(rw, int(plen)); err != nil {
		return
	}
	if !(plen == 1 && passwd[0] == 0x00) {
		// tor will set the password to 'NUL' if there are no arguments.
		req.Password = string(passwd)
	}

	// Mash the username/password together and parse it as a pluggable
	// transport argument string.
	if req.Args, err = parseClientParameters(req.Username + req.Password); err != nil {
		sendErrResp()
	} else {
		resp := []byte{socksAuthRFC1929Ver, socksAuthRFC1929Success}
		_, err = rw.Write(resp[:])
	}
	return
}
Exemple #7
0
func (b BinProt) GetWithOpaque(rw *bufio.ReadWriter, key []byte, opaque int) ([]byte, error) {
	// Header
	writeReq(rw, Get, len(key), 0, len(key), opaque)
	// Body
	rw.Write(key)

	rw.Flush()

	// consume all of the response and return
	return consumeResponseCheckOpaque(rw.Reader, opaque)
}
Exemple #8
0
func pushResponse(bufrw *bufio.ReadWriter, authCode string) bool {
	_, err := bufrw.Write([]byte(authCode))
	if err == nil {
		err = bufrw.Flush()
	}
	if err != nil {
		go_lib.LogErrorf("PushAuthCodeError: %s\n", err)
		return false
	}
	return true
}
Exemple #9
0
func (b BinProt) GetE(rw *bufio.ReadWriter, key []byte) ([]byte, uint32, uint32, error) {
	// Header
	writeReq(rw, GetE, len(key), 0, len(key), 0)
	// Body
	rw.Write(key)

	rw.Flush()

	// consume all of the response and return
	return consumeResponseE(rw.Reader)
}
Exemple #10
0
func (b BinProt) Get(rw *bufio.ReadWriter, key []byte) error {
	// Header
	writeReq(rw, Get, len(key), 0, len(key))
	// Body
	rw.Write(key)

	rw.Flush()

	// consume all of the response and discard
	return consumeResponse(rw.Reader)
}
Exemple #11
0
func ChangeNick(rw *bufio.ReadWriter, nick string) error {
	var (
		str string = fmt.Sprintf("NICK %s\r\n", nick)
		err error
	)
	_, err = rw.Write([]byte(str))
	if err != nil {
		return err
	}
	rw.Flush()
	return nil
}
Exemple #12
0
func pushResponse(bufrw *bufio.ReadWriter, content interface{}, group string, op string) {
	literals := fmt.Sprintf("%v", content)
	_, err := bufrw.Write([]byte(literals))
	if err == nil {
		err = bufrw.Flush()
	}
	if err != nil {
		base.Logger().Errorf("Pushing response error (content=%v, group=%s, op=%s): %s\n", literals, group, op, err)
	} else {
		base.Logger().Errorf("The response '%v' has been pushed. (group=%s, op=%s)\n", literals, group, op)
	}
}
Exemple #13
0
func Message(rw *bufio.ReadWriter, target, message string) error {
	var (
		str string = fmt.Sprintf("PRIVMSG %s :%s\r\n", target, message)
		err error
	)
	_, err = rw.Write([]byte(str))
	if err != nil {
		return err
	}
	rw.Flush()
	return nil
}
Exemple #14
0
func Register(rw *bufio.ReadWriter, user, real string) error {
	var (
		str string = fmt.Sprintf("USER %s 0 * :%s\r\n", user, real)
		err error
	)
	_, err = rw.Write([]byte(str))
	if err != nil {
		return err
	}
	rw.Flush()
	return nil
}
Exemple #15
0
func Quit(rw *bufio.ReadWriter, reason string) error {
	var (
		str string = fmt.Sprintf("QUIT %s\r\n", reason)
		err error
	)
	_, err = rw.Write([]byte(str))
	if err != nil {
		return err
	}
	rw.Flush()
	return nil
}
Exemple #16
0
func Part(rw *bufio.ReadWriter, channel string) error {
	var (
		str string = fmt.Sprintf("PART %s\r\n", channel)
		err error
	)
	_, err = rw.Write([]byte(str))
	if err != nil {
		return err
	}
	rw.Flush()
	return nil
}
Exemple #17
0
func (b BinProt) Delete(rw *bufio.ReadWriter, key []byte) error {
	// Header
	writeReq(rw, Delete, len(key), 0, len(key), 0)
	// Body
	rw.Write(key)

	rw.Flush()

	// consume all of the response and discard
	_, err := consumeResponse(rw.Reader)
	return err
}
Exemple #18
0
func Ping(rw *bufio.ReadWriter, message string) error {
	var (
		str string = fmt.Sprintf("PING %s\r\n", message)
		err error
	)
	_, err = rw.Write([]byte(str))
	if err != nil {
		return err
	}
	rw.Flush()
	return nil
}
Exemple #19
0
func (b BinProt) Touch(rw *bufio.ReadWriter, key []byte) error {
	// Header
	writeReq(rw, Touch, len(key), 4, len(key)+4)
	// Extras
	binary.Write(rw, binary.BigEndian, common.Exp())
	// Body
	rw.Write(key)

	rw.Flush()

	// consume all of the response and discard
	return consumeResponse(rw.Reader)
}
Exemple #20
0
func (b BinProt) GAT(rw *bufio.ReadWriter, key []byte) ([]byte, error) {
	// Header
	writeReq(rw, GAT, len(key), 4, len(key), 0)
	// Extras
	binary.Write(rw, binary.BigEndian, common.Exp())
	// Body
	rw.Write(key)

	rw.Flush()

	// consume all of the response and return
	return consumeResponse(rw.Reader)
}
Exemple #21
0
func simple_handshake(rw *bufio.ReadWriter, input []byte) bool {
	s1 := make([]byte, HANDSHAKE_SIZE-4)
	s2 := input
	uptime := uint32(136793223)
	buf := new(bytes.Buffer)
	buf.WriteByte(0x03)
	binary.Write(buf, binary.BigEndian, uptime)
	buf.Write(s1)
	buf.Write(s2)
	rw.Write(buf.Bytes())
	rw.Flush()
	ReadBuf(rw, HANDSHAKE_SIZE)
	return true
}
Exemple #22
0
func copy(dest *bufio.ReadWriter, src *bufio.ReadWriter) {
	buf := make([]byte, 40*1024)
	for {
		n, err := src.Read(buf)
		if err != nil && err != io.EOF {
			return
		}
		if n == 0 {
			return
		}
		dest.Write(buf[0:n])
		dest.Flush()
	}
}
Exemple #23
0
func (b BinProt) BatchGet(rw *bufio.ReadWriter, keys [][]byte) ([][]byte, error) {
	for _, key := range keys {
		// Header
		writeReq(rw, GetQ, len(key), 0, len(key), 0)
		// Body
		rw.Write(key)
	}

	writeReq(rw, Noop, 0, 0, 0, 0)

	rw.Flush()

	// consume all of the responses and return
	return consumeBatchResponse(rw.Reader)
}
Exemple #24
0
func Copy(dest *bufio.ReadWriter, src *bufio.ReadWriter) {
	buf := make([]byte, 40*1024)
	for {
		n, err := src.Read(buf)
		if err != nil && err != io.EOF {
			log.Printf("Read failed: %v", err)
			return
		}
		if n == 0 {
			return
		}
		dest.Write(buf[0:n])
		dest.Flush()
	}
}
Exemple #25
0
func (b BinProt) Prepend(rw *bufio.ReadWriter, key, value []byte) error {
	// Prepend packet contains the req header and key only

	// Header
	bodylen := len(key) + len(value)
	writeReq(rw, Prepend, len(key), 0, bodylen, 0)
	// Body / data
	rw.Write(key)
	rw.Write(value)

	rw.Flush()

	// consume all of the response and discard
	_, err := consumeResponse(rw.Reader)
	return err
}
Exemple #26
0
func getLocalIntoBuf(rw *bufio.ReadWriter, cmd []byte, tokenBuf []byte, buf []byte, totalDataLength int) error {
	if _, err := rw.Write(cmd); err != nil {
		return err
	}

	if err := rw.Flush(); err != nil {
		return err
	}

	resHeader, err := binprot.ReadResponseHeader(rw)
	if err != nil {
		return err
	}

	err = binprot.DecodeError(resHeader)
	if err != nil {
		if _, ioerr := rw.Discard(int(resHeader.TotalBodyLength)); ioerr != nil {
			return ioerr
		}
		return err
	}

	serverFlags := make([]byte, 4)
	binary.Read(rw, binary.BigEndian, &serverFlags)

	// Read in token if requested
	if tokenBuf != nil {
		if _, err := io.ReadFull(rw, tokenBuf); err != nil {
			return err
		}
	}

	// Read in value
	if _, err := io.ReadFull(rw, buf); err != nil {
		return err
	}

	// consume padding at end of chunk if needed
	if len(buf) < totalDataLength {
		if _, ioerr := rw.Discard(totalDataLength - len(buf)); ioerr != nil {
			return ioerr
		}
	}

	return nil
}
Exemple #27
0
func client_simple_handshake(rw *bufio.ReadWriter) bool {
	c1 := make([]byte, HANDSHAKE_SIZE-4)
	uptime := uint32(136793223)
	buf := new(bytes.Buffer)
	buf.WriteByte(0x03)
	binary.Write(buf, binary.BigEndian, uptime)
	buf.Write(c1)
	rw.Write(buf.Bytes())
	rw.Flush()
	s0 := ReadBuf(rw, 1)
	if s0[0] != 0x03 {
		return false
	}
	s1 := ReadBuf(rw, HANDSHAKE_SIZE)
	c2 := s1
	rw.Write(c2)
	rw.Flush()
	ReadBuf(rw, HANDSHAKE_SIZE)
	return true
}
Exemple #28
0
func (b BinProt) Set(rw *bufio.ReadWriter, key, value []byte) error {
	// set packet contains the req header, flags, and expiration
	// flags are irrelevant, and are thus zero.
	// expiration could be important, so hammer with random values from 1 sec up to 1 hour

	// Header
	bodylen := 8 + len(key) + len(value)
	writeReq(rw, Set, len(key), 8, bodylen)
	// Extras
	binary.Write(rw, binary.BigEndian, uint32(0))
	binary.Write(rw, binary.BigEndian, common.Exp())
	// Body / data
	rw.Write(key)
	rw.Write(value)

	rw.Flush()

	// consume all of the response and discard
	return consumeResponse(rw.Reader)
}
Exemple #29
0
func (c *Client) populateOne(rw *bufio.ReadWriter, verb string, item *Item) error {
	if !legalKey(item.Key) {
		return ErrMalformedKey
	}
	var err error
	if verb == "cas" {
		_, err = fmt.Fprintf(rw, "%s %s %d %d %d %d\r\n",
			verb, item.Key, item.Flags, item.Expiration, len(item.Value), item.casid)
	} else {
		_, err = fmt.Fprintf(rw, "%s %s %d %d %d\r\n",
			verb, item.Key, item.Flags, item.Expiration, len(item.Value))
	}
	if err != nil {
		return err
	}
	if _, err = rw.Write(item.Value); err != nil {
		return err
	}
	if _, err := rw.Write(crlf); err != nil {
		return err
	}
	if err := rw.Flush(); err != nil {
		return err
	}
	line, err := rw.ReadSlice('\n')
	if err != nil {
		return err
	}
	switch {
	case bytes.Equal(line, resultStored):
		return nil
	case bytes.Equal(line, resultNotStored):
		return ErrNotStored
	case bytes.Equal(line, resultExists):
		return ErrCASConflict
	case bytes.Equal(line, resultNotFound):
		return ErrCacheMiss
	}
	return fmt.Errorf("memcache: unexpected response line from %q: %q", verb, string(line))
}
Exemple #30
0
// Copy from src buffer to destination buffer. One way.
func Copy(dest *bufio.ReadWriter, src *bufio.ReadWriter, route **Route) {
	buf := make([]byte, 40*1024)
	for {
		n, err := src.Read(buf)
		if err != nil && err != io.EOF {
			//log.Error("Read failed: %v", err)
			return
		}
		if n == 0 {
			return
		}
		(*route).Seen()
		_, err = dest.Write(buf[0:n])
		if err != nil && err != io.EOF {
			log.Warning("Could not write to dest", err)
		}
		err = dest.Flush()

		if err != nil && err != io.EOF {
			log.Warning("Could not flush to dest", err)
		}
	}
}