cwtch/server/app/main.go

101 lines
2.6 KiB
Go
Raw Normal View History

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
"encoding/base64"
2021-04-09 01:22:08 +00:00
"git.openprivacy.ca/cwtch.im/tapir/primitives"
"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"
"os/signal"
"syscall"
2020-10-13 18:53:18 +00:00
"time"
2018-03-30 21:16:51 +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")
2021-05-03 23:32:48 +00:00
log.SetLevel(log.LevelDebug)
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
}
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
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-22 18:01:04 +00:00
defer acn.Close()
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
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))
2021-05-03 23:32:48 +00:00
invite, err := group.Invite()
2020-09-21 21:26:28 +00:00
if err != nil {
panic(err)
}
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))
2021-05-03 23:32:48 +00:00
log.Infof("Server Tofu Bundle: tofubundle:server:%s||%s", base64.StdEncoding.EncodeToString(bundle), invite)
// Graceful Shutdown
c := make(chan os.Signal, 1)
2021-05-05 19:49:24 +00:00
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
acn.Close()
server.Close()
os.Exit(1)
}()
2020-07-14 00:46:05 +00:00
server.Run(acn)
2020-12-17 01:40:03 +00:00
for {
time.Sleep(time.Second)
}
2018-06-19 22:38:22 +00:00
}