tor-dam

tor distributed announce mechanism (not a dht)
git clone https://git.parazyd.org/tor-dam
Log | Files | Refs | README | LICENSE

helpers.go (2852B)


      1 package damlib
      2 
      3 /*
      4  * Copyright (c) 2017-2018 Dyne.org Foundation
      5  * tor-dam is written and maintained by Ivan Jelincic <parazyd@dyne.org>
      6  *
      7  * This file is part of tor-dam
      8  *
      9  * This program is free software: you can redistribute it and/or modify
     10  * it under the terms of the GNU Affero General Public License as published by
     11  * the Free Software Foundation, either version 3 of the License, or
     12  * (at your option) any later version.
     13  *
     14  * This program is distributed in the hope that it will be useful,
     15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17  * GNU Affero General Public License for more details.
     18  *
     19  * You should have received a copy of the GNU Affero General Public License
     20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     21  */
     22 
     23 import (
     24 	"bytes"
     25 	"compress/gzip"
     26 	"crypto/rand"
     27 	"encoding/base64"
     28 	"log"
     29 	"math/big"
     30 	"strings"
     31 )
     32 
     33 // CheckError is a handler for errors. It takes an error type as an argument,
     34 // and issues a log.Fatalln, printing the error and exiting with os.Exit(1).
     35 func CheckError(err error) {
     36 	if err != nil {
     37 		log.Fatalln(err)
     38 	}
     39 }
     40 
     41 // StringInSlice loops over a slice of strings and checks if a given string is
     42 // already an existing element. Returns true if so, and false if not.
     43 func StringInSlice(str string, slice []string) bool {
     44 	for _, i := range slice {
     45 		if str == i {
     46 			return true
     47 		}
     48 	}
     49 	return false
     50 }
     51 
     52 // GzipEncode compresses a given slice of bytes using gzip, and returns it as
     53 // a base64 encoded string. Returns error upon failure.
     54 func GzipEncode(data []byte) (string, error) {
     55 	var b bytes.Buffer
     56 	gz := gzip.NewWriter(&b)
     57 	if _, err := gz.Write(data); err != nil {
     58 		return "", err
     59 	}
     60 	if err := gz.Flush(); err != nil {
     61 		return "", err
     62 	}
     63 	if err := gz.Close(); err != nil {
     64 		return "", err
     65 	}
     66 	return base64.StdEncoding.EncodeToString(b.Bytes()), nil
     67 }
     68 
     69 // ParseDirs parses and appends a given slice of bytes and returns an appended
     70 // slice of strings with new contents.
     71 func ParseDirs(sl []string, data []byte) []string {
     72 	dirStr := string(data)
     73 	_dirs := strings.Split(dirStr, "\n")
     74 	for _, j := range _dirs {
     75 		if strings.HasPrefix(j, "DIR:") {
     76 			t := strings.Split(j, "DIR:")
     77 			if !(StringInSlice(t[1], sl)) {
     78 				if ValidateOnionAddress(t[1]) {
     79 					sl = append(sl, t[1])
     80 				}
     81 			}
     82 		}
     83 	}
     84 	return sl
     85 }
     86 
     87 // GenRandomASCII generates a random ASCII string of a given length.
     88 // Takes length int as argument, and returns a string of that length on success
     89 // and error on failure.
     90 func GenRandomASCII(length int) (string, error) {
     91 	var res string
     92 	for {
     93 		if len(res) >= length {
     94 			return res, nil
     95 		}
     96 		num, err := rand.Int(rand.Reader, big.NewInt(int64(127)))
     97 		if err != nil {
     98 			return "", err
     99 		}
    100 		n := num.Int64()
    101 		if n > 32 && n < 127 {
    102 			res += string(n)
    103 		}
    104 	}
    105 }