Browse Source

all server files now go in configDir, cwtch docker entrypoint sets it by default to where it's expected

pull/154/head
Dan Ballard 3 years ago
parent
commit
9be69bda99
  1. 7
      Dockerfile
  2. 2
      server/app/main.go
  3. 4
      server/docker/docker-entrypoint
  4. 7
      server/metrics/monitors.go
  5. 4
      server/server.go
  6. 15
      server/serverConfig.go
  7. 11
      storage/message_store.go
  8. 2
      testing/cwtch_peer_server_intergration_test.go

7
Dockerfile

@ -28,10 +28,9 @@ RUN apk --no-cache add --update \
zlib-dev \
zstd \
zstd-dev \
#&& wget -q https://www.torproject.org/dist/tor-0.3.4.8.tar.gz \
&& wget -q https://www.torproject.org/dist/tor-0.3.5.2-alpha.tar.gz \
&& tar xf tor-0.3.5.2-alpha.tar.gz \
&& cd tor-0.3.5.2-alpha \
&& wget -q https://www.torproject.org/dist/tor-0.3.5.3-alpha.tar.gz \
&& tar xf tor-0.3.5.3-alpha.tar.gz \
&& cd tor-0.3.5.3-alpha \
&& ./configure \
&& make install \
&& ls -R /usr/local/

2
server/app/main.go

@ -13,7 +13,7 @@ const (
func main() {
configDir := os.Getenv("CWTCH_CONFIG_DIR")
serverConfig := cwtchserver.LoadConfig(configDir + serverConfigFile)
serverConfig := cwtchserver.LoadConfig(configDir, serverConfigFile)
server := new(cwtchserver.Server)
log.Printf("starting cwtch server...")

4
server/docker/docker-entrypoint

@ -25,6 +25,10 @@ tor -f /etc/tor/torrc
#Cwtch will crash and burn if 9051 isn't ready
sleep 15
if [ -z "${CWTCH_CONFIG_DIR}" ]; then
CWTCH_CONFIG_DIR=/etc/cwtch/
fi
#Run cwtch (or whatever the user passed)
exec "$@"
#$@

7
server/metrics/monitors.go

@ -7,6 +7,7 @@ import (
"github.com/struCoder/pidusage"
"log"
"os"
"path"
"time"
)
@ -24,11 +25,13 @@ type Monitors struct {
starttime time.Time
breakChannel chan bool
log bool
configDir string
}
// Start initializes a Monitors's monitors
func (mp *Monitors) Start(ra *application.RicochetApplication, log bool) {
func (mp *Monitors) Start(ra *application.RicochetApplication, configDir string, log bool) {
mp.log = log
mp.configDir = configDir
mp.starttime = time.Now()
mp.breakChannel = make(chan bool)
mp.MessageCounter = NewCounter()
@ -54,7 +57,7 @@ func (mp *Monitors) run() {
}
func (mp *Monitors) report() {
f, err := os.Create(reportFile)
f, err := os.Create(path.Join(mp.configDir, reportFile))
if err != nil {
log.Println("ERROR: Could not open monitor reporting file: ", err)
return

4
server/server.go

@ -24,7 +24,7 @@ type Server struct {
func (s *Server) Run(serverConfig Config) {
s.config = serverConfig
cwtchserver := new(application.RicochetApplication)
s.metricsPack.Start(cwtchserver, s.config.ServerReporting.LogMetricsToFile)
s.metricsPack.Start(cwtchserver, serverConfig.ConfigDir, s.config.ServerReporting.LogMetricsToFile)
l, err := application.SetupOnionV3("127.0.0.1:9051", "tcp4", "", s.config.PrivateKey, utils.GetTorV3Hostname(s.config.PublicKey), 9878)
@ -35,7 +35,7 @@ func (s *Server) Run(serverConfig Config) {
af := application.ApplicationInstanceFactory{}
af.Init()
ms := new(storage.MessageStore)
err = ms.Init(".", s.config.MaxBufferLines, s.metricsPack.MessageCounter)
err = ms.Init(serverConfig.ConfigDir, s.config.MaxBufferLines, s.metricsPack.MessageCounter)
if err != nil {
log.Fatal(err)
}

15
server/serverConfig.go

@ -7,6 +7,7 @@ import (
"golang.org/x/crypto/ed25519"
"io/ioutil"
"log"
"path"
)
// Reporting is a struct for storing a the config a server needs to be a peer, and connect to a group to report
@ -18,6 +19,7 @@ type Reporting struct {
// Config is a struct for storing basic server configuration
type Config struct {
ConfigDir string `json:"-"`
MaxBufferLines int `json:"maxBufferLines"`
PublicKey ed25519.PublicKey `json:"publicKey"`
PrivateKey ed25519.PrivateKey `json:"privateKey"`
@ -30,19 +32,20 @@ func (config *Config) Identity() identity.Identity {
}
// Save dumps the latest version of the config to a json file given by filename
func (config *Config) Save(filename string) {
log.Println("Saving config to %s", filename)
func (config *Config) Save(dir, filename string) {
log.Printf("Saving config to %s\n", path.Join(dir, filename))
bytes, _ := json.MarshalIndent(config, "", "\t")
ioutil.WriteFile(filename, bytes, 0600)
ioutil.WriteFile(path.Join(dir, filename), bytes, 0600)
}
// LoadConfig loads a Config from a json file specified by filename
func LoadConfig(filename string) Config {
func LoadConfig(configDir, filename string) Config {
log.Printf("Loading config from %s\n", filename)
config := Config{}
config.ConfigDir = configDir
config.MaxBufferLines = 100000
config.ServerReporting.LogMetricsToFile = false
raw, err := ioutil.ReadFile(filename)
raw, err := ioutil.ReadFile(path.Join(configDir, filename))
if err == nil {
err = json.Unmarshal(raw, &config)
@ -56,6 +59,6 @@ func LoadConfig(filename string) Config {
}
// Always save (first time generation, new version with new variables populated)
config.Save(filename)
config.Save(configDir, filename)
return config
}

11
storage/message_store.go

@ -8,6 +8,7 @@ import (
"fmt"
"log"
"os"
"path"
"sync"
)
@ -57,7 +58,7 @@ func (ms *MessageStore) initAndLoadFiles() error {
ms.activeLogFile = nil
for i := fileStorePartitions - 1; i >= 0; i-- {
ms.filePos = 0
filename := fmt.Sprintf("%s%s.%d", ms.storeDirectory, fileStoreFilename, i)
filename := path.Join(ms.storeDirectory, fmt.Sprintf("%s.%d", fileStoreFilename, i))
f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0600)
if err != nil {
log.Printf("Error: MessageStore could not open: %v: %v", filename, err)
@ -96,13 +97,13 @@ func (ms *MessageStore) updateFile(gm *protocol.GroupMessage) {
func (ms *MessageStore) rotateFileStore() {
ms.activeLogFile.Close()
os.Remove(fmt.Sprintf("%s%s.%d", ms.storeDirectory, fileStoreFilename, fileStorePartitions-1))
os.Remove(path.Join(ms.storeDirectory, fmt.Sprintf("%s.%d", fileStoreFilename, fileStorePartitions-1)))
for i := fileStorePartitions - 2; i >= 0; i-- {
os.Rename(fmt.Sprintf("%s%s.%d", ms.storeDirectory, fileStoreFilename, i), fmt.Sprintf("%s%s.%d", ms.storeDirectory, fileStoreFilename, i+1))
os.Rename(path.Join(ms.storeDirectory, fmt.Sprintf("%s.%d", fileStoreFilename, i)), path.Join(ms.storeDirectory, fmt.Sprintf("%s.%d", fileStoreFilename, i+1)))
}
f, err := os.OpenFile(fmt.Sprintf("%s%s.%d", ms.storeDirectory, fileStoreFilename, 0), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0600)
f, err := os.OpenFile(path.Join(ms.storeDirectory, fmt.Sprintf("%s.%d", fileStoreFilename, 0)), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0600)
if err != nil {
log.Printf("ERROR: Could not open new message store file in: %s", ms.storeDirectory)
}
@ -112,7 +113,7 @@ func (ms *MessageStore) rotateFileStore() {
// Init sets up a MessageStore of size maxBufferLines (# of messages) backed by filename
func (ms *MessageStore) Init(appDirectory string, maxBufferLines int, messageCounter metrics.Counter) error {
ms.storeDirectory = appDirectory + "/" + directory + "/"
ms.storeDirectory = path.Join(appDirectory, directory)
os.Mkdir(ms.storeDirectory, 0700)
ms.bufferPos = 0

2
testing/cwtch_peer_server_intergration_test.go

@ -88,7 +88,7 @@ func TestCwtchPeerIntegration(t *testing.T) {
fmt.Println("No server found!")
server = new(cwtchserver.Server)
fmt.Println("Starting cwtch server...")
config := cwtchserver.LoadConfig("server-test.json")
config := cwtchserver.LoadConfig(".", "server-test.json")
identity := config.Identity()
serverAddr = identity.Hostname()
go server.Run(config)

Loading…
Cancel
Save