/
keys.go
101 lines (84 loc) · 1.99 KB
/
keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package cmd
import (
"errors"
"fmt"
"github.com/freifunk-mwu/fastd-limiter/common"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
"os"
"regexp"
)
// extract fastd public key from file
func findKey(path string, re *regexp.Regexp) (key string, err error) {
file, err := ioutil.ReadFile(path)
if err != nil {
return
}
match := re.FindSubmatch(file)
if match == nil {
return "", errors.New("string not found")
}
return string(match[1]), err
}
// find all fastd public keys in provided directory
func loadKeys(dirname string) (keys []string, err error) {
dir, err := os.Open(dirname)
defer dir.Close()
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(2)
}
files, err := dir.Readdir(-1)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(2)
}
re := regexp.MustCompile(`key +\"([0-9a-z]{64})\"\;`)
for _, file := range files {
if file.IsDir() == false {
path := fmt.Sprintf("%s/%s", dirname, file.Name())
key, err := findKey(path, re)
if err != nil {
continue
}
keys = append(keys, key)
}
}
return
}
// keysCmd represents the keys command
var keysCmd = &cobra.Command{
Use: "keys",
Short: "update public keys",
Run: func(cmd *cobra.Command, args []string) {
// get config vars
redisDb := viper.GetString("redis_db")
ttl := viper.GetInt("key_ttl")
// check if fastd_keys is defined in config
if !viper.IsSet("fastd_keys") {
fmt.Println("fastd_keys not defined in config file")
os.Exit(1)
}
keysDir := viper.GetString("fastd_keys")
// connect to redis server
conn := common.ConnectRedis(redisDb)
defer conn.Close()
// load fastd public keys from files
keys, err := loadKeys(keysDir)
// insert keys into redis
for _, key := range keys {
_, err = conn.Do("SET", fmt.Sprintf(common.KEY, key), true, "EX", ttl)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
}
if verbose {
fmt.Printf("inserted %d keys with ttl=%d\n", len(keys), ttl)
}
},
}
func init() {
rootCmd.AddCommand(keysCmd)
}