mps

morphux package server
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/mps

commit 99be9de6f513075e1f553c8d4f316f914b1d3a75
parent 0b884ac2024ed28c284bf305ada640f59424286f
Author: MrGosti <gostitsog@gmail.com>
Date:   Mon, 20 Mar 2017 15:36:52 +0100

Is this it ?

Diffstat:
Dauth.go | 12------------
Dauthack.go | 8--------
Mdatabase.go | 72+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Derror.go | 26--------------------------
Dheader.go | 27---------------------------
Dmessage.go | 34----------------------------------
Amessage/error.go | 26++++++++++++++++++++++++++
Amessage/header.go | 27+++++++++++++++++++++++++++
Amessage/message.go | 34++++++++++++++++++++++++++++++++++
Amessage/payload.go | 26++++++++++++++++++++++++++
Mnetwork.go | 18++++++++++++------
Dpayload.go | 26--------------------------
Dreq_get_cat.go | 8--------
Dreq_get_file.go | 9---------
Dreq_get_news.go | 9---------
Dreq_get_pkg.go | 68--------------------------------------------------------------------
Dreq_get_upd.go | 8--------
Arequest/auth.go | 10++++++++++
Arequest/req_get_cat.go | 10++++++++++
Arequest/req_get_file.go | 11+++++++++++
Arequest/req_get_news.go | 11+++++++++++
Arequest/req_get_pkg.go | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Arequest/req_get_upd.go | 10++++++++++
Dresp_cat.go | 10----------
Dresp_file.go | 11-----------
Dresp_news.go | 14--------------
Dresp_pkg.go | 22----------------------
Aresponse/authack.go | 10++++++++++
Aresponse/resp_cat.go | 12++++++++++++
Aresponse/resp_file.go | 13+++++++++++++
Aresponse/resp_news.go | 16++++++++++++++++
Aresponse/resp_pkg.go | 24++++++++++++++++++++++++
Dstruct.go | 71-----------------------------------------------------------------------
Atest_database/fake.db | 0
Atest_database/foo.db | 0
Atest_database/mock.db | 0
36 files changed, 370 insertions(+), 372 deletions(-)

diff --git a/auth.go b/auth.go @@ -1,12 +0,0 @@ -package main - -type Auth struct { - Message - - mpm_major_version uint8 - mpm_minor_version uint8 -} - -func Version() []byte { - return []byte{0, 1} -} diff --git a/authack.go b/authack.go @@ -1,8 +0,0 @@ -package main - -type AuthACK struct { - Message - - MPMMajorVersion uint8 - MPMMinorVersion uint8 -} diff --git a/database.go b/database.go @@ -2,8 +2,14 @@ package main import ( "database/sql" + "errors" + "fmt" "log" + "strconv" + "strings" + "github.com/Morphux/mps/request" + "github.com/Morphux/mps/response" _ "github.com/mattn/go-sqlite3" ) @@ -15,13 +21,73 @@ type Package struct { Category string Description string Archive string - SBU uint64 + SBU float64 Dependencies string - ArchiveSize uint64 - InstalledSize uint64 + ArchiveSize float64 + InstalledSize float64 ArchiveHash string } +func RequestPackage(data []byte, db *sql.DB) (int, Package, error) { + pkg := Package{} + + req := new(request.ReqGetPKG) + + n, err := req.Unpack(data) + + if err != nil { + return n, pkg, err + } + + fmt.Println(req) + + if req.ID != 0 && req.NameLen == 0 && req.CategLen == 0 { + fmt.Println("search By id") + pkg, err = QueryPkgID(req.ID, req.State, db) + } else if req.ID == 0 && req.NameLen != 0 && req.CategLen != 0 { + fmt.Println("search By category") + pkg, err = QueryPkgNameAndCat(req.Name, req.Category, req.State, db) + } else { + err = errors.New("A packet send by the client is wrong") + } + + return n, pkg, err +} + +func PkgtoRespPkg(pkg Package) (error, *response.RespPkg) { + + dep := strings.Split(pkg.Dependencies, ",") + + var depID []uint64 + for _, v := range dep { + i, err := strconv.Atoi(v) + if err != nil { + continue + } + depID = append(depID, uint64(i)) + } + + ret := new(response.RespPkg) + ret.ID = pkg.ID + ret.CompTime = pkg.SBU + ret.InstSize = pkg.InstalledSize + ret.ArchSize = pkg.ArchiveSize + ret.NameLen = uint64(len(pkg.Name)) + ret.CategoryLen = uint16(len(pkg.Category)) + ret.VersionLen = uint16(len(pkg.Version)) + ret.ArchiveLen = uint16(len(pkg.Archive)) + ret.ChecksumLen = uint16(len(pkg.ArchiveHash)) + ret.DependenciesSize = uint16(len(depID)) + ret.Name = pkg.Name + ret.Category = pkg.Category + ret.Version = pkg.Version + ret.Archive = pkg.Archive + ret.Checksum = pkg.ArchiveHash + ret.Dependencies = depID + + return nil, ret +} + func QueryPkgNameAndCat(name string, category string, state uint8, db *sql.DB) (Package, error) { pkg := Package{} diff --git a/error.go b/error.go @@ -1,26 +0,0 @@ -package main - -//An error happened server side -const ERR_SERVER_FAULT uint8 = 0x1 - -//A packet send by the client is wrong -const ERR_MALFORMED_PACKET uint8 = 0x2 - -//A request send by the client find no result -const ERR_RES_NOT_FOUND uint8 = 0x3 - -type Error struct { - Message - - ErrorType uint8 - ErrorLen uint16 - Error string -} - -func (p *Error) PackError(err error, errortype uint8) ([]byte, error) { - p.ErrorType = errortype - p.Error = err.Error() - p.ErrorLen = uint16(len(p.Error)) - - return p.Pack() -} diff --git a/header.go b/header.go @@ -1,27 +0,0 @@ -package main - -import "github.com/Nyarum/barrel" - -type Header struct { - Message - - Type uint8 - Size uint16 - NextPkgLen uint8 - NextPkg string -} - -func (h *Header) Unpack(data []byte) (int, error) { - - barrel := barrel.NewBarrel() - load := barrel.Load(h, data, false) - - err := barrel.Unpack(load) - if err != nil { - return 0, err - } - - h.NextPkg = string(data[4 : 4+h.NextPkgLen]) - - return 3 + int(h.NextPkgLen), nil -} diff --git a/message.go b/message.go @@ -1,34 +0,0 @@ -package main - -import "github.com/Nyarum/barrel" - -//Message is the basic structure to send and receive Message all the methods can be overwritted if needed -type Message struct { -} - -func (p *Message) Default() { - //default value -} - -func (p Message) Check(stats *barrel.Stats) bool { - - return true -} - -func (p *Message) Unpack(data []byte) error { - barrel := barrel.NewBarrel() - load := barrel.Load(p, data, false) - - err := barrel.Unpack(load) - - return err -} - -func (p *Message) Pack() ([]byte, error) { - barrel := barrel.NewBarrel() - load := barrel.Load(p, []byte{}, true) - - err := barrel.Pack(load) - - return barrel.Bytes(), err -} diff --git a/message/error.go b/message/error.go @@ -0,0 +1,26 @@ +package message + +//An error happened server side +const ERR_SERVER_FAULT uint8 = 0x1 + +//A packet send by the client is wrong +const ERR_MALFORMED_PACKET uint8 = 0x2 + +//A request send by the client find no result +const ERR_RES_NOT_FOUND uint8 = 0x3 + +type Error struct { + Message + + ErrorType uint8 + ErrorLen uint16 + Error string +} + +func (p *Error) PackError(err error, errortype uint8) ([]byte, error) { + p.ErrorType = errortype + p.Error = err.Error() + p.ErrorLen = uint16(len(p.Error)) + + return p.Pack() +} diff --git a/message/header.go b/message/header.go @@ -0,0 +1,27 @@ +package message + +import "github.com/Nyarum/barrel" + +type Header struct { + Message + + Type uint8 + Size uint16 + NextPkgLen uint8 + NextPkg string +} + +func (h *Header) Unpack(data []byte) (int, error) { + + barrel := barrel.NewBarrel() + load := barrel.Load(h, data, false) + + err := barrel.Unpack(load) + if err != nil { + return 0, err + } + + h.NextPkg = string(data[4 : 4+h.NextPkgLen]) + + return 3 + int(h.NextPkgLen), nil +} diff --git a/message/message.go b/message/message.go @@ -0,0 +1,34 @@ +package message + +import "github.com/Nyarum/barrel" + +//Message is the basic structure to send and receive Message all the methods can be overwritted if needed +type Message struct { +} + +func (p *Message) Default() { + //default value +} + +func (p Message) Check(stats *barrel.Stats) bool { + + return true +} + +func (p *Message) Unpack(data []byte) error { + barrel := barrel.NewBarrel() + load := barrel.Load(p, data, false) + + err := barrel.Unpack(load) + + return err +} + +func (p *Message) Pack() ([]byte, error) { + barrel := barrel.NewBarrel() + load := barrel.Load(p, []byte{}, true) + + err := barrel.Pack(load) + + return barrel.Bytes(), err +} diff --git a/message/payload.go b/message/payload.go @@ -0,0 +1,26 @@ +package message + +import "github.com/Nyarum/barrel" + +type Payload struct { + Number uint8 +} + +func (p *Payload) Default() { + +} +func (p Payload) Check(stats *barrel.Stats) bool { + return true +} + +func (p *Payload) Unpack(data []byte) error { + barrel := barrel.NewBarrel() + load := barrel.Load(p, data, false) + + err := barrel.Unpack(load) + if err != nil { + return err + } + + return nil +} diff --git a/network.go b/network.go @@ -5,19 +5,25 @@ import ( "errors" "fmt" "net" + + "github.com/Morphux/mps/message" ) -func ParseRequest(message []byte, conn net.Conn, db *sql.DB) error { +func Version() []byte { + return []byte{0, 1} +} + +func ParseRequest(data []byte, conn net.Conn, db *sql.DB) error { var cursor int - var header = new(Header) + var header = new(message.Header) - if len(message) < 5 { + if len(data) < 5 { return errors.New("Header too short") } - headerSize := uint8(message[3]) + headerSize := uint8(data[3]) - c, err := header.Unpack(message[0 : 3+headerSize+2]) + c, err := header.Unpack(data[0 : 3+headerSize+2]) cursor += c if header == nil || err != nil { @@ -32,7 +38,7 @@ func ParseRequest(message []byte, conn net.Conn, db *sql.DB) error { case 0x01: conn.Write(Version()) case 0x10: - c, _, _ := RequestPackage(message[cursor+1:], db) + c, _, _ := RequestPackage(data[cursor+1:], db) cursor += c default: fmt.Println(header.Type) diff --git a/payload.go b/payload.go @@ -1,26 +0,0 @@ -package main - -import "github.com/Nyarum/barrel" - -type Payload struct { - Number uint8 -} - -func (p *Payload) Default() { - -} -func (p Payload) Check(stats *barrel.Stats) bool { - return true -} - -func (p *Payload) Unpack(data []byte) error { - barrel := barrel.NewBarrel() - load := barrel.Load(p, data, false) - - err := barrel.Unpack(load) - if err != nil { - return err - } - - return nil -} diff --git a/req_get_cat.go b/req_get_cat.go @@ -1,8 +0,0 @@ -package main - -type ReqGetCat struct { - Message - - CategoriesLen uint16 - Categories []uint64 -} diff --git a/req_get_file.go b/req_get_file.go @@ -1,9 +0,0 @@ -package main - -type ReqGetFile struct { - Message - - ID uint64 - PathLen uint16 - Path string -} diff --git a/req_get_news.go b/req_get_news.go @@ -1,9 +0,0 @@ -package main - -type ReqGetNews struct { - Message - - LastRequest uint32 - PkgsIDsSize uint16 - PkgsIDs []uint64 -} diff --git a/req_get_pkg.go b/req_get_pkg.go @@ -1,68 +0,0 @@ -package main - -import ( - "database/sql" - "fmt" - - "github.com/Nyarum/barrel" -) - -type ReqGetPKG struct { - Message - - ID uint64 - State uint8 - NameLen uint16 - CategLen uint16 - VersionLen uint16 - Name string - Category string - Version string -} - -func RequestPackage(data []byte, db *sql.DB) (int, Package, error) { - var err error - pkg := Package{} - - req := new(ReqGetPKG) - - req.Unpack(data) - - fmt.Println(req) - - if req.ID != 0 && req.NameLen == 0 && req.CategLen == 0 { - fmt.Println("search By id") - pkg, err = QueryPkgID(req.ID, req.State, db) - } else if req.ID == 0 && req.NameLen != 0 && req.CategLen != 0 { - fmt.Println("search By category") - pkg, err = QueryPkgNameAndCat(req.Name, req.Category, req.State, db) - } else { - fmt.Println("wtfmate") - } - - fmt.Println(pkg) - return 0, pkg, err -} - -func (h *ReqGetPKG) Unpack(data []byte) (int, error) { - - var l uint16 = 15 - - fmt.Println(data) - - barrel := barrel.NewBarrel() - load := barrel.Load(h, data, false) - - err := barrel.Unpack(load) - if err != nil { - return 0, err - } - - fmt.Println(h.ID, h.NameLen, h.CategLen, h.VersionLen) - - h.Name = string(data[l : l+h.NameLen]) - h.Category = string(data[l+h.NameLen : l+h.NameLen+h.CategLen]) - h.Version = string(data[l+h.NameLen+h.CategLen : l+h.NameLen+h.CategLen+h.VersionLen]) - - return int(l + h.NameLen + h.CategLen + h.VersionLen), nil -} diff --git a/req_get_upd.go b/req_get_upd.go @@ -1,8 +0,0 @@ -package main - -type ReqGetUPD struct { - Message - - PkgsLen uint64 - Pkgs []uint64 -} diff --git a/request/auth.go b/request/auth.go @@ -0,0 +1,10 @@ +package request + +import "github.com/Morphux/mps/message" + +type Auth struct { + message.Message + + mpm_major_version uint8 + mpm_minor_version uint8 +} diff --git a/request/req_get_cat.go b/request/req_get_cat.go @@ -0,0 +1,10 @@ +package request + +import "github.com/Morphux/mps/message" + +type ReqGetCat struct { + message.Message + + CategoriesLen uint16 + Categories []uint64 +} diff --git a/request/req_get_file.go b/request/req_get_file.go @@ -0,0 +1,11 @@ +package request + +import "github.com/Morphux/mps/message" + +type ReqGetFile struct { + message.Message + + ID uint64 + PathLen uint16 + Path string +} diff --git a/request/req_get_news.go b/request/req_get_news.go @@ -0,0 +1,11 @@ +package request + +import "github.com/Morphux/mps/message" + +type ReqGetNews struct { + message.Message + + LastRequest uint32 + PkgsIDsSize uint16 + PkgsIDs []uint64 +} diff --git a/request/req_get_pkg.go b/request/req_get_pkg.go @@ -0,0 +1,49 @@ +package request + +import ( + "errors" + "fmt" + + "github.com/Morphux/mps/message" + "github.com/Nyarum/barrel" +) + +type ReqGetPKG struct { + message.Message + + ID uint64 + State uint8 + NameLen uint16 + CategLen uint16 + VersionLen uint16 + Name string + Category string + Version string +} + +func (h *ReqGetPKG) Unpack(data []byte) (int, error) { + + var l uint16 = 15 + + fmt.Println(data) + + barrel := barrel.NewBarrel() + load := barrel.Load(h, data, false) + + err := barrel.Unpack(load) + if err != nil { + return 0, err + } + + fmt.Println(h.ID, h.NameLen, h.CategLen, h.VersionLen) + + if len(data) < int(l+h.NameLen+h.CategLen+h.VersionLen) { + return 0, errors.New("A packet send by the client is wrong") + } + + h.Name = string(data[l : l+h.NameLen]) + h.Category = string(data[l+h.NameLen : l+h.NameLen+h.CategLen]) + h.Version = string(data[l+h.NameLen+h.CategLen : l+h.NameLen+h.CategLen+h.VersionLen]) + + return int(l + h.NameLen + h.CategLen + h.VersionLen), nil +} diff --git a/request/req_get_upd.go b/request/req_get_upd.go @@ -0,0 +1,10 @@ +package request + +import "github.com/Morphux/mps/message" + +type ReqGetUPD struct { + message.Message + + PkgsLen uint64 + Pkgs []uint64 +} diff --git a/resp_cat.go b/resp_cat.go @@ -1,10 +0,0 @@ -package main - -type RespCat struct { - Message - - ID uint64 - ParentID uint64 - NameLen uint64 - Name string -} diff --git a/resp_file.go b/resp_file.go @@ -1,11 +0,0 @@ -package main - -type RespFile struct { - Message - - ID uint64 - Type uint8 - ParentID uint64 - PathLen uint16 - Path string -} diff --git a/resp_news.go b/resp_news.go @@ -1,14 +0,0 @@ -package main - -type RespNews struct { - Message - - ID uint64 - ParentID uint64 - AuthorLen uint16 - AuthorMailLen uint16 - TextLen uint16 - Author string - AuthorMail string - Text string -} diff --git a/resp_pkg.go b/resp_pkg.go @@ -1,22 +0,0 @@ -package main - -type RespPkg struct { - Message - - ID uint64 - CompTime float64 - InstSize float64 - ArchSize float64 - NameLen uint64 - CategoryLen uint16 - VersionLen uint16 - ArchiveLen uint16 - ChecksumLen uint16 - DependenciesSize uint16 - Name string - Category string - Version string - Archive string - Checksum string - Dependencies []uint64 -} diff --git a/response/authack.go b/response/authack.go @@ -0,0 +1,10 @@ +package response + +import "github.com/Morphux/mps/message" + +type AuthACK struct { + message.Message + + MPMMajorVersion uint8 + MPMMinorVersion uint8 +} diff --git a/response/resp_cat.go b/response/resp_cat.go @@ -0,0 +1,12 @@ +package response + +import "github.com/Morphux/mps/message" + +type RespCat struct { + message.Message + + ID uint64 + ParentID uint64 + NameLen uint64 + Name string +} diff --git a/response/resp_file.go b/response/resp_file.go @@ -0,0 +1,13 @@ +package response + +import "github.com/Morphux/mps/message" + +type RespFile struct { + message.Message + + ID uint64 + Type uint8 + ParentID uint64 + PathLen uint16 + Path string +} diff --git a/response/resp_news.go b/response/resp_news.go @@ -0,0 +1,16 @@ +package response + +import "github.com/Morphux/mps/message" + +type RespNews struct { + message.Message + + ID uint64 + ParentID uint64 + AuthorLen uint16 + AuthorMailLen uint16 + TextLen uint16 + Author string + AuthorMail string + Text string +} diff --git a/response/resp_pkg.go b/response/resp_pkg.go @@ -0,0 +1,24 @@ +package response + +import "github.com/Morphux/mps/message" + +type RespPkg struct { + message.Message + + ID uint64 + CompTime float64 + InstSize float64 + ArchSize float64 + NameLen uint64 + CategoryLen uint16 + VersionLen uint16 + ArchiveLen uint16 + ChecksumLen uint16 + DependenciesSize uint16 + Name string + Category string + Version string + Archive string + Checksum string + Dependencies []uint64 +} diff --git a/struct.go b/struct.go @@ -1,71 +0,0 @@ -package main - -type header struct { - T uint8 - Number uint8 -} - -type getPkg_ struct { - H header - - ID uint64 - State uint8 - NameLen uint16 - CategLen uint16 - VersionLen uint16 -} - -type respPkg struct { - H header - - ID uint64 - CompTime float64 - InstSize float64 - ArchSize float64 - NameLen uint64 - CategoryLen uint16 - VersionLen uint16 - ArchiveLen uint16 - ChecksumLen uint16 - DependenciesSize uint16 - Name []byte - Category []byte - Version []byte - Archive []byte - Checksum []byte - Dependencies []uint64 -} - -type respPkg_ struct { - H header - - ID uint64 - CompTime float64 - InstSize float64 - ArchSize float64 - NameLen uint64 - CategoryLen uint16 - VersionLen uint16 - ArchiveLen uint16 - ChecksumLen uint16 - DependenciesSize uint16 -} - -type resp_file_t struct { - ID uint64 - kind uint8 - parentID uint64 - pathLen uint16 - path []byte -} - -type resp_news_t struct { - ID uint64 - parentID uint64 - authorLen uint16 - authorMailLen uint16 - textLen uint16 - author []byte - authorMail []byte - text []byte -} diff --git a/test_database/fake.db b/test_database/fake.db Binary files differ. diff --git a/test_database/foo.db b/test_database/foo.db Binary files differ. diff --git a/test_database/mock.db b/test_database/mock.db Binary files differ.