2021-01-13 05:46:33 +00:00
|
|
|
package cwtch
|
|
|
|
//package main
|
2021-01-06 23:07:09 +00:00
|
|
|
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"cwtch.im/cwtch/app"
|
|
|
|
"cwtch.im/cwtch/event"
|
|
|
|
"cwtch.im/cwtch/peer"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"encoding/base64"
|
|
|
|
"git.openprivacy.ca/openprivacy/connectivity/tor"
|
|
|
|
"git.openprivacy.ca/openprivacy/log"
|
|
|
|
mrand "math/rand"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var application app.Application
|
|
|
|
var acnQueue event.Queue
|
|
|
|
var contactEventsQueue event.Queue
|
|
|
|
|
2021-01-13 05:46:33 +00:00
|
|
|
//export c_StartCwtch
|
|
|
|
func c_StartCwtch(dir_c *C.char, len C.int, tor_c *C.char, torLen C.int) {
|
|
|
|
dir := C.GoStringN(dir_c, len)
|
|
|
|
tor := C.GoStringN(tor_c, torLen)
|
|
|
|
StartCwtch(dir, tor)
|
2021-01-06 23:07:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 05:46:33 +00:00
|
|
|
func StartCwtch(appDir string, torPath string) {
|
|
|
|
//go func (appDir string, torPath string) {
|
|
|
|
log.SetLevel(log.LevelDebug)
|
|
|
|
|
|
|
|
log.Infof("Loading Cwtch Directory %v and tor path: %v", appDir, torPath)
|
|
|
|
|
|
|
|
mrand.Seed(int64(time.Now().Nanosecond()))
|
|
|
|
port := mrand.Intn(1000) + 9600
|
|
|
|
controlPort := port + 1
|
|
|
|
|
|
|
|
// generate a random password (actually random, stored in memory, for the control port)
|
|
|
|
key := make([]byte, 64)
|
|
|
|
_, err := rand.Read(key)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("making directory %v", appDir)
|
|
|
|
os.MkdirAll(path.Join(appDir, "/.tor", "tor"), 0700)
|
|
|
|
tor.NewTorrc().WithSocksPort(port).WithOnionTrafficOnly().WithControlPort(controlPort).WithHashedPassword(base64.StdEncoding.EncodeToString(key)).Build(filepath.Join(appDir, ".tor", "tor", "torrc"))
|
|
|
|
acn, err := tor.NewTorACNWithAuth(path.Join(appDir, "/.tor"), torPath, controlPort, tor.HashedPasswordAuthenticator{base64.StdEncoding.EncodeToString(key)})
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("\nError connecting to Tor: %v\n", err)
|
|
|
|
}
|
|
|
|
//acn.WaitTillBootstrapped()
|
|
|
|
|
|
|
|
newApp := app.NewApp(acn, appDir)
|
|
|
|
acnQueue = event.NewQueue()
|
|
|
|
newApp.GetPrimaryBus().Subscribe(event.ACNStatus, acnQueue)
|
|
|
|
peer.DefaultEventsToHandle = []event.Type{
|
|
|
|
event.EncryptedGroupMessage,
|
|
|
|
event.NewMessageFromPeer,
|
|
|
|
event.PeerAcknowledgement,
|
|
|
|
event.NewGroupInvite,
|
|
|
|
event.PeerError,
|
|
|
|
event.SendMessageToGroupError,
|
|
|
|
event.NewGetValMessageFromPeer,
|
|
|
|
event.PeerStateChange,
|
|
|
|
}
|
|
|
|
newApp.LoadProfiles("be gay do crime")
|
|
|
|
newApp.LaunchPeers()
|
|
|
|
application = newApp
|
|
|
|
//}(appDir, torPath)
|
|
|
|
}
|
2021-01-06 23:07:09 +00:00
|
|
|
|
2021-01-13 05:46:33 +00:00
|
|
|
//export c_ACNEvents
|
|
|
|
func c_ACNEvents() *C.char {
|
|
|
|
return C.CString(ACNEvents())
|
2021-01-06 23:07:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 05:46:33 +00:00
|
|
|
func ACNEvents() string {
|
2021-01-06 23:07:09 +00:00
|
|
|
select {
|
2021-01-07 19:38:58 +00:00
|
|
|
case myevent := <- acnQueue.OutChan():
|
2021-01-13 05:46:33 +00:00
|
|
|
return fmt.Sprintf("%v", myevent)
|
2021-01-06 23:07:09 +00:00
|
|
|
default:
|
2021-01-13 05:46:33 +00:00
|
|
|
return ""
|
2021-01-06 23:07:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 05:46:33 +00:00
|
|
|
//export c_GetProfiles
|
|
|
|
func c_GetProfiles() *C.char {
|
|
|
|
return C.CString(GetProfiles())
|
2021-01-06 23:07:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 05:46:33 +00:00
|
|
|
func GetProfiles() string {
|
2021-01-06 23:07:09 +00:00
|
|
|
profiles := application.ListPeers()
|
|
|
|
jsonBytes,_ := json.Marshal(profiles)
|
2021-01-13 05:46:33 +00:00
|
|
|
return string(jsonBytes)
|
2021-01-06 23:07:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 05:46:33 +00:00
|
|
|
|
2021-01-06 23:07:09 +00:00
|
|
|
type Contact struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Onion string `json:"onion"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
|
|
|
|
2021-01-13 05:46:33 +00:00
|
|
|
//export c_GetContacts
|
|
|
|
func c_GetContacts(onion_ptr *C.char, onion_len C.int) *C.char {
|
|
|
|
return C.CString(GetContacts(C.GoStringN(onion_ptr, onion_len)))
|
|
|
|
}
|
2021-01-06 23:07:09 +00:00
|
|
|
|
2021-01-13 05:46:33 +00:00
|
|
|
func GetContacts(onion string)string {
|
2021-01-06 23:07:09 +00:00
|
|
|
log.Infof("Get Contacts for %v", onion)
|
2021-01-07 19:38:58 +00:00
|
|
|
mypeer := application.GetPeer(onion)
|
2021-01-06 23:07:09 +00:00
|
|
|
|
|
|
|
contactEventsQueue = event.NewQueue()
|
|
|
|
application.GetEventBus(onion).Subscribe(event.PeerStateChange, contactEventsQueue)
|
|
|
|
|
|
|
|
var contacts []Contact
|
2021-01-07 19:38:58 +00:00
|
|
|
for _,contact := range mypeer.GetContacts() {
|
|
|
|
contactInfo := mypeer.GetContact(contact)
|
2021-01-06 23:07:09 +00:00
|
|
|
log.Infof("contactInfo %v", contactInfo)
|
|
|
|
contacts = append(contacts, Contact{Name: contactInfo.Name, Onion: contactInfo.Onion, Status: contactInfo.State})
|
|
|
|
}
|
|
|
|
|
2021-01-07 19:38:58 +00:00
|
|
|
bytes,_ := json.Marshal(contacts)
|
2021-01-13 05:46:33 +00:00
|
|
|
return string(bytes)
|
2021-01-06 23:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-13 05:46:33 +00:00
|
|
|
//export c_SelectProfile
|
|
|
|
func c_SelectProfile(onion_ptr *C.char, onion_len C.int) *C.char {
|
|
|
|
return C.CString(SelectProfile(C.GoStringN(onion_ptr, onion_len)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func SelectProfile(onion string) string {
|
2021-01-06 23:07:09 +00:00
|
|
|
log.Infof("Select Profile: %v", onion)
|
|
|
|
contactEventsQueue = event.NewQueue()
|
|
|
|
application.GetEventBus(onion).Subscribe(event.PeerStateChange, contactEventsQueue)
|
2021-01-13 05:46:33 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
//export c_ContactEvents
|
|
|
|
func c_ContactEvents() *C.char {
|
|
|
|
return C.CString(ContactEvents())
|
2021-01-06 23:07:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 05:46:33 +00:00
|
|
|
func ContactEvents() string {
|
2021-01-06 23:07:09 +00:00
|
|
|
select {
|
2021-01-07 19:38:58 +00:00
|
|
|
case myevent := <- contactEventsQueue.OutChan():
|
2021-01-13 05:46:33 +00:00
|
|
|
return fmt.Sprintf("%v", myevent)
|
2021-01-06 23:07:09 +00:00
|
|
|
default:
|
2021-01-13 05:46:33 +00:00
|
|
|
return ""
|
2021-01-06 23:07:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 05:46:33 +00:00
|
|
|
//export c_NumMessages
|
|
|
|
func c_NumMessages(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int) (n C.int) {
|
2021-01-07 19:38:58 +00:00
|
|
|
profile := C.GoStringN(profile_ptr, profile_len)
|
|
|
|
handle := C.GoStringN(handle_ptr, handle_len)
|
2021-01-13 05:46:33 +00:00
|
|
|
return C.int(NumMessages(profile, handle))
|
|
|
|
}
|
|
|
|
|
|
|
|
func NumMessages(profile, handle string) (n int) {
|
|
|
|
n = len(application.GetPeer(profile).GetContact(handle).Timeline.Messages)
|
2021-01-07 19:38:58 +00:00
|
|
|
log.Infof("NumMessagse(%s, %s) = %d", profile, handle, n)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-13 05:46:33 +00:00
|
|
|
//export c_GetMessage
|
|
|
|
func c_GetMessage(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int, message_index C.int) *C.char {
|
2021-01-07 19:38:58 +00:00
|
|
|
profile := C.GoStringN(profile_ptr, profile_len)
|
|
|
|
handle := C.GoStringN(handle_ptr, handle_len)
|
2021-01-13 05:46:33 +00:00
|
|
|
return C.CString(GetMessage(profile, handle, int(message_index)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetMessage(profile, handle string, message_index int) string {
|
2021-01-07 19:38:58 +00:00
|
|
|
message := application.GetPeer(profile).GetContact(handle).Timeline.Messages[message_index]
|
|
|
|
bytes,_ := json.Marshal(message)
|
|
|
|
log.Infof("GetMessage(%s, %s, %d) = %s", profile, handle, message_index, string(bytes))
|
2021-01-13 05:46:33 +00:00
|
|
|
return string(bytes)
|
2021-01-07 19:38:58 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 00:35:54 +00:00
|
|
|
// Leave as is, needed by ffi
|
2021-01-13 05:46:33 +00:00
|
|
|
//func main() {}
|