/
bla.go
90 lines (78 loc) · 1.73 KB
/
bla.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
package main
import (
"fmt"
"os"
"encoding/json"
"encoding/hex"
"crypto/sha512"
"net"
"net/http"
"time"
)
import "libblockify/bucket"
import "libblockify_net/glue"
import "webgui"
/*
const bktpath = `K:\developement\golang\blockify_tools\experiments\_bucket\`
var mybucket = bucket.FsBucket(bktpath)
*/
type Config struct{
Bucket string `json:"bucket"`
Ip string `json:"ip,omitempty"`
DhtPort string `json:"dht-port"`
BucketPort string `json:"bucket-port"`
Gen string `json:"gen"`
Http []string `json:"http"`
}
func hash(gen []byte) []byte{
h := sha512.New()
h.Write(gen)
return h.Sum([]byte{})
}
func main(){
var addrs []string
var config Config
{
f,e := os.Open(os.Args[1])
fmt.Println("config:",os.Args[1])
if e!=nil { fmt.Println(e) ; return }
e = json.NewDecoder(f).Decode(&config)
if e!=nil { fmt.Println(e) ; return }
f.Close()
}
mybucket := bucket.FsBucket(config.Bucket)
{
f,e := os.Open(os.Args[2])
fmt.Println("addrs:",os.Args[2])
if e!=nil { fmt.Println(e) ; return }
e = json.NewDecoder(f).Decode(&addrs)
if e!=nil { fmt.Println(e) ; return }
f.Close()
}
fmt.Println(config)
gen,e := hex.DecodeString(config.Gen)
if e!=nil { fmt.Println(e) ; return }
id := hash(gen)
ht,e := net.Listen(config.Http[0],config.Http[1])
if e!=nil { fmt.Println(e) ; return }
g := new(glue.Glue).Init(id,mybucket,config.Ip,config.DhtPort,config.BucketPort)
g.DebugOn()
go g.ServeBucket()
go g.ServeDht()
go g.RunPull()
hashes := make(chan []byte)
go func(){
for h := range hashes {
g.Want(h)
}
}()
h := webgui.NewHandler(g.GetBucket(),hashes)
go func(){
time.Sleep(time.Second)
for _,a := range addrs {
time.Sleep(time.Millisecond*100)
g.PingUdp(a)
}
}()
http.Serve(ht,h)
}