Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support poe reset for gs316xx, fix issue #70 #71

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: "setup go"
uses: actions/setup-go@v3
with:
go-version: '1.20'
go-version: '1.23'
- name: Get the version
id: get_version
run: echo "VERSION=$(echo $GITHUB_REF | cut -d / -f 3)" >> $GITHUB_OUTPUT
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: "setup go"
uses: actions/setup-go@v3
with:
go-version: '1.20'
go-version: '1.23'
- name: "go mod download"
run: "go mod download"
- name: "go test ./..."
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/update-readme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: "setup go"
uses: actions/setup-go@v3
with:
go-version: '1.20'
go-version: '1.23'
- name: "build binary and print --help"
run: |
go build -o ntgrrc .
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@

# ntgrrc (Netgear Remote Control) CHANGELOG

## v0.11.0 (WiP)

* Add support poe reset (cycle power) for gs316xx (#70)
* Switch to Go v1.23

----

## v0.10.1

* added outputs to standard output by @oalfreda in #64
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module ntgrrc

go 1.20
go 1.23.1

require (
github.com/PuerkitoBio/goquery v1.9.2
github.com/PuerkitoBio/goquery v1.10.0
github.com/alecthomas/kong v1.2.1
github.com/corbym/gocrest v1.1.2
golang.org/x/term v0.24.0
)

require (
github.com/andybalholm/cascadia v1.3.2 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
)
11 changes: 7 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
github.com/PuerkitoBio/goquery v1.9.2 h1:4/wZksC3KgkQw7SQgkKotmKljk0M6V8TUvA8Wb4yPeE=
github.com/PuerkitoBio/goquery v1.9.2/go.mod h1:GHPCaP0ODyyxqcNoFGYlAprUFH81NuRPd0GX3Zu2Mvk=
github.com/PuerkitoBio/goquery v1.10.0 h1:6fiXdLuUvYs2OJSvNRqlNPoBm6YABE226xrbavY5Wv4=
github.com/PuerkitoBio/goquery v1.10.0/go.mod h1:TjZZl68Q3eGHNBA8CWaxAN7rOU1EbDz3CWuolcO5Yu4=
github.com/alecthomas/assert/v2 v2.10.0 h1:jjRCHsj6hBJhkmhznrCzoNpbA3zqy0fYiUcYZP/GkPY=
github.com/alecthomas/assert/v2 v2.10.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/alecthomas/kong v1.2.1 h1:E8jH4Tsgv6wCRX2nGrdPyHDUCSG83WH2qE4XLACD33Q=
github.com/alecthomas/kong v1.2.1/go.mod h1:rKTSFhbdp3Ryefn8x5MOEprnRFQ7nlmMC01GKhehhBM=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
github.com/corbym/gocrest v1.1.2 h1:HwMyOILE0E/BqC1vs/JjanEj+HXeYPGZzbppIgd1/Os=
github.com/corbym/gocrest v1.1.2/go.mod h1:vhNebfdBGx5l0Nh0OM/CvIVqGAnR9AAbI5qA9OxRUOU=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand All @@ -19,8 +22,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
53 changes: 52 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,61 @@ func postPage(args *GlobalOptions, host string, url string, requestBody string)
return doHttpRequestAndReadResponse(args, http.MethodPost, host, url, requestBody)
}

func doHttpRequestAndReadResponse_justCookie_FormData(args *GlobalOptions, httpMethod string, host string, requestUrl string, requestBody string) (string, error) {
model, token, err := readTokenAndModel2GlobalOptions(args, host)
if err != nil {
return "", err
}

if args.Verbose {
println(fmt.Sprintf("send HTTP %s request to: %s", httpMethod, requestUrl))
}

//if isModel316(model) {
// if strings.Contains(requestUrl, "?") {
// splits := strings.Split(requestUrl, "?")
// requestUrl = splits[0] + "?Gambit=" + token + "&" + splits[1]
// } else {
// requestUrl = requestUrl + "?Gambit=" + token
// }
//}

req, err := http.NewRequest(httpMethod, requestUrl, strings.NewReader(requestBody))
if err != nil {
return "", err
}

if isModel30x(model) {
req.Header.Set("Cookie", "SID="+token)
} else if isModel316(model) {
req.Header.Set("Cookie", "gambitcookie="+token)
} else {
panic("model not supported")
}

req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if args.Verbose {
println(resp.Status)
}
bytes, err := io.ReadAll(resp.Body)
return string(bytes), err
}

func doHttpRequestAndReadResponse(args *GlobalOptions, httpMethod string, host string, requestUrl string, requestBody string) (string, error) {
model, token, err := readTokenAndModel2GlobalOptions(args, host)
if err != nil {
return "", err
}

if args.Verbose {
println("Fetching data from: " + requestUrl)
println(fmt.Sprintf("send HTTP %s request to: %s", httpMethod, requestUrl))
}

if isModel316(model) {
Expand All @@ -41,6 +88,10 @@ func doHttpRequestAndReadResponse(args *GlobalOptions, httpMethod string, host s

if isModel30x(model) {
req.Header.Set("Cookie", "SID="+token)
} else if isModel316(model) {
req.Header.Set("Cookie", "gambitCookie="+token)
} else {
panic("model not supported")
}

client := &http.Client{}
Expand Down
84 changes: 77 additions & 7 deletions poe_cycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package main
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
)

type PoeCyclePowerCommand struct {
Expand All @@ -12,11 +14,24 @@ type PoeCyclePowerCommand struct {
}

func (poe *PoeCyclePowerCommand) Run(args *GlobalOptions) error {
err := ensureModelIs30x(args, poe.Address)
if err != nil {
return err
model := args.model
if len(model) == 0 {
var err error
model, err = detectNetgearModel(args, poe.Address)
if err != nil {
return err
}
}
if isModel30x(model) {
return poe.cyclePowerGs30xEPx(args)
}
if isModel316(model) {
return poe.cyclePowerGs316EPx(args)
}
panic("model not supported")
}

func (poe *PoeCyclePowerCommand) cyclePowerGs30xEPx(args *GlobalOptions) error {
poeExt := &PoeExt{}

settings, err := requestPoeConfiguration(args, poe.Address, poeExt)
Expand All @@ -30,19 +45,19 @@ func (poe *PoeCyclePowerCommand) Run(args *GlobalOptions) error {
}

for _, switchPort := range poe.Ports {
if switchPort > len(settings) || switchPort < 1 {
if switchPort < 1 || switchPort > len(settings) {
return errors.New(fmt.Sprintf("given port id %d, doesn't fit in range 1..%d", switchPort, len(settings)))
}
poeSettings.Add(fmt.Sprintf("port%d", switchPort-1), "checked")
}

result, err := requestPoeSettingsUpdate(args, poe.Address, poeSettings.Encode())
if result != "SUCCESS" {
return errors.New(result)
}
if err != nil {
return err
}
if result != "SUCCESS" {
return errors.New(result)
}

settings, err = requestPoeConfiguration(args, poe.Address, poeExt)
if err != nil {
Expand All @@ -53,3 +68,58 @@ func (poe *PoeCyclePowerCommand) Run(args *GlobalOptions) error {
prettyPrintSettings(args.OutputFormat, changedPorts)
return nil
}

func (poe *PoeCyclePowerCommand) cyclePowerGs316EPx(args *GlobalOptions) error {
for _, switchPort := range poe.Ports {
if switchPort < 1 || switchPort > 16 {
return errors.New(fmt.Sprintf("given port id %d, doesn't fit in range 1..16", switchPort))
}
}

_, token, err := readTokenAndModel2GlobalOptions(args, poe.Address)
if err != nil {
return err
}
urlStr := fmt.Sprintf("http://%s/iss/specific/poePortConf.html", poe.Address)
//reqForm := url.Values{}
//reqForm.Add("TYPE", "resetPoe")
//reqForm.Add("Gambit", token)
//reqForm.Add("PoePort", createPortResetPayloadGs316EPx(poe.Ports))
payload := fmt.Sprintf("Gambit=%s&TYPE=resetPoe&PoePort=%s", token, createPortResetPayloadGs316EPx(poe.Ports))
result, err := doHttpRequestAndReadResponse_justCookie_FormData(args, http.MethodPost, poe.Address, urlStr, payload)
//print(reqForm.Encode() + "\n")
if err != nil {
return err
}
if result != "SUCCESS" {
return errors.New(result)
}
print(fmt.Sprintf("result = '%v'", result))
// TODO print POE configuration
//settings, err := requestPoeConfiguration(args, poe.Address, poeExt)
//if err != nil {
// return err
//}
//changedPorts := collectChangedPoePortConfiguration(poe.Ports, settings)
//prettyPrintSettings(args.OutputFormat, changedPorts)
return nil
}

func createPortResetPayloadGs316EPx(poePorts []int) string {
result := strings.Builder{}
const maxPorts = 16
for i := 0; i < maxPorts; i++ {
written := false
for _, p := range poePorts {
if p-1 == i {
result.WriteString("1")
written = true
break
}
}
if !written {
result.WriteString("0")
}
}
return result.String()
}
15 changes: 15 additions & 0 deletions poe_cycle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"github.com/corbym/gocrest/is"
"github.com/corbym/gocrest/then"
"testing"
)

func TestPoePortReset(t *testing.T) {
s := createPortResetPayloadGs316EPx([]int{3, 5})
then.AssertThat(t, s, is.EqualTo("0010100000000000"))

s = createPortResetPayloadGs316EPx([]int{1})
then.AssertThat(t, s, is.EqualTo("1000000000000000"))
}
Loading