mps

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

commit 59c33fed609dce8394aaae81f00303c29632311f
parent 2243ad0af07aca45cf4816cffc6fe970f2194ab6
Author: MrGosti <gostitsog@gmail.com>
Date:   Tue, 14 Mar 2017 00:29:41 +0100

Super duper experimental test release req_get_pkgs

Diffstat:
Aauth.go | 12++++++++++++
Aauthack.go | 8++++++++
Mdatabase.go | 58+++++++++++++++++++++++++++++++++++++---------------------
Aerror.go | 26++++++++++++++++++++++++++
Mheader.go | 18++++++++----------
Amessage.go | 34++++++++++++++++++++++++++++++++++
Mmps.go | 28++++++++++++++++++++++------
Mnetwork.go | 25+++++++++++++++++++++----
Apayload.go | 26++++++++++++++++++++++++++
Areq_get_cat.go | 8++++++++
Areq_get_file.go | 9+++++++++
Areq_get_news.go | 9+++++++++
Mreq_get_pkg.go | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
Areq_get_upd.go | 8++++++++
Aresp_cat.go | 10++++++++++
Aresp_file.go | 11+++++++++++
Aresp_news.go | 14++++++++++++++
Aresp_pkg.go | 22++++++++++++++++++++++
18 files changed, 339 insertions(+), 49 deletions(-)

diff --git a/auth.go b/auth.go @@ -0,0 +1,12 @@ +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 @@ -0,0 +1,8 @@ +package main + +type AuthACK struct { + Message + + MPMMajorVersion uint8 + MPMMinorVersion uint8 +} diff --git a/database.go b/database.go @@ -2,7 +2,6 @@ package main import ( "database/sql" - "fmt" "log" _ "github.com/mattn/go-sqlite3" @@ -10,47 +9,64 @@ import ( // MPM package type Package struct { - id uint64 - name string - version string - category string - description string - archive string - sbu uint64 - dependencies string - archive_size uint64 - installed_size uint64 - archive_hash string + ID uint64 + Name string + Version string + Category string + Description string + Archive string + SBU uint64 + Dependencies string + ArchiveSize uint64 + InstalledSize uint64 + ArchiveHash string } -func queryPackageByName(name string, state int, db *sql.DB) { +func QueryPkgNameAndCat(name string, category string, state uint8, db *sql.DB) (Package, error) { pkg := Package{} - rows, err := db.Query("SELECT * FROM pkgs where name = ?", name) + + var err error + + var rows *sql.Rows + + if name != "" && category != "" { + rows, err = db.Query("SELECT * FROM pkgs where name = ? AND category = ?", name, category) + } else if name != "" { + rows, err = db.Query("SELECT * FROM pkgs where name = ?", name) + } else { + rows, err = db.Query("SELECT * FROM pkgs where category = ?", category) + } + if err != nil { log.Fatalln(err) } for rows.Next() { - err := rows.Scan(&pkg.id, &pkg.name, &pkg.version, &pkg.category, &pkg.description, - &pkg.archive, &pkg.sbu, &pkg.dependencies, &pkg.archive_size, &pkg.installed_size, &pkg.archive_hash) + err := rows.Scan(&pkg.ID, &pkg.Name, &pkg.Version, &pkg.Category, &pkg.Description, + &pkg.Archive, &pkg.SBU, &pkg.Dependencies, &pkg.ArchiveSize, &pkg.InstalledSize, &pkg.ArchiveHash) if err != nil { + return pkg, err log.Fatalln(err) } - fmt.Printf("%#v\n", pkg) + return pkg, nil } + + return pkg, nil } -func queryPackageByid(id uint64, state int, db *sql.DB) { +func QueryPkgID(id uint64, state uint8, db *sql.DB) (Package, error) { pkg := Package{} rows, err := db.Query("SELECT * FROM pkgs where id = ?", id) if err != nil { log.Fatalln(err) } for rows.Next() { - err := rows.Scan(&pkg.id, &pkg.name, &pkg.version, &pkg.category, &pkg.description, - &pkg.archive, &pkg.sbu, &pkg.dependencies, &pkg.archive_size, &pkg.installed_size, &pkg.archive_hash) + err := rows.Scan(&pkg.ID, &pkg.Name, &pkg.Version, &pkg.Category, &pkg.Description, + &pkg.Archive, &pkg.SBU, &pkg.Dependencies, &pkg.ArchiveSize, &pkg.InstalledSize, &pkg.ArchiveHash) if err != nil { + return pkg, err log.Fatalln(err) } - fmt.Printf("%#v\n", pkg) + return pkg, nil } + return pkg, nil } diff --git a/error.go b/error.go @@ -0,0 +1,26 @@ +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 @@ -3,27 +3,25 @@ package main import "github.com/Nyarum/barrel" type Header struct { + Message + Type uint8 Size uint16 NextPkgLen uint8 - NextPkgs string + NextPkg string } -func (p *Header) Default() { +func (h *Header) Unpack(data []byte) (int, error) { -} -func (p Header) Check(stats *barrel.Stats) bool { - return true -} - -func (h *Header) Unpack(data []byte) error { barrel := barrel.NewBarrel() load := barrel.Load(h, data, false) err := barrel.Unpack(load) if err != nil { - return err + return 0, err } - return nil + h.NextPkg = string(data[4 : 4+h.NextPkgLen]) + + return 3 + int(h.NextPkgLen), nil } diff --git a/message.go b/message.go @@ -0,0 +1,34 @@ +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/mps.go b/mps.go @@ -4,7 +4,6 @@ import ( "database/sql" "flag" "fmt" - "io/ioutil" "log" "net" "os" @@ -12,6 +11,9 @@ import ( _ "github.com/mattn/go-sqlite3" ) +const MPM_MAJOR_VERSION uint8 = 0 +const MPM_MINOR_VERSION uint8 = 1 + func main() { databasePtr := flag.String("db", "", "a sqlite database") @@ -50,11 +52,25 @@ func main() { } func handleRequest(conn net.Conn, db *sql.DB) { + buf := make([]byte, 4096) - message, err := ioutil.ReadAll(conn) - if err != nil { - log.Print("Error reading:", err.Error()) + //var auth bool = false + + for { + n, err := conn.Read(buf) + fmt.Println("receive :", buf[0:n]) + + ParseRequest(buf[0:n], conn, db) + + if err != nil || n == 0 { + conn.Close() + break + } + + if err != nil { + conn.Close() + break + } } - ParseRequest(message, conn) - conn.Close() + } diff --git a/network.go b/network.go @@ -1,12 +1,15 @@ package main import ( + "database/sql" "errors" + "fmt" "net" ) -func ParseRequest(message []byte, conn net.Conn) error { - var header *Header +func ParseRequest(message []byte, conn net.Conn, db *sql.DB) error { + var cursor int + var header = new(Header) if len(message) < 5 { return errors.New("Header too short") @@ -14,11 +17,25 @@ func ParseRequest(message []byte, conn net.Conn) error { headerSize := uint8(message[3]) - header.Unpack(message[0 : 3+headerSize]) + c, err := header.Unpack(message[0 : 3+headerSize+2]) + cursor += c + + if header == nil || err != nil { + return errors.New("Header too short") + } + + //dismiss payload for now + + cursor = cursor + 1 switch header.Type { + case 0x01: + conn.Write(Version()) case 0x10: - //handle request + c, _, _ := RequestPackage(message[cursor+1:], db) + cursor += c + default: + fmt.Println(header.Type) } return nil diff --git a/payload.go b/payload.go @@ -0,0 +1,26 @@ +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 @@ -0,0 +1,8 @@ +package main + +type ReqGetCat struct { + Message + + CategoriesLen uint16 + Categories []uint64 +} diff --git a/req_get_file.go b/req_get_file.go @@ -0,0 +1,9 @@ +package main + +type ReqGetFile struct { + Message + + ID uint64 + PathLen uint16 + Path string +} diff --git a/req_get_news.go b/req_get_news.go @@ -0,0 +1,9 @@ +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,22 +1,68 @@ package main -import "github.com/Nyarum/barrel" +import ( + "database/sql" + "fmt" + + "github.com/Nyarum/barrel" +) + +type ReqGetPKG struct { + Message -type GetPkg struct { ID uint64 State uint8 NameLen uint16 CategLen uint16 VersionLen uint16 - Name []byte - Category []byte - Version []byte + Name string + Category string + Version string } -func (p *GetPkg) Default() { +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 (p GetPkg) Check(stats *barrel.Stats) bool { - return true +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 @@ -0,0 +1,8 @@ +package main + +type ReqGetUPD struct { + Message + + PkgsLen uint64 + Pkgs []uint64 +} diff --git a/resp_cat.go b/resp_cat.go @@ -0,0 +1,10 @@ +package main + +type RespCat struct { + Message + + ID uint64 + ParentID uint64 + NameLen uint64 + Name string +} diff --git a/resp_file.go b/resp_file.go @@ -0,0 +1,11 @@ +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 @@ -0,0 +1,14 @@ +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 @@ -0,0 +1,22 @@ +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 +}