2016-07-01 03:18:55 +00:00
|
|
|
package core
|
|
|
|
|
2016-10-16 00:04:19 +00:00
|
|
|
import (
|
|
|
|
cryptorand "crypto/rand"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"math/big"
|
|
|
|
"math/rand"
|
2016-11-06 04:09:18 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
2016-10-16 00:04:19 +00:00
|
|
|
)
|
|
|
|
|
2016-09-11 04:57:54 +00:00
|
|
|
type Ricochet struct {
|
|
|
|
Config *Config
|
|
|
|
Network *Network
|
|
|
|
Identity *Identity
|
|
|
|
}
|
|
|
|
|
2016-11-06 04:09:18 +00:00
|
|
|
func (core *Ricochet) Init(conf *Config) (err error) {
|
2016-10-16 00:04:19 +00:00
|
|
|
initRand()
|
|
|
|
|
2016-09-11 04:57:54 +00:00
|
|
|
core.Config = conf
|
2016-11-06 04:09:18 +00:00
|
|
|
|
2016-09-11 04:57:54 +00:00
|
|
|
core.Network = CreateNetwork()
|
2016-11-06 04:09:18 +00:00
|
|
|
core.setupNetwork()
|
2016-09-11 04:57:54 +00:00
|
|
|
core.Identity, err = CreateIdentity(core)
|
2016-11-06 04:09:18 +00:00
|
|
|
return
|
2016-07-01 03:18:55 +00:00
|
|
|
}
|
2016-10-16 00:04:19 +00:00
|
|
|
|
|
|
|
func initRand() {
|
|
|
|
n, err := cryptorand.Int(cryptorand.Reader, big.NewInt(math.MaxInt64))
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("rng failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rand.Seed(n.Int64())
|
|
|
|
}
|
2016-11-06 04:09:18 +00:00
|
|
|
|
|
|
|
func (core *Ricochet) setupNetwork() {
|
|
|
|
socket := os.Getenv("TOR_CONTROL_SOCKET")
|
|
|
|
host := os.Getenv("TOR_CONTROL_HOST")
|
|
|
|
port := os.Getenv("TOR_CONTROL_PORT")
|
|
|
|
passwd := os.Getenv("TOR_CONTROL_PASSWD")
|
|
|
|
|
|
|
|
if socket != "" {
|
|
|
|
core.Network.SetControlAddress("unix:" + socket)
|
|
|
|
} else if host != "" {
|
|
|
|
if port == "" {
|
|
|
|
port = "9051"
|
|
|
|
}
|
|
|
|
core.Network.SetControlAddress(net.JoinHostPort(host, port))
|
|
|
|
} else {
|
|
|
|
core.Network.SetControlAddress("127.0.0.1:9051")
|
|
|
|
}
|
|
|
|
|
|
|
|
if passwd != "" {
|
|
|
|
core.Network.SetControlPassword(passwd)
|
|
|
|
}
|
|
|
|
}
|