Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ on:
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:

build:
Expand Down
133 changes: 133 additions & 0 deletions blackhole.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package fastnetmon

import (
"fmt"

"github.com/levigross/grequests"
)

// Blocks some specified blackhole host
func (client *FastNetMonClient) Blackhole(ip_address string) (bool, error) {
resp, err := grequests.Put(client.Prefix+"/blackhole/"+ip_address, grequests.FromRequestOptions(client.Ro))

if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}

if !resp.Ok {
return false, apiStatusError(resp)
}

response := ErrorJson{}
err = decodeResponseJSON(resp, &response)
if err != nil {
return false, err
}

return response.Success, nil
}

// Returns all IPs blocked using blackhole
func (client *FastNetMonClient) GetBlackhole() ([]BlackholeAnnounces, error) {
resp, err := grequests.Get(client.Prefix+"/blackhole", grequests.FromRequestOptions(client.Ro))

if err != nil {
return nil, fmt.Errorf("Cannot connect to API: %w", err)
}

if !resp.Ok {
return nil, apiStatusError(resp)
}

ban_list_response := ResponseRemoteBlackholeListJson{}
err = decodeResponseJSON(resp, &ban_list_response)
if err != nil {
return nil, err
}

return ban_list_response.Values, nil
}

// Removes blackhole entry using UUID
func (client *FastNetMonClient) RemoveBlackhole(mitigation_uuid string) (bool, error) {
resp, err := grequests.Delete(client.Prefix+"/blackhole/"+mitigation_uuid, grequests.FromRequestOptions(client.Ro))

if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}

if !resp.Ok {
return false, apiStatusError(resp)
}

response := ErrorJson{}
err = decodeResponseJSON(resp, &response)
if err != nil {
return false, err
}

return response.Success, nil
}

// Blocks some specified remote blackhole host
func (client *FastNetMonClient) BlackholeRemote(ip_address string) (bool, error) {
resp, err := grequests.Put(client.Prefix+"/remote_blackhole/"+ip_address, grequests.FromRequestOptions(client.Ro))

if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}

if !resp.Ok {
return false, apiStatusError(resp)
}

response := ErrorJson{}
err = decodeResponseJSON(resp, &response)
if err != nil {
return false, err
}

return response.Success, nil
}

// Returns all IPs blocked using remote blackhole
func (client *FastNetMonClient) GetRemoteBlackhole() ([]BlackholeAnnounces, error) {
resp, err := grequests.Get(client.Prefix+"/remote_blackhole", grequests.FromRequestOptions(client.Ro))

if err != nil {
return nil, fmt.Errorf("Cannot connect to API: %w", err)
}

if !resp.Ok {
return nil, apiStatusError(resp)
}

ban_list_response := ResponseRemoteBlackholeListJson{}
err = decodeResponseJSON(resp, &ban_list_response)
if err != nil {
return nil, err
}

return ban_list_response.Values, nil
}

// Removes remote blackhole entry using UUID
func (client *FastNetMonClient) RemoveRemoteBlackhole(mitigation_uuid string) (bool, error) {
resp, err := grequests.Delete(client.Prefix+"/remote_blackhole/"+mitigation_uuid, grequests.FromRequestOptions(client.Ro))

if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}

if !resp.Ok {
return false, apiStatusError(resp)
}

response := ErrorJson{}
err = decodeResponseJSON(resp, &response)
if err != nil {
return false, err
}

return response.Success, nil
}
74 changes: 74 additions & 0 deletions flowsspec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package fastnetmon

import (
"fmt"

"github.com/levigross/grequests"
)

// Adds Flow Spec announce
func (client *FastNetMonClient) AddFlowSpecRule(flow_spec_rule FlowSpecRule) (bool, error) {
request_options := client.Ro

request_options.JSON = flow_spec_rule

resp, err := grequests.Put(client.Prefix+"/flowspec", grequests.FromRequestOptions(client.Ro))

if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}

if !resp.Ok {
return false, apiStatusError(resp)
}

response := ErrorJson{}
err = decodeResponseJSON(resp, &response)
if err != nil {
return false, err
}

return response.Success, nil
}

// Returns all active flow spec announces
func (client *FastNetMonClient) GetFlowSpecRules() ([]ResponseFlowSpecAnnounce, error) {
resp, err := grequests.Get(client.Prefix+"/flowspec", grequests.FromRequestOptions(client.Ro))

if err != nil {
return nil, fmt.Errorf("Cannot connect to API: %w", err)
}

if !resp.Ok {
return nil, apiStatusError(resp)
}

flow_spec_response := ResponseFlowSpecJson{}
err = decodeResponseJSON(resp, &flow_spec_response)
if err != nil {
return nil, err
}

return flow_spec_response.Values, nil
}

// Removes Flow Spec entry using UUID
func (client *FastNetMonClient) RemoveFlowSpecRule(mitigation_uuid string) (bool, error) {
resp, err := grequests.Delete(client.Prefix+"/flowspec/"+mitigation_uuid, grequests.FromRequestOptions(client.Ro))

if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}

if !resp.Ok {
return false, apiStatusError(resp)
}

response := ErrorJson{}
err = decodeResponseJSON(resp, &response)
if err != nil {
return false, err
}

return response.Success, nil
}
Loading
Loading