2021-05-07 18:36:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
2021-07-12 21:34:55 +00:00
|
|
|
"flag"
|
2021-05-07 18:36:34 +00:00
|
|
|
cwtchserver "git.openprivacy.ca/cwtch.im/server"
|
|
|
|
"git.openprivacy.ca/cwtch.im/tapir/primitives"
|
|
|
|
"git.openprivacy.ca/openprivacy/connectivity/tor"
|
|
|
|
"git.openprivacy.ca/openprivacy/log"
|
2021-07-12 21:34:55 +00:00
|
|
|
"io/ioutil"
|
2021-05-07 18:36:34 +00:00
|
|
|
mrand "math/rand"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2021-08-10 20:07:55 +00:00
|
|
|
"path"
|
2021-05-07 18:36:34 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-07-12 21:34:55 +00:00
|
|
|
flagDebug := flag.Bool("debug", false, "Enable debug logging")
|
2021-08-10 20:07:55 +00:00
|
|
|
flagExportTofu := flag.Bool("exportTofuBundle", false, "Export the tofubundle to a file called tofubundle")
|
2021-07-12 21:34:55 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2021-05-07 18:36:34 +00:00
|
|
|
log.AddEverythingFromPattern("server/app/main")
|
|
|
|
log.AddEverythingFromPattern("server/server")
|
|
|
|
log.ExcludeFromPattern("service.go")
|
2021-07-12 21:34:55 +00:00
|
|
|
log.SetLevel(log.LevelInfo)
|
|
|
|
if *flagDebug {
|
|
|
|
log.Infoln("enableing Debug logging")
|
|
|
|
log.SetLevel(log.LevelDebug)
|
|
|
|
}
|
|
|
|
configDir := os.Getenv("CWTCH_HOME")
|
2021-05-07 18:36:34 +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: "",
|
|
|
|
}
|
2021-10-04 23:21:41 +00:00
|
|
|
config.ConfigDir = "."
|
|
|
|
config.FilePath = cwtchserver.ServerConfigFile
|
|
|
|
config.Encrypted = false
|
|
|
|
config.Save()
|
2021-05-07 18:36:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-04 23:21:41 +00:00
|
|
|
serverConfig, err := cwtchserver.LoadCreateDefaultConfigFile(configDir, cwtchserver.ServerConfigFile, false, "")
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not load/create config file: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
2021-05-07 18:36:34 +00:00
|
|
|
// we don't need real randomness for the port, just to avoid a possible conflict...
|
|
|
|
mrand.Seed(int64(time.Now().Nanosecond()))
|
|
|
|
controlPort := mrand.Intn(1000) + 9052
|
|
|
|
|
|
|
|
// generate a random password
|
|
|
|
key := make([]byte, 64)
|
2021-10-04 23:21:41 +00:00
|
|
|
_, err = rand.Read(key)
|
2021-05-07 18:36:34 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.MkdirAll("tordir/tor", 0700)
|
|
|
|
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)})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("\nError connecting to Tor: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer acn.Close()
|
|
|
|
|
2021-10-04 23:21:41 +00:00
|
|
|
server := cwtchserver.NewServer(serverConfig)
|
2021-05-07 18:36:34 +00:00
|
|
|
log.Infoln("starting cwtch server...")
|
2021-10-04 23:21:41 +00:00
|
|
|
log.Infof("Server 'hash name': %s\n", server.HashName())
|
2021-05-07 18:36:34 +00:00
|
|
|
|
2021-10-04 23:21:41 +00:00
|
|
|
log.Infof("Server bundle (import into client to use server): %s\n", log.Magenta(server.Server()))
|
2021-05-07 18:36:34 +00:00
|
|
|
|
2021-07-12 21:34:55 +00:00
|
|
|
if *flagExportTofu {
|
2021-10-04 23:21:41 +00:00
|
|
|
// Todo: change all to server export
|
|
|
|
ioutil.WriteFile(path.Join(serverConfig.ConfigDir, "tofubundle"), []byte(server.TofuBundle()), 0600)
|
2021-07-12 21:34:55 +00:00
|
|
|
}
|
2021-05-07 18:36:34 +00:00
|
|
|
|
|
|
|
// Graceful Shutdown
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
<-c
|
|
|
|
acn.Close()
|
|
|
|
server.Close()
|
|
|
|
os.Exit(1)
|
|
|
|
}()
|
|
|
|
|
|
|
|
server.Run(acn)
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|