2018-03-30 21:16:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-11-02 23:53:13 +00:00
|
|
|
"crypto/rand"
|
2020-09-21 21:26:28 +00:00
|
|
|
"cwtch.im/cwtch/model"
|
2018-05-28 18:05:06 +00:00
|
|
|
cwtchserver "cwtch.im/cwtch/server"
|
2020-09-21 21:26:28 +00:00
|
|
|
"cwtch.im/tapir/primitives"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2020-02-10 22:09:24 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/connectivity/tor"
|
|
|
|
"git.openprivacy.ca/openprivacy/log"
|
2020-10-13 18:53:18 +00:00
|
|
|
mrand "math/rand"
|
2018-11-01 17:32:26 +00:00
|
|
|
"os"
|
2020-10-13 18:53:18 +00:00
|
|
|
"time"
|
2018-03-30 21:16:51 +00:00
|
|
|
)
|
|
|
|
|
2018-06-18 22:57:21 +00:00
|
|
|
const (
|
|
|
|
serverConfigFile = "serverConfig.json"
|
|
|
|
)
|
2018-05-03 22:45:50 +00:00
|
|
|
|
2018-03-30 21:16:51 +00:00
|
|
|
func main() {
|
2018-12-04 02:52:11 +00:00
|
|
|
log.AddEverythingFromPattern("server/app/main")
|
|
|
|
log.AddEverythingFromPattern("server/server")
|
2018-11-01 17:32:26 +00:00
|
|
|
configDir := os.Getenv("CWTCH_CONFIG_DIR")
|
|
|
|
|
2020-09-21 21:26:28 +00:00
|
|
|
if len(os.Args) == 2 && os.Args[1] == "gen1" {
|
|
|
|
config := new(cwtchserver.Config)
|
|
|
|
id, pk := primitives.InitializeEphemeralIdentity()
|
|
|
|
tid, tpk := primitives.InitializeEphemeralIdentity()
|
|
|
|
config.PrivateKey = pk
|
|
|
|
config.PublicKey = id.PublicKey()
|
|
|
|
config.TokenServerPrivateKey = tpk
|
|
|
|
config.TokenServerPublicKey = tid.PublicKey()
|
|
|
|
config.MaxBufferLines = 100000
|
|
|
|
config.ServerReporting = cwtchserver.Reporting{
|
|
|
|
LogMetricsToFile: true,
|
|
|
|
ReportingGroupID: "",
|
|
|
|
ReportingServerAddr: "",
|
|
|
|
}
|
|
|
|
config.Save(".", "serverConfig.json")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-01 20:22:29 +00:00
|
|
|
serverConfig := cwtchserver.LoadConfig(configDir, serverConfigFile)
|
2018-05-03 22:45:50 +00:00
|
|
|
|
2020-10-13 18:53:18 +00:00
|
|
|
// we don't need real randomness for the port, just to avoid a possible conflict...
|
|
|
|
mrand.Seed(int64(time.Now().Nanosecond()))
|
2020-11-02 23:53:13 +00:00
|
|
|
controlPort := mrand.Intn(1000) + 9052
|
2020-10-13 18:53:18 +00:00
|
|
|
|
|
|
|
// generate a random password
|
|
|
|
key := make([]byte, 64)
|
|
|
|
_, err := rand.Read(key)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-11-02 23:53:13 +00:00
|
|
|
os.MkdirAll("tordir/tor", 0700)
|
2020-10-13 18:53:18 +00:00
|
|
|
tor.NewTorrc().WithHashedPassword(base64.StdEncoding.EncodeToString(key)).WithControlPort(controlPort).Build("./tordir/tor/torrc")
|
|
|
|
acn, err := tor.NewTorACNWithAuth("tordir", "", controlPort, tor.HashedPasswordAuthenticator{Password: base64.StdEncoding.EncodeToString(key)})
|
2020-10-16 05:39:57 +00:00
|
|
|
|
2018-11-10 22:14:12 +00:00
|
|
|
if err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Errorf("\nError connecting to Tor: %v\n", err)
|
|
|
|
os.Exit(1)
|
2018-11-10 22:14:12 +00:00
|
|
|
}
|
2018-11-22 18:01:04 +00:00
|
|
|
defer acn.Close()
|
2018-11-10 22:14:12 +00:00
|
|
|
|
2018-03-30 21:16:51 +00:00
|
|
|
server := new(cwtchserver.Server)
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Infoln("starting cwtch server...")
|
2018-03-30 21:16:51 +00:00
|
|
|
|
2018-11-10 22:14:12 +00:00
|
|
|
// TODO: respond to HUP so t.Close is gracefully called
|
2020-07-14 00:46:05 +00:00
|
|
|
server.Setup(serverConfig)
|
2020-09-21 21:26:28 +00:00
|
|
|
|
|
|
|
// TODO create a random group for testing
|
|
|
|
group, _ := model.NewGroup(tor.GetTorV3Hostname(serverConfig.PublicKey))
|
|
|
|
group.SignGroup([]byte{})
|
|
|
|
invite, err := group.Invite([]byte{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-10-16 05:39:57 +00:00
|
|
|
fmt.Printf("%v\n", "torv3"+base64.StdEncoding.EncodeToString(invite))
|
2020-09-21 21:26:28 +00:00
|
|
|
|
2020-10-01 17:13:45 +00:00
|
|
|
bundle := server.KeyBundle().Serialize()
|
2020-09-21 21:26:28 +00:00
|
|
|
log.Infof("Server Config: server:%s", base64.StdEncoding.EncodeToString(bundle))
|
2020-07-14 00:46:05 +00:00
|
|
|
server.Run(acn)
|
2018-06-19 22:38:22 +00:00
|
|
|
}
|