Exemple #1
0
// inputData reads data from filename. Filename can be empty or "-" for stdin, other for file
// if filename can be converted to int (decimal), then it is treated as file descriptor that has been
// opened by a parent process.
func inputData(filename string, maxData int64) ([]byte, error) {
	if filename == "" || filename == "-" {
		return utils.MaxStdinRead(maxData)
	}
	fdI, err := strconv.Atoi(filename)
	if err == nil {
		fd := os.NewFile(uintptr(fdI), "fd/"+filename)
		defer fd.Close()
		return utils.MaxRead(maxData, fd)
	}
	return utils.MaxReadFile(maxData, filename)
}
Exemple #2
0
// LimitGet fetches at most limit bytes from url. Throws error if more data is available
func (sprox Proxy) LimitGet(url string, limit int64) ([]byte, error) {
	resp, err := sprox.Get(url)
	if err != nil {
		return nil, err
	}
	body, err := utils.MaxRead(limit, resp.Body)
	defer resp.Body.Close()
	if err != nil {
		return nil, err
	}
	return body, nil
}
Exemple #3
0
// LimitPost posts body and reads a maximum of limit. Throws error if more data is available
func (sprox Proxy) LimitPost(url string, bodyType string, body io.Reader, limit int64) ([]byte, error) {
	resp, err := sprox.Post(url, bodyType, body)
	if err != nil {
		return nil, err
	}
	retbody, err := utils.MaxRead(limit, resp.Body)
	defer resp.Body.Close()
	if err != nil {
		return nil, err
	}
	return retbody, nil
}
Exemple #4
0
func TestProxy_Get(t *testing.T) {
	// this test requires that Tor is in a working state
	if testing.Short() {
		t.Skip("test skipped in -short run")
	}
	url := "http://google.com/"
	socksProxy := Proxy("socks5://127.0.0.1:9050")
	resp, err := socksProxy.Get(url)
	if err != nil {
		t.Fatalf("Could not call: %s", err)
	}
	body, err := utils.MaxRead(250000, resp.Body)
	fmt.Println(string(body))
}
Exemple #5
0
// ProcessPost verifies and adds a post to the database.
func (ms MessageServer) ProcessPost(postdata io.ReadCloser, oneTime bool, expireRequest uint64) string {
	data, err := utils.MaxRead(ms.MaxPostSize, postdata)
	if err != nil {
		return "ERROR: Message too big\n"
	}
	if len(data) < ms.MinPostSize {
		return "ERROR: Message too small\n"
	}
	signheader, err := message.Base64Message(data).GetSignHeader()
	if err != nil {
		log.Debugf("Post:GetSignHeader: %s\n", err)
		return "ERROR: Sign Header\n"
	}
	details, err := message.VerifySignature(*signheader, ms.MinHashCashBits)
	if err != nil {
		log.Debugf("Post:VerifySignature: %s\n", err)
		return "ERROR: HashCash\n"
	}
	constantRecipientPub, MessageID, err := deferVerify(data)
	if err != nil {
		log.Debugf("Post:deferVerify: %s\n", err)
		return "ERROR: Verify\n"
	}
	if *MessageID != details.MsgID {
		log.Debugs("Post:MessageID\n")
		return "ERROR: MessageID\n"
	}
	msgStruct := &structs.MessageStruct{
		MessageID:              *MessageID,
		ReceiverConstantPubKey: *constantRecipientPub,
		SignerPub:              details.PublicKey,
		OneTime:                oneTime,
		Sync:                   false,
		Hidden:                 false,
		ExpireRequest:          expireRequest,
	}
	if !oneTime {
		if message.KeyIsSync(constantRecipientPub) {
			msgStruct.Sync = true
		}
	} else {
		msgStruct.Sync = false
	}
	if message.KeyIsHidden(constantRecipientPub) {
		msgStruct.Hidden = true
	}

	sigStruct := &structs.SignerStruct{
		PublicKey: details.PublicKey,
		Nonce:     details.HashCashNonce,
		Bits:      details.HashCashBits,
	}
	sigStruct.MaxMessagesPosted, sigStruct.MaxMessagesRetained, sigStruct.ExpireTarget = ms.calcLimits(details.HashCashBits)
	_, _ = msgStruct, sigStruct
	ms.RandomSleep()
	// err = ms.DB.Put(msgStruct, sigStruct, data)
	err = ms.DB.PutNotify(msgStruct, sigStruct, data, ms.notifyChan)
	ms.RandomSleep()
	if err != nil {
		log.Debugf("Post:MessageDB: %s\n", err)
		return fmt.Sprintf("ERROR: %s\n", err)
	}
	log.Debugf("Post:Added: %x\n", MessageID[:12])
	if ms.Stat {
		stat.Input <- stat.Post
	}
	return "SUCCESS: Connection close\n"
}