group-manager-bot/groupbot.go

132 lines
4.4 KiB
Go

package main
import (
"crypto/rand"
"encoding/base64"
mrand "math/rand"
"os"
"path/filepath"
"time"
"cwtch.im/cwtch/app"
"cwtch.im/cwtch/event"
"cwtch.im/cwtch/functionality/filesharing"
"cwtch.im/cwtch/functionality/hybrid"
"cwtch.im/cwtch/model/attr"
"cwtch.im/cwtch/model/constants"
"cwtch.im/cwtch/peer"
"git.openprivacy.ca/openprivacy/connectivity/tor"
"git.openprivacy.ca/openprivacy/log"
_ "github.com/mutecomm/go-sqlcipher/v4"
)
func main() {
log.AddEverythingFromPattern("connectivity")
log.SetLevel(log.LevelInfo)
log.ExcludeFromPattern("connection/connection")
log.ExcludeFromPattern("outbound/3dhauthchannel")
log.ExcludeFromPattern("event/eventmanager")
log.ExcludeFromPattern("tapir")
os.Mkdir("tordir", 0700)
dataDir := filepath.Join("tordir", "tor")
os.MkdirAll(dataDir, 0700)
// we don't need real randomness for the port, just to avoid a possible conflict...
socksPort := mrand.Intn(1000) + 9051
controlPort := mrand.Intn(1000) + 9052
// generate a random password
key := make([]byte, 64)
_, err := rand.Read(key)
if err != nil {
panic(err)
}
useCache := true
torDataDir := ""
if useCache {
log.Infof("using tor cache")
torDataDir = filepath.Join(dataDir, "data-dir-torcache")
os.MkdirAll(torDataDir, 0700)
} else {
log.Infof("using clean tor data dir")
if torDataDir, err = os.MkdirTemp(dataDir, "data-dir-"); err != nil {
log.Errorf("could not create data dir")
panic("")
}
}
tor.NewTorrc().WithSocksPort(socksPort).WithOnionTrafficOnly().WithHashedPassword(base64.StdEncoding.EncodeToString(key)).WithControlPort(controlPort).Build("tordir/tor/torrc")
acn, err := tor.NewTorACNWithAuth("./tordir", filepath.Join("..", "tor"), torDataDir, controlPort, tor.HashedPasswordAuthenticator{Password: base64.StdEncoding.EncodeToString(key)})
if err != nil {
log.Errorf("Could not start Tor: %v", err)
panic("")
}
log.Infof("Waiting for tor to bootstrap...")
acn.WaitTillBootstrapped()
defer acn.Close()
// ***** Cwtch Server management *****
managerApp := app.NewApp(acn, "./managerstorage", app.LoadAppSettings("./managerstorage"))
managerSettings := managerApp.ReadSettings()
managerSettings.ExperimentsEnabled = true
managerSettings.Experiments[constants.GroupsExperiment] = true
managerSettings.Experiments[constants.GroupManagerExperiment] = true
managerSettings.Experiments[constants.FileSharingExperiment] = true
managerSettings.Experiments[constants.ImagePreviewsExperiment] = true
managerSettings.DownloadPath = "./"
managerApp.UpdateSettings(managerSettings)
//manager := MakeProfile(managerApp, "Manager")
managerApp.LoadProfiles("nopassword")
if len(managerApp.ListProfiles()) == 0 {
managerApp.CreateProfile("Manager", "", true)
}
peers := managerApp.ListProfiles()
manager := managerApp.GetPeer(peers[0])
conversations, _ := manager.FetchConversations()
managerApp.ActivatePeerEngine(manager.GetOnion())
managerApp.ConfigureConnections(manager.GetOnion(), true, true, true)
manager.SetScopedZonedAttribute(attr.PublicScope, attr.ProfileZone, constants.Name, "Sarah's Test Group")
// Will currently only work on Sarah's custom fork (testing custom profile images)
fh := new(filesharing.Functionality)
fileKey, _, err := fh.ShareFile("./knott.png", manager)
log.Infof("sharing file: %v %v", fileKey, err)
const CustomProfileImageKey = "custom-profile-image"
manager.SetScopedZonedAttribute(attr.PublicScope, attr.ProfileZone, CustomProfileImageKey, fileKey)
hgmf := hybrid.GroupManagerFunctionality{}
if len(conversations) == 0 {
_, err = hgmf.ManageNewGroup(manager)
if err != nil {
log.Errorf("could not create hybrid group: %v", err)
}
}
//mg, _ := hgmf.GetManagedGroup(manager)
err = hgmf.AddHybridContact(manager, "icyt7rvdsdci42h6si2ibtwucdmjrlcb2ezkecuagtquiiflbkxf2cqd")
log.Infof("Adding Sarah: %v\n", err)
err = hgmf.AddHybridContact(manager, "etmb3zsslh3xozp67n22e3kuuivj5eprnngn6glrhd6z2btoogny6kyd")
log.Infof("Adding Alice: %v\n", err)
log.Infof("Import Hybrid Groups managed:%s", manager.GetOnion())
for {
time.Sleep(time.Second * 30)
}
}
func MakeProfile(application app.Application, name string) peer.CwtchPeer {
application.CreateProfile(name, "nopassword", true)
p := app.WaitGetPeer(application, name)
application.ConfigureConnections(p.GetOnion(), true, true, false)
log.Infof("%s created: %s", name, p.GetOnion())
p.AutoHandleEvents([]event.Type{event.PeerStateChange, event.ServerStateChange, event.NewGroupInvite, event.NewRetValMessageFromPeer})
return p
}