Skip to content

Commit

Permalink
client side
Browse files Browse the repository at this point in the history
  • Loading branch information
ZORbit01 committed Apr 9, 2023
1 parent 8cd3874 commit 70964ed
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
21 changes: 21 additions & 0 deletions client/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package client

type LeaderAPI struct {
Name string `json:"name"`
}

type SoldierAPI struct {
Name string `json:"name"`
}

type CampSettings struct {
Status string `json:"status"`
VictimServer string `json:"victim"`
DDOSType string `json:"ddos_type"`
}

type CampAPI struct {
Leader LeaderAPI `json:"leader"`
Soldiers []SoldierAPI `json:"soldiers"`
Settings CampSettings `json:"camp_settings"`
}
115 changes: 115 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -1 +1,116 @@
package client

import (
"bytes"
"encoding/json"
"errors"
"net/http"
)

type Leader struct {
Client
Password string
}

type Client struct {
http.Client `json:"-"`
Name string `name:"name"`
DispatcherServer string `json:"-"`
}

func (c *Client) GetCamp() (CampAPI, error) {
//get request to dispatcher server /camp

rq, err := http.NewRequest("GET", c.DispatcherServer+"/camp", nil)
if err != nil {
return CampAPI{}, err
}
do, err := c.Do(rq)
defer do.Body.Close()

if err != nil {
var msg string
_ = json.NewDecoder(do.Body).Decode(&msg)
return CampAPI{}, errors.New(msg)
}
var camp CampAPI
err = json.NewDecoder(do.Body).Decode(&camp)
if err != nil {
return CampAPI{}, err
}
return camp, nil
}

func (c *Client) JoinCamp() error {
//post request to dispatcher server /camp/
//with body {name: c.Name}
rq, err := http.NewRequest("POST", c.DispatcherServer+"/camp/", nil)
if err != nil {
return err
}
do, err := c.Do(rq)
defer do.Body.Close()
if err != nil {
return err
}
if do.StatusCode == http.StatusOK {
return nil
} else {
//return the body as error message
var msg string
_ = json.NewDecoder(do.Body).Decode(&msg)
return errors.New(msg)
}
}

func (c *Leader) RemoveFromCamp(password string) error {
//delete request to dispatcher server /camp/
//with body {name: c.Name}
rq, err := http.NewRequest("DELETE", c.DispatcherServer+"/camp/", nil)
rq.Header.Add("Authorization", password)

if err != nil {
return err
}
do, err := c.Do(rq)
defer do.Body.Close()
if err != nil {
return err
}
if do.StatusCode == http.StatusOK {
return nil
} else {
//return the body as error message
var msg string
_ = json.NewDecoder(do.Body).Decode(&msg)
return errors.New(msg)
}
}

func (c *Leader) UpdateCampSettings(settings CampSettings, password string) error {
//put request to dispatcher server /camp/
//convert settings to json and put it in the body
jr, err := json.Marshal(settings)
if err != nil {
return err
}

rq, err := http.NewRequest("PUT", c.DispatcherServer+"/camp/", bytes.NewReader(jr))
if err != nil {
return err
}
rq.Header.Add("Authorization", password)

do, err := c.Do(rq)
defer do.Body.Close()
if err != nil {
return err
}
if do.StatusCode == http.StatusOK {
return nil
} else {
var msg string
_ = json.NewDecoder(do.Body).Decode(&msg)
return errors.New(msg)
}
}

0 comments on commit 70964ed

Please sign in to comment.