From bf011f41e1fb90fd291f8b1d0653bddfc2f2bc35 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Sun, 17 Nov 2024 20:58:44 -0700 Subject: [PATCH] Remove go code --- Makefile | 8 -- cmd/devls/main.go | 40 ---------- devls.go | 149 ------------------------------------ go.mod | 3 - internal/netgear/netgear.go | 73 ------------------ internal/netrc/netrc.go | 58 -------------- 6 files changed, 331 deletions(-) delete mode 100644 Makefile delete mode 100644 cmd/devls/main.go delete mode 100644 devls.go delete mode 100644 go.mod delete mode 100644 internal/netgear/netgear.go delete mode 100644 internal/netrc/netrc.go diff --git a/Makefile b/Makefile deleted file mode 100644 index fada655..0000000 --- a/Makefile +++ /dev/null @@ -1,8 +0,0 @@ -devls: - go build ./cmd/devls - -run: - go run ./cmd/devls - -install: - go install ./cmd/devls diff --git a/cmd/devls/main.go b/cmd/devls/main.go deleted file mode 100644 index d377384..0000000 --- a/cmd/devls/main.go +++ /dev/null @@ -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) - } -} diff --git a/devls.go b/devls.go deleted file mode 100644 index 700cc25..0000000 --- a/devls.go +++ /dev/null @@ -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) - } -} diff --git a/go.mod b/go.mod deleted file mode 100644 index 5e77d6a..0000000 --- a/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module git.wbrawner.com/wbrawner/devls - -go 1.23.0 diff --git a/internal/netgear/netgear.go b/internal/netgear/netgear.go deleted file mode 100644 index f4a2b07..0000000 --- a/internal/netgear/netgear.go +++ /dev/null @@ -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 -} diff --git a/internal/netrc/netrc.go b/internal/netrc/netrc.go deleted file mode 100644 index fa301f6..0000000 --- a/internal/netrc/netrc.go +++ /dev/null @@ -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() -}