Remove go code
This commit is contained in:
parent
13f95e4ce5
commit
bf011f41e1
6 changed files with 0 additions and 331 deletions
8
Makefile
8
Makefile
|
@ -1,8 +0,0 @@
|
|||
devls:
|
||||
go build ./cmd/devls
|
||||
|
||||
run:
|
||||
go run ./cmd/devls
|
||||
|
||||
install:
|
||||
go install ./cmd/devls
|
|
@ -1,40 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"git.wbrawner.com/wbrawner/devls/internal/netgear"
|
||||
"git.wbrawner.com/wbrawner/devls/internal/netrc"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
hostPtr := flag.String(
|
||||
"host",
|
||||
"192.168.1.1",
|
||||
"the host or ip address for your router",
|
||||
)
|
||||
flag.Parse()
|
||||
credentials, err := netrc.ReadCredentials(*hostPtr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
jar, err := cookiejar.New(nil)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to create cookie jar")
|
||||
}
|
||||
client := &http.Client{Jar: jar}
|
||||
netgear.Login(client, credentials)
|
||||
// TODO: List the devices in a TUI to change sort by IP or name
|
||||
devices := netgear.ListDevices(client)
|
||||
sort.SliceStable(devices.ConnDevices, func(i, j int) bool {
|
||||
return strings.ToLower(devices.ConnDevices[i].Name) < strings.ToLower(devices.ConnDevices[j].Name)
|
||||
})
|
||||
for _, device := range devices.ConnDevices {
|
||||
fmt.Printf("%32s%15s\n", device.Name, device.IP)
|
||||
}
|
||||
}
|
149
devls.go
149
devls.go
|
@ -1,149 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"os"
|
||||
"path"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type DeviceList struct {
|
||||
NumberOfDevices int `json:"numberOfDevices"`
|
||||
Mode string `json:"mode"`
|
||||
ConnDevices []struct {
|
||||
CheckDev int `json:"checkDev"`
|
||||
Mac string `json:"mac"`
|
||||
Scene string `json:"scene"`
|
||||
Mark string `json:"mark"`
|
||||
Connection string `json:"connection"`
|
||||
Priority int `json:"priority"`
|
||||
PriorityStr string `json:"priorityStr"`
|
||||
CurBarDownStr string `json:"curBarDownStr"`
|
||||
CurBarUpStr string `json:"curBarUpStr"`
|
||||
DownloadSpeedStr string `json:"downloadSpeedStr"`
|
||||
UploadSpeedStr string `json:"uploadSpeedStr"`
|
||||
Type string `json:"type"`
|
||||
Model string `json:"model"`
|
||||
Name string `json:"name"`
|
||||
IP string `json:"ip"`
|
||||
} `json:"connDevices"`
|
||||
}
|
||||
|
||||
type NetrcCredential struct {
|
||||
Machine string
|
||||
Login string
|
||||
Password string
|
||||
}
|
||||
|
||||
func (credential *NetrcCredential) httpHost() string {
|
||||
return fmt.Sprintf("http://%s/", credential.Machine)
|
||||
}
|
||||
|
||||
func (credential *NetrcCredential) base64() string {
|
||||
formattedCredentials := fmt.Sprintf("%s:%s", credential.Login, credential.Password)
|
||||
credentialBytes := []byte(formattedCredentials)
|
||||
return base64.StdEncoding.EncodeToString(credentialBytes)
|
||||
}
|
||||
|
||||
func readCredentials(host string) (NetrcCredential, error) {
|
||||
credentials := NetrcCredential{Machine: host}
|
||||
netrcPath := path.Join(os.Getenv("HOME"), ".netrc")
|
||||
netrcFile, err := os.Open(netrcPath)
|
||||
if err != nil {
|
||||
return credentials, fmt.Errorf("failed to read ~/.netrc: %v", err)
|
||||
}
|
||||
defer netrcFile.Close()
|
||||
scanner := bufio.NewScanner(netrcFile)
|
||||
extractCredentials := false
|
||||
for {
|
||||
if !scanner.Scan() {
|
||||
break
|
||||
}
|
||||
line := scanner.Text()
|
||||
if strings.HasPrefix(line, "machine ") {
|
||||
extractCredentials = strings.HasSuffix(line, credentials.Machine)
|
||||
} else if strings.HasPrefix(line, "login ") && extractCredentials {
|
||||
credentials.Login = strings.TrimPrefix(line, "login ")
|
||||
} else if strings.HasPrefix(line, "password ") && extractCredentials {
|
||||
credentials.Password = strings.TrimPrefix(line, "password ")
|
||||
}
|
||||
}
|
||||
return credentials, scanner.Err()
|
||||
}
|
||||
|
||||
func login(client *http.Client, credentials NetrcCredential) {
|
||||
req, err := http.NewRequest("GET", credentials.httpHost(), nil)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to create request")
|
||||
}
|
||||
req.Header.Set(
|
||||
"Authorization",
|
||||
fmt.Sprintf("Basic %s", credentials.base64()),
|
||||
)
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to send first login request")
|
||||
}
|
||||
if res.StatusCode == 200 {
|
||||
return
|
||||
}
|
||||
res, err = client.Do(req)
|
||||
if err != nil || res.StatusCode != 200 {
|
||||
log.Fatalln("Failed to send second login request")
|
||||
}
|
||||
}
|
||||
|
||||
func listDevices(client *http.Client) DeviceList {
|
||||
req, err := http.NewRequest("GET", "http://192.168.1.1/ajax/devices_table_result", nil)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to create request")
|
||||
}
|
||||
req.Header.Set("Authorization", "Basic YWRtaW46UGVtVi0yMDE1")
|
||||
res, err := client.Do(req)
|
||||
if err != nil || res.StatusCode != 200 {
|
||||
log.Fatalln("Expected 401 for first login attempt")
|
||||
}
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(res.Body)
|
||||
var devices DeviceList
|
||||
if err = json.Unmarshal(body, &devices); err != nil {
|
||||
log.Fatalln("Failed to parse JSON response from router")
|
||||
}
|
||||
return devices
|
||||
}
|
||||
|
||||
func main() {
|
||||
hostPtr := flag.String(
|
||||
"host",
|
||||
"192.168.1.1",
|
||||
"the host or ip address for your router",
|
||||
)
|
||||
flag.Parse()
|
||||
credentials, err := readCredentials(*hostPtr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
jar, err := cookiejar.New(nil)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to create cookie jar")
|
||||
}
|
||||
client := &http.Client{Jar: jar}
|
||||
login(client, credentials)
|
||||
// TODO: List the devices in a TUI to change sort by IP or name
|
||||
devices := listDevices(client)
|
||||
sort.SliceStable(devices.ConnDevices, func(i, j int) bool {
|
||||
return strings.ToLower(devices.ConnDevices[i].Name) < strings.ToLower(devices.ConnDevices[j].Name)
|
||||
})
|
||||
for _, device := range devices.ConnDevices {
|
||||
fmt.Printf("%32s%15s\n", device.Name, device.IP)
|
||||
}
|
||||
}
|
3
go.mod
3
go.mod
|
@ -1,3 +0,0 @@
|
|||
module git.wbrawner.com/wbrawner/devls
|
||||
|
||||
go 1.23.0
|
|
@ -1,73 +0,0 @@
|
|||
package netgear
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.wbrawner.com/wbrawner/devls/internal/netrc"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type DeviceList struct {
|
||||
NumberOfDevices int `json:"numberOfDevices"`
|
||||
Mode string `json:"mode"`
|
||||
ConnDevices []struct {
|
||||
CheckDev int `json:"checkDev"`
|
||||
Mac string `json:"mac"`
|
||||
Scene string `json:"scene"`
|
||||
Mark string `json:"mark"`
|
||||
Connection string `json:"connection"`
|
||||
Priority int `json:"priority"`
|
||||
PriorityStr string `json:"priorityStr"`
|
||||
CurBarDownStr string `json:"curBarDownStr"`
|
||||
CurBarUpStr string `json:"curBarUpStr"`
|
||||
DownloadSpeedStr string `json:"downloadSpeedStr"`
|
||||
UploadSpeedStr string `json:"uploadSpeedStr"`
|
||||
Type string `json:"type"`
|
||||
Model string `json:"model"`
|
||||
Name string `json:"name"`
|
||||
IP string `json:"ip"`
|
||||
} `json:"connDevices"`
|
||||
}
|
||||
|
||||
func Login(client *http.Client, credentials netrc.NetrcCredential) {
|
||||
req, err := http.NewRequest("GET", credentials.HttpHost(), nil)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to create request")
|
||||
}
|
||||
req.Header.Set(
|
||||
"Authorization",
|
||||
fmt.Sprintf("Basic %s", credentials.Base64()),
|
||||
)
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to send first login request")
|
||||
}
|
||||
if res.StatusCode == 200 {
|
||||
return
|
||||
}
|
||||
res, err = client.Do(req)
|
||||
if err != nil || res.StatusCode != 200 {
|
||||
log.Fatalln("Failed to send second login request")
|
||||
}
|
||||
}
|
||||
|
||||
func ListDevices(client *http.Client) DeviceList {
|
||||
req, err := http.NewRequest("GET", "http://192.168.1.1/ajax/devices_table_result", nil)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to create request")
|
||||
}
|
||||
req.Header.Set("Authorization", "Basic YWRtaW46UGVtVi0yMDE1")
|
||||
res, err := client.Do(req)
|
||||
if err != nil || res.StatusCode != 200 {
|
||||
log.Fatalln("Expected 401 for first login attempt")
|
||||
}
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(res.Body)
|
||||
var devices DeviceList
|
||||
if err = json.Unmarshal(body, &devices); err != nil {
|
||||
log.Fatalln("Failed to parse JSON response from router")
|
||||
}
|
||||
return devices
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
package netrc
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type NetrcCredential struct {
|
||||
Machine string
|
||||
Login string
|
||||
Password string
|
||||
}
|
||||
|
||||
func (credential *NetrcCredential) HttpHost() string {
|
||||
return fmt.Sprintf("http://%s/", credential.Machine)
|
||||
}
|
||||
|
||||
func (credential *NetrcCredential) Base64() string {
|
||||
formattedCredentials := fmt.Sprintf("%s:%s", credential.Login, credential.Password)
|
||||
credentialBytes := []byte(formattedCredentials)
|
||||
return base64.StdEncoding.EncodeToString(credentialBytes)
|
||||
}
|
||||
|
||||
func ReadCredentials(host string) (NetrcCredential, error) {
|
||||
credentials := NetrcCredential{Machine: host}
|
||||
netrcPath := path.Join(os.Getenv("HOME"), ".netrc")
|
||||
netrcFile, err := os.Open(netrcPath)
|
||||
if err != nil {
|
||||
return credentials, fmt.Errorf("failed to read ~/.netrc: %v", err)
|
||||
}
|
||||
defer netrcFile.Close()
|
||||
scanner := bufio.NewScanner(netrcFile)
|
||||
extractCredentials := false
|
||||
for {
|
||||
if !scanner.Scan() {
|
||||
break
|
||||
}
|
||||
line := scanner.Text()
|
||||
if strings.HasPrefix(line, "machine ") {
|
||||
extractCredentials = strings.HasSuffix(line, credentials.Machine)
|
||||
} else if strings.HasPrefix(line, "login ") && extractCredentials {
|
||||
credentials.Login = strings.TrimPrefix(line, "login ")
|
||||
} else if strings.HasPrefix(line, "password ") && extractCredentials {
|
||||
credentials.Password = strings.TrimPrefix(line, "password ")
|
||||
}
|
||||
}
|
||||
if credentials.Login == "" {
|
||||
return credentials, fmt.Errorf("error: no login found for host \"%s\" in ~/.netrc", credentials.Machine)
|
||||
}
|
||||
if credentials.Password == "" {
|
||||
return credentials, fmt.Errorf("error: no password found for host \"%s\" in ~/.netrc", credentials.Machine)
|
||||
}
|
||||
return credentials, scanner.Err()
|
||||
}
|
Loading…
Reference in a new issue