Esempio n. 1
0
// startAgent executes ssh-agent, and returns a Agent interface to it.
func startAgent(t *testing.T) (client Agent, socket string, cleanup func()) {
	if testing.Short() {
		// ssh-agent is not always available, and the key
		// types supported vary by platform.
		t.Skip("skipping test due to -short")
	}

	bin, err := exec.LookPath("ssh-agent")
	if err != nil {
		t.Skip("could not find ssh-agent")
	}

	cmd := exec.Command(bin, "-s")
	out, err := cmd.Output()
	if err != nil {
		t.Fatalf("cmd.Output: %v", err)
	}

	/* Output looks like:

		   SSH_AUTH_SOCK=/tmp/ssh-P65gpcqArqvH/agent.15541; export SSH_AUTH_SOCK;
	           SSH_AGENT_PID=15542; export SSH_AGENT_PID;
	           echo Agent pid 15542;
	*/
	fields := bytes.Split(out, []byte(";"))
	line := bytes.SplitN(fields[0], []byte("="), 2)
	line[0] = bytes.TrimLeft(line[0], "\n")
	if string(line[0]) != "SSH_AUTH_SOCK" {
		t.Fatalf("could not find key SSH_AUTH_SOCK in %q", fields[0])
	}
	socket = string(line[1])

	line = bytes.SplitN(fields[2], []byte("="), 2)
	line[0] = bytes.TrimLeft(line[0], "\n")
	if string(line[0]) != "SSH_AGENT_PID" {
		t.Fatalf("could not find key SSH_AGENT_PID in %q", fields[2])
	}
	pidStr := line[1]
	pid, err := strconv.Atoi(string(pidStr))
	if err != nil {
		t.Fatalf("Atoi(%q): %v", pidStr, err)
	}

	conn, err := net.Dial("unix", string(socket))
	if err != nil {
		t.Fatalf("net.Dial: %v", err)
	}

	ac := NewClient(conn)
	return ac, socket, func() {
		proc, _ := os.FindProcess(pid)
		if proc != nil {
			proc.Kill()
		}
		conn.Close()
		os.RemoveAll(filepath.Dir(socket))
	}
}
Esempio n. 2
0
// trimCut cuts the given label at min(maxlen, len(label)) and ensures the left
// and right cutsets are trimmed from their respective ends.
func trimCut(label []byte, maxlen int, left, right string) []byte {
	trim := bytes.TrimLeft(label, left)
	size := min(len(trim), maxlen)
	head := bytes.TrimRight(trim[:size], right)
	if len(head) == size {
		return head
	}
	tail := bytes.TrimLeft(trim[size:], right)
	if len(tail) > 0 {
		return append(head, tail[:size-len(head)]...)
	}
	return head
}
Esempio n. 3
0
func testWalkOutputs(t *testing.T, root node.Node, opts Options, expect []byte) {
	expect = bytes.TrimLeft(expect, "\n")

	buf := new(bytes.Buffer)
	walk := func(current State) error {
		s := fmt.Sprintf("%d %s\n", current.Depth, current.Node.(*mdag.ProtoNode).Data())
		t.Logf("walk: %s", s)
		buf.Write([]byte(s))
		return nil
	}

	opts.Func = walk
	if err := Traverse(root, opts); err != nil {
		t.Error(err)
		return
	}

	actual := buf.Bytes()
	if !bytes.Equal(actual, expect) {
		t.Error("error: outputs differ")
		t.Logf("expect:\n%s", expect)
		t.Logf("actual:\n%s", actual)
	} else {
		t.Logf("expect matches actual:\n%s", expect)
	}
}
Esempio n. 4
0
func (s *Server) ListenAndServe() error {
	uaddr, err := net.ResolveUDPAddr("udp", s.Addr)
	if err != nil {
		return err
	}
	conn, err := net.ListenUDP("udp", uaddr)
	if err != nil {
		return err
	}
	defer conn.Close()
	log.Println("listening on", uaddr)
	newmsg := make(chan Message)
	go messageReceiver(s, newmsg)
	for {
		b := make([]byte, 1024)
		n, addr, err := conn.ReadFrom(b)
		if err != nil {
			log.Println("error %v", err)
			continue
		}
		heartbeat := Message{From: addr.String()}
		b = bytes.TrimLeft(b[:n], "\n")
		heartbeat.extract(b) // remove newline
		newmsg <- heartbeat
	}

}
Esempio n. 5
0
File: para.go Progetto: akavel/vfmd
func (p ParagraphDetector) Detect(first, second Line, detectors Detectors) Handler {
	block := md.ParagraphBlock{}
	return HandlerFunc(func(next Line, ctx Context) (bool, error) {
		if next.EOF() {
			return p.close(block, ctx)
		}
		if len(block.Raw) == 0 {
			block.Raw = append(block.Raw, md.Run(next))
			return true, nil
		}
		prev := Line(block.Raw[len(block.Raw)-1])
		// TODO(akavel): support HTML parser & related interactions [#paragraph-line-sequence]
		if prev.isBlank() {
			return p.close(block, ctx)
		}
		nextBytes := bytes.TrimRight(next.Bytes, "\n")
		if !next.hasFourSpacePrefix() {
			if reHorizontalRule.Match(nextBytes) ||
				(p.InQuote && bytes.HasPrefix(bytes.TrimLeft(next.Bytes, " "), []byte(">"))) ||
				(p.InList && reOrderedList.Match(nextBytes)) ||
				(p.InList && reUnorderedList.Match(nextBytes)) {
				return p.close(block, ctx)
			}
		}
		block.Raw = append(block.Raw, md.Run(next))
		return true, nil
	})
}
Esempio n. 6
0
func Default(buffer chan []Module, confpath string) {
	cmd := exec.Command("/usr/bin/i3status", "-c", confpath)
	reader, err := cmd.StdoutPipe()
	if err != nil {
		panic(err)
	}
	cmd.Start()

	bufreader := bufio.NewReader(reader)
	for {
		line, _, err := bufreader.ReadLine()
		if err != nil {
			// fmt.Println(err)
			buffer <- []Module{Module{FullText: "error"}}
			continue
		}
		line = bytes.TrimLeft(line, ",")

		var modules []Module
		err = json.Unmarshal(line, &modules)

		if err != nil {
			buffer <- []Module{Module{FullText: "error"}}
			continue
		}
		buffer <- modules
	}
}
Esempio n. 7
0
func parseRuleInfo(ruleinfo []byte) (map[string]string, error) {
	if !bytes.HasPrefix(ruleinfo, expectedRuleChunkHeader) {
		logger.LogSteamError(ErrPacketHeader)
		return nil, ErrPacketHeader
	}

	ruleinfo = bytes.TrimLeft(ruleinfo, headerStr)
	numrules := int(binary.LittleEndian.Uint16(ruleinfo[1:3]))

	if numrules == 0 {
		return nil, ErrNoRules
	}

	b := bytes.Split(ruleinfo[3:], []byte{0x00})
	m := make(map[string]string)

	var key string
	for i, y := range b {
		if i%2 != 1 {
			key = strings.TrimRight(string(y), "\x00")
		} else {
			m[key] = strings.TrimRight(string(b[i]), "\x00")
		}
	}

	return m, nil
}
Esempio n. 8
0
File: expr.go Progetto: zchee/kati
// concatLine concatinates line with "\\\n" in function expression.
// TODO(ukai): less alloc?
func concatLine(v Value) Value {
	switch v := v.(type) {
	case literal:
		for {
			s := string(v)
			i := strings.Index(s, "\\\n")
			if i < 0 {
				return v
			}
			v = literal(s[:i] + strings.TrimLeft(s[i+2:], " \t"))
		}
	case tmpval:
		for {
			b := []byte(v)
			i := bytes.Index(b, []byte{'\\', '\n'})
			if i < 0 {
				return v
			}
			var buf bytes.Buffer
			buf.Write(b[:i])
			buf.Write(bytes.TrimLeft(b[i+2:], " \t"))
			v = tmpval(buf.Bytes())
		}
	case expr:
		for i := range v {
			switch vv := v[i].(type) {
			case literal, tmpval:
				v[i] = concatLine(vv)
			}
		}
		return v
	}
	return v
}
Esempio n. 9
0
func fetchYahooData(t time.Time) (*yahooCurrencyResponse, error) {
	r, err := http.Get("http://finance.yahoo.com/connection/currency-converter-cache?date=" + string(toDate(t)))

	if err != nil {
		return nil, err
	}

	defer r.Body.Close()

	body, err := ioutil.ReadAll(r.Body)

	if err != nil {
		return nil, err
	}

	// Yahoo finance cache returns JavaScript. Remove that.
	body = bytes.TrimSpace(body)
	body = bytes.TrimLeft(body, "/**/YAHOO.Finance.CurrencyConverter.addConversionRates(")
	body = bytes.TrimRight(body, ");")

	target := new(yahooCurrencyResponse)
	err = json.Unmarshal(body, target)

	if err != nil {
		return nil, err
	}

	return target, nil
}
Esempio n. 10
0
File: para.go Progetto: akavel/vfmd
func trim(region md.Raw) md.Raw {
	// rtrim
	for len(region) > 0 {
		n := len(region)
		l := bytes.TrimRight(region[n-1].Bytes, mdutils.Whites)
		if len(l) == 0 {
			region = region[:n-1]
			continue
		}
		if len(l) < len(region[n-1].Bytes) {
			region = append(append(md.Raw{}, region[:n-1]...), md.Run{
				Line:  region[n-1].Line,
				Bytes: l,
			})
		}
		break
	}
	// ltrim
	for len(region) > 0 {
		l := bytes.TrimLeft(region[0].Bytes, mdutils.Whites)
		if len(l) == 0 {
			region = region[1:]
			continue
		}
		if len(l) < len(region[0].Bytes) {
			region = append(md.Raw{{
				Line:  region[0].Line,
				Bytes: l,
			}}, region[1:]...)
		}
		break
	}
	return region
}
Esempio n. 11
0
func main() {
	whitespace := " \t\r\n"

	padded := []byte("  \t\r\n\r\n\r\n  hello!!!    \t\t\t\t")
	trimmed := bytes.Trim(padded, whitespace)
	log.Printf("Trim removed runes in %q from the ends of %q to produce %q", whitespace, padded, trimmed)

	rhyme := []byte("aabbccddee")
	trimFunced := bytes.TrimFunc(rhyme, trimOdd)
	log.Printf("TrimFunc removed 'odd' runes from %q to produce %q", rhyme, trimFunced)

	leftTrimmed := bytes.TrimLeft(padded, whitespace)
	log.Printf("TrimLeft removed runes in %q from the left side of %q to produce %q", whitespace, padded, leftTrimmed)

	leftTrimFunced := bytes.TrimLeftFunc(rhyme, trimOdd)
	log.Printf("TrimLeftFunc removed 'odd' runes from the left side of %q to produce %q", rhyme, leftTrimFunced)

	rightTrimmed := bytes.TrimRight(padded, whitespace)
	log.Printf("TrimRight removed runes in %q from the right side of %q to produce %q", whitespace, padded, rightTrimmed)

	rightTrimFunced := bytes.TrimRightFunc(rhyme, trimOdd)
	log.Printf("TrimRightFunc removed 'odd' runes from the right side of %q to produce %q", rhyme, rightTrimFunced)

	spaceTrimmed := bytes.TrimSpace(padded)
	log.Printf("TrimSpace trimmed all whitespace from the ends of %q to produce %q", padded, spaceTrimmed)
}
Esempio n. 12
0
func getRulesInfo(host string, timeout int) ([]byte, error) {
	conn, err := net.DialTimeout("udp", host, time.Duration(timeout)*time.Second)
	if err != nil {
		logger.LogSteamError(ErrHostConnection(err.Error()))
		return nil, ErrHostConnection(err.Error())
	}

	conn.SetDeadline(time.Now().Add(time.Duration(timeout-1) * time.Second))
	defer conn.Close()

	_, err = conn.Write(rulesChallengeReq)
	if err != nil {
		logger.LogSteamError(ErrDataTransmit(err.Error()))
		return nil, ErrDataTransmit(err.Error())
	}

	challengeNumResp := make([]byte, maxPacketSize)
	_, err = conn.Read(challengeNumResp)
	if err != nil {
		logger.LogSteamError(ErrDataTransmit(err.Error()))
		return nil, ErrDataTransmit(err.Error())
	}
	if !bytes.HasPrefix(challengeNumResp, expectedRulesRespHeader) {
		logger.LogSteamError(ErrChallengeResponse)
		return nil, ErrChallengeResponse
	}

	challengeNum := bytes.TrimLeft(challengeNumResp, headerStr)
	challengeNum = challengeNum[1:5]
	request := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0x56}
	request = append(request, challengeNum...)

	_, err = conn.Write(request)
	if err != nil {
		logger.LogSteamError(ErrDataTransmit(err.Error()))
		return nil, ErrDataTransmit(err.Error())
	}

	var buf [maxPacketSize]byte
	numread, err := conn.Read(buf[:maxPacketSize])
	if err != nil {
		logger.LogSteamError(ErrDataTransmit(err.Error()))
		return nil, ErrDataTransmit(err.Error())
	}
	var rulesInfo []byte
	if bytes.HasPrefix(buf[:maxPacketSize], multiPacketRespHeader) {
		// handle multi-packet response
		first := buf[:maxPacketSize]
		first = first[:numread]
		rulesInfo, err = handleMultiPacketResponse(conn, first)
		if err != nil {
			logger.LogSteamError(ErrDataTransmit(err.Error()))
			return nil, ErrDataTransmit(err.Error())
		}
	} else {
		rulesInfo = make([]byte, numread)
		copy(rulesInfo, buf[:numread])
	}
	return rulesInfo, nil
}
Esempio n. 13
0
// parser bytes in reader to blocks
func (cp *CommonParser) ParseReader(r io.Reader) ([]Block, error) {
	var (
		currentBlock Block   = nil
		blocks       []Block = nil
		reader               = bufio.NewReader(r)
	)
	for {
		lineData, _, err := reader.ReadLine()
		// first block
		if currentBlock == nil {
			if len(lineData) == 0 {
				continue
			}
			if currentBlock = cp.Detect(bytes.TrimLeft(lineData, COMMON_PARSER_PREFIX)); currentBlock == nil {
				return nil, errors.New("block-parse-first-error")
			}
			continue
		}

		if bytes.HasPrefix(lineData, []byte(COMMON_PARSER_PREFIX)) {
			// try to switch
			newBlock := cp.Detect(bytes.TrimLeft(lineData, COMMON_PARSER_PREFIX))
			if newBlock != nil {
				blocks = append(blocks, currentBlock)
				currentBlock = newBlock
				continue
			}
		}

		// write block
		if err := currentBlock.Write(append(lineData, []byte("\n")...)); err != nil {
			return nil, err
		}

		if err != nil {
			if err != io.EOF {
				return nil, err
			}
			break
		}
	}
	// do not forget last block
	if currentBlock != nil {
		blocks = append(blocks, currentBlock)
	}
	return blocks, nil
}
Esempio n. 14
0
func Decode(raw []byte) (msg *Message) {
	msg = new(Message)
	raw = bytes.TrimLeft(raw, " ")

	if len(raw) <= 0 {
		return nil
	}

	//If message has a prefix, pull it off
	if raw[0] == ':' {
		ind := bytes.IndexByte(raw, ' ')
		msg.Prefix = string(raw[1:ind])
		raw = raw[ind+1:]
		raw = bytes.TrimLeft(raw, " ")
	}

	//If message has <trailing> pull it off
	if msgStart := bytes.IndexByte(raw, ':'); msgStart > -1 {
		trailBytes := raw[msgStart+1:]
		raw = raw[0:msgStart]

		//Check if the message contains a CTCP command
		if len(trailBytes) > 0 && trailBytes[0] == '\x01' {
			//Find the terminating 0x01
			trailBytes = trailBytes[1:]
			ctcpEnd := bytes.IndexByte(trailBytes, ' ')
			if ctcpEnd < 0 { //Nothing in the ctcp but the command
				msg.Ctcp = string(trailBytes[:len(trailBytes)-1])
			} else {
				msg.Ctcp = string(trailBytes[:ctcpEnd])
				msg.Trailing = string(trailBytes[ctcpEnd+1 : len(trailBytes)-1])
			}
		} else {
			msg.Trailing = string(trailBytes)
		}
	}

	args := bytes.Fields(raw)
	msg.Command = string(args[0])

	msg.Args = make([]string, len(args)-1)
	for i, v := range args[1:] {
		msg.Args[i] = string(v)
	}

	return
}
Esempio n. 15
0
func (c *StateObject) setAddr(addr []byte, value common.Hash) {
	v, err := rlp.EncodeToBytes(bytes.TrimLeft(value[:], "\x00"))
	if err != nil {
		// if RLPing failed we better panic and not fail silently. This would be considered a consensus issue
		panic(err)
	}
	c.trie.Update(addr, v)
}
Esempio n. 16
0
// ParseFile creates a new Config and parses the file configuration from the
// named file.
func LoadConfig(name string) (*Config, error) {
	file, err := os.Open(name)
	if err != nil {
		return nil, err
	}

	cfg := &Config{
		file.Name(),
		make(map[int][]string),
		make(map[string]string),
		make(map[string]int64),
		sync.RWMutex{},
	}
	cfg.Lock()
	defer cfg.Unlock()
	defer file.Close()

	var comment bytes.Buffer
	buf := bufio.NewReader(file)

	for nComment, off := 0, int64(1); ; {
		line, _, err := buf.ReadLine()
		if err == io.EOF {
			break
		}
		if bytes.Equal(line, bEmpty) {
			continue
		}

		off += int64(len(line))

		if bytes.HasPrefix(line, bComment) {
			line = bytes.TrimLeft(line, "#")
			line = bytes.TrimLeftFunc(line, unicode.IsSpace)
			comment.Write(line)
			comment.WriteByte('\n')
			continue
		}
		if comment.Len() != 0 {
			cfg.comment[nComment] = []string{comment.String()}
			comment.Reset()
			nComment++
		}

		val := bytes.SplitN(line, bEqual, 2)
		if bytes.HasPrefix([]byte(strings.TrimSpace(string(val[1]))), bDQuote) {
			val[1] = bytes.Trim([]byte(strings.TrimSpace(string(val[1]))), `"`)
		}

		key := strings.TrimSpace(string(val[0]))
		cfg.comment[nComment-1] = append(cfg.comment[nComment-1], key)
		cfg.data[key] = strings.TrimSpace(string(val[1]))
		cfg.offset[key] = off
	}
	AppConf = cfg

	return cfg, nil
}
Esempio n. 17
0
func nextArg(message []byte, sep string) (string, []byte) {
	message = bytes.TrimLeft(message, " \n\r\t")
	data := bytes.SplitN(message, []byte(sep), 2)
	if len(data) == 2 {
		return string(data[0]), data[1]
	} else {
		return string(data[0]), []byte{}
	}
}
Esempio n. 18
0
func (l *LDIF) parseEntry(lines [][]byte) (entry *Entry, err error) {
	// for i, line := range lines {
	//     fmt.Fprintf(os.Stderr, "% 2d %s\n", i, line)
	// }
	if l.version == 0 && bytes.HasPrefix(lines[0], []byte("version:")) {
		line := bytes.TrimLeft(lines[0][8:], SPACES)
		if version, err := strconv.Atoi(string(line)); err != nil {
			return nil, err
		} else {
			if version != 1 {
				return nil, errors.New("Invalid version spec " + string(line))
			}
			l.version = 1
			lines = lines[1:]
		}
	}

	if !bytes.HasPrefix(lines[0], []byte("dn:")) {
		return nil, errors.New("Missing dn:")
	}
	_, val, err := l.parseLine(lines[0])
	if err != nil {
		return nil, err
	}
	dn := val

	lines = lines[1:]
	if bytes.HasPrefix(lines[0], []byte("changetype:")) {
		_, val, err := l.parseLine(lines[0])
		if err != nil {
			return nil, err
		}
		l.changeType = val
		lines = lines[1:]
	}
	if l.changeType != "" {
		return nil, errors.New("change records not supported")
	}

	attrs := make(map[string][]string)
	for i := 0; i < len(lines); i++ {
		attr, val, err := l.parseLine(lines[i])
		if err != nil {
			if !l.RelaxedParser {
				return nil, err
			} else {
				continue
			}
		}
		if _, ok := attrs[attr]; ok {
			attrs[attr] = append(attrs[attr], string(val))
		} else {
			attrs[attr] = []string{string(val)}
		}
	}
	return NewEntry(dn, attrs), nil
}
Esempio n. 19
0
File: main.go Progetto: kiyor/stf
// dump request , body true/false, print true/false
func dumpResponse(r *http.Response, b, p bool, host string) []byte {
	dump, err := httputil.DumpResponse(r, b)
	if err != nil {
		log.Println(err.Error())
	}
	// 	isGzip := false
	// 	if v, ok := r.Header["Accept-Encoding"]; ok {
	// 		if strings.Contains(v[0], "gzip") {
	// 			isGzip = true
	// 			log.Println("is gzip")
	// 		}
	// 	}
	if p {
		index := bytes.Index(dump, []byte("\r\n\r\n"))
		headers := dump[:index]
		body := bytes.TrimLeft(dump[index:], "\r\n\r\n")
		// 		body = bytes.TrimLeft(body, string([]byte{13, 10, 13, 10}))

		// 		if isGzip {
		// 			reader := bytes.NewReader(body)
		// 			g, err := gzip.NewReader(reader)
		// 			if err != nil {
		// 				log.Println(err.Error())
		// 			}
		// 			body, err = ioutil.ReadAll(g)
		// 			if err != nil {
		// 				log.Println(err.Error())
		// 			}
		// 		}
		if *veryverbose {
			now := time.Now()
			dirname := "/tmp/stfdump/" + host
			if _, err := os.Stat(dirname); err != nil {
				if err := os.MkdirAll(dirname, 0755); err != nil {
					log.Fatalln(err.Error())
				}
			}
			filename := fmt.Sprintf("%s/%d<", dirname, now.UnixNano())
			ioutil.WriteFile(filename, body, 0644)
		}
		if *colors {
			// 			color.Printf("@{b}%s@{|}", string(dump))
			color.Printf("@{c}%v@{|}\n", string(headers))
			if *showBody {
				color.Printf("@{g}%v@{|}\n", string(body))
			}
			// 			color.Printf("@{g}%v@{|}\n", ehex.EncodeToString(body))
			// 			color.Printf("@{g}%v@{|}\n", body)
		} else {
			// 			fmt.Print(string(dump))
			fmt.Println(string(headers))
			fmt.Println(string(body))
		}
	}
	return dump
}
Esempio n. 20
0
func (l *LDIF) parseLine(line []byte) (attr, val string, err error) {
	off := 0
	for len(line) > off && line[off] != ':' {
		off++
		if off >= len(line) {
			return
		}
	}
	if off == len(line) {
		err = errors.New("Missing : in line")
		return
	}
	if off > len(line)-2 {
		err = errors.New("empty value")
		return
	}

	attr = string(line[0:off])
	if err = validAttr(attr); err != nil {
		attr = ""
		val = ""
		return
	}

	switch line[off+1] {
	case ':':
		var n int
		value := bytes.TrimLeft(line[off+2:], SPACES)
		// fmt.Fprintf(os.Stderr, "LINE=%s\nVALUE=%s\n", line, value)
		dec := make([]byte, base64.StdEncoding.DecodedLen(len(value)))
		n, err = base64.StdEncoding.Decode(dec, value)
		if err != nil {
			return
		}
		val = string(dec[:n])
	case '<': // FIXME missing return for *net.URL type
		val = string(bytes.TrimLeft(line[off+2:], SPACES))
	default:
		val = string(bytes.TrimLeft(line[off+2:], SPACES))
	}

	return
}
Esempio n. 21
0
// New creates a gelf logger using the configuration passed in on the
// context. Supported context configuration variables are
// gelf-address, & gelf-tag.
func New(ctx logger.Context) (logger.Logger, error) {
	// parse gelf address
	address, err := parseAddress(ctx.Config["gelf-address"])
	if err != nil {
		return nil, err
	}

	// collect extra data for GELF message
	hostname, err := ctx.Hostname()
	if err != nil {
		return nil, fmt.Errorf("gelf: cannot access hostname to set source field")
	}

	// remove trailing slash from container name
	containerName := bytes.TrimLeft([]byte(ctx.ContainerName), "/")

	// parse log tag
	tag, err := loggerutils.ParseLogTag(ctx, "")
	if err != nil {
		return nil, err
	}

	extra := map[string]interface{}{
		"_container_id":   ctx.ContainerID,
		"_container_name": string(containerName),
		"_image_id":       ctx.ContainerImageID,
		"_image_name":     ctx.ContainerImageName,
		"_command":        ctx.Command(),
		"_tag":            tag,
		"_created":        ctx.ContainerCreated,
	}

	extraAttrs := ctx.ExtraAttributes(func(key string) string {
		if key[0] == '_' {
			return key
		}
		return "_" + key
	})
	for k, v := range extraAttrs {
		extra[k] = v
	}

	// create new gelfWriter
	gelfWriter, err := gelf.NewWriter(address)
	if err != nil {
		return nil, fmt.Errorf("gelf: cannot connect to GELF endpoint: %s %v", address, err)
	}

	return &gelfLogger{
		writer:   gelfWriter,
		ctx:      ctx,
		hostname: hostname,
		extra:    extra,
	}, nil
}
Esempio n. 22
0
// parser Reader to blocks
func (mp *MdParser) ParseReader(r io.Reader) ([]Block, error) {
	var (
		currentBlock Block   = nil
		blocks       []Block = nil
		reader               = bufio.NewReader(r)
		isLined      bool
	)
	for {
		lineData, _, err := reader.ReadLine()
		if currentBlock == nil {
			if len(lineData) == 0 {
				continue
			}
			if len(blocks) == 0 {
				// the first block need be MetaBlock
				if currentBlock = mp.Detect(bytes.TrimLeft(lineData, MD_PARSER_PREFIX)); currentBlock == nil {
					return nil, errors.New("block-parse-first-error")
				}
				continue
			}
		}

		// when parsing first block, check end mark to close the block.
		if len(blocks) == 0 && bytes.Equal(lineData, []byte(MD_PARSER_PREFIX)) {
			blocks = append(blocks, currentBlock)
			currentBlock = new(MarkdownBlock).New()
			continue
		}

		// write block
		if len(lineData) > 0 || isLined {
			if err := currentBlock.Write(append(lineData, []byte("\n")...)); err != nil {
				return nil, err
			}
			if len(blocks) > 0 {
				isLined = true
			}
		}

		if err != nil {
			if err != io.EOF {
				return nil, err
			}
			break
		}
	}

	// this block must be md block,
	// if empty, do not use it, so same behavior as without -----markdown block
	if currentBlock != nil && len(currentBlock.Bytes()) > 0 {
		blocks = append(blocks, currentBlock)
	}
	return blocks, nil
}
Esempio n. 23
0
// Split will split a set of HL7 messages
//	\x0b MESSAGE \x1c\x0d
func Split(buf []byte) [][]byte {
	msgSep := []byte{'\x1c', '\x0d'}
	msgs := bytes.Split(buf, msgSep)
	vmsgs := [][]byte{}
	for _, msg := range msgs {
		if len(msg) < 4 {
			continue
		}
		msg = bytes.TrimLeft(msg, "\x0b")
		vmsgs = append(vmsgs, msg)
	}
	return vmsgs
}
Esempio n. 24
0
// updateTrie writes cached storage modifications into the object's storage trie.
func (self *StateObject) updateTrie(db trie.Database) {
	tr := self.getTrie(db)
	for key, value := range self.dirtyStorage {
		delete(self.dirtyStorage, key)
		if (value == common.Hash{}) {
			tr.Delete(key[:])
			continue
		}
		// Encoding []byte cannot fail, ok to ignore the error.
		v, _ := rlp.EncodeToBytes(bytes.TrimLeft(value[:], "\x00"))
		tr.Update(key[:], v)
	}
}
Esempio n. 25
0
func (r *StraceRecipe) Prepare(ctx *types.BuildContext) error {
	srcdir := r.UnpackedDir(ctx, r.Info())

	// Patch
	cmd := exec.Command("patch", "-p2")
	cmd.Stdin = bytes.NewBuffer(bytes.TrimLeft(stracePatch, "\r\n"))
	cmd.Dir = srcdir

	if err := cmd.Run(); err != nil {
		log.WithField("err", err).Error("Could not patch source")
		return err
	}

	return nil
}
Esempio n. 26
0
// Make things a little more readable. Format as strings with %q when we can,
// strip down empty slices, and don't print the internals from buffers.
func formatter(i interface{}, size int) (s string) {
	// don't show the internal state of buffers
	switch i := i.(type) {
	case *bufio.Reader:
		s = "&bufio.Reader{}"
	case *bufio.Writer:
		s = "&bufio.Writer{}"
	case *bytes.Buffer:
		s = fmt.Sprintf("&bytes.Buffer{%q}", i.String())
	case *bytes.Reader:
		v := reflect.ValueOf(i)
		// TODO: should probably iterate to find the slice in case the name changes
		if b, ok := v.FieldByName("s").Interface().([]byte); ok {
			if len(b) > size {
				b = b[:size]
			}
			s = fmt.Sprintf("&bytes.Reader{%q}", b)
		}
	case *strings.Reader:
		v := reflect.ValueOf(i)
		if f, ok := v.FieldByName("s").Interface().(string); ok {
			s = fmt.Sprintf("&strings.Reader{%q}", f)
		}
	case []byte:
		// bytes slices are often empty, so trim them down
		b := bytes.TrimLeft(i, "\x00")
		if len(b) == 0 {
			s = "[]byte{0...}"
		} else if utf8.Valid(i) {
			s = fmt.Sprintf("[]byte{%q}", i)
		} else {
			s = fmt.Sprintf("%#v", i)
		}
	case string:
		s = fmt.Sprintf("%q", i)
	}

	if s == "" {
		s = fmt.Sprintf("%#v", i)
	}

	if len(s) > size {
		last := s[len(s)-1]
		s = s[:size] + "..." + string(last)
	}

	return s
}
Esempio n. 27
0
func getPlayerInfo(host string, timeout int) ([]byte, error) {
	conn, err := net.DialTimeout("udp", host, time.Duration(timeout)*time.Second)
	if err != nil {
		logger.LogSteamError(ErrHostConnection(err.Error()))
		return nil, ErrHostConnection(err.Error())
	}

	defer conn.Close()
	conn.SetDeadline(time.Now().Add(time.Duration(timeout-1) * time.Second))

	_, err = conn.Write(playerChallengeReq)
	if err != nil {
		logger.LogSteamError(ErrDataTransmit(err.Error()))
		return nil, ErrDataTransmit(err.Error())
	}

	challengeNumResp := make([]byte, maxPacketSize)
	_, err = conn.Read(challengeNumResp)
	if err != nil {
		logger.LogSteamError(ErrDataTransmit(err.Error()))
		return nil, ErrDataTransmit(err.Error())
	}
	if !bytes.HasPrefix(challengeNumResp, expectedPlayerRespHeader) {
		logger.LogSteamError(ErrChallengeResponse)
		return nil, ErrChallengeResponse
	}
	challengeNum := bytes.TrimLeft(challengeNumResp, headerStr)
	challengeNum = challengeNum[1:5]
	request := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0x55}
	request = append(request, challengeNum...)

	_, err = conn.Write(request)
	if err != nil {
		logger.LogSteamError(ErrDataTransmit(err.Error()))
		return nil, ErrDataTransmit(err.Error())
	}
	var buf [maxPacketSize]byte
	numread, err := conn.Read(buf[:maxPacketSize])
	if err != nil {
		logger.LogSteamError(ErrDataTransmit(err.Error()))
		return nil, ErrDataTransmit(err.Error())
	}
	pi := make([]byte, numread)
	copy(pi, buf[:numread])

	return pi, nil
}
Esempio n. 28
0
// New creates a gelf logger using the configuration passed in on the
// context. Supported context configuration variables are
// gelf-address, & gelf-tag.
func New(ctx logger.Context) (logger.Logger, error) {
	// parse gelf address
	address, err := parseAddress(ctx.Config["gelf-address"])
	if err != nil {
		return nil, err
	}

	// collect extra data for GELF message
	hostname, err := ctx.Hostname()
	if err != nil {
		return nil, fmt.Errorf("gelf: cannot access hostname to set source field")
	}

	// remove trailing slash from container name
	containerName := bytes.TrimLeft([]byte(ctx.ContainerName), "/")

	// parse log tag
	tag, err := loggerutils.ParseLogTag(ctx, "")
	if err != nil {
		return nil, err
	}

	fields := gelfFields{
		hostname:      hostname,
		containerID:   ctx.ContainerID,
		containerName: string(containerName),
		imageID:       ctx.ContainerImageID,
		imageName:     ctx.ContainerImageName,
		command:       ctx.Command(),
		tag:           tag,
		created:       ctx.ContainerCreated,
	}

	// create new gelfWriter
	gelfWriter, err := gelf.NewWriter(address)
	if err != nil {
		return nil, fmt.Errorf("gelf: cannot connect to GELF endpoint: %s %v", address, err)
	}

	return &gelfLogger{
		writer: gelfWriter,
		ctx:    ctx,
		fields: fields,
	}, nil
}
Esempio n. 29
0
// Compile .go file, import data from .o file, and write Go string version.
func mkbuiltin(w io.Writer, name string) {
	args := []string{"tool", "compile", "-A"}
	if name == "runtime" {
		args = append(args, "-u")
	}
	args = append(args, "builtin/"+name+".go")

	if err := exec.Command("go", args...).Run(); err != nil {
		log.Fatal(err)
	}
	obj := name + ".o"
	defer os.Remove(obj)

	b, err := ioutil.ReadFile(obj)
	if err != nil {
		log.Fatal(err)
	}

	// Look for $$ that introduces imports.
	i := bytes.Index(b, []byte("\n$$\n"))
	if i < 0 {
		log.Fatal("did not find beginning of imports")
	}
	i += 4

	// Look for $$ that closes imports.
	j := bytes.Index(b[i:], []byte("\n$$\n"))
	if j < 0 {
		log.Fatal("did not find end of imports")
	}
	j += i + 4

	// Process and reformat imports.
	fmt.Fprintf(w, "\nconst %simport = \"\"", name)
	for _, p := range bytes.SplitAfter(b[i:j], []byte("\n")) {
		// Chop leading white space.
		p = bytes.TrimLeft(p, " \t")
		if len(p) == 0 {
			continue
		}

		fmt.Fprintf(w, " +\n\t%q", p)
	}
	fmt.Fprintf(w, "\n")
}
Esempio n. 30
0
func GetPageConfigJson(content []byte) ([]byte, error) {
	var jStr []byte
	for _, line := range bytes.Split(content, []byte("\n")) {
		if len(line) == 0 {
			continue
		}
		line = bytes.TrimLeft(line, " ")
		if bytes.HasPrefix(line, []byte("g_page_config = ")) {
			jStr = line[16:]
			jStr = jStr[:len(jStr)-1]
			break
		}
	}
	if len(jStr) == 0 {
		return nil, fmt.Errorf("g_global_config not found")
	}
	return jStr, nil
}