Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
number571 committed Sep 14, 2024
1 parent 41d4eeb commit 1519e63
Show file tree
Hide file tree
Showing 53 changed files with 33,044 additions and 6,804 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

*??? ??, ????*

### IMPROVEMENTS

- Update `cmd/hidden_lake/helpers`: add new service HLD (distributor)

### CHANGES

- Update `cmd/tools/pmanager`: delete constant from crypto/symmetric package, update keySize 32 -> 33 bytes
Expand Down
10 changes: 10 additions & 0 deletions cmd/hidden_lake/_configs/hld.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
settings: {}
logging:
- info
- warn
- erro
address:
http: 127.0.0.1:9521
services:
hidden-lake-messenger: 127.0.0.1:9592
hidden-lake-filesharer: 127.0.0.1:9542
2 changes: 1 addition & 1 deletion cmd/hidden_lake/_test/result/badge_coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32,813 changes: 28,757 additions & 4,056 deletions cmd/hidden_lake/_test/result/coverage.out

Large diffs are not rendered by default.

859 changes: 442 additions & 417 deletions cmd/hidden_lake/_test/result/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ func HandleIncomingPushHTTP(

fPubKey := asymmetric.LoadRSAPubKey(pR.Header.Get(hls_settings.CHeaderPublicKey))
if fPubKey == nil {
panic("public key is nil (invalid data from HLS)!")
pLogger.PushErro(logBuilder.WithMessage("load_pubkey"))
_ = api.Response(pW, http.StatusForbidden, "failed: load public key")
return
}

if err := isValidMsgBytes(rawMsgBytes); err != nil {
Expand Down
13 changes: 13 additions & 0 deletions cmd/hidden_lake/helpers/distributor/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM --platform=linux/amd64 ubuntu:20.04

RUN apt-get update && apt-get install -y wget gcc
RUN wget https://go.dev/dl/go1.16.linux-amd64.tar.gz && \
tar -C /opt -xzf go1.16.linux-amd64.tar.gz

WORKDIR /go-peer
ENV PATH="${PATH}:/opt/go/bin"
COPY ./ ./
RUN go build -o hll ./cmd/hidden_lake/helpers/distributor/cmd/hld

ENV SERVICE_PATH="/mounted"
CMD ./hld -path="${SERVICE_PATH}"
26 changes: 26 additions & 0 deletions cmd/hidden_lake/helpers/distributor/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
N=1
GC=go build
ROOPATH=../../../..
BINPATH=$(ROOPATH)/bin

.PHONY: default build run test clean

default: build run
default-config:
cp $(ROOPATH)/cmd/hidden_lake/_configs/hld.yml .
build: default-config
$(GC) -o $(BINPATH)/hld ./cmd/hld

for arch in amd64 arm64; \
do \
CGO_ENABLED=0 GOOS=linux GOARCH=$${arch} go build -o $(BINPATH)/hld_$${arch}_linux ./cmd/hld; \
CGO_ENABLED=0 GOOS=windows GOARCH=$${arch} go build -o $(BINPATH)/hld_$${arch}_windows.exe ./cmd/hld; \
CGO_ENABLED=0 GOOS=darwin GOARCH=$${arch} go build -o $(BINPATH)/hld_$${arch}_darwin ./cmd/hld; \
done;
run:
./$(BINPATH)/hld
test:
for i in {1..$(N)}; do go clean -testcache; echo $$i; go test ./...; done
clean:
rm -f $(BINPATH)/hld $(BINPATH)/hld_*
rm -rf hld.yml
3 changes: 3 additions & 0 deletions cmd/hidden_lake/helpers/distributor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# HLD

> Hidden Lake Distributor
24 changes: 24 additions & 0 deletions cmd/hidden_lake/helpers/distributor/_daemon/install_hld.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# root mode
echo "
[Unit]
Description=HiddenLakeDistributor
[Service]
ExecStart=/root/hld_amd64_linux -path=/root
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
" > /etc/systemd/system/hidden_lake_distributor.service

cd /root && \
rm -f hld_amd64_linux && \
wget https://github.com/number571/go-peer/releases/latest/download/hld_amd64_linux && \
chmod +x hld_amd64_linux

systemctl daemon-reload
systemctl enable hidden_lake_distributor.service
systemctl restart hidden_lake_distributor.service
3 changes: 3 additions & 0 deletions cmd/hidden_lake/helpers/distributor/_daemon/journal_hld.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

journalctl -eu hidden_lake_distributor.service
4 changes: 4 additions & 0 deletions cmd/hidden_lake/helpers/distributor/_daemon/restart_hld.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

# root mode
systemctl restart hidden_lake_distributor.service
3 changes: 3 additions & 0 deletions cmd/hidden_lake/helpers/distributor/_daemon/status_hld.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

watch -c SYSTEMD_COLORS=1 systemctl status -o cat hidden_lake_distributor.service
4 changes: 4 additions & 0 deletions cmd/hidden_lake/helpers/distributor/_daemon/stop_hld.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

# root mode
systemctl stop hidden_lake_distributor.service
5 changes: 5 additions & 0 deletions cmd/hidden_lake/helpers/distributor/cmd/hld/hld_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

import "testing"

func TestNothing(_ *testing.T) {}
48 changes: 48 additions & 0 deletions cmd/hidden_lake/helpers/distributor/cmd/hld/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"context"
"errors"
"fmt"
"log"
"os"
"os/signal"
"syscall"

gopeer "github.com/number571/go-peer"
"github.com/number571/go-peer/cmd/hidden_lake/helpers/distributor/pkg/app"
"github.com/number571/go-peer/internal/flag"
)

func main() {
args := os.Args[1:]

if flag.GetBoolFlagValue(args, "version") {
fmt.Println(gopeer.CVersion)
return
}

app, err := app.InitApp(args, ".")
if err != nil {
panic(err)
}

shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)

ctx, cancel := context.WithCancel(context.Background())
closed := make(chan struct{})
defer func() {
cancel()
<-closed
}()

go func() {
defer func() { closed <- struct{}{} }()
if err := app.Run(ctx); err != nil && !errors.Is(err, context.Canceled) {
log.Fatal(err)
}
}()

<-shutdown
}
128 changes: 128 additions & 0 deletions cmd/hidden_lake/helpers/distributor/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package config

import (
"os"

logger "github.com/number571/go-peer/internal/logger/std"
"github.com/number571/go-peer/pkg/encoding"
"github.com/number571/go-peer/pkg/utils"
)

var (
_ IConfig = &SConfig{}
_ IAddress = &SAddress{}
)

type SConfigSettings struct {
}

type SConfig struct {
fLogging logger.ILogging

FSettings *SConfigSettings `yaml:"settings"`
FLogging []string `yaml:"logging,omitempty"`
FAddress *SAddress `yaml:"address"`
FServices map[string]string `yaml:"services,omitempty"`
}

type SAddress struct {
FHTTP string `yaml:"http"`
FPPROF string `yaml:"pprof,omitempty"`
}

func BuildConfig(pFilepath string, pCfg *SConfig) (IConfig, error) {
if _, err := os.Stat(pFilepath); !os.IsNotExist(err) {
return nil, utils.MergeErrors(ErrConfigAlreadyExist, err)
}

if err := pCfg.initConfig(); err != nil {
return nil, utils.MergeErrors(ErrInitConfig, err)
}

if err := os.WriteFile(pFilepath, encoding.SerializeYAML(pCfg), 0o600); err != nil {
return nil, utils.MergeErrors(ErrWriteConfig, err)
}

return pCfg, nil
}

func LoadConfig(pFilepath string) (IConfig, error) {
if _, err := os.Stat(pFilepath); os.IsNotExist(err) {
return nil, utils.MergeErrors(ErrConfigNotExist, err)
}

bytes, err := os.ReadFile(pFilepath)
if err != nil {
return nil, utils.MergeErrors(ErrReadConfig, err)
}

cfg := new(SConfig)
if err := encoding.DeserializeYAML(bytes, cfg); err != nil {
return nil, utils.MergeErrors(ErrDeserializeConfig, err)
}

if err := cfg.initConfig(); err != nil {
return nil, utils.MergeErrors(ErrInitConfig, err)
}

return cfg, nil
}

func (p *SConfig) isValid() bool {
return true &&
p.FAddress.FHTTP != ""
}

func (p *SConfig) initConfig() error {
if p.FSettings == nil {
p.FSettings = new(SConfigSettings)
}

if p.FAddress == nil {
p.FAddress = new(SAddress)
}

if !p.isValid() {
return ErrInvalidConfig
}

if err := p.loadLogging(); err != nil {
return utils.MergeErrors(ErrLoadLogging, err)
}

return nil
}

func (p *SConfig) loadLogging() error {
result, err := logger.LoadLogging(p.FLogging)
if err != nil {
return utils.MergeErrors(ErrInvalidLogging, err)
}
p.fLogging = result
return nil
}

func (p *SConfig) GetSettings() IConfigSettings {
return p.FSettings
}

func (p *SConfig) GetAddress() IAddress {
return p.FAddress
}

func (p *SAddress) GetHTTP() string {
return p.FHTTP
}

func (p *SAddress) GetPPROF() string {
return p.FPPROF
}

func (p *SConfig) GetService(name string) (string, bool) {
service, ok := p.FServices[name]
return service, ok
}

func (p *SConfig) GetLogging() logger.ILogging {
return p.fLogging
}
85 changes: 85 additions & 0 deletions cmd/hidden_lake/helpers/distributor/internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package config

import (
"os"
"testing"
)

const (
tcConfigFile = "config_test.txt"
tcLogging = true
tcAddress1 = "test_address1"
tcAddress2 = "test_address2"
tcService1 = "test_service1"
tcServiceAddress1 = "test_service_address1"
tcWorkSize = 22
tcCapMessages = 1000
)

func TestError(t *testing.T) {
t.Parallel()

str := "value"
err := &SConfigError{str}
if err.Error() != errPrefix+str {
t.Error("incorrect err.Error()")
return
}
}

func testConfigDefaultInit(configPath string) {
_, _ = BuildConfig(configPath, &SConfig{
FSettings: &SConfigSettings{},
FLogging: []string{"info", "erro"},
FAddress: &SAddress{
FHTTP: tcAddress1,
FPPROF: tcAddress2,
},
FServices: map[string]string{
tcService1: tcServiceAddress1,
},
})
}

func TestConfig(t *testing.T) {
t.Parallel()

testConfigDefaultInit(tcConfigFile)
defer os.Remove(tcConfigFile)

cfg, err := LoadConfig(tcConfigFile)
if err != nil {
t.Error(err)
return
}

if cfg.GetLogging().HasInfo() != tcLogging {
t.Error("logging.info is invalid")
return
}

if cfg.GetLogging().HasErro() != tcLogging {
t.Error("logging.erro is invalid")
return
}

if cfg.GetLogging().HasWarn() == tcLogging {
t.Error("logging.warn is invalid")
return
}

if cfg.GetAddress().GetHTTP() != tcAddress1 {
t.Error("address http is invalid")
return
}

if cfg.GetAddress().GetPPROF() != tcAddress2 {
t.Error("address pprof is invalid")
return
}

if v, ok := cfg.GetService(tcService1); !ok || v != tcServiceAddress1 {
t.Error("service address is invalid")
return
}
}
Loading

0 comments on commit 1519e63

Please sign in to comment.