Skip to content

Commit

Permalink
major update (#1)
Browse files Browse the repository at this point in the history
major restruct

Signed-off-by: Yuji Ito <llamerada.jp@gmail.com>
  • Loading branch information
llamerada-jp committed Dec 23, 2023
1 parent cff095a commit c79b450
Show file tree
Hide file tree
Showing 140 changed files with 21,227 additions and 1,139 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CI
on:
pull_request:
push:
branches:
- main
jobs:
test-node:
name: CI
runs-on: ubuntu-22.04
timeout-minutes: 30
steps:
- uses: actions/checkout@v2
- name: setup go
uses: actions/setup-go@v4
with:
go-version: "1.20"
check-latest: true
cache-dependency-path: "go.sum"
- name: cache setup files
uses: actions/cache@v3
with:
path: |
build
node_modules
key: ${{ runner.os }}-${{ hashFiles('Makefile') }}-${{hashFiles('package-lock.json')}}
- shell: bash
run: |
echo '{ "cookie_key_pair": "RmdxSzJhWUxEMmtpV0F3aHdqWStnQ0Yva0E3V0JrSGlDUGZCK3NWNTFKZz0=", "google_api_key":"DUMMY", "google_map_id":"DUMMY"}' > secrets.json
./ci.sh
28 changes: 26 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
/.vscode/
/www/js/colonio.js
/www/js/colonio.wasm
/bin/seed
/build/
/cmd/node/commit_hash.txt
/cmd/seed/commit_hash.txt
/coverage
/dist/*.map
/dist/*.wasm
/dist/colonio_go.js
/dist/colonio.js
/dist/container.js
/dist/controller.js
/dist/oinari.js
/dist/test/*.map
/dist/test/*.wasm
/dist/test/test.js
/dist/wasm_exec.js
/docs/.hugo_build.lock
/docs/resources/_gen/
/localhost.crt
/localhost.key
/node_modules
/secrets.json
/src/colonio_go.d.ts
/src/colonio.d.ts
/src/colonio.js
/src/keys.ts
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "docs/themes/hugo-book"]
path = docs/themes/hugo-book
url = https://github.com/alex-shpak/hugo-book.git
97 changes: 97 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
SHELL := /bin/bash -o pipefail

BUILD_TYPE ?= Debug
COLONIO_BRANCH := main
COLONIO_FILES := dist/colonio.js dist/colonio_go.js dist/colonio.wasm
GO_FILES := $(shell find . -name *.go | grep -v ./build/)
TS_FILES := $(shell find ./src/ -name *.ts) src/colonio.d.ts src/colonio_go.d.ts src/keys.ts
OINARI_FILES := dist/wasm_exec.js
MAP_FILES := dist/colonio.wasm.map dist/colonio_go.js.map

.PHONY: build
build: $(COLONIO_FILES) $(OINARI_FILES) build-go build-ts

.PHONY: build-go
build-go: $(GO_FILES) go.mod go.sum
git show --format='%H' --no-patch > ./cmd/seed/commit_hash.txt
go build -o ./bin/seed ./cmd/seed
git show --format='%H' --no-patch > ./cmd/node/commit_hash.txt
GOOS=js GOARCH=wasm go build -o ./dist/oinari.wasm ./cmd/node
GOOS=js GOARCH=wasm go build -o ./dist/test/exit.wasm ./cmd/app/exit
GOOS=js GOARCH=wasm go build -o ./dist/test/fox.wasm ./cmd/app/fox
GOOS=js GOARCH=wasm go build -o ./dist/test/sleep.wasm ./cmd/app/sleep
GOOS=js GOARCH=wasm go build -o ./dist/test/sleep_core.wasm ./cmd/app/sleep_core
GOOS=js GOARCH=wasm go test -o ./dist/test/test_crosslink.wasm -c ./lib/crosslink/
## should edit TESTS@src/test.ts to run test build by wasm
GOOS=js GOARCH=wasm go test -o ./dist/test/test_api_core.wasm -c ./api/core/
GOOS=js GOARCH=wasm go test -o ./dist/test/test_node.wasm -c ./cmd/node/

.PHONY: build-ts
build-ts: $(TS_FILES) package.json tsconfig.json webpack.config.js
npm run build
mv ./dist/test.js ./dist/test/test.js

.PHONY: setup
setup:
mkdir -p build
# colonio
rm -rf build/colonio
if [ "$${COLONIO_DEV_PATH}" = "" ]; \
then git clone -b $(COLONIO_BRANCH) --depth=1 https://github.com/llamerada-jp/colonio.git build/colonio; \
else ln -s $${COLONIO_DEV_PATH} build/colonio; \
fi
$(MAKE) -C build/colonio build BUILD_TYPE=$(BUILD_TYPE)
npm install

.PHONY: s
s: build generate-cert
sudo sysctl -w net.core.rmem_max=2500000
while true; do go run ./cmd/seed --debug; done

.PHONY: test
test: build generate-cert
sudo sysctl -w net.core.rmem_max=2500000
npm t
go run ./cmd/seed --test

dist/colonio.js: build/colonio/output/colonio.js
cp $< $@

dist/colonio.wasm: build/colonio/output/colonio.wasm
cp $< $@

dist/colonio.wasm.map: build/colonio/output/colonio.wasm.map
cp $< $@

dist/colonio_go.js: build/colonio/src/js/colonio_go.js
cp $< $@

dist/colonio_go.js.map: build/colonio/src/js/colonio_go.js.map
cp $< $@

dist/wasm_exec.js: $(shell go env GOROOT)/misc/wasm/wasm_exec.js
cp $< $@

src/colonio.d.ts: build/colonio/src/js/core.d.ts
cp $< $@

src/colonio_go.d.ts: build/colonio/src/js/colonio_go.d.ts
cp $< $@

src/keys.ts: src/keys.temp secrets.json
go run ./cmd/tool template -i src/keys.temp -v secrets.json > $@

.PHONY: generate-cert
generate-cert:
openssl req -x509 -out localhost.crt -keyout localhost.key \
-newkey rsa:2048 -nodes -sha256 \
-subj '/CN=localhost' -extensions EXT -config <( \
printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")

.PHONY: clean
clean:
rm -f ./dist/*.js ./dist/**/*.js ./dist/*.map ./dist/**/*.map ./dist/*.wasm ./dist/**/*.wasm $(OINARI_FILES) ./src/colonio_go.d.ts ./src/colonio.d.ts ./src/keys.ts localhost.crt localhost.key

.PHONY: deisclean
deisclean: clean
rm -fr ./node_modules ./coverage ./build
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Oinari

Oinari(オイナリ) is the PoC project using distributed algorithms. In this program, by connecting users' browsers and collaborating with each other, it is possible to diffuse the data with little use of the server.

## Commands

Prepare keys for embedding and build this project.

```sh
$ cat secrets.json
{
"cookie_key_pair": "<random base64 encoded key for cookie>",
"google_api_key": "<google api key>",
"google_map_id": "<ID of google map>"
}

$ make setup
$ make build
```

## License

Apache License 2.0
135 changes: 135 additions & 0 deletions api/core/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright 2018 Yuji Ito <llamerada.jp@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package core

import (
"crypto/sha256"
"encoding/hex"
"fmt"

"golang.org/x/exp/slices"
)

type Account struct {
Meta *ObjectMeta `json:"meta"`
State *AccountState `json:"state"`
}

type AccountState struct {
// map describing pod's uuid and pod state
Pods map[string]AccountPodState `json:"pods"`
// map describing nid and timestamp of keepalive
Nodes map[string]AccountNodeState `json:"nodes"`
}

type AccountPodState struct {
RunningNode string `json:"runningNode"`
Timestamp string `json:"timestamp"`
}

type NodeType string

const (
NodeTypeMobile NodeType = "Mobile"
NodeTypeSmallDevice NodeType = "SmallDevice"
NodeTypePC NodeType = "PC"
NodeTypeServer NodeType = "Server"
NodeTypeGrass NodeType = "Grass"
NodeTypeOther NodeType = "Other"
)

var NodeTypeAccepted = []NodeType{
NodeTypeMobile,
NodeTypeSmallDevice,
NodeTypePC,
NodeTypeServer,
NodeTypeGrass,
NodeTypeOther,
}

type AccountNodeState struct {
Name string `json:"name"`
Timestamp string `json:"timestamp"`
NodeType NodeType `json:"nodeType"`
Position *Vector3 `json:"position,omitempty"`
}

// use sha256 hash as account's uuid
func GenerateAccountUuid(name string) string {
hash := sha256.Sum256([]byte(name))
return hex.EncodeToString(hash[:])
}

func (account *Account) Validate() error {
if account.Meta == nil {
return fmt.Errorf("metadata field should be filled")
}

if err := account.Meta.Validate(ResourceTypeAccount); err != nil {
return fmt.Errorf("invalid metadata for %s %w", account.Meta.Name, err)
}

if account.Meta.Uuid != GenerateAccountUuid(account.Meta.Name) {
return fmt.Errorf("invalid uuid for %s", account.Meta.Name)
}

if account.State == nil {
return fmt.Errorf("state filed should be filled")
}

if err := account.State.validate(); err != nil {
return fmt.Errorf("invalid account state for %s %w", account.Meta.Name, err)
}
return nil
}

func (state *AccountState) validate() error {
if state.Pods == nil {
return fmt.Errorf("pods field should be filled")
}

for podUuid, podState := range state.Pods {
if err := ValidatePodUuid(podUuid); err != nil {
return fmt.Errorf("there is an invalid pod uuid for pods: %w", err)
}
if len(podState.RunningNode) != 0 {
if err := ValidateNodeId(podState.RunningNode); err != nil {
return fmt.Errorf("there is an invalid node if for pod %s: %w", podUuid, err)
}
}
if err := ValidateTimestamp(podState.Timestamp); err != nil {
return fmt.Errorf("there is an invalid timestamp for pod %s: %w", podUuid, err)
}
}

if state.Nodes == nil {
return fmt.Errorf("nodes field should be filled")
}

for nid, nodeState := range state.Nodes {
if err := ValidateNodeId(nid); err != nil {
return fmt.Errorf("there is an invalid node id for nodes: %w", err)
}
if err := ValidateTimestamp(nodeState.Timestamp); err != nil {
return fmt.Errorf("there is an invalid timestamp for nodes: %w", err)
}
if !slices.Contains(NodeTypeAccepted, nodeState.NodeType) {
return fmt.Errorf("there is an unsupported node type in the node state")
}
}

return nil
}
Loading

0 comments on commit c79b450

Please sign in to comment.