forked from quantum1423-dustbin/kirisurf-legacy
/
subcircuit_client.go
68 lines (64 loc) · 1.66 KB
/
subcircuit_client.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
package main
import (
"crypto/subtle"
"io"
"kirisurf/ll/dirclient"
"kirisurf/ll/kiss"
"math/big"
"net"
)
type Subcircuit struct {
cpath []dirclient.KNode
wire io.ReadWriteCloser
}
func build_subcircuit() (*Subcircuit, error) {
slc := dirclient.FindPath(MasterConfig.Network.MinCircuitLen)
DEBUG("Building a subcicruit with minlen %d...", len(slc))
// this returns a checker whether a public key is valid
pubkey_checker := func(hsh string) kiss.Verifier {
return func(k *big.Int) bool {
hashed := hash_base32(k.Bytes())
return subtle.ConstantTimeCompare([]byte(hashed), []byte(hsh)) == 1
}
}
// circuit-building loop
iwire, err := net.Dial("tcp", slc[0].Address)
if err != nil {
return nil, err
}
iwire, err = kiss.Kiriobfs_handshake_client(iwire)
if err != nil {
//iwire.Close()
return nil, err
}
wire, err := kiss.KiSS_handshake_client(
iwire, pubkey_checker(slc[0].PublicKey))
if err != nil {
//wire.Close()
return nil, err
}
for idx, ele := range slc[1:] {
DEBUG("Extending circuit to length %d...", idx+2)
// extend wire
err = write_sc_message(sc_message{SC_EXTEND, ele.PublicKey}, wire)
if err != nil {
wire.Close()
return nil, err
}
verifier := pubkey_checker(ele.PublicKey)
// at this point wire is raw (well unobfs) connection to next
wire, err = kiss.KiSS_handshake_client(wire, verifier)
if err != nil {
//wire.Close()
return nil, err
}
DEBUG("Extended circuit to length %d", idx+2)
}
err = write_sc_message(sc_message{SC_TERMINATE, ""}, wire)
if err != nil {
return nil, err
}
DEBUG("Subcircuit building complete with length %d", len(slc))
toret := Subcircuit{slc, wire}
return &toret, nil
}