-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |