-
Notifications
You must be signed in to change notification settings - Fork 2
/
bitscreen.go
187 lines (150 loc) · 4.16 KB
/
bitscreen.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package bitscreen
import (
"crypto/sha256"
"encoding/hex"
"github.com/Jeffail/gabs"
"github.com/ipfs/go-cid"
"github.com/pebbe/zmq4"
"io/ioutil"
"log"
"os"
"path/filepath"
)
type updaterResponse struct {
reject int
dealCid string
cid string
err string
}
/* Supported env vars */
// BITSCREEN_FILENAME -- name of file containing the CIDs to block, defaults to `bitscreen`
const BITSCREEN_FILENAME = "BITSCREEN_FILENAME"
// BITSCREEN_PATH -- path to the bitscreen file, defaults to `.murmuration` in the user home dir
const BITSCREEN_PATH = "BITSCREEN_PATH"
// BITSCREEN_SOCKET_PORT -- server socket port of the bitscreen-updater process
const BITSCREEN_SOCKET_PORT = "BITSCREEN_SOCKET_PORT"
// LOTUS_BLOCK_FROM_FILE -- specify whether to use the bitscreen file for checking cids.
//
// Default is to use the bitscreen-updater process (connects to socket port BITSCREEN_SOCKET_PORT)
const LOTUS_BLOCK_FROM_FILE = "LOTUS_BLOCK_FROM_FILE"
func IsLoadFromFileEnabled() bool {
loadFromFile, exists := os.LookupEnv(LOTUS_BLOCK_FROM_FILE)
if !exists || loadFromFile == "" {
loadFromFile = "false"
}
return (loadFromFile == "1") || (loadFromFile == "true")
}
func GetBitscreenFilename() string {
filename, exists := os.LookupEnv(BITSCREEN_FILENAME)
if !exists || filename == "" {
filename = "bitscreen"
}
return filename
}
func GetSocketPort() string {
socketPort, exists := os.LookupEnv(BITSCREEN_SOCKET_PORT)
if !exists || socketPort == "" {
socketPort = "5555"
}
return socketPort
}
// GetPath returns the filepath to the bitscreen file
func GetPath() string {
fn := GetBitscreenFilename()
path, exists := os.LookupEnv(BITSCREEN_PATH)
if !exists || path == "" {
homeDir, _ := os.UserHomeDir()
defaultPath := filepath.Join(homeDir, ".murmuration")
return filepath.Join(defaultPath, fn)
} else {
return filepath.Join(path, fn)
}
}
// MaybeCreateBitscreen generates instance of BitScreen struct
// and if needed, creates a bitscreen file
func MaybeCreateBitscreen() bool {
p := GetPath()
if !FileExists(p) {
dir, _ := filepath.Split(p)
os.MkdirAll(dir, os.ModePerm)
err := ioutil.WriteFile(p, []byte(""), os.ModePerm)
if err != nil {
log.Fatal(err)
return false
}
}
return true
}
// Handle errors
func sigterm(e error) {
if e != nil {
log.Fatal(e)
panic(e)
}
}
// FileExists checks whether a directory or file exists
func FileExists(path string) bool {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
// BlockCidFromFile checks for a CID in ./murmuration/bitscreen
func BlockCidFromFile(cidToCheck cid.Cid) bool {
MaybeCreateBitscreen()
p := GetPath()
unhashedCid := cidToCheck.String()
hash := sha256.New()
var buf []byte
hash.Write([]byte(unhashedCid))
buf = hash.Sum(nil)
var hashedCid = hex.EncodeToString(buf)
cidList, err := gabs.ParseJSONFile(p)
if err != nil {
panic(err)
}
stringList, err := cidList.Children()
for _, cidString := range stringList {
string := cidString.Data().(string)
if string == hashedCid || string == unhashedCid {
return true
}
}
return false
}
// BlockCidFromProcess requests the block status of cid from
// the bitscreen-updater process
func BlockCidFromProcess(cidToCheck cid.Cid) bool {
socketPort := GetSocketPort()
zctx, _ := zmq4.NewContext()
// Socket to talk to server
s, _ := zctx.NewSocket(zmq4.REQ)
s.Connect("tcp://localhost:" + socketPort)
request := getRequestForCid(cidToCheck)
s.Send(request, 0)
responseJSON, _ := s.Recv(0)
response := getResponseFromJSON(responseJSON)
return response.reject == 1
}
func getRequestForCid(cid cid.Cid) string {
json := gabs.New()
json.Set(cid.String(), "cid")
return json.String()
}
func getResponseFromJSON(responseJSON string) updaterResponse {
response := updaterResponse{}
parsed, err := gabs.ParseJSON([]byte(responseJSON))
if err != nil {
return response
}
if parsed.Exists("error") {
response.err = parsed.Path("error").Data().(string)
return response
}
response.reject = int(parsed.Path("reject").Data().(float64))
response.dealCid = parsed.Path("dealCid").Data().(string)
response.cid = parsed.Path("cid").Data().(string)
return response
}