add partnershipping mechanism

This commit is contained in:
mr
2026-01-23 08:07:17 +01:00
parent 24cb25c350
commit 9e4d31e797
6 changed files with 171 additions and 44 deletions

View File

@@ -3,6 +3,8 @@ package conf
import "sync" import "sync"
type Config struct { type Config struct {
Name string
Hostname string
PublicKeyPath string PublicKeyPath string
PrivateKeyPath string PrivateKeyPath string
DHTEndpointPort int64 DHTEndpointPort int64

View File

@@ -1,10 +1,10 @@
package controllers package controllers
import ( import (
"encoding/json"
oclib "cloud.o-forge.io/core/oc-lib" oclib "cloud.o-forge.io/core/oc-lib"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/models/peer" "cloud.o-forge.io/core/oc-lib/models/peer"
"cloud.o-forge.io/core/oc-lib/tools"
beego "github.com/beego/beego/v2/server/web" beego "github.com/beego/beego/v2/server/web"
) )
@@ -28,23 +28,6 @@ func (o *PeerController) Search() {
o.ServeJSON() o.ServeJSON()
} }
// @Title Update
// @Description create peers
// @Param id path string true "the peer id you want to get"
// @Param body body models.peer true "The peer content"
// @Success 200 {object} models.peer
// @router /:id [put]
func (o *PeerController) Put() {
// store and return Id or post with UUID
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
var res map[string]interface{}
id := o.Ctx.Input.Param(":id")
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
data := oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).UpdateOne(res, id)
o.Data["json"] = data
o.ServeJSON()
}
// @Title GetAll // @Title GetAll
// @Description find all peer // @Description find all peer
// @Param is_draft query string false // @Param is_draft query string false
@@ -53,7 +36,16 @@ func (o *PeerController) Put() {
func (o *PeerController) GetAll() { func (o *PeerController) GetAll() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request) user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
isDraft := o.Ctx.Input.Query("is_draft") isDraft := o.Ctx.Input.Query("is_draft")
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).LoadAll(isDraft == "true") verify := o.Ctx.Input.Query("verify")
if verify == "true" {
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).Search(&dbs.Filters{
And: map[string][]dbs.Filter{
"verify": {{Operator: dbs.EQUAL.String(), Value: true}},
},
}, "", false)
} else {
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).LoadAll(isDraft == "true")
}
o.ServeJSON() o.ServeJSON()
} }
@@ -69,18 +61,83 @@ func (o *PeerController) Get() {
o.ServeJSON() o.ServeJSON()
} }
// @Title Partner // @Title Link
// @Description add partner peer by peerid // @Description find peer by peerid
// @Param id path string true "the peer id you want to partner" // @Param id path string true "the peer id you want to get"
// @Success 200 {peer} models.peer // @Success 200 {peer} models.peer
// @router /:id/partner [post] // @router /:from/link/:relation [get]
func (o *PeerController) Nano() { func (o *PeerController) Link() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":from")
if ok, _ := peer.IsMySelf(peerID); ok {
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 400,
"error": "can't link relation to ourself",
}
o.ServeJSON()
return
}
if ok, _ := peer.IsMySelf(id); !ok {
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 400,
"error": "can't link relation",
}
o.ServeJSON()
return
}
relation := o.Ctx.Input.Param(":relation") // as partner, blacklist, unknown
req := oclib.NewRequestAdmin(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil)
l := req.LoadOne(id)
if p := l.ToPeer(); p != nil {
if peer.GetRelationPath(relation) != -1 {
o.Data["json"] = req.UpdateOne(map[string]interface{}{
"relation": peer.GetRelationPath(relation),
"verify": !(p.Relation == peer.PENDING_PARTNER || relation == peer.NONE.Path()),
}, p.GetID())
return
}
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 400,
"error": "relation unavailable",
}
o.ServeJSON()
return
}
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 404,
"error": "peer not found",
}
o.ServeJSON()
}
// @Title unknown
// @Description add unknown peer by peerid
// @Param id path string true "the peer id you want to blacklist"
// @Success 200 {peer} models.peer
// @router /:id/unknown [post]
func (o *PeerController) Unknown() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request) user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id") id := o.Ctx.Input.Param(":id")
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).UpdateOne(map[string]interface{}{ req := oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil)
"state": peer.PARTNER, data := req.LoadOne(id)
}, id) o.changeRelation(data.ToPeer(), peer.NONE, req)
o.ServeJSON() }
// @Title Partner
// @Description add partner peer by peerid
// @Param id path string true "the peer id you want to blacklist"
// @Success 200 {peer} models.peer
// @router /:id/partner [post]
func (o *PeerController) Partner() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
req := oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil)
data := req.LoadOne(id)
o.changeRelation(data.ToPeer(), peer.PARTNER, req)
} }
// @Title Blacklist // @Title Blacklist
@@ -92,8 +149,59 @@ func (o *PeerController) Blacklist() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request) user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id") id := o.Ctx.Input.Param(":id")
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).UpdateOne(map[string]interface{}{ o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).UpdateOne(map[string]interface{}{
"state": peer.BLACKLIST, "relation": peer.BLACKLIST,
}, id) }, id)
}
// used from : peer ask, or response, only from peer origin is authorized to change...
func (o *PeerController) changeRelation(dest *peer.Peer, relation peer.PeerRelation, request *oclib.Request) {
if ok, _ := peer.IsMySelf(request.PeerID); !ok {
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 400,
"error": "can't change relation",
}
o.ServeJSON()
return
}
if ok, _ := peer.IsMySelf(dest.GetID()); ok {
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 400,
"error": "can't change ourself",
}
o.ServeJSON()
return
}
// store and return Id or post with UUID
if dest != nil {
if !dest.Verify && relation == peer.PARTNER {
relation = peer.PENDING_PARTNER
if _, err := tools.NewHTTPCaller(map[tools.DataType]map[tools.METHOD]string{}).CallGet(dest.Url, "/"+request.PeerID+"/link/"+relation.Path()); err != nil {
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 400,
"error": err.Error(),
}
o.ServeJSON()
}
}
if dest.Verify && relation == peer.PENDING_PARTNER {
relation = peer.PARTNER
}
data := request.UpdateOne(map[string]interface{}{
"relation": relation,
}, dest.GetID())
o.Data["json"] = data
o.ServeJSON()
return
}
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 400,
"error": "peer not found.",
}
o.ServeJSON() o.ServeJSON()
} }

2
go.mod
View File

@@ -5,7 +5,7 @@ go 1.24.6
toolchain go1.24.11 toolchain go1.24.11
require ( require (
cloud.o-forge.io/core/oc-lib v0.0.0-20260115122757-1c3b9218f7fb cloud.o-forge.io/core/oc-lib v0.0.0-20260123065115-f3d7c65b18d1
github.com/beego/beego/v2 v2.3.8 github.com/beego/beego/v2 v2.3.8
github.com/smartystreets/goconvey v1.7.2 github.com/smartystreets/goconvey v1.7.2
) )

12
go.sum
View File

@@ -4,6 +4,18 @@ cloud.o-forge.io/core/oc-lib v0.0.0-20260115112656-7c5d5c491f41 h1:O82m02OUvyVSe
cloud.o-forge.io/core/oc-lib v0.0.0-20260115112656-7c5d5c491f41/go.mod h1:vHWauJsS6ryf7UDqq8hRXoYD5RsONxcFTxeZPOztEuI= cloud.o-forge.io/core/oc-lib v0.0.0-20260115112656-7c5d5c491f41/go.mod h1:vHWauJsS6ryf7UDqq8hRXoYD5RsONxcFTxeZPOztEuI=
cloud.o-forge.io/core/oc-lib v0.0.0-20260115122757-1c3b9218f7fb h1:blCzhIrRW1gLTsAVVxFxfhA5LXenxiVNT4kn1MTphLg= cloud.o-forge.io/core/oc-lib v0.0.0-20260115122757-1c3b9218f7fb h1:blCzhIrRW1gLTsAVVxFxfhA5LXenxiVNT4kn1MTphLg=
cloud.o-forge.io/core/oc-lib v0.0.0-20260115122757-1c3b9218f7fb/go.mod h1:vHWauJsS6ryf7UDqq8hRXoYD5RsONxcFTxeZPOztEuI= cloud.o-forge.io/core/oc-lib v0.0.0-20260115122757-1c3b9218f7fb/go.mod h1:vHWauJsS6ryf7UDqq8hRXoYD5RsONxcFTxeZPOztEuI=
cloud.o-forge.io/core/oc-lib v0.0.0-20260122131802-b98728675928 h1:/JATUIWRD632NX+89nxawYPSCUETwy+v0yp6l6F5HkM=
cloud.o-forge.io/core/oc-lib v0.0.0-20260122131802-b98728675928/go.mod h1:vHWauJsS6ryf7UDqq8hRXoYD5RsONxcFTxeZPOztEuI=
cloud.o-forge.io/core/oc-lib v0.0.0-20260122145527-00bcca379fe6 h1:P2ocksh1qJ/7LDfVvKzqg8KudZgn43Bp4ATQeY00wmI=
cloud.o-forge.io/core/oc-lib v0.0.0-20260122145527-00bcca379fe6/go.mod h1:vHWauJsS6ryf7UDqq8hRXoYD5RsONxcFTxeZPOztEuI=
cloud.o-forge.io/core/oc-lib v0.0.0-20260122151154-8f5f3e331d1d h1:kSzGiETAjBHUS582OB8c1fIIsk3Agx550Pk1tE3GWBg=
cloud.o-forge.io/core/oc-lib v0.0.0-20260122151154-8f5f3e331d1d/go.mod h1:vHWauJsS6ryf7UDqq8hRXoYD5RsONxcFTxeZPOztEuI=
cloud.o-forge.io/core/oc-lib v0.0.0-20260123063820-b71b1e741d8c h1:Vq3/bZJ4GUIPo4tyJLSDRwLC1d+sxJCkmZbZzTHu27c=
cloud.o-forge.io/core/oc-lib v0.0.0-20260123063820-b71b1e741d8c/go.mod h1:vHWauJsS6ryf7UDqq8hRXoYD5RsONxcFTxeZPOztEuI=
cloud.o-forge.io/core/oc-lib v0.0.0-20260123064804-d06c9e933783 h1:ztJplXt5FIrSTB7quEFRYUaMrZTxuIN/OJaJ4hBBtXc=
cloud.o-forge.io/core/oc-lib v0.0.0-20260123064804-d06c9e933783/go.mod h1:vHWauJsS6ryf7UDqq8hRXoYD5RsONxcFTxeZPOztEuI=
cloud.o-forge.io/core/oc-lib v0.0.0-20260123065115-f3d7c65b18d1 h1:K7ind/dAshdoFb0om35YY6phWJcYhHj1YMlTrrwKH4s=
cloud.o-forge.io/core/oc-lib v0.0.0-20260123065115-f3d7c65b18d1/go.mod h1:vHWauJsS6ryf7UDqq8hRXoYD5RsONxcFTxeZPOztEuI=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/beego/beego/v2 v2.3.8 h1:wplhB1pF4TxR+2SS4PUej8eDoH4xGfxuHfS7wAk9VBc= github.com/beego/beego/v2 v2.3.8 h1:wplhB1pF4TxR+2SS4PUej8eDoH4xGfxuHfS7wAk9VBc=
github.com/beego/beego/v2 v2.3.8/go.mod h1:8vl9+RrXqvodrl9C8yivX1e6le6deCK6RWeq8R7gTTg= github.com/beego/beego/v2 v2.3.8/go.mod h1:8vl9+RrXqvodrl9C8yivX1e6le6deCK6RWeq8R7gTTg=

View File

@@ -26,6 +26,7 @@ type DHTRecord struct {
PeerID string PeerID string
PubKey []byte PubKey []byte
URL string URL string
NATSUrl string
Signature []byte Signature []byte
ExpiryDate time.Time ExpiryDate time.Time
} }
@@ -63,20 +64,19 @@ func Init(ctx context.Context) (*DHTService, error) {
return nil, err return nil, err
} }
singletonService = service singletonService = service
for { if VerifyPubWithPriv() {
if VerifyPubWithPriv() { if _, err := singletonService.ClaimName(context.Background(),
o := oclib.GetConfLoader() conf.GetConfig().Name,
if _, err := singletonService.ClaimName(context.Background(), o.GetStringDefault("NAME", "local"), o.GetStringDefault("HOSTNAME", "http://localhost")); err == nil { conf.GetConfig().Hostname); err == nil {
go func() { go func() {
for { for {
singletonService.RefreshName(context.Background()) singletonService.RefreshName(context.Background())
time.Sleep(59 * time.Minute) time.Sleep(59 * time.Minute)
} }
}() }()
}
} }
break
} }
return service, service.DHT.Bootstrap(ctx) return service, service.DHT.Bootstrap(ctx)
} }
@@ -106,6 +106,7 @@ func (d *DHTService) ClaimName(
rec.Signature = sig rec.Signature = sig
rec.URL = endPoint rec.URL = endPoint
rec.NATSUrl = oclib.GetConfig().NATSUrl
rec.State = peer.ONLINE.EnumIndex() rec.State = peer.ONLINE.EnumIndex()
rec.ExpiryDate = expiry rec.ExpiryDate = expiry
@@ -145,6 +146,7 @@ func (d *DHTService) ClaimName(
PeerID: d.Host.ID().String(), PeerID: d.Host.ID().String(),
PublicKey: pubStr, PublicKey: pubStr,
Url: endPoint, Url: endPoint,
NATSUrl: oclib.GetConfig().NATSUrl,
WalletAddress: "my-wallet", WalletAddress: "my-wallet",
} }
if founded, _, err := access.Search(nil, fmt.Sprintf("%v", peer.SELF.EnumIndex()), false); err != nil || len(founded) == 0 { if founded, _, err := access.Search(nil, fmt.Sprintf("%v", peer.SELF.EnumIndex()), false); err != nil || len(founded) == 0 {
@@ -200,6 +202,7 @@ func (d *DHTService) treatPeer(ctx context.Context, key string, data []byte) (*p
PeerID: rec.PeerID, PeerID: rec.PeerID,
PublicKey: pubStr, PublicKey: pubStr,
Url: rec.URL, Url: rec.URL,
NATSUrl: rec.NATSUrl,
} }
access := peer.NewAccessor(&tools.APIRequest{Admin: true}) access := peer.NewAccessor(&tools.APIRequest{Admin: true})
if now.After(rec.ExpiryDate) { if now.After(rec.ExpiryDate) {
@@ -291,7 +294,6 @@ func (d *DHTService) existsDHT(ctx context.Context) (*DHTRecord, error) {
Name: rec.Name, Name: rec.Name,
PeerID: rec.PeerID, PeerID: rec.PeerID,
PubKey: rec.PubKey, PubKey: rec.PubKey,
URL: rec.URL,
} }
payload, _ := json.Marshal(dht) payload, _ := json.Marshal(dht)
@@ -325,6 +327,7 @@ func (d *DHTService) RefreshName( // peer should regulary refresh your host to n
PeerID: rec.PeerID, PeerID: rec.PeerID,
PublicKey: string(rec.PubKey), PublicKey: string(rec.PubKey),
Url: rec.URL, Url: rec.URL,
NATSUrl: rec.NATSUrl,
} }
if founded, _, err := access.Search(nil, rec.Name, false); err != nil || len(founded) == 0 { if founded, _, err := access.Search(nil, rec.Name, false); err != nil || len(founded) == 0 {
access.StoreOne(p) access.StoreOne(p)

View File

@@ -30,6 +30,8 @@ func main() {
o.GetStringDefault("LOG_LEVEL", "info"), o.GetStringDefault("LOG_LEVEL", "info"),
) )
conf.GetConfig().Name = o.GetStringDefault("NAME", "local")
conf.GetConfig().Hostname = o.GetStringDefault("HOSTNAME", "http://localhost")
conf.GetConfig().PublicKeyPath = o.GetStringDefault("PUBLIC_KEY_PATH", "./pem/public.pem") conf.GetConfig().PublicKeyPath = o.GetStringDefault("PUBLIC_KEY_PATH", "./pem/public.pem")
conf.GetConfig().PrivateKeyPath = o.GetStringDefault("PRIVATE_KEY_PATH", "./pem/private.pem") conf.GetConfig().PrivateKeyPath = o.GetStringDefault("PRIVATE_KEY_PATH", "./pem/private.pem")
conf.GetConfig().DHTEndpointPort = o.GetInt64Default("DHT_ENDPOINT_PORT", 80) conf.GetConfig().DHTEndpointPort = o.GetInt64Default("DHT_ENDPOINT_PORT", 80)